Class: Kontena::Cli::Stacks::YAML::StackFileLoader
- Inherits:
-
Object
- Object
- Kontena::Cli::Stacks::YAML::StackFileLoader
- Defined in:
- lib/kontena/cli/stacks/yaml/stack_file_loader.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.for(source, parent = nil) ⇒ StackFileLoader
The main interface for getting a new loader.
-
.inherited(where) ⇒ Object
A base class for loading stack files.
- .loaders ⇒ Object
Instance Method Summary collapse
-
#content ⇒ String
Raw file content.
-
#dependencies(recurse: true) ⇒ Array<Hash>
Builds an array of hashes that represent the dependency tree starting from the target file.
-
#flat_dependencies(basename, opts = {}) ⇒ Hash
Returns a non nested hash of all dependencies.
- #initialize(source, parent = nil) ⇒ StackFileLoader constructor
-
#inspect ⇒ String
A stripped down version of inspect without all the yaml source.
- #read_content ⇒ Object
-
#reader(*args) ⇒ Reader
An accessor to YAML::Reader for the target file.
-
#stack_name ⇒ StackName
An accessor to StackName for the target file.
- #to_h ⇒ Object
-
#yaml ⇒ Hash
A hash parsed from the YAML content.
Constructor Details
#initialize(source, parent = nil) ⇒ StackFileLoader
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
#parent ⇒ Object (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 |
#source ⇒ Object (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
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 |
.loaders ⇒ Object
19 20 21 |
# File 'lib/kontena/cli/stacks/yaml/stack_file_loader.rb', line 19 def self.loaders @loaders ||= [] end |
Instance Method Details
#content ⇒ String
Returns 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.
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
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 |
#inspect ⇒ String
Returns 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_content ⇒ Object
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.
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_name ⇒ StackName
Returns 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_h ⇒ Object
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 |
#yaml ⇒ Hash
Returns 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 |