lib/beaker-puppet/helpers/puppet_helpers.rb in beaker-puppet-1.29.0 vs lib/beaker-puppet/helpers/puppet_helpers.rb in beaker-puppet-2.0.0

- old
+ new

@@ -1,32 +1,30 @@ -# coding: utf-8 require 'timeout' require 'inifile' require 'resolv' module Beaker module DSL module Helpers # Methods that help you interact with your puppet installation, puppet must be installed # for these methods to execute correctly module PuppetHelpers - # Return the regular expression pattern for an IPv4 address def ipv4_regex - return /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/ + /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/ end # Return the IP address that given hostname returns when resolved on # the given host. # # @ param [Host] host One object that acts like a Beaker::Host # @ param [String] hostname The hostname to perform a DNS resolution on # # @return [String, nil] An IP address, or nil. def resolve_hostname_on(host, hostname) - match = curl_on(host, "--verbose #{hostname}", :accept_all_exit_codes => true).stderr.match(ipv4_regex) - return match ? match[0] : nil + match = curl_on(host, "--verbose #{hostname}", accept_all_exit_codes: true).stderr.match(ipv4_regex) + match ? match[0] : nil end # @!macro [new] common_opts # @param [Hash{Symbol=>String}] opts Options to alter execution. # @option opts [Boolean] :silent (false) Do not produce log output @@ -150,67 +148,73 @@ # # ...tests to be run... # end # def with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) - raise(ArgumentError, "with_puppet_running_on's conf_opts must be a Hash. You provided a #{conf_opts.class}: '#{conf_opts}'") if !conf_opts.kind_of?(Hash) + unless conf_opts.is_a?(Hash) + raise(ArgumentError, + "with_puppet_running_on's conf_opts must be a Hash. You provided a #{conf_opts.class}: '#{conf_opts}'") + end + cmdline_args = conf_opts[:__commandline_args__] service_args = conf_opts[:__service_args__] || {} restart_when_done = true restart_when_done = host[:restart_when_done] if host.has_key?(:restart_when_done) restart_when_done = conf_opts.fetch(:restart_when_done, restart_when_done) - conf_opts = conf_opts.reject { |k,v| [:__commandline_args__, :__service_args__, :restart_when_done].include?(k) } + conf_opts = conf_opts.reject do |k, v| + %i[__commandline_args__ __service_args__ restart_when_done].include?(k) + end curl_retries = host['master-start-curl-retries'] || options['master-start-curl-retries'] logger.debug "Setting curl retries to #{curl_retries}" if options[:is_puppetserver] || host[:is_puppetserver] confdir = puppet_config(host, 'confdir', section: 'master') vardir = puppet_config(host, 'vardir', section: 'master') if cmdline_args - split_args = cmdline_args.split() + split_args = cmdline_args.split split_args.each do |arg| case arg when /--confdir=(.*)/ - confdir = $1 + confdir = ::Regexp.last_match(1) when /--vardir=(.*)/ - vardir = $1 + vardir = ::Regexp.last_match(1) end end end puppetserver_opts = { - "jruby-puppet" => { - "master-conf-dir" => confdir, - "master-var-dir" => vardir, + 'jruby-puppet' => { + 'master-conf-dir' => confdir, + 'master-var-dir' => vardir, }, - "certificate-authority" => { - "allow-subject-alt-names" => true - } + 'certificate-authority' => { + 'allow-subject-alt-names' => true, + }, } - puppetserver_conf = File.join("#{host['puppetserver-confdir']}", "puppetserver.conf") + puppetserver_conf = File.join("#{host['puppetserver-confdir']}", 'puppetserver.conf') modify_tk_config(host, puppetserver_conf, puppetserver_opts) end begin backup_file = backup_the_file(host, puppet_config(host, 'confdir', section: 'master'), testdir, 'puppet.conf') lay_down_new_puppet_conf host, conf_opts, testdir if host.use_service_scripts? && !service_args[:bypass_service_script] - bounce_service( host, host['puppetservice'], curl_retries ) + bounce_service(host, host['puppetservice'], curl_retries) else - puppet_master_started = start_puppet_from_source_on!( host, cmdline_args ) + puppet_master_started = start_puppet_from_source_on!(host, cmdline_args) end yield self if block_given? - # FIXME: these test-flow-control exceptions should be using throw + # FIXME: these test-flow-control exceptions should be using throw # they can be caught in test_case. current layout dows not allow it rescue Beaker::DSL::Outcomes::PassTest => early_assertion pass_test(early_assertion) rescue Beaker::DSL::Outcomes::FailTest => early_assertion fail_test(early_assertion) @@ -221,133 +225,124 @@ rescue Beaker::DSL::Assertions, Minitest::Assertion => early_assertion fail_test(early_assertion) rescue Exception => early_exception original_exception = RuntimeError.new("PuppetAcceptance::DSL::Helpers.with_puppet_running_on failed (check backtrace for location) because: #{early_exception}\n#{early_exception.backtrace.join("\n")}\n") raise(original_exception) - ensure begin - if host.use_service_scripts? && !service_args[:bypass_service_script] - restore_puppet_conf_from_backup( host, backup_file ) + restore_puppet_conf_from_backup(host, backup_file) if restart_when_done - bounce_service( host, host['puppetservice'], curl_retries ) + bounce_service(host, host['puppetservice'], curl_retries) else host.exec puppet_resource('service', host['puppetservice'], 'ensure=stopped') end else if puppet_master_started - stop_puppet_from_source_on( host ) + stop_puppet_from_source_on(host) else dump_puppet_log(host) end - restore_puppet_conf_from_backup( host, backup_file ) + restore_puppet_conf_from_backup(host, backup_file) end - rescue Exception => teardown_exception begin - if !host.is_pe? - dump_puppet_log(host) - end + dump_puppet_log(host) unless host.is_pe? rescue Exception => dumping_exception logger.error("Raised during attempt to dump puppet logs: #{dumping_exception}") end - if original_exception - logger.error("Raised during attempt to teardown with_puppet_running_on: #{teardown_exception}\n---\n") - raise original_exception - else - raise teardown_exception - end + raise teardown_exception unless original_exception + + logger.error("Raised during attempt to teardown with_puppet_running_on: #{teardown_exception}\n---\n") + raise original_exception end end end # Test Puppet running in a certain run mode with specific options, # on the default host # @see #with_puppet_running_on - def with_puppet_running conf_opts, testdir = host.tmpdir(File.basename(@path)), &block + def with_puppet_running(conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) with_puppet_running_on(default, conf_opts, testdir, &block) end # @!visibility private - def restore_puppet_conf_from_backup( host, backup_file ) + def restore_puppet_conf_from_backup(host, backup_file) puppet_conf = puppet_config(host, 'config', section: 'master') if backup_file - host.exec( Command.new( "if [ -f '#{backup_file}' ]; then " + + host.exec(Command.new("if [ -f '#{backup_file}' ]; then " + "cat '#{backup_file}' > " + "'#{puppet_conf}'; " + "rm -f '#{backup_file}'; " + - "fi" ) ) + 'fi')) else - host.exec( Command.new( "rm -f '#{puppet_conf}'" )) + host.exec(Command.new("rm -f '#{puppet_conf}'")) end - end # @!visibility private - def start_puppet_from_source_on! host, args = '' - host.exec( puppet( 'master', args ) ) + def start_puppet_from_source_on!(host, args = '') + host.exec(puppet('master', args)) logger.debug 'Waiting for the puppet master to start' - unless port_open_within?( host, 8140, 10 ) - raise Beaker::DSL::FailTest, 'Puppet master did not start in a timely fashion' - end + raise Beaker::DSL::FailTest, 'Puppet master did not start in a timely fashion' unless port_open_within?( + host, 8140, 10 + ) + logger.debug 'The puppet master has started' - return true + true end # @!visibility private - def stop_puppet_from_source_on( host ) - pid = host.exec( Command.new('cat `puppet config print --section master pidfile`') ).stdout.chomp - host.exec( Command.new( "kill #{pid}" ) ) + def stop_puppet_from_source_on(host) + pid = host.exec(Command.new('cat `puppet config print --section master pidfile`')).stdout.chomp + host.exec(Command.new("kill #{pid}")) Timeout.timeout(10) do - while host.exec( Command.new( "kill -0 #{pid}"), :acceptable_exit_codes => [0,1] ).exit_code == 0 do + while host.exec(Command.new("kill -0 #{pid}"), acceptable_exit_codes: [0, 1]).exit_code == 0 # until kill -0 finds no process and we know that puppet has finished cleaning up sleep 1 end end end # @!visibility private def dump_puppet_log(host) syslogfile = case host['platform'] - when /fedora|centos|el|redhat|scientific/ then '/var/log/messages' - when /ubuntu|debian|cumulus/ then '/var/log/syslog' - else return - end + when /fedora|centos|el|redhat|scientific/ then '/var/log/messages' + when /ubuntu|debian/ then '/var/log/syslog' + else return + end logger.notify "\n*************************" - logger.notify "* Dumping master log *" - logger.notify "*************************" - host.exec( Command.new( "tail -n 100 #{syslogfile}" ), :acceptable_exit_codes => [0,1]) + logger.notify '* Dumping master log *' + logger.notify '*************************' + host.exec(Command.new("tail -n 100 #{syslogfile}"), acceptable_exit_codes: [0, 1]) logger.notify "*************************\n" end # @!visibility private - def lay_down_new_puppet_conf( host, configuration_options, testdir ) + def lay_down_new_puppet_conf(host, configuration_options, testdir) puppetconf_main = puppet_config(host, 'config', section: 'master') puppetconf_filename = File.basename(puppetconf_main) puppetconf_test = File.join(testdir, puppetconf_filename) - new_conf = puppet_conf_for( host, configuration_options ) + new_conf = puppet_conf_for(host, configuration_options) create_remote_file host, puppetconf_test, new_conf.to_s host.exec( - Command.new( "cat #{puppetconf_test} > #{puppetconf_main}" ), - :silent => true + Command.new("cat #{puppetconf_test} > #{puppetconf_main}"), + silent: true, ) - host.exec( Command.new( "cat #{puppetconf_main}" ) ) + host.exec(Command.new("cat #{puppetconf_main}")) end # @!visibility private - def puppet_conf_for host, conf_opts - puppetconf = host.exec( Command.new( "cat #{puppet_config(host, 'config', section: 'master')}" ) ).stdout - new_conf = IniFile.new(default: 'main', content: puppetconf).merge( conf_opts ) - - new_conf + def puppet_conf_for(host, conf_opts) + puppetconf = host.exec(Command.new("cat #{puppet_config(host, 'config', section: 'master')}")).stdout + IniFile.new(default: 'main', content: puppetconf).merge(conf_opts) end # Restarts the named puppet service # # @param [Host] host Host the service runs on @@ -355,26 +350,25 @@ # @param [Fixnum] curl_retries Number of seconds to wait for the restart to complete before failing # @param [Fixnum] port Port to check status at # # @return [Result] Result of last status check # @!visibility private - def bounce_service host, service, curl_retries = nil, port = nil + def bounce_service(host, service, curl_retries = nil, port = nil) curl_retries = 120 if curl_retries.nil? port = options[:puppetserver_port] if port.nil? if host.graceful_restarts? service = host.check_for_command('apache2ctl') ? 'apache2ctl' : 'apachectl' apachectl_path = host.is_pe? ? "#{host['puppetsbindir']}/#{service}" : service host.exec(Command.new("#{apachectl_path} graceful")) else result = host.exec(Command.new("service #{service} reload"), - :acceptable_exit_codes => [0,1,3]) - if result.exit_code == 0 - return result - else - host.exec puppet_resource('service', service, 'ensure=stopped') - host.exec puppet_resource('service', service, 'ensure=running') - end + acceptable_exit_codes: [0, 1, 3]) + return result if result.exit_code == 0 + + host.exec puppet_resource('service', service, 'ensure=stopped') + host.exec puppet_resource('service', service, 'ensure=running') + end curl_with_retries(" #{service} ", host, "https://localhost:#{port}", [35, 60], curl_retries) end # Runs 'puppet apply' on a remote host, piping manifest through stdin @@ -452,11 +446,11 @@ # # @return [Array<Result>, Result, nil] An array of results, a result # object, or nil. Check {Beaker::Shared::HostManager#run_block_on} for # more details on this. def apply_manifest_on(host, manifest, opts = {}, &block) - block_on host, opts do | host | + block_on host, opts do |host| on_options = {} on_options[:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes]) puppet_apply_opts = {} if opts[:debug] || ENV['BEAKER_PUPPET_DEBUG'] @@ -473,11 +467,12 @@ # From puppet help: # "... an exit code of '2' means there were changes, an exit code of # '4' means there were failures during the transaction, and an exit # code of '6' means there were both changes and failures." - if [opts[:catch_changes],opts[:catch_failures],opts[:expect_failures],opts[:expect_changes]].compact.length > 1 + if [opts[:catch_changes], opts[:catch_failures], opts[:expect_failures], + opts[:expect_changes],].compact.length > 1 raise(ArgumentError, 'Cannot specify more than one of `catch_failures`, ' + '`catch_changes`, `expect_failures`, or `expect_changes` ' + 'for a single manifest') end @@ -513,19 +508,17 @@ # the *args Array; if you do so, it will be treated specially. So, here # we check to see if our caller passed us a hash of environment variables # that they want to set for the puppet command. If so, we set the final # value of *args to a new hash with just one entry (the value of which # is our environment variables hash) - if opts.has_key?(:environment) - puppet_apply_opts['ENV'] = opts[:environment] - end + puppet_apply_opts['ENV'] = opts[:environment] if opts.has_key?(:environment) - file_path = host.tmpfile(%(apply_manifest_#{Time.now.strftime("%H%M%S%L")}.pp)) + file_path = host.tmpfile(%(apply_manifest_#{Time.now.strftime('%H%M%S%L')}.pp)) create_remote_file(host, file_path, manifest + "\n") if host[:default_apply_opts].respond_to? :merge - puppet_apply_opts = host[:default_apply_opts].merge( puppet_apply_opts ) + puppet_apply_opts = host[:default_apply_opts].merge(puppet_apply_opts) end on host, puppet('apply', file_path, puppet_apply_opts), on_options, &block end end @@ -535,13 +528,13 @@ def apply_manifest(manifest, opts = {}, &block) apply_manifest_on(default, manifest, opts, &block) end # @deprecated - def run_agent_on(host, arg='--no-daemonize --verbose --onetime --test', - options={}, &block) - block_on host do | host | + def run_agent_on(host, arg = '--no-daemonize --verbose --onetime --test', + options = {}, &block) + block_on host do |host| on host, puppet_agent(arg), options, &block end end # This method using the puppet resource 'host' will setup host aliases @@ -555,29 +548,29 @@ # @param ip_spec [Hash{String=>String}] a hash containing the host to ip # mappings # @param alias_spec [Hash{String=>Array[String]] an hash containing the host to alias(es) mappings to apply # @example Stub puppetlabs.com on the master to 127.0.0.1 with an alias example.com # stub_hosts_on(master, {'puppetlabs.com' => '127.0.0.1'}, {'puppetlabs.com' => ['example.com']}) - def stub_hosts_on(machine, ip_spec, alias_spec={}) - block_on machine do | host | + def stub_hosts_on(machine, ip_spec, alias_spec = {}) + block_on machine do |host| ip_spec.each do |address, ip| aliases = alias_spec[address] || [] - manifest =<<-EOS.gsub /^\s+/, "" + manifest = <<-EOS.gsub /^\s+/, '' host { '#{address}': \tensure => present, \tip => '#{ip}', \thost_aliases => #{aliases}, } EOS logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}") - apply_manifest_on( host, manifest ) + apply_manifest_on(host, manifest) end teardown do ip_spec.each do |address, ip| logger.notify("Unstubbing address #{address} to IP #{ip} on machine #{host}") - on( host, puppet('resource', 'host', address, 'ensure=absent') ) + on(host, puppet('resource', 'host', address, 'ensure=absent')) end end end end @@ -591,39 +584,36 @@ # @param alias_spec [Hash{String=>Array[String]] an hash containing the host to alias(es) mappings to apply # @example Stub forgeapi.puppetlabs.com on the master to 127.0.0.1 with an alias forgeapi.example.com # with_host_stubbed_on(master, {'forgeapi.puppetlabs.com' => '127.0.0.1'}, {'forgeapi.puppetlabs.com' => ['forgeapi.example.com']}) do # puppet( "module install puppetlabs-stdlib" ) # end - def with_host_stubbed_on(host, ip_spec, alias_spec={}, &block) - begin - block_on host do |host| - # this code is duplicated from the `stub_hosts_on` method. The - # `stub_hosts_on` method itself is not used here because this - # method is used by modules tests using `beaker-rspec`. Since - # the `stub_hosts_on` method contains a `teardown` step, it is - # incompatible with `beaker_rspec`. - ip_spec.each do |address, ip| - aliases = alias_spec[address] || [] - manifest =<<-EOS.gsub /^\s+/, "" + def with_host_stubbed_on(host, ip_spec, alias_spec = {}, &block) + block_on host do |host| + # this code is duplicated from the `stub_hosts_on` method. The + # `stub_hosts_on` method itself is not used here because this + # method is used by modules tests using `beaker-rspec`. Since + # the `stub_hosts_on` method contains a `teardown` step, it is + # incompatible with `beaker_rspec`. + ip_spec.each do |address, ip| + aliases = alias_spec[address] || [] + manifest = <<-EOS.gsub /^\s+/, '' host { '#{address}': \tensure => present, \tip => '#{ip}', \thost_aliases => #{aliases}, } - EOS - logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}") - apply_manifest_on( host, manifest ) - end + EOS + logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}") + apply_manifest_on(host, manifest) end + end - block.call - - ensure - ip_spec.each do |address, ip| - logger.notify("Unstubbing address #{address} to IP #{ip} on machine #{host}") - on( host, puppet('resource', 'host', address, 'ensure=absent') ) - end + block.call + ensure + ip_spec.each do |address, ip| + logger.notify("Unstubbing address #{address} to IP #{ip} on machine #{host}") + on(host, puppet('resource', 'host', address, 'ensure=absent')) end end # This method accepts a block and using the puppet resource 'host' will # setup host aliases before and after that block on the default host @@ -646,18 +636,20 @@ # # @param machine [String] the host to perform the stub on # @param forge_host [String] The URL to use as the forge alias, will default to using :forge_host in the # global options hash def stub_forge_on(machine, forge_host = nil) - #use global options hash + # use global options hash primary_forge_name = 'forge.puppetlabs.com' forge_host ||= options[:forge_host] forge_ip = resolve_hostname_on(machine, forge_host) raise "Failed to resolve forge host '#{forge_host}'" unless forge_ip + @forge_ip ||= forge_ip - block_on machine do | host | - stub_hosts_on(host, {primary_forge_name => @forge_ip}, {primary_forge_name => ['forge.puppet.com','forgeapi.puppetlabs.com','forgeapi.puppet.com']}) + block_on machine do |host| + stub_hosts_on(host, { primary_forge_name => @forge_ip }, + { primary_forge_name => ['forge.puppet.com', 'forgeapi.puppetlabs.com', 'forgeapi.puppet.com'] }) end end # This wraps the method `with_host_stubbed_on` and makes the stub specific to # the forge alias. @@ -669,38 +661,40 @@ # breaks TLS validation. # # @param host [String] the host to perform the stub on # @param forge_host [String] The URL to use as the forge alias, will default to using :forge_host in the # global options hash - def with_forge_stubbed_on( host, forge_host = nil, &block ) - #use global options hash + def with_forge_stubbed_on(host, forge_host = nil, &block) + # use global options hash primary_forge_name = 'forge.puppetlabs.com' forge_host ||= options[:forge_host] forge_ip = resolve_hostname_on(host, forge_host) raise "Failed to resolve forge host '#{forge_host}'" unless forge_ip + @forge_ip ||= forge_ip - with_host_stubbed_on( host, {primary_forge_name => @forge_ip}, {primary_forge_name => ['forge.puppet.com','forgeapi.puppetlabs.com','forgeapi.puppet.com']}, &block ) + with_host_stubbed_on(host, { primary_forge_name => @forge_ip }, + { primary_forge_name => ['forge.puppet.com', 'forgeapi.puppetlabs.com', 'forgeapi.puppet.com'] }, &block) end # This wraps `with_forge_stubbed_on` and provides it the default host # @see with_forge_stubbed_on # # @deprecated this method should not be used because stubbing the host # breaks TLS validation. - def with_forge_stubbed( forge_host = nil, &block ) - with_forge_stubbed_on( default, forge_host, &block ) + def with_forge_stubbed(forge_host = nil, &block) + with_forge_stubbed_on(default, forge_host, &block) end # This wraps the method `stub_hosts` and makes the stub specific to # the forge alias. # # @deprecated this method should not be used because stubbing the host # breaks TLS validation. # # @see #stub_forge_on def stub_forge(forge_host = nil) - #use global options hash + # use global options hash forge_host ||= options[:forge_host] stub_forge_on(default, forge_host) end # Waits until a successful curl check has happened against puppetdb @@ -712,23 +706,23 @@ # @return [Result] Result of the last HTTPS status check def sleep_until_puppetdb_started(host, nonssl_port = nil, ssl_port = nil) nonssl_port = options[:puppetdb_port_nonssl] if nonssl_port.nil? ssl_port = options[:puppetdb_port_ssl] if ssl_port.nil? pe_ver = host['pe_ver'] || '0' - if version_is_less(pe_ver, '2016.1.0') then + if version_is_less(pe_ver, '2016.1.0') # the status endpoint was introduced in puppetdb 4.0. The earliest # PE release with the 4.x pdb version was 2016.1.0 endpoint = 'pdb/meta/v1/version' expected_regex = '\"version\" \{0,\}: \{0,\}\"[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\"' else endpoint = 'status/v1/services/puppetdb-status' expected_regex = '\"state\" \{0,\}: \{0,\}\"running\"' end retry_on(host, "curl -m 1 http://localhost:#{nonssl_port}/#{endpoint} | grep '#{expected_regex}'", - {:max_retries => 120}) - curl_with_retries("start puppetdb (ssl)", + { max_retries: 120 }) + curl_with_retries('start puppetdb (ssl)', host, "https://#{host.node_name}:#{ssl_port}", [35, 60]) end # Waits until a successful curl check has happened against puppetserver # @@ -736,11 +730,11 @@ # @param [Fixnum] port Port to make the HTTPS status check over # # @return [Result] Result of the last HTTPS status check def sleep_until_puppetserver_started(host, port = nil) port = options[:puppetserver_port] if port.nil? - curl_with_retries("start puppetserver (ssl)", + curl_with_retries('start puppetserver (ssl)', host, "https://#{host.node_name}:#{port}", [35, 60]) end # Waits until a successful curl check has happaned against node classifier # @@ -748,26 +742,26 @@ # @param [Fixnum] port Port to make the HTTPS status check over # # @return [Result] Result of the last HTTPS status check def sleep_until_nc_started(host, port = nil) port = options[:nodeclassifier_port] if port.nil? - curl_with_retries("start nodeclassifier (ssl)", + curl_with_retries('start nodeclassifier (ssl)', host, "https://#{host.node_name}:#{port}", [35, 60]) end - #stops the puppet agent running on the host + # stops the puppet agent running on the host # @param [Host, Array<Host>, String, Symbol] agent One or more hosts to act upon, # or a role (String or Symbol) that identifies one or more hosts. # @param [Hash{Symbol=>String}] opts Options to alter execution. # @option opts [Boolean] :run_in_parallel Whether to run on each host in parallel. def stop_agent_on(agent, opts = {}) - block_on agent, opts do | host | + block_on agent, opts do |host| vardir = host.puppet_configprint['vardir'] # In 4.0 this was changed to just be `puppet` agent_service = 'puppet' - if !aio_version?(host) + unless aio_version?(host) # The agent service is `pe-puppet` everywhere EXCEPT certain linux distros on PE 2.8 # In all the case that it is different, this init script will exist. So we can assume # that if the script doesn't exist, we should just use `pe-puppet` agent_service = 'pe-puppet-agent' agent_service = 'pe-puppet' unless host.file_exist?('/etc/init.d/pe-puppet-agent') @@ -776,41 +770,38 @@ # Under a number of stupid circumstances, we can't stop the # agent using puppet. This is usually because of issues with # the init script or system on that particular configuration. avoid_puppet_at_all_costs = false avoid_puppet_at_all_costs ||= host['platform'] =~ /el-4/ - avoid_puppet_at_all_costs ||= host['pe_ver'] && version_is_less(host['pe_ver'], '3.2') && host['platform'] =~ /sles/ + avoid_puppet_at_all_costs ||= host['pe_ver'] && version_is_less(host['pe_ver'], + '3.2') && host['platform'] =~ /sles/ if avoid_puppet_at_all_costs # When upgrading, puppet is already stopped. On EL4, this causes an exit code of '1' - on host, "/etc/init.d/#{agent_service} stop", :acceptable_exit_codes => [0, 1] + on host, "/etc/init.d/#{agent_service} stop", acceptable_exit_codes: [0, 1] else on host, puppet_resource('service', agent_service, 'ensure=stopped') end - #Ensure that a puppet run that was started before the last lock check is completed + # Ensure that a puppet run that was started before the last lock check is completed agent_running = true while agent_running agent_running = host.file_exist?("#{vardir}/state/agent_catalog_run.lock") - if agent_running - sleep 2 - end + sleep 2 if agent_running end - end end - #stops the puppet agent running on the default host + # stops the puppet agent running on the default host # @see #stop_agent_on def stop_agent stop_agent_on(default) end - #wait for a given host to appear in the dashboard + # wait for a given host to appear in the dashboard # @deprecated this method should be removed in the next release since we don't believe the check is necessary. def wait_for_host_in_dashboard(host) - hostname = host.node_name hostcert = dashboard.puppet['hostcert'] key = dashboard.puppet['hostprivkey'] cacert = dashboard.puppet['localcacert'] retry_on(dashboard, "curl --cert #{hostcert} --key #{key} --cacert #{cacert}\ @@ -827,68 +818,68 @@ # @raise [FailTest] if process times out def sign_certificate_for(host = []) hostnames = [] hosts = host.is_a?(Array) ? host : [host] puppet_version = on(master, puppet('--version')).stdout.chomp - hosts.each{ |current_host| + hosts.each do |current_host| if [master, dashboard, database].include? current_host - on current_host, puppet( 'agent -t' ), :acceptable_exit_codes => [0,1,2] + on current_host, puppet('agent -t'), acceptable_exit_codes: [0, 1, 2] if version_is_less(puppet_version, '5.99') - on master, puppet("cert --allow-dns-alt-names sign #{current_host}" ), :acceptable_exit_codes => [0,24] + on master, puppet("cert --allow-dns-alt-names sign #{current_host}"), acceptable_exit_codes: [0, 24] else on master, "puppetserver ca sign --certname #{current_host}" end else - hostnames << Regexp.escape( current_host.node_name ) + hostnames << Regexp.escape(current_host.node_name) end - } + end if hostnames.size < 1 if version_is_less(puppet_version, '5.99') - on master, puppet("cert --sign --all --allow-dns-alt-names"), - :acceptable_exit_codes => [0,24] + on master, puppet('cert --sign --all --allow-dns-alt-names'), + acceptable_exit_codes: [0, 24] else - on master, 'puppetserver ca sign --all', :acceptable_exit_codes => [0, 24] + on master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24] end return end while hostnames.size > 0 last_sleep = 0 next_sleep = 1 - (0..10).each do |i| + 11.times do |i| if i == 10 fail_test("Failed to sign cert for #{hostnames}") hostnames.clear end if version_is_less(puppet_version, '5.99') - on master, puppet("cert --sign --all --allow-dns-alt-names"), :acceptable_exit_codes => [0,24] - out = on(master, puppet("cert --list --all")).stdout + on master, puppet('cert --sign --all --allow-dns-alt-names'), acceptable_exit_codes: [0, 24] + out = on(master, puppet('cert --list --all')).stdout if hostnames.all? { |hostname| out =~ /\+ "?#{hostname}"?/ } hostnames.clear break end else - on master, 'puppetserver ca sign --all', :acceptable_exit_codes => [0, 24] + on master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24] out = on(master, 'puppetserver ca list --all').stdout if out !~ /.*Requested.*/ && hostnames.all? { |hostname| out =~ /\b#{hostname}\b/ } hostnames.clear break end end sleep next_sleep - (last_sleep, next_sleep) = next_sleep, last_sleep+next_sleep + (last_sleep, next_sleep) = next_sleep, last_sleep + next_sleep end end host end - #prompt the master to sign certs then check to confirm the cert for the default host is signed - #@see #sign_certificate_for + # prompt the master to sign certs then check to confirm the cert for the default host is signed + # @see #sign_certificate_for def sign_certificate sign_certificate_for(default) end # Create a temp directory on remote host, optionally owned by specified user and group. @@ -905,18 +896,18 @@ # an array of names of newly-created dirs per-host # # @note While tempting, this method should not be "optimized" to coalesce calls to # chown user:group when both options are passed, as doing so will muddy the spec. def create_tmpdir_on(hosts, path_prefix = '', user = nil, group = nil) - block_on hosts do | host | + block_on hosts do |host| # create the directory dir = host.tmpdir(path_prefix) # only chown if explicitly passed; don't make assumptions about perms # only `chown user` for cleaner codepaths if user # ensure user exists - if not host.user_get(user).success? + unless host.user_get(user).success? # clean up host.rm_rf("#{dir}") raise "User #{user} does not exist on #{host}." end # chown only user @@ -924,11 +915,11 @@ # on host, "chown #{user} #{dir}" end # only chgrp if explicitly passed; don't make assumptions about perms if group # ensure group exists - if not host.group_get(group).success? + unless host.group_get(group).success? # clean up # on host, "rmdir #{dir}" host.rm_rf(dir) raise "Group #{group} does not exist on #{host}." end @@ -951,10 +942,10 @@ # directory. If no username is specified, use `puppet config print user # --section master` to obtain username from master. Raise RuntimeError # if this puppet command returns a non-zero exit code. # # @return [String] Returns the name of the newly-created dir. - def create_tmpdir_for_user(host, name='/tmp/beaker', user=nil) + def create_tmpdir_for_user(host, name = '/tmp/beaker', user = nil) user ||= puppet_config(host, 'user', section: 'master') create_tmpdir_on(host, name, user) end end end