Class: RefArchSetup::BoltHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ref_arch_setup/bolt_helper.rb

Overview

Bolt helper methods

Defined Under Namespace

Classes: BoltCommandError

Constant Summary collapse

BOLT_RUN_AS_USER =

the user RAS will provide to the bolt –run-as option

"root".freeze
BOLT_DEFAULT_OPTIONS =

the default options to specify when running bolt commands

{ "run-as" => BOLT_RUN_AS_USER }.freeze

Class Method Summary collapse

Class Method Details

.bolt_options(options_hash, overwrite = false) ⇒ Object

Merges the specified bolt options with the default options or optionally overwriting the default options

Parameters:

  • options_hash (hash)

    The user-specified bolt options hash

  • overwrite (boolean) (defaults to: false)

    The flag indicating whether the default options should be overwritten

Author:

  • Bill Claytor



39
40
41
42
43
44
45
# File 'lib/ref_arch_setup/bolt_helper.rb', line 39

def self.bolt_options(options_hash, overwrite = false)
  @bolt_options = if overwrite
                    options_hash
                  else
                    @bolt_options.merge(options_hash)
                  end
end

.bolt_options=(options_hash) ⇒ Object

Merges the default bolt options with the specified options

Parameters:

  • options_hash (hash)

    The user-specified bolt options hash

Author:

  • Bill Claytor



53
54
55
# File 'lib/ref_arch_setup/bolt_helper.rb', line 53

def self.bolt_options=(options_hash)
  bolt_options(options_hash)
end

.bolt_options_stringstring

Gets the bolt options as a string

Returns:

  • (string)

    the string value for bolt options

Author:

  • Sam Woods



61
62
63
64
65
66
67
# File 'lib/ref_arch_setup/bolt_helper.rb', line 61

def self.bolt_options_string
  bolt_options_string = ""
  @bolt_options.each do |key, value|
    bolt_options_string << " --#{key} #{value}"
  end
  bolt_options_string
end

.initObject

Initializes the bolt options to the default

Author:

  • Bill Claytor



26
27
28
# File 'lib/ref_arch_setup/bolt_helper.rb', line 26

def self.init
  @bolt_options = BOLT_DEFAULT_OPTIONS
end

.install_forge_modulesstring

Install modules from the forge via Puppetfile The modules are defined in Boltdir/Puppetfile

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Bill Claytor



257
258
259
260
261
# File 'lib/ref_arch_setup/bolt_helper.rb', line 257

def self.install_forge_modules
  command = "cd #{RAS_PATH} && bolt puppetfile install --modulepath #{FORGE_MODULE_PATH}"
  output = run_command(command, "ERROR: bolt puppetfile install failed!")
  return output
end

.make_dir(dir, nodes) ⇒ true, false

Creates a dir on the target_host Doesn’t fail if dir is already there Uses -p to create parent dirs if needed

Parameters:

  • dir (string)

    Directory to create

  • nodes (string)

    Hosts to make dir on

Returns:

  • (true, false)

    Based on the output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Randell Pelak



81
82
83
84
85
86
87
# File 'lib/ref_arch_setup/bolt_helper.rb', line 81

def self.make_dir(dir, nodes)
  error_message = "ERROR: Failed to make dir #{dir} on all nodes"
  cmd = "mkdir -p #{dir}"
  output = run_cmd_with_bolt(cmd, nodes, error_message)
  success = output.nil? ? false : true
  return success
end

.params_to_string(params) ⇒ String

Convert params to string for bolt format is space separated list of name=value

Parameters:

  • params (Array)

    params to convert

Returns:

  • (String)

    stringified params

Author:

  • Randell Pelak



223
224
225
226
# File 'lib/ref_arch_setup/bolt_helper.rb', line 223

def self.params_to_string(params)
  str = params.map { |k, v| "#{k}=#{v}" }.join(" ")
  return str
end

.run_cmd_with_bolt(cmd, nodes, error_message = "ERROR: bolt command failed!") ⇒ string

Run a command with bolt on given nodes

Parameters:

  • cmd (string)

    Command to run on the specified nodes

  • nodes (string)

    Nodes on which the command should be run

  • error_message (string) (defaults to: "ERROR: bolt command failed!")

    The message that should be used if an error is raised

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Randell Pelak



126
127
128
129
130
131
132
133
# File 'lib/ref_arch_setup/bolt_helper.rb', line 126

def self.run_cmd_with_bolt(cmd, nodes, error_message = "ERROR: bolt command failed!")
  command = "bolt command run '#{cmd}'"
  command << " --nodes #{nodes}"
  command << bolt_options_string

  output = run_command(command, error_message)
  return output
end

.run_command(command, error_message = "ERROR: command failed!") ⇒ string

Run the specified command

