Class: Rzo::App::Generate

Inherits:
Subcommand show all
Defined in:
lib/rzo/app/generate.rb

Overview

Produce a Vagrantfile in the top level puppet control repo.

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

Constant Summary collapse

DEFAULT_NO_PROXY =

Constants used by the Vagrantfile.erb template.

'localhost,127.0.0.1'.freeze

Constants included from ConfigValidation

ConfigValidation::CHECKS_PERSONAL_CONFIG, ConfigValidation::CHECKS_REPO_CONFIG, ConfigValidation::RZO_PERSONAL_CONFIG_SCHEMA, ConfigValidation::RZO_REPO_CONFIG_SCHEMA

Instance Attribute Summary collapse

Attributes inherited from Subcommand

#opts, #pwd

Instance Method Summary collapse

Methods inherited from Subcommand

#initialize, #load_config!, #load_repo_configs, load_rizzo_config, #load_rizzo_config, #project_dir, #raise_ip_err, #raise_port_err, #readable?, #reorder_repos, #validate_forwarded_ports, #validate_ip_addresses, #write_file

Methods included from Logging

#debug, #error, #fatal, #info, #input_stream, log, #log, #map_file_option, map_file_option, #reset_logging!, reset_logging!, #say, stream_logger, syslog_logger, #warn, #write_output

Methods included from ConfigValidation

#compute_issues, #validate_complete_config!, #validate_control_repos, #validate_defaults_key, #validate_existence, #validate_inform!, #validate_personal_config!, #validate_personal_schema, #validate_schema

Constructor Details

This class inherits a constructor from Rzo::App::Subcommand

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/rzo/app/generate.rb', line 13

def config
  @config
end

Instance Method Details

#deep_convert_boolean(hash) ⇒ Object (private)

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Object (private)

Convert boolean strings to boolean for use after deep_merge



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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

#proxy_config(config) ⇒ String?

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

template.

Parameters:

  • config (Hash)

    The configuration hash used to fill in the ERB

Returns:

  • (String, nil)

    proxy exclusion list or nil if not specified.



97
98
99
100
101
102
# 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) ⇒ Object

dump out the puppetmaster settings from the config.



143
144
145
146
147
# File 'lib/rzo/app/generate.rb', line 143

def puppetmaster_settings(config)
  log.debug "config['puppetmaster'] = \n" + \
    config['puppetmaster'].pretty_inspect
  config['puppetmaster']
end

#runObject

The main run method for the subcommand.



16
17
18
19
20
21
22
23
24
25
# 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

#timestampObject

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



107
108
109
# File 'lib/rzo/app/generate.rb', line 107

def timestamp
  Time.now
end

#vagrantfile_agents(config) ⇒ Array<Hash>

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

template.

template. rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

Parameters:

  • config (Hash)

    The configuration hash used to fill in the ERB

Returns:

  • (Array<Hash>)

    list of agent nodes to fill in the Vagrantfile



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ String

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.

template.

Parameters:

  • template (String)

    The fully qualified path to the ERB template.

  • config (Hash)

    The configuration hash used to fill in the ERB

Returns:

  • (String)

    the content of the filled in template.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Array<Hash>

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

template.

Vagrantfile template.

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

Parameters:

  • config (Hash)

    The configuration hash used to fill in the ERB

Returns:

  • (Array<Hash>)

    list of puppet master nodes to fill in the



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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