class Ogre::SetPrivateKey

Set Private Key is used to set a chef validation key via a vco workflow

Public Instance Methods

set_private_key() click to toggle source

rubocop:disable CyclomaticComplexity, PerceivedComplexity Execute vcoworkflows gem to call set private key

# File lib/ogre/set-private-key.rb, line 20
def set_private_key
  # get workflow
  # rubocop:disable AlignParameters
  workflow = VcoWorkflows::Workflow.new(options[:vco_wf_name]    || Config.options[:vco_wf_name],
                            url:        options[:vco_url]        || Config.options[:vco_url],
                            verify_ssl: options[:vco_verify_ssl] || Config.options[:vco_verify_ssl],
                            username:   options[:vco_user]       || Config.options[:vco_user],
                            password:   options[:vco_password]   || Config.options[:vco_password])
  # rubocop:enable AlignParameters

  # set parameters
  workflow.parameter('chefHostname', chef_hostname)
  workflow.parameter('userid', chef_validator_name)
  workflow.parameter('pem', File.read(key_path))

  # run workflow
  execution_id = workflow.execute

  # check status
  finished = false
  until finished
    # Fetch a new workflow token to check the status of the workflow execution
    wf_token = workflow.token
    # If the execution is no longer alive, exit the loop
    unless wf_token.alive?
      finished = true
      execution_id
    end
    sleep 5
  end

  # output result
  log = workflow.token(execution_id).to_s
  puts log.slice(0..log.index('Input Parameters:') - 2)
end