Parameters:

  • command (string)

    The command to run

  • error_message (string) (defaults to: "ERROR: command failed!")

    The error to raise if the command is not successful

Returns:

  • (string)

    The output returned from the command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Bill Claytor



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ref_arch_setup/bolt_helper.rb', line 99

def self.run_command(command, error_message = "ERROR: command failed!")
  puts "Running: #{command}"
  output = `#{command}`
  puts "Output was: #{output}"

  success = $?.success? # rubocop:disable Style/SpecialGlobalVars
  puts "Exit status was: #{$?.exitstatus}" # rubocop:disable Style/SpecialGlobalVars
  puts

  # raise error_message unless success
  raise BoltCommandError.new(error_message, output) if output.nil?
  raise BoltCommandError.new(error_message, output) unless success

  return output
end

.run_forge_plan_with_bolt(plan, params, nodes) ⇒ string

Run a plan from the forge with bolt on given nodes

Parameters:

  • plan (string)

    Plan to run on nodes

  • params (hash)

    Plan parameters to send to bolt

  • nodes (string)

    Host or space delimited hosts to run plan on

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Bill Claytor



209
210
211
212
213
# File 'lib/ref_arch_setup/bolt_helper.rb', line 209

def self.run_forge_plan_with_bolt(plan, params, nodes)
  install_forge_modules
  output = run_plan_with_bolt(plan, params, nodes, FORGE_MODULE_PATH)
  return output
end

.run_forge_task_with_bolt(task, params, nodes) ⇒ string

Run a task from the forge with bolt on given nodes

Parameters:

  • task (string)

    Task to run on nodes

  • params (hash)

    Task parameters to send to bolt

  • nodes (string)

    Host or space delimited hosts to run task on

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Bill Claytor



192
193
194
195
196
# File 'lib/ref_arch_setup/bolt_helper.rb', line 192

def self.run_forge_task_with_bolt(task, params, nodes)
  install_forge_modules
  output = run_task_with_bolt(task, params, nodes, FORGE_MODULE_PATH)
  return output
end

.run_plan_with_bolt(plan, params, nodes, modulepath = RAS_MODULE_PATH) ⇒ string

Run a plan with bolt on given nodes

Parameters:

  • plan (string)

    Plan to run on nodes

  • params (hash)

    Plan parameters to send to bolt

  • nodes (string)

    Host or space delimited hosts to run plan on

  • modulepath (string) (defaults to: RAS_MODULE_PATH)

    The modulepath to use when running bolt

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Sam Woods



170
171
172
173
174
175
176
177
178
179
# File 'lib/ref_arch_setup/bolt_helper.rb', line 170

def self.run_plan_with_bolt(plan, params, nodes, modulepath = RAS_MODULE_PATH)
  params_str = ""
  params_str = params_to_string(params) unless params.nil?
  command = "bolt plan run #{plan} #{params_str}"
  command << " --modulepath #{modulepath} --nodes #{nodes}"
  command << bolt_options_string

  output = run_command(command, "ERROR: bolt plan failed!")
  return output
end

.run_task_with_bolt(task, params, nodes, modulepath = RAS_MODULE_PATH) ⇒ string

Run a task with bolt on given nodes

Parameters:

  • task (string)

    Task to run on nodes

  • params (hash)

    task parameters to send to bolt

  • nodes (string)

    Host or space delimited hosts to run task on

  • modulepath (string) (defaults to: RAS_MODULE_PATH)

    The modulepath to use when running bolt

Returns:

  • (string)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Sam Woods



147
148
149
150
151
152
153
154
155
156
# File 'lib/ref_arch_setup/bolt_helper.rb', line 147

def self.run_task_with_bolt(task, params, nodes, modulepath = RAS_MODULE_PATH)
  params_str = ""
  params_str = params_to_string(params) unless params.nil?
  command = "bolt task run #{task} #{params_str}"
  command << " --modulepath #{modulepath} --nodes #{nodes}"
  command << bolt_options_string

  output = run_command(command, "ERROR: bolt task failed!")
  return output
end

.upload_file(source, destination, nodes) ⇒ output

Upload a file to given nodes

Parameters:

  • source (string)

    File to upload

  • destination (string)

    Path to upload to

  • nodes (string)

    Host to put files on

Returns:

  • (output)

    The output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Randell Pelak



239
240
241
242
243
244
245
246
247
# File 'lib/ref_arch_setup/bolt_helper.rb', line 239

def self.upload_file(source, destination, nodes)
  command = "bolt file upload #{source} #{destination}"
  command << " --nodes #{nodes}"
  command << bolt_options_string

  error_message = "ERROR: failed to upload file #{source} to #{destination} on #{nodes}"
  output = run_command(command, error_message)
  return output
end