Module: MasterManipulator::Service

Included in:
Beaker::TestCase
Defined in:
lib/master_manipulator/service.rb

Instance Method Summary collapse

Instance Method Details

#pe_version(master_host) ⇒ String

Return the version of PE installed on the specified Puppet master

Examples:

Return the version of PE running on master

ver = pe_version(master)

Parameters:

  • master_host (Beaker::Host)

    the master to query.

Returns:

  • (String)

    The version of Puppet Enterprise, or ‘version unknown’ if undetermined



96
97
98
99
100
101
102
103
104
# File 'lib/master_manipulator/service.rb', line 96

def pe_version(master_host)
  if on(master_host, 'test -f /opt/puppet/pe_version', :acceptable_exit_codes => [0,1]).exit_code == 0
    return on(master_host, 'cat /opt/puppet/pe_version').stdout.chomp
  elsif on(master_host, 'test -f /opt/puppetlabs/server/pe_version', :acceptable_exit_codes => [0,1]).exit_code == 0
    return on(master_host, 'cat /opt/puppetlabs/server/pe_version').stdout.chomp
  else
    return 'version unknown'
  end
end

#reload_puppet_server(master_host, opts = {}) ⇒ Object

Reload puppetserver, causing it to reread its config without restarting the JVM. use this instead of restart_puppet_server unless you absolutely have to stop and restart the server because JVM restarts are very expensive. This implementation relies on puppetserver’s “reload” subcommand, which handles all the waiting for services to refresh. Older code using a forced HUP on the server process should be replaced with this.

Examples:

Restart the puppetserver process on a PE or FOSS master

restart_puppet_server(master)

Parameters:

  • master_host (Beaker::Host)

    The host to manipulate.

  • opts (Hash) (defaults to: {})

    Optional options hash containing options

Returns:

  • nil



18
19
20
21
22
23
24
# File 'lib/master_manipulator/service.rb', line 18

def reload_puppet_server(master_host, opts = {})
  # 2015.x (Everett) and newer
  rc = on(master_host, "puppetserver reload", :accept_all_exit_codes => true)
  if rc.exit_code != 0
    raise "'puppetserver reload' failed, returned: #{rc.exit_code}"
  end
end

#restart_puppet_server(master_host, opts = {}) ⇒ Object

Restart the puppet server and wait for it to come back up or raise an error if the wait times out

Examples:

Restart the puppetserver process on a PE master

restart_puppet_server(master)

Restart the puppetserver process on a FOSS master

restart_puppet_server(master, {:is_pe? => false})

Restart the puppetserver process on a PE master, timing out after 20 attempts

restart_puppet_server(master, {:wait_cycles => 20})

Restart the puppetserver process on a FOSS master, timing out after 20 attempts

restart_puppet_server(master, {:is_pe? => false, :wait_cycles => 20})

Parameters:

  • master_host (Beaker::Host)

    The host to manipulate.

  • opts (Hash) (defaults to: {})

    Optional options hash containing options

Options Hash (opts):

  • :is_pe? (Boolean)

    true if host is running PE, false otherwise

  • :wait_cycles (Integer)

    How many attempt to make before failing

Returns:

  • nil



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/master_manipulator/service.rb', line 41

def restart_puppet_server(master_host, opts = {})

  start_time = Time.now

  opts[:is_pe?] ||= true
  opts[:is_pe?] ? service_name = 'pe-puppetserver' : service_name = 'puppetserver'

  on(master_host, puppet("resource service #{service_name} ensure=stopped"))
  on(master_host, puppet("resource service #{service_name} ensure=running"))
  hostname = on(master_host, 'hostname').stdout.chomp
  opts[:wait_cycles] ||= 10

  pe_ver = pe_version(master_host)
  three_eight_regex = /^3\.8/

  # This logic is not ideal refactor in the future
  # if its PE and not 3.8 use the status endpoint
  # it its not PE or it is 3.8 use the simple curl call
  if pe_ver && !pe_ver.match(three_eight_regex)
    curl_call = "-k -X GET -H 'Content-Type: application/json' https://#{hostname}:8140/status/v1/services?level=debug"
  else
    curl_call = "-I -k https://#{hostname}:8140"
  end

  (1..opts[:wait_cycles]).each do |i|
    @result = curl_on(master_host, curl_call, :acceptable_exit_codes => [0,1,7])
    # parse body if we are using PE and we are not in PE 3.8
    (pe_ver && !pe_ver.match(three_eight_regex)) ? @body = JSON.parse(@result.stdout) : @body = []

    case @result.exit_code.to_s
      when '0'
        sleep 20
        pe_ver.match(/three_eight_regex/) ? return : (return if @body.all? { |k, v| v['state'] == 'running' })
      when '1', '7'
        # Exit code 7 is 'connection refused'
        sleep (i**(1.2))
    end
  end

  total_time = Time.now - start_time
  message = "Attempted to restart #{opts[:wait_cycles]} times, waited #{total_time} seconds total."
  message << "\nHere is the status reported by the puppetserver"

  @body.each do |k,v|
    message << "\n'#{k}' state: #{v['state']} "
  end
  raise message

end