Class: LeapCli::Config::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/leap_cli/config/manager.rb

Overview

A class to manage all the objects in all the configuration files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/leap_cli/config/manager.rb', line 21

def initialize
  @environments = {} # hash of `Environment` objects, keyed by name.

  # load macros and other custom ruby in provider base
  platform_ruby_files = Dir[Path.provider_base + '/lib/*.rb']
  if platform_ruby_files.any?
    $: << Path.provider_base + '/lib'
    platform_ruby_files.each do |rb_file|
      require rb_file
    end
  end
  Config::Object.send(:include, LeapCli::Macro)
end

Instance Attribute Details

#base_commonObject (readonly)

Returns the value of attribute base_common.



40
41
42
# File 'lib/leap_cli/config/manager.rb', line 40

def base_common
  @base_common
end

#base_providerObject (readonly)

Returns the value of attribute base_provider.



40
41
42
# File 'lib/leap_cli/config/manager.rb', line 40

def base_provider
  @base_provider
end

#base_servicesObject (readonly)

Returns the value of attribute base_services.



40
41
42
# File 'lib/leap_cli/config/manager.rb', line 40

def base_services
  @base_services
end

#base_tagsObject (readonly)

Returns the value of attribute base_tags.



40
41
42
# File 'lib/leap_cli/config/manager.rb', line 40

def base_tags
  @base_tags
end

#commonObject (readonly)

ATTRIBUTES



39
40
41
# File 'lib/leap_cli/config/manager.rb', line 39

def common
  @common
end

#nodesObject (readonly)

ATTRIBUTES



39
40
41
# File 'lib/leap_cli/config/manager.rb', line 39

def nodes
  @nodes
end

#secretsObject (readonly)

ATTRIBUTES



39
40
41
# File 'lib/leap_cli/config/manager.rb', line 39

def secrets
  @secrets
end

Instance Method Details

#default_environmentObject



90
91
92
# File 'lib/leap_cli/config/manager.rb', line 90

def default_environment
  LeapCli.leapfile.environment
end

#disabled_node(name) ⇒ Object

returns a single node that is disabled



279
280
281
# File 'lib/leap_cli/config/manager.rb', line 279

def disabled_node(name)
  @disabled_nodes[name]
end

#each_node(&block) ⇒ Object

yields each node, in sorted order



286
287
288
# File 'lib/leap_cli/config/manager.rb', line 286

def each_node(&block)
  nodes.each_node &block
end

#env(env = nil) {|e| ... } ⇒ Object

Returns the appropriate environment variable

Yields:

  • (e)


68
69
70
71
72
73
# File 'lib/leap_cli/config/manager.rb', line 68

def env(env=nil)
  env ||= 'default'
  e = @environments[env] ||= Environment.new
  yield e if block_given?
  e
end

#environment_namesObject

returns an Array of all the environments defined for this provider. the returned array includes nil (for the default environment)



61
62
63
# File 'lib/leap_cli/config/manager.rb', line 61

def environment_names
  @environment_names ||= [nil] + env.tags.collect {|name, tag| tag['environment']}.compact
end

#export_nodes(node_list = nil) ⇒ Object

save compiled hiera .yaml files

if a node_list is specified, only update those .yaml files. otherwise, update all files, destroying files that are no longer used.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/leap_cli/config/manager.rb', line 185

