module Beaker::TaskHelper

Beaker task Helper

Constants

DEFAULT_BOLT_VERSION
DEFAULT_PASSWORD
VERSION

Public Class Methods

bolt_version(beakerhost = nil) click to toggle source

Returns the version of Bolt installed on the beakerhost. Used to determine which inventory file version to generate.

# File lib/beaker-task_helper.rb, line 32
def self.bolt_version(beakerhost = nil)
  return ENV['BEAKER_BOLT_VERSION'] if ENV['BEAKER_BOLT_VERSION']

  beakerhost ||= default
  begin
    on(beakerhost, "#{bolt_path} --version")
  rescue Beaker::Host::CommandFailure
    # If bolt isn't installed, default to 1.18.0 which is the latest version
    # to support only v1 inventory
    '1.18.0'
  end
end

Public Instance Methods

bolt_path() click to toggle source
# File lib/beaker-task_helper.rb, line 11
def bolt_path
  if fact_on(default, 'osfamily') == 'windows'
    if ENV['BEAKER_PUPPET_COLLECTION'].nil? || %w[pc1 puppet5 puppet6].include?(ENV['BEAKER_PUPPET_COLLECTION'])
      '/cygdrive/c/Program\ Files/Puppet\ Labs/Puppet/sys/ruby/bin/bolt.bat'
    else
      '/cygdrive/c/Program\ Files/Puppet\ Labs/Puppet/puppet/bin/bolt.bat'
    end
  else
    # If bolt was installed as a package
    if File.exist?('/opt/puppetlabs/puppet/bin/bolt')
      '/opt/puppetlabs/puppet/bin/bolt'
    else
    # Or as a gem
      'bolt'
    end
  end
end
expect_multiple_regexes(result:, regexes:) click to toggle source
# File lib/beaker-task_helper.rb, line 211
def expect_multiple_regexes(result:, regexes:)
  regexes.each do |regex|
    expect(result).to match(regex)
  end
end
install_bolt_on(hosts, version = DEFAULT_BOLT_VERSION, source = nil) click to toggle source
# File lib/beaker-task_helper.rb, line 63
  def install_bolt_on(hosts, version = DEFAULT_BOLT_VERSION, source = nil)
    unless default[:docker_image_commands].nil?
      if default[:docker_image_commands].to_s.include? 'yum'
        on(hosts, 'yum install -y make gcc ruby-devel', acceptable_exit_codes: [0, 1]).stdout
      elsif default[:docker_image_commands].to_s.include? 'apt-get'
        on(hosts, 'apt-get install -y make gcc ruby-dev', acceptable_exit_codes: [0, 1]).stdout
      end
    end

    Array(hosts).each do |host|

      # Work around for BOLT-845
      pp0 = <<-INSTALL_FFI_PP
  package { 'ffi' :
    provider => 'puppet_gem',
    ensure   => '1.9.18', }
INSTALL_FFI_PP

      apply_manifest_on(host, pp0) if fact_on(host, 'osfamily') == 'RedHat' && fact_on(host, 'operatingsystemmajrelease') == '5'

      pp = <<-INSTALL_BOLT_PP
  package { 'bolt' :
    provider => 'puppet_gem',
    ensure   => '#{version}',
INSTALL_BOLT_PP
      pp << "source   => '#{source}'" if source
      pp << '}'
      apply_manifest_on(host, pp)

      bolt_confdir = "#{on(host, 'echo $HOME').stdout.chomp}/.puppetlabs/bolt"
      on host, "mkdir -p #{bolt_confdir}"
      create_remote_file(host, "#{bolt_confdir}/analytics.yaml", { 'disabled' => true }.to_yaml)
    end
  end
pe_install?() click to toggle source
# File lib/beaker-task_helper.rb, line 98
def pe_install?
  ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
end
puppet_version() click to toggle source
# File lib/beaker-task_helper.rb, line 7
def puppet_version
  (on default, puppet('--version')).output.chomp
end
run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD, host: '127.0.0.1', format: 'human', module_path: nil) click to toggle source
# File lib/beaker-task_helper.rb, line 160
def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD,
                  host: '127.0.0.1', format: 'human', module_path: nil)
  if fact_on(default, 'osfamily') == 'windows'
    module_path ||= 'C:/ProgramData/PuppetLabs/code/modules'

    if version_is_less('0.15.0', self.bolt_version)
      check = '--no-ssl'
    else
      check = '--insecure'
    end
  else
    module_path ||='/etc/puppetlabs/code/modules'

    if version_is_less('0.15.0', self.bolt_version)
      check = '--no-host-key-check'
    else
      check = '--insecure'
    end
  end

  bolt_full_cli = "#{bolt_path} task run #{task_name} #{check} -m #{module_path} " \
                  "#{target_flag} #{host} --password #{password}"
  bolt_full_cli << " --format #{format}" if format != 'human'
  bolt_full_cli << if params.class == Hash
                     " --params '#{params.to_json}'"
                   else
                     " #{params}"
                   end
  # windows is special
  if fact_on(default, 'osfamily') == 'windows'
    bolt_full_cli << ' --transport winrm --user Administrator'
  end
  puts "BOLT_CLI: #{bolt_full_cli}" if ENV['BEAKER_debug']
  on(default, bolt_full_cli, acceptable_exit_codes: [0, 1, 2]).stdout
