Module: Beaker::TaskHelper

Includes:
DSL
Defined in:
lib/beaker-task_helper.rb,
lib/beaker-task_helper/version.rb

Overview

Beaker task Helper

Constant Summary collapse

VERSION =
'1.4.4'.freeze
DEFAULT_PASSWORD =
if ENV.has_key?('BEAKER_password')
  ENV['BEAKER_password']
elsif !defined?(default)
  'root'
elsif default[:hypervisor] == 'vagrant'
  'puppet'
else
  'root'
end
BOLT_VERSION =
'0.16.1'.freeze

Instance Method Summary collapse

Instance Method Details

#expect_multiple_regexes(result:, regexes:) ⇒ Object



139
140
141
142
143
# File 'lib/beaker-task_helper.rb', line 139

def expect_multiple_regexes(result:, regexes:)
  regexes.each do |regex|
    expect(result).to match(regex)
  end
end

#install_bolt_on(hosts, version = BOLT_VERSION, source = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/beaker-task_helper.rb', line 23

def install_bolt_on(hosts, version = 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|
    pp = "package { 'bolt' :\n  provider => 'puppet_gem',\n  ensure   => '\#{version}',\n"
    pp << "source   => '#{source}'" if source
    pp << '}'
    apply_manifest_on(host, pp)
  end
end

#pe_install?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/beaker-task_helper.rb', line 44

def pe_install?
  ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
end

#puppet_versionObject



7
8
9
# 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: 'localhost', format: 'human', module_path: '/etc/puppetlabs/code/modules') ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/beaker-task_helper.rb', line 85

def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD,
                  host: 'localhost', format: 'human', module_path: '/etc/puppetlabs/code/modules')
  if fact_on(default, 'osfamily') == 'windows'
    bolt_path = '/cygdrive/c/Program\ Files/Puppet\ Labs/Puppet/sys/ruby/bin/bolt.bat'
    unless module_path
      module_path = 'C:/ProgramData/PuppetLabs/code/modules'
    end

    if version_is_less('0.15.0', BOLT_VERSION)
      check = '--no-ssl'
    else
      check = '--insecure'
    end
  else
    bolt_path = '/opt/puppetlabs/puppet/bin/bolt'

    if version_is_less('0.15.0', 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} " \
                  "--nodes #{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: '~!@#$%^*-/ aZ', lifetime: '5y') ⇒ Object



48
49
50
# File 'lib/beaker-task_helper.rb', line 48

def (user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
  on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
end

#run_puppet_task(task_name:, params: nil, host: 'localhost', format: 'human') ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/beaker-task_helper.rb', line 124

def run_puppet_task(task_name:, params: nil, host: 'localhost', format: 'human')
  args = ['task', 'run', task_name, '--nodes', 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') ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/beaker-task_helper.rb', line 67

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 = 'localhost' 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) ⇒ Object

Setup ssh access between task runner and nodes TODO: Implement on windows

Parameters:

  • Task (Host)

    runner

  • Array<Host> (Array<Host> Nodes on which to run the task)

    Nodes on which to run the task



57
58
59
60
61
62
63
64
65
# File 'lib/beaker-task_helper.rb', line 57

def setup_ssh_access(task_runner, nodes)
  ssh_dir_path = '/root/.ssh/'
  rsa_pub_path = "#{ssh_dir_path}/id_rsa.pub"

  on task_runner, "ssh-keygen -f #{ssh_dir_path}/id_rsa -t rsa -N ''"
  public_key = on(task_runner, "cat #{rsa_pub_path}").stdout
  create_remote_file(nodes, "#{rsa_pub_path}", public_key)
  on(nodes, "cat #{rsa_pub_path} >> #{ssh_dir_path}/authorized_keys")
end

#task_summary_line(total_hosts: 1, success_hosts: 1) ⇒ Object



145
146
147
# File 'lib/beaker-task_helper.rb', line 145

def task_summary_line(total_hosts: 1, success_hosts: 1)
  "Job completed. #{success_hosts}/#{total_hosts} nodes succeeded|Ran on #{total_hosts} node"
end