Class: RefArchSetup::BoltHelper
- Inherits:
-
Object
- Object
- RefArchSetup::BoltHelper
- 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
-
.bolt_options(options_hash, overwrite = false) ⇒ Object
Merges the specified bolt options with the default options or optionally overwriting the default options.
-
.bolt_options=(options_hash) ⇒ Object
Merges the default bolt options with the specified options.
-
.bolt_options_string ⇒ string
Gets the bolt options as a string.
-
.init ⇒ Object
Initializes the bolt options to the default.
-
.install_forge_modules ⇒ string
Install modules from the forge via Puppetfile The modules are defined in Boltdir/Puppetfile.
-
.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.
-
.params_to_string(params) ⇒ String
Convert params to string for bolt format is space separated list of name=value.
-
.run_cmd_with_bolt(cmd, nodes, error_message = "ERROR: bolt command failed!") ⇒ string
Run a command with bolt on given nodes.
-
.run_command(command, error_message = "ERROR: command failed!") ⇒ string
Run the specified command.
-
.run_forge_plan_with_bolt(plan, params, nodes) ⇒ string
Run a plan from the forge with bolt on given nodes.
-
.run_forge_task_with_bolt(task, params, nodes) ⇒ string
Run a task from the forge with bolt on given nodes.
-
.run_plan_with_bolt(plan, params, nodes, modulepath = RAS_MODULE_PATH) ⇒ string
Run a plan with bolt on given nodes.
-
.run_task_with_bolt(task, params, nodes, modulepath = RAS_MODULE_PATH) ⇒ string
Run a task with bolt on given nodes.
-
.upload_file(source, destination, nodes) ⇒ output
Upload a file to given nodes.
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
39 40 41 42 43 44 45 |
# File 'lib/ref_arch_setup/bolt_helper.rb', line 39 def self.(, overwrite = false) = if overwrite else .merge() end end |
.bolt_options=(options_hash) ⇒ Object
Merges the default bolt options with the specified options
53 54 55 |
# File 'lib/ref_arch_setup/bolt_helper.rb', line 53 def self.=() () end |
.bolt_options_string ⇒ string
Gets the bolt options as a string
61 62 63 64 65 66 67 |
# File 'lib/ref_arch_setup/bolt_helper.rb', line 61 def self. = "" .each do |key, value| << " --#{key} #{value}" end end |
.init ⇒ Object
Initializes the bolt options to the default
26 27 28 |
# File 'lib/ref_arch_setup/bolt_helper.rb', line 26 def self.init = BOLT_DEFAULT_OPTIONS end |
.install_forge_modules ⇒ string
Install modules from the forge via Puppetfile The modules are defined in Boltdir/Puppetfile
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
81 82 83 84 85 86 87 |
# File 'lib/ref_arch_setup/bolt_helper.rb', line 81 def self.make_dir(dir, nodes) = "ERROR: Failed to make dir #{dir} on all nodes" cmd = "mkdir -p #{dir}" output = run_cmd_with_bolt(cmd, nodes, ) 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
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
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: bolt command failed!") command = "bolt command run '#{cmd}'" command << " --nodes #{nodes}" command << output = run_command(command, ) return output end |
.run_command(command, error_message = "ERROR: command failed!") ⇒ string
Run the specified command
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: 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(, output) if output.nil? raise BoltCommandError.new(, 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
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
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
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 << 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
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 << output = run_command(command, "ERROR: bolt task failed!") return output end |
.upload_file(source, destination, nodes) ⇒ output
Upload a file to given nodes
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 << = "ERROR: failed to upload file #{source} to #{destination} on #{nodes}" output = run_command(command, ) return output end |