Class: Beaker::Answers

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/answers.rb

Overview

This class provides methods for generating PE answer file information.

Direct Known Subclasses

Version20, Version28, Version30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, hosts, options) ⇒ Hash

When given a Puppet Enterprise version, a list of hosts and other qualifying data this method will return a hash (keyed from the hosts) of default Puppet Enterprise answer file data hashes.

Parameters:

  • version (String)

    Puppet Enterprise version to generate answer data for

  • hosts (Array<Beaker::Host>)

    An array of host objects.

  • options (Hash)

    options for answer files

Options Hash (options):

  • :type (Symbol)

    Should be one of :upgrade or :install.



62
63
64
65
66
# File 'lib/beaker/answers.rb', line 62

def initialize(version, hosts, options)
  @version = version
  @hosts = hosts
  @options = options
end

Class Method Details

.create(version, hosts, options) ⇒ Hash

When given a Puppet Enterprise version, a list of hosts and other qualifying data this method will return the appropriate object that can be used to generate answer file data.

Parameters:

  • version (String)

    Puppet Enterprise version to generate answer data for

  • hosts (Array<Beaker::Host>)

    An array of host objects.

  • options (Hash)

    options for answer files

Options Hash (options):

  • :type (Symbol)

    Should be one of :upgrade or :install.

Returns:

  • (Hash)

    A hash (keyed from hosts) containing hashes of answer file data.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/beaker/answers.rb', line 16

def self.create version, hosts, options
  case version
  when /\A4\.0/
    return Version34.new(version, hosts, options)
  when /\A3\.99/
    return Version40.new(version, hosts, options)
  when /\A3\.8/
    return Version34.new(version, hosts, options)
  when /\A3\.7/
    return Version34.new(version, hosts, options)
  when /\A3\.4/
    return Version34.new(version, hosts, options)
  when /\A3\.[2-3]/
    return Version32.new(version, hosts, options)
  when /\A3\.1/
    return Version30.new(version, hosts, options)
  when /\A3\.0/
    return Version30.new(version, hosts, options)
  when /\A2\.8/
    return Version28.new(version, hosts, options)
  when /\A2\.0/
    return Version20.new(version, hosts, options)
  else
    raise NotImplementedError, "Don't know how to generate answers for #{version}"
  end
end

Instance Method Details

#answer_for(options, q, default = nil) ⇒ String

The answer value for a provided question. Use the user answer when available, otherwise return the default

Parameters:

  • options (Hash)

    options for answer file

  • default (String) (defaults to: nil)

    Should there be no user value for the provided question name return this default

Options Hash (options):

  • :answer (Symbol)

    Contains a hash of user provided question name and answer value pairs.

Returns:

  • (String)

    The answer value



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

def answer_for(options, q, default = nil)
  options[:answers][q] ? options[:answers][q] : default
end

#answer_string(host) ⇒ String

This converts a data hash provided by answers, and returns a Puppet Enterprise compatible answer file ready for use.

end

Examples:

Generating an answer file for a series of hosts

hosts.each do |host|
  answers = Beaker::Answers.new("2.0", hosts, "master")
  create_remote_file host, "/mypath/answer", answers.answer_string(host, answers)

Parameters:

  • host (Beaker::Host)

    Host object in question to generate the answer file for.

Returns:

  • (String)

    a string of answers



91
92
93
# File 'lib/beaker/answers.rb', line 91

def answer_string(host)
  answers[host.name].map { |k,v| "#{k}=#{v}" }.join("\n")
end

#answersHash

Access the answers hash for this version, host and option information. If the answers have not yet been calculated, generate them.

Returns:

  • (Hash)

    A hash of answers keyed by host.name



76
77
78
# File 'lib/beaker/answers.rb', line 76

def answers
  @answers ||= generate_answers
end

#generate_answersObject

Generate the answers hash based upon version, host and option information



69
70
71
# File 'lib/beaker/answers.rb', line 69

def generate_answers
  raise "This should be handled by subclasses!"
end