Class: Kontena::Cli::Stacks::YAML::StackFileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/cli/stacks/yaml/stack_file_loader.rb

Direct Known Subclasses

FileLoader, RegistryLoader, UriLoader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, parent = nil) ⇒ StackFileLoader

Parameters:

  • source (String)

    stack file source string (filename, url, ..)

  • parent (StackFileLoader) (defaults to: nil)

    define a parent for recursion



41
42
43
44
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 41

def initialize(source, parent = nil)
  @source = source
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



36
37
38
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 36

def parent
  @parent
end

#sourceObject (readonly)

Returns the value of attribute source.



36
37
38
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 36

def source
  @source
end

Class Method Details

.for(source, parent = nil) ⇒ StackFileLoader

The main interface for getting a new loader

Parameters:

  • source (String)

    stack file source string (filename, url, ..)

  • parent (StackFileLoader) (defaults to: nil)

    define a parent for recursion

Returns:



28
29
30
31
32
33
34
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 28

def self.for(source, parent = nil)
  loader = loaders.find { |l| l.match?(source, parent) }
  if loader.nil?
    raise RuntimeError, "Not found: no such file #{source} or invalid uri scheme"
  end
  loader.new(source, parent)
end

.inherited(where) ⇒ Object

A base class for loading stack files. You can define more loaders by inheriting from this class.

The purpose of StackFileLoader is to provide a generic interface for loading stack YAML’s from different sources, such as local files, stack registry or URLs



15
16
17
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 15

def self.inherited(where)
  loaders << where
end

.loadersObject



19
20
21
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 19

def self.loaders
  @loaders ||= []
end

Instance Method Details

#contentString

Returns raw file content.

Returns:

  • (String)

    raw file content



57
58
59
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 57

def content
  @content ||= read_content
end

#dependencies(recurse: true) ⇒ Array<Hash>

Builds an array of hashes that represent the dependency tree starting from the target file. Unless recurse is set to false, the tree will contain also nested dependencies from any child stacks.

Parameters:

  • recurse (TrueClass, FalseClass) (defaults to: true)

    recurse child dependencies?

Returns:

  • (Array<Hash>)

    an array of hashes (‘name’, ‘stack’, ‘variables’, and ‘depends’)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 81

def dependencies(recurse: true)
  return @dependencies if @dependencies
  if depends.nil? || depends.empty?
    @dependencies = nil
  else
    @dependencies = depends.map do |name, dependency|
      loader = StackFileLoader.for(dependency['stack'], self)
      deps = { 'name' => name, 'stack' => loader.source, 'variables' => dependency.fetch('variables', Hash.new) }
      if recurse
        child_deps = loader.dependencies
        deps['depends'] = child_deps unless child_deps.nil?
      end
      deps
    end
  end
end

#flat_dependencies(basename, opts = {}) ⇒ Hash

Returns a non nested hash of all dependencies. Processes :variables hash and moves the related variables to children

Parameters:

  • basename (String)

    installed stack name

  • opts (Hash) (defaults to: {})

    extra data such as variable lists

Returns:

  • (Hash)

    { installation_name => { ‘name’ => installation-name, ‘stack’ => stack_name, :loader => self }, child_install_name => { … } }



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 111

def flat_dependencies(basename, opts = {})
  opt_variables = opts[:variables] || {}

  result = {
    basename => self.to_h.merge(opts).merge(
      name: basename,
      variables: opt_variables.reject { |k, _| k.include?('.') }
    )
  }

  depends.each do |as_name, data|
    variables = {}

    opt_variables.select { |k, _| k.start_with?(as_name + '.') }.each do |k,v|
      variables[k.split('.', 2).last] = v
    end

    data['variables'] ||= {}

    loader = StackFileLoader.for(data['stack'], self)
    result.merge!(
     loader.flat_dependencies(
       basename + '-' + as_name,
       variables: data['variables'].merge(variables),
       parent_name: basename
     )
    )
  end

  result
end

#inspectString

Returns a stripped down version of inspect without all the yaml source.

Returns:

  • (String)

    a stripped down version of inspect without all the yaml source



47
48
49
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 47

def inspect
  "#<#{self.class.name}:#{object_id} @source=#{source.inspect} @parent=#{parent.nil? ? 'nil' : parent.source}>"
end

#read_contentObject



61
62
63
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 61

def read_content
  raise "Implement in inheriting class"
end

#reader(*args) ⇒ Reader

Returns an accessor to YAML::Reader for the target file.

Returns:

  • (Reader)

    an accessor to YAML::Reader for the target file



71
72
73
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 71

def reader(*args)
  @reader ||= Reader.new(self, *args)
end

#stack_nameStackName

Returns an accessor to StackName for the target file.

Returns:

  • (StackName)

    an accessor to StackName for the target file



66
67
68
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 66

def stack_name
  @stack_name = Kontena::Cli::Stacks::StackName.new(yaml['stack'], yaml['version'])
end

#to_hObject



98
99
100
101
102
103
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 98

def to_h
  {
    'stack' => stack_name.stack_name,
    :loader => self,
  }
end

#yamlHash

Returns a hash parsed from the YAML content.

Returns:

  • (Hash)

    a hash parsed from the YAML content



52
53
54
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 52

def yaml
  @yaml ||= ::YAML.safe_load(content, [], [], true, source)
end