Module: Beaker::DSL::Helpers::PuppetHelpers
- Included in:
- Beaker::DSL::Helpers
- Defined in:
- lib/beaker/dsl/helpers/puppet_helpers.rb
Overview
Methods that help you interact with your puppet installation, puppet must be installed for these methods to execute correctly
Instance Method Summary collapse
-
#apply_manifest(manifest, opts = {}, &block) ⇒ Object
Runs ‘puppet apply’ on default host, piping manifest through stdin.
-
#apply_manifest_on(host, manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on a remote host, piping manifest through stdin.
-
#create_tmpdir_for_user(host, name = '/tmp/beaker', user = nil) ⇒ String
Create a temp directory on remote host with a user.
-
#puppet_group(host) ⇒ Object
Return the name of the puppet group.
-
#puppet_user(host) ⇒ Object
Return the name of the puppet user.
- #run_agent_on(host, arg = '--no-daemonize --verbose --onetime --test', options = {}, &block) ⇒ Object deprecated Deprecated.
-
#sign_certificate ⇒ Object
prompt the master to sign certs then check to confirm the cert for the default host is signed.
-
#sign_certificate_for(host) ⇒ Object
Ensure the host has requested a cert, then sign it.
-
#sleep_until_nc_started(host, port = nil) ⇒ Result
Waits until a successful curl check has happaned against node classifier.
-
#sleep_until_puppetdb_started(host, nonssl_port = nil, ssl_port = nil) ⇒ Result
Waits until a successful curl check has happened against puppetdb.
-
#sleep_until_puppetserver_started(host, port = nil) ⇒ Result
Waits until a successful curl check has happened against puppetserver.
-
#stop_agent ⇒ Object
stops the puppet agent running on the default host.
-
#stop_agent_on(agent) ⇒ Object
stops the puppet agent running on the host.
-
#stub_forge(forge_host = nil) ⇒ Object
This wraps the method ‘stub_hosts` and makes the stub specific to the forge alias.
-
#stub_forge_on(machine, forge_host = nil) ⇒ Object
This wraps the method ‘stub_hosts_on` and makes the stub specific to the forge alias.
-
#stub_hosts(ip_spec) ⇒ Object
This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block on the default host.
-
#stub_hosts_on(machine, ip_spec) ⇒ Object
This method using the puppet resource ‘host’ will setup host aliases and register the remove of host aliases via Beaker::TestCase#teardown.
-
#wait_for_host_in_dashboard(host) ⇒ Object
wait for a given host to appear in the dashboard.
-
#with_forge_stubbed(forge_host = nil, &block) ⇒ Object
This wraps ‘with_forge_stubbed_on` and provides it the default host.
-
#with_forge_stubbed_on(host, forge_host = nil, &block) ⇒ Object
This wraps the method ‘with_host_stubbed_on` and makes the stub specific to the forge alias.
-
#with_host_stubbed_on(host, ip_spec, &block) ⇒ Object
This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block.
-
#with_puppet_running(conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object
Test Puppet running in a certain run mode with specific options, on the default host.
-
#with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object
Test Puppet running in a certain run mode with specific options.
Instance Method Details
#apply_manifest(manifest, opts = {}, &block) ⇒ Object
Runs ‘puppet apply’ on default host, piping manifest through stdin
475 476 477 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 475 def apply_manifest(manifest, opts = {}, &block) apply_manifest_on(default, manifest, opts, &block) end |
#apply_manifest_on(host, manifest, opts = {}, &block) ⇒ Array<Result>, ...
Runs ‘puppet apply’ on a remote host, piping manifest through stdin
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 397 def apply_manifest_on(host, manifest, opts = {}, &block) block_on host do | host | = {} [:acceptable_exit_codes] = Array(opts[:acceptable_exit_codes]) puppet_apply_opts = {} if opts[:debug] puppet_apply_opts[:debug] = nil else puppet_apply_opts[:verbose] = nil end puppet_apply_opts[:parseonly] = nil if opts[:parseonly] puppet_apply_opts[:trace] = nil if opts[:trace] puppet_apply_opts[:parser] = 'future' if opts[:future_parser] puppet_apply_opts[:modulepath] = opts[:modulepath] if opts[:modulepath] puppet_apply_opts[:noop] = nil if opts[:noop] # 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 raise(ArgumentError, 'Cannot specify more than one of `catch_failures`, ' + '`catch_changes`, `expect_failures`, or `expect_changes` ' + 'for a single manifest') end if opts[:catch_changes] puppet_apply_opts['detailed-exitcodes'] = nil # We're after idempotency so allow exit code 0 only. [:acceptable_exit_codes] |= [0] elsif opts[:catch_failures] puppet_apply_opts['detailed-exitcodes'] = nil # We're after only complete success so allow exit codes 0 and 2 only. [:acceptable_exit_codes] |= [0, 2] elsif opts[:expect_failures] puppet_apply_opts['detailed-exitcodes'] = nil # We're after failures specifically so allow exit codes 1, 4, and 6 only. [:acceptable_exit_codes] |= [1, 4, 6] elsif opts[:expect_changes] puppet_apply_opts['detailed-exitcodes'] = nil # We're after changes specifically so allow exit code 2 only. [:acceptable_exit_codes] |= [2] else # Either use the provided acceptable_exit_codes or default to [0] [:acceptable_exit_codes] |= [0] end # Not really thrilled with this implementation, might want to improve it # later. Basically, there is a magic trick in the constructor of # PuppetCommand which allows you to pass in a Hash for the last value in # 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 file_path = host.tmpfile('apply_manifest.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 ) end on host, puppet('apply', file_path, puppet_apply_opts), , &block end end |
#create_tmpdir_for_user(host, name = '/tmp/beaker', user = nil) ⇒ String
Create a temp directory on remote host with a user. Default user is puppet master user.
the ownership of a temp directory. directory. Default value is ‘/tmp/beaker’ directory. If no username is specified, use ‘puppet master –configprint user` to obtain username from master. Raise RuntimeError if this puppet command returns a non-zero exit code.
757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 757 def create_tmpdir_for_user(host, name='/tmp/beaker', user=nil) if not user result = on host, puppet("master --configprint user") if not result.exit_code == 0 raise "`puppet master --configprint` failed, check that puppet is installed on #{host} or explicitly pass in a user name." end user = result.stdout.strip end create_tmpdir_on(host, name, user) end |
#puppet_group(host) ⇒ Object
This method assumes puppet is installed on the host.
Return the name of the puppet group.
49 50 51 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 49 def puppet_group(host) return host.puppet('master')['group'] end |
#puppet_user(host) ⇒ Object
This method assumes puppet is installed on the host.
Return the name of the puppet user.
39 40 41 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 39 def puppet_user(host) return host.puppet('master')['user'] end |
#run_agent_on(host, arg = '--no-daemonize --verbose --onetime --test', options = {}, &block) ⇒ Object
480 481 482 483 484 485 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 480 def run_agent_on(host, arg='--no-daemonize --verbose --onetime --test', ={}, &block) block_on host do | host | on host, puppet_agent(arg), , &block end end |
#sign_certificate ⇒ Object
prompt the master to sign certs then check to confirm the cert for the default host is signed
740 741 742 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 740 def sign_certificate sign_certificate_for(default) end |
#sign_certificate_for(host) ⇒ Object
Ensure the host has requested a cert, then sign it
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 712 def sign_certificate_for(host) block_on host do | host | if [master, dashboard, database].include? host on host, puppet( 'agent -t' ), :acceptable_exit_codes => [0,1,2] on master, puppet( "cert --allow-dns-alt-names sign #{host}" ), :acceptable_exit_codes => [0,24] else hostname = Regexp.escape host.node_name last_sleep = 0 next_sleep = 1 (0..10).each do |i| fail_test("Failed to sign cert for #{hostname}") if i == 10 on master, puppet("cert --sign --all --allow-dns-alt-names"), :acceptable_exit_codes => [0,24] break if on(master, puppet("cert --list --all")).stdout =~ /\+ "?#{hostname}"?/ sleep next_sleep (last_sleep, next_sleep) = next_sleep, last_sleep+next_sleep end end end end |
#sleep_until_nc_started(host, port = nil) ⇒ Result
Waits until a successful curl check has happaned against node classifier
642 643 644 645 646 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 642 def sleep_until_nc_started(host, port = nil) port = [:nodeclassifier_port] if port.nil? curl_with_retries("start nodeclassifier (ssl)", host, "https://#{host.node_name}:#{port}", [35, 60]) end |
#sleep_until_puppetdb_started(host, nonssl_port = nil, ssl_port = nil) ⇒ Result
Waits until a successful curl check has happened against puppetdb
616 617 618 619 620 621 622 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 616 def sleep_until_puppetdb_started(host, nonssl_port = nil, ssl_port = nil) nonssl_port = [:puppetdb_port_nonssl] if nonssl_port.nil? ssl_port = [:puppetdb_port_ssl] if ssl_port.nil? curl_with_retries("start puppetdb", host, "http://localhost:#{nonssl_port}", 0, 120) curl_with_retries("start puppetdb (ssl)", host, "https://#{host.node_name}:#{ssl_port}", [35, 60]) end |
#sleep_until_puppetserver_started(host, port = nil) ⇒ Result
Waits until a successful curl check has happened against puppetserver
630 631 632 633 634 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 630 def sleep_until_puppetserver_started(host, port = nil) port = [:puppetserver_port] if port.nil? curl_with_retries("start puppetserver (ssl)", host, "https://#{host.node_name}:#{port}", [35, 60]) end |
#stop_agent ⇒ Object
stops the puppet agent running on the default host
690 691 692 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 690 def stop_agent stop_agent_on(default) end |
#stop_agent_on(agent) ⇒ Object
stops the puppet agent running on the host
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 651 def stop_agent_on(agent) block_on agent do | host | vardir = agent.puppet_configprint['vardir'] agent_running = true while agent_running agent_running = agent.file_exist?("#{vardir}/state/agent_catalog_run.lock") if agent_running sleep 2 end end # In 4.0 this was changed to just be `puppet` agent_service = 'puppet' if !aio_version?(agent) # 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 agent.file_exist?('/etc/init.d/pe-puppet-agent') end # 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 ||= agent['platform'] =~ /el-4/ avoid_puppet_at_all_costs ||= agent['pe_ver'] && version_is_less(agent['pe_ver'], '3.2') && agent['platform'] =~ /sles/ if avoid_puppet_at_all_costs # When upgrading, puppet is already stopped. On EL4, this causes an exit code of '1' on agent, "/etc/init.d/#{agent_service} stop", :acceptable_exit_codes => [0, 1] else on agent, puppet_resource('service', agent_service, 'ensure=stopped') end end end |
#stub_forge(forge_host = nil) ⇒ Object
This wraps the method ‘stub_hosts` and makes the stub specific to the forge alias.
603 604 605 606 607 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 603 def stub_forge(forge_host = nil) #use global options hash forge_host ||= [:forge_host] stub_forge_on(default, forge_host) end |
#stub_forge_on(machine, forge_host = nil) ⇒ Object
This wraps the method ‘stub_hosts_on` and makes the stub specific to the forge alias.
forge api v1 canonical source is forge.puppetlabs.com forge api v3 canonical source is forgeapi.puppetlabs.com
564 565 566 567 568 569 570 571 572 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 564 def stub_forge_on(machine, forge_host = nil) #use global options hash forge_host ||= [:forge_host] @forge_ip ||= Resolv.getaddress(forge_host) block_on machine do | host | stub_hosts_on(host, 'forge.puppetlabs.com' => @forge_ip) stub_hosts_on(host, 'forgeapi.puppetlabs.com' => @forge_ip) end end |
#stub_hosts(ip_spec) ⇒ Object
This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block on the default host
551 552 553 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 551 def stub_hosts(ip_spec) stub_hosts_on(default, ip_spec) end |
#stub_hosts_on(machine, ip_spec) ⇒ Object
This method using the puppet resource ‘host’ will setup host aliases and register the remove of host aliases via Beaker::TestCase#teardown
A teardown step is also added to make sure unstubbing of the host is removed always.
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 499 def stub_hosts_on(machine, ip_spec) block_on machine do | host | ip_spec.each do |address, ip| logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}") on( host, puppet('resource', 'host', address, 'ensure=present', "ip=#{ip}") ) 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') ) end end end end |
#wait_for_host_in_dashboard(host) ⇒ Object
wait for a given host to appear in the dashboard
695 696 697 698 699 700 701 702 703 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 695 def wait_for_host_in_dashboard(host) hostname = host.node_name if host['platform'] =~ /aix/ then curl_opts = '--tlsv1 -I' else curl_opts = '--tlsv1 -k -I' end retry_on(dashboard, "! curl #{curl_opts} https://#{dashboard}/nodes/#{hostname} | grep '404 Not Found'") end |
#with_forge_stubbed(forge_host = nil, &block) ⇒ Object
This wraps ‘with_forge_stubbed_on` and provides it the default host
595 596 597 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 595 def with_forge_stubbed( forge_host = nil, &block ) with_forge_stubbed_on( default, forge_host, &block ) end |
#with_forge_stubbed_on(host, forge_host = nil, &block) ⇒ Object
This wraps the method ‘with_host_stubbed_on` and makes the stub specific to the forge alias.
forge api v1 canonical source is forge.puppetlabs.com forge api v3 canonical source is forgeapi.puppetlabs.com
583 584 585 586 587 588 589 590 591 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 583 def with_forge_stubbed_on( host, forge_host = nil, &block ) #use global options hash forge_host ||= [:forge_host] @forge_ip ||= Resolv.getaddress(forge_host) with_host_stubbed_on( host, {'forge.puppetlabs.com' => @forge_ip, 'forgeapi.puppetlabs.com' => @forge_ip}, &block ) end |
#with_host_stubbed_on(host, ip_spec, &block) ⇒ Object
This method accepts a block and using the puppet resource ‘host’ will setup host aliases before and after that block.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 526 def with_host_stubbed_on(host, ip_spec, &block) begin block_on host do |host| ip_spec.each_pair do |address, ip| logger.notify("Stubbing address #{address} to IP #{ip} on machine #{host}") on( host, puppet('resource', 'host', address, 'ensure=present', "ip=#{ip}") ) 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 end end |
#with_puppet_running(conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object
Test Puppet running in a certain run mode with specific options, on the default host
220 221 222 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 220 def with_puppet_running conf_opts, testdir = host.tmpdir(File.basename(@path)), &block with_puppet_running_on(default, conf_opts, testdir, &block) end |
#with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(@path)), &block) ⇒ Object
Whether Puppet is started or restarted depends on what kind of server you’re running. Passenger and puppetserver are restarted before. Webrick is started before and stopped after yielding, unless you’re using service scripts, then it’ll behave like passenger & puppetserver. Passenger and puppetserver (or webrick using service scripts) restart after yielding by default. You can stop this from happening by setting the :restart_when_done flag of the conf_opts argument.
Test Puppet running in a certain run mode with specific options. This ensures the following steps are performed:
-
The pre-test Puppet configuration is backed up
-
A new Puppet configuraton file is layed down
-
Puppet is started or restarted in the specified run mode
-
Ensure Puppet has started correctly
-
Further tests are yielded to
-
Revert Puppet to the pre-test state
-
Testing artifacts are saved in a folder named for the test
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/beaker/dsl/helpers/puppet_helpers.rb', line 124 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) 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) } curl_retries = host['master-start-curl-retries'] || ['master-start-curl-retries'] logger.debug "Setting curl retries to #{curl_retries}" if [:is_puppetserver] || host[:is_puppetserver] confdir = host.puppet('master')['confdir'] vardir = host.puppet('master')['vardir'] if cmdline_args split_args = cmdline_args.split() split_args.each do |arg| case arg when /--confdir=(.*)/ confdir = $1 when /--vardir=(.*)/ vardir = $1 end end end puppetserver_opts = { "jruby-puppet" => { "master-conf-dir" => confdir, "master-var-dir" => vardir, }} 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, host.puppet('master')['confdir'], 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 ) else puppet_master_started = start_puppet_from_source_on!( host, cmdline_args ) end yield self if block_given? 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 ) if restart_when_done 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 ) else dump_puppet_log(host) end restore_puppet_conf_from_backup( host, backup_file ) end rescue Exception => teardown_exception begin if !host.is_pe? dump_puppet_log(host) end 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 end end end |