def export_nodes(node_list=nil)
  updated_hiera = []
  updated_files = []
  existing_hiera = nil
  existing_files = nil

  unless node_list
    node_list = self.nodes
    existing_hiera = Dir.glob(Path.named_path([:hiera, '*'], @provider_dir))
    existing_files = Dir.glob(Path.named_path([:node_files_dir, '*'], @provider_dir))
  end

  node_list.each_node do |node|
    filepath = Path.named_path([:node_files_dir, node.name], @provider_dir)
    hierapath = Path.named_path([:hiera, node.name], @provider_dir)
    Util::write_file!(hierapath, node.dump_yaml)
    updated_files << filepath
    updated_hiera << hierapath
  end

  if @disabled_nodes
    # make disabled nodes appear as if they are still active
    @disabled_nodes.each_node do |node|
      updated_files << Path.named_path([:node_files_dir, node.name], @provider_dir)
      updated_hiera << Path.named_path([:hiera, node.name], @provider_dir)
    end
  end

  # remove files that are no longer needed
  if existing_hiera
    (existing_hiera - updated_hiera).each do |filepath|
      Util::remove_file!(filepath)
    end
  end
  if existing_files
    (existing_files - updated_files).each do |filepath|
      Util::remove_directory!(filepath)
    end
  end
end

#export_secrets(clean_unused_secrets = false) ⇒ Object



226
227
228
229
230
# File 'lib/leap_cli/config/manager.rb', line 226

def export_secrets(clean_unused_secrets = false)
  if @secrets.any?
    Util.write_file!([:secrets_config, @provider_dir], @secrets.dump_json(clean_unused_secrets) + "\n")
  end
end

#factsObject

returns the Hash of the contents of facts.json



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/leap_cli/config/manager.rb', line 45

def facts
  @facts ||= begin
    content = Util.read_file(:facts)
    if !content || content.empty?
      content = "{}"
    end
    JSON.parse(content)
  rescue SyntaxError, JSON::ParserError => exc
    Util::bail! "Could not parse facts.json -- #{exc}"
  end
end

#filter(filters = nil, options = {}) ⇒ Object

returns a node list consisting only of nodes that satisfy the filter criteria.

filter: condition [condition] [condition] [+condition] condition: [node_name | service_name | tag_name | environment_name]

if conditions is prefixed with +, then it works like an AND. Otherwise, it works like an OR.

args: filter – array of filter terms, one per item

options: :local – if :local is false and the filter is empty, then local nodes are excluded. :nopin – if true, ignore environment pinning



251
252
253
# File 'lib/leap_cli/config/manager.rb', line 251

def filter(filters=nil, options={})
  Filter.new(filters, options, self).nodes()
end

#filter!(filters, options = {}) ⇒ Object

same as filter(), but exits if there is no matching nodes



258
259
260
261
262
# File 'lib/leap_cli/config/manager.rb', line 258

def filter!(filters, options={})
  node_list = filter(filters, options)
  Util::assert! node_list.any?, "Could not match any nodes from '#{filters.join ' '}'"
  return node_list
end

#load(options = {}) ⇒ Object

load .json configuration files



101
102
103
104
105
106
107
108
109
110
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/leap_cli/config/manager.rb', line 101

