class Rzo::App::Generate

Produce a `Vagrantfile` in the top level puppet control repo.

Load all rizzo config files, then produce the Vagrantfile from an ERB template.

Constants

DEFAULT_NO_PROXY

Constants used by the Vagrantfile.erb template.

Attributes

config[R]

Public Instance Methods

proxy_config(config) click to toggle source

Return the proxy configuration exception list as a string, or nil if not set.

@param [Hash] config The configuration hash used to fill in the ERB template.

@return [String,nil] proxy exclusion list or nil if not specified.

# File lib/rzo/app/generate.rb, line 97
def proxy_config(config)
  # Proxy Setting
  return nil unless config['config']

  config['config']['no_proxy'] || DEFAULT_NO_PROXY
end
puppetmaster_settings(config) click to toggle source

dump out the puppetmaster settings from the config.

# File lib/rzo/app/generate.rb, line 143
def puppetmaster_settings(config)
  log.debug "config['puppetmaster'] = \n" + \
    config['puppetmaster'].pretty_inspect
  config['puppetmaster']
end
run() click to toggle source

The main run method for the subcommand.

# File lib/rzo/app/generate.rb, line 16
def run
  exit_status = 0
  load_config!
  # Vagrantfile
  erbfile = File.expand_path('templates/Vagrantfile.erb', __dir__)
  content = vagrantfile_content(erbfile, config)
  write_file(opts[:vagrantfile]) { |fd| fd.write(content) }
  say "Wrote vagrant config to #{opts[:vagrantfile]}"
  exit_status
end
timestamp() click to toggle source

Return a timestamp to embed in the output Vagrantfile. This is a method so it may be stubbed out in the tests.

# File lib/rzo/app/generate.rb, line 107
def timestamp
  Time.now
end
vagrantfile_agents(config) click to toggle source

Return a list of agent node definitions suitable for the Vagrantfile template.

@param [Hash] config The configuration hash used to fill in the ERB template.

@return [Array<Hash>] list of agent nodes to fill in the Vagrantfile template. rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/rzo/app/generate.rb, line 38
def vagrantfile_agents(config)
  pm_settings = puppetmaster_settings(config)
  agent_nodes = [*config['nodes']].reject do |n|
    pm_settings['name'].include?(n['name'])
  end

  agent_nodes.map! do |n|
    new_n = deep_convert_boolean(n)
    defaults = deep_convert_boolean(config['defaults'])
    new_n.deep_merge(defaults)
    n = deep_unconvert_boolean(new_n)
    log.debug "puppetagent #{n['name']} = \n" + n.pretty_inspect
    n
  end

  agent_nodes
end
vagrantfile_content(template, config) click to toggle source

Return a string which is the Vagrantfile content of a filled in Vagrantfile erb template. The configuration data parsed by load_config! is expected as input, along with the template to fill in.

The base templates directory is relative to the directory containing this file.

@param [String] template The fully qualified path to the ERB template.

@param [Hash] config The configuration hash used to fill in the ERB template.

@return [String] the content of the filled in template.

# File lib/rzo/app/generate.rb, line 125
def vagrantfile_content(template, config)
  renderer = ERB.new(File.read(template), 0, '-')

  no_proxy = proxy_config(config)

  # Agent nodes [Array<Hash>]
  agent_nodes = vagrantfile_agents(config)
  # Puppet Master nodes [Array<Hash>]
  puppet_master_nodes = vagrantfile_puppet_masters(config)

  # nodes is used by the Vagrantfile.erb template
  nodes = [*puppet_master_nodes, *agent_nodes]
  content = renderer.result(binding)
  content
end
vagrantfile_puppet_masters(config) click to toggle source

Return a list of puppetmaster node definitions suitable for the Vagrantfile template.

@param [Hash] config The configuration hash used to fill in the ERB template.

@return [Array<Hash>] list of puppet master nodes to fill in the Vagrantfile template.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/rzo/app/generate.rb, line 70
def vagrantfile_puppet_masters(config)
  pm_settings = puppetmaster_settings(config)
  pm_names = pm_settings['name']

  nodes = [*config['nodes']].find_all { |n| pm_names.include?(n['name']) }
  nodes.map! do |n|
    new_n = deep_convert_boolean(n)
    defaults = deep_convert_boolean(config['defaults'])
    new_pm_settings = deep_convert_boolean(pm_settings)
    new_n.deep_merge(defaults)
    new_n.deep_merge(new_pm_settings)
    n = deep_unconvert_boolean(new_n)
    n[:puppetmaster] = true
    log.debug "puppetmaster #{n['name']} = \n" + n.pretty_inspect
    n
  end
end

Private Instance Methods

deep_convert_boolean(hash) click to toggle source

Convert boolean to string for deep_merge rubocop:disable Metrics/MethodLength

# File lib/rzo/app/generate.rb, line 157
def deep_convert_boolean(hash)
  new_hash = {}
  hash.each_pair do |key, value|
    if value.is_a?(Hash)
      new_hash[key] = deep_convert_boolean(value)
    elsif value.is_a?(TrueClass)
      new_hash[key] = 'true'
    elsif value.is_a?(FalseClass)
      new_hash[key] = 'false'
    else
      new_hash[key] = value
    end
  end
  return new_hash
end
deep_unconvert_boolean(hash) click to toggle source

Convert boolean strings to boolean for use after deep_merge

# File lib/rzo/app/generate.rb, line 175
def deep_unconvert_boolean(hash)
  new_hash = {}
  hash.each_pair do |key, value|
    if value.is_a?(Hash)
      new_hash[key] = deep_unconvert_boolean(value)
    elsif value == 'true'
      new_hash[key] = true
    elsif value == 'false'
      new_hash[key] = false
    else
      new_hash[key] = value
    end
  end
  return new_hash
end