class Opsmgr::BoshCommandRunner

Attributes

bosh_command[R]
logger[R]

Public Class Methods

new(bosh_command:, logger:) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 3
def initialize(bosh_command:, logger:)
  @bosh_command = bosh_command
  @logger = logger
end

Public Instance Methods

run(command) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 8
def run(command)
  system_or_fail(
    "#{bosh_command_prefix} #{command}",
    "bosh #{command} failed"
  )
end
run_and_capture_output(command) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 15
def run_and_capture_output(command)
  capture_output_or_fail(
    "#{bosh_command_prefix} #{command}",
    "bosh #{command} failed"
  )
end
run_with_deployment(command, deployment) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 22
def run_with_deployment(command, deployment)
  bosh_deployment = bosh_deployments[/#{deployment}-[0-9a-f]{8,}/]
  fail 'Deployment not found' unless bosh_deployment

  deployment_file = "#{ENV.fetch('TMPDIR', '/tmp')}/#{deployment}.yml"

  begin
    run(
      "-n download manifest #{bosh_deployment} #{deployment_file}"
    )
  rescue RuntimeError
    raise 'bosh download manifest failed'
  end

  run("-d #{deployment_file} #{command}")
end

Private Instance Methods

bosh_command_prefix() click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 70
def bosh_command_prefix
  @bosh_command_prefix ||= bosh_command.command
end
bosh_deployments() click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 41
def bosh_deployments
  run_and_capture_output('deployments')
rescue RuntimeError
  raise 'bosh deployments failed'
end
capture_output_or_fail(command, failure_message) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 52
def capture_output_or_fail(command, failure_message)
  clean_and_log(command)

  Bundler.with_clean_env do
    output, status = Open3.capture2(command)
    fail(failure_message) unless status.success?
    output
  end
end
clean_and_log(command) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 62
def clean_and_log(command)
  scrubbed_command = command.slice(command.index('bosh')..-1)
  scrubbed_command.sub!(/\s\-p\s\b\w+\b/, ' -p <PASSWORD>')
  scrubbed_command.sub!(/\s\-u\s\b\w+\b/, ' -u <USER>')

  logger.info("Running #{scrubbed_command}")
end
system_or_fail(command, failure_message) click to toggle source
# File lib/opsmgr/bosh_command_runner.rb, line 47
def system_or_fail(command, failure_message)
  clean_and_log(command)
  Bundler.clean_system(command) || fail(failure_message)
end