end
run_puppet_access_login(user:, password: '~!@ click to toggle source
# File lib/beaker-task_helper.rb, line 110
def run_puppet_access_login(user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
  peconf_password = get_unwrapped_pe_conf_value("console_admin_password")
  password = peconf_password if peconf_password != nil && peconf_password != ""
  on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
end
run_puppet_task(task_name:, params: nil, host: '127.0.0.1', format: 'human') click to toggle source
# File lib/beaker-task_helper.rb, line 196
def run_puppet_task(task_name:, params: nil, host: '127.0.0.1', format: 'human')
  args = ['task', 'run', task_name, target_flag(master), host]
  if params.class == Hash
    args << '--params'
    args << params.to_json
  else
    args << params
  end
  if format != 'human'
    args << '--format'
    args << format
  end
  on(master, puppet(*args), acceptable_exit_codes: [0, 1]).stdout
end
run_task(task_name:, params: nil, password: DEFAULT_PASSWORD, host: nil, format: 'human') click to toggle source
# File lib/beaker-task_helper.rb, line 142
def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD, host: nil, format: 'human')
  output = if pe_install?
             host = master.hostname if host.nil?
             run_puppet_task(task_name: task_name, params: params, host: host, format: format)
           else
             host = '127.0.0.1' if host.nil?
             run_bolt_task(task_name: task_name, params: params,
                           password: password, host: host, format: format)
           end

  if format == 'json'
    output = JSON.parse(output)
    output['items'][0]
  else
    output
  end
end
setup_ssh_access(task_runner, nodes) click to toggle source

Setup ssh access between task runner and nodes @param [Host] Task runner @param [Array<Host> Nodes on which to run the task

TODO: Implement on windows

# File lib/beaker-task_helper.rb, line 121
def setup_ssh_access(task_runner, nodes)
  task_runner_ssh_dir = '/root/.ssh'
  task_runner_rsa_pub = "#{task_runner_ssh_dir}/id_rsa.pub"
  on task_runner, "ssh-keygen -f #{task_runner_ssh_dir}/id_rsa -t rsa -N ''"
  public_key = on(task_runner, "cat #{task_runner_rsa_pub}").stdout

  nodes.each do |node|
      case node.platform
      when /solaris-10/
          ssh_dir_path = '/.ssh/'
      when /osx-10\.1(2|3|4)/
          ssh_dir_path = '/var/root/.ssh/'
      else
          ssh_dir_path = '/root/.ssh'
      end
      rsa_pub_path = "#{ssh_dir_path}/id_rsa.pub"
      create_remote_file(node, "#{rsa_pub_path}", public_key)
      on(node, "cat #{rsa_pub_path} >> #{ssh_dir_path}/authorized_keys")
  end
end
target_flag(host=default) click to toggle source
# File lib/beaker-task_helper.rb, line 102
def target_flag(host=default)
  if version_is_less('1.18.0', self.bolt_version(host))
    '--targets'
  else
    '--nodes'
  end
end
task_summary_line(total_hosts: 1, success_hosts: 1) click to toggle source
# File lib/beaker-task_helper.rb, line 217
def task_summary_line(total_hosts: 1, success_hosts: 1)
  "Job completed. #{success_hosts}/#{total_hosts} targets succeeded|Ran on #{total_hosts} target"
end