def load(options = {})
  @provider_dir = Path.provider

  # load base
  @base_services = load_all_json(Path.named_path([:service_config, '*'], Path.provider_base), Config::Tag)
  @base_tags     = load_all_json(Path.named_path([:tag_config, '*'],     Path.provider_base), Config::Tag)
  @base_common   = load_json(    Path.named_path(:common_config,         Path.provider_base), Config::Object)
  @base_provider = load_json(    Path.named_path(:provider_config,       Path.provider_base), Config::Provider)

  # load provider
  @nodes    = load_all_json(Path.named_path([:node_config, '*'],  @provider_dir), Config::Node)
  @common   = load_json(    Path.named_path(:common_config,       @provider_dir), Config::Object)
  @secrets  = load_json(    Path.named_path(:secrets_config,      @provider_dir), Config::Secrets)
  @common.inherit_from! @base_common

  # For the default environment, load provider services, tags, and provider.json
  log 3, :loading, 'default environment...'
  env('default') do |e|
    e.services = load_all_json(Path.named_path([:service_config, '*'], @provider_dir), Config::Tag, :no_dots => true)
    e.tags     = load_all_json(Path.named_path([:tag_config, '*'],     @provider_dir), Config::Tag, :no_dots => true)
    e.provider = load_json(    Path.named_path(:provider_config,       @provider_dir), Config::Provider, :assert => true)
    e.services.inherit_from! @base_services
    e.tags.inherit_from!     @base_tags
    e.provider.inherit_from! @base_provider
    validate_provider(e.provider)
  end

  # create a special '_all_' environment, used for tracking the union
  # of all the environments
  env('_all_') do |e|
    e.services = Config::ObjectList.new
    e.tags     = Config::ObjectList.new
    e.provider = Config::Provider.new
    e.services.inherit_from! env('default').services
    e.tags.inherit_from!     env('default').tags
    e.provider.inherit_from! env('default').provider
  end

  # For each defined environment, load provider services, tags, and provider.json.
  environment_names.each do |ename|
    next unless ename
    log 3, :loading, '%s environment...' % ename
    env(ename) do |e|
      e.services = load_all_json(Path.named_path([:service_env_config, '*', ename], @provider_dir), Config::Tag, :env => ename)
      e.tags     = load_all_json(Path.named_path([:tag_env_config, '*', ename],     @provider_dir), Config::Tag, :env => ename)
      e.provider = load_json(    Path.named_path([:provider_env_config, ename],     @provider_dir), Config::Provider, :env => ename)
      e.services.inherit_from! env('default').services
      e.tags.inherit_from!     env('default').tags
      e.provider.inherit_from! env('default').provider
      validate_provider(e.provider)
    end
  end

  # apply inheritance
  @nodes.each do |name, node|
    Util::assert! name =~ /^[0-9a-z-]+$/, "Illegal character(s) used in node name '#{name}'"
    @nodes[name] = apply_inheritance(node)
  end

  # do some node-list post-processing
  cleanup_node_lists(options)

  # apply control files
  @nodes.each do |name, node|
    control_files(node).each do |file|
      begin
        node.eval_file file
      rescue ConfigError => exc
        if options[:continue_on_error]
          exc.log
        else
          raise exc
        end
      end
    end
  end
end

#node(name) ⇒ Object

returns a single Config::Object that corresponds to a Node.



267
268
269
270
271
272
273
274
# File 'lib/leap_cli/config/manager.rb', line 267

def node(name)
  if name =~ /\./
    # probably got a fqdn, since periods are not allowed in node names.
    # so, take the part before the first period as the node name
    name = name.split('.').first
  end
  @nodes[name]
end

#partials(partial_path) ⇒ Object

returns all the partial data for the specified partial path. partial path is always relative to provider root, but there must be multiple files that match because provider root might be the base provider or the local provider.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/leap_cli/config/manager.rb', line 299

def partials(partial_path)
  @partials ||= {}
  if @partials[partial_path].nil?
    [Path.provider_base, Path.provider].each do |provider_dir|
      path = File.join(provider_dir, partial_path)
      if File.exists?(path)
        @partials[partial_path] ||= []
        @partials[partial_path] << load_json(path, Config::Object)
      end
    end
    if @partials[partial_path].nil?
      raise RuntimeError, 'no such partial path `%s`' % partial_path, caller
    end
  end
  @partials[partial_path]
end

#providerObject



86
87
88
# File 'lib/leap_cli/config/manager.rb', line 86

def provider
  env(default_environment).provider
end

#reload_node!(node) ⇒ Object



290
291
292
# File 'lib/leap_cli/config/manager.rb', line 290

def reload_node!(node)
  @nodes[node.name] = apply_inheritance!(node)
end

#servicesObject

The default accessors for services, tags, and provider. For these defaults, use ‘default’ environment, or whatever environment is pinned.



80
81
82
# File 'lib/leap_cli/config/manager.rb', line 80

def services
  env(default_environment).services
end

#tagsObject



83
84
85
# File 'lib/leap_cli/config/manager.rb', line 83

def tags
  env(default_environment).tags
end