Module: RSpecSystemPuppet::Helpers

Includes:
RSpecSystem::Helpers, RSpecSystem::Log
Defined in:
lib/rspec-system-puppet/helpers.rb

Instance Method Summary collapse

Instance Method Details

#facter(opts = {}) {|result| ... } ⇒ Hash

Run facter on a remote machine

Parameters:

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

    a hash of opts

Options Hash (opts):

  • :node (RSpecSystem::Node)

    node to execute DSL on

Yields:

  • (result)

    yields result when called as a block

Yield Parameters:

  • result (Hash)

    a hash containing :facts, :exit_code, :stdout and :stderr

Returns:

  • (Hash)

    a hash of results



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rspec-system-puppet/helpers.rb', line 240

def facter(opts = {})
  # Defaults
  opts = {
    :node => rspec_system_node_set.default_node,
  }.merge(opts)

  node = opts[:node]

  raise "Must specify a node" unless node

  cmd = "facter -y"
  result = system_run(:n => node, :c => cmd)

  begin
    facts = YAML::load(result[:stdout])
    result[:facts] = facts
  rescue
  end

  if block_given?
    yield(result)
  else
    result
  end
end

#puppet_agent(opts = {}) {|result| ... } ⇒ Hash

Run puppet agent

Examples:

puppet_agent.do |r|
  r[:exit_code].should == 0
end

with debugging enabled

puppet_agent(:debug => true).do |r|
  r[:exit_code].should == 0
end

Parameters:

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

    a hash of opts

Options Hash (opts):

  • :node (RSpecSystem::Node)

    node to execute DSL on

  • :debug (Boolean)

    true if debugging required

  • :trace (Boolean)

    true if trace required

Yields:

  • (result)

    yields result when called as a block

Yield Parameters:

  • result (Hash)

    a hash containing :exit_code, :stdout and :stderr

Returns:

  • (Hash)

    a hash of results



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rspec-system-puppet/helpers.rb', line 98

def puppet_agent(opts = {})
  # Defaults etc.
  opts = {
    :node => rspec_system_node_set.default_node,
    :debug => false,
    :trace => true,
  }.merge(opts)

  node = opts[:node]

  cmd = "puppet agent -t --detailed-exitcodes"
  cmd += " --debug" if opts[:debug]
  cmd += " --trace" if opts[:trace]
  result = system_run(:n => node, :c => cmd)

  if block_given?
    yield(result)
  else
    result
  end
end

#puppet_apply(opts) {|result| ... } ⇒ Hash

Run puppet DSL code directly with ‘puppet apply`.

This takes a string of PuppetDSL code, uploads it to the test server and executes it directly with ‘puppet apply`.

Examples:

it "run notice" do
  puppet_apply("notice('foo')") do |r|
    r[:stdout].should =~ /foo/
  end
end

Parameters:

  • opts (Hash, String)

    a hash of opts, or a string containing the code to execute with option defaults

Options Hash (opts):

  • :code (String)

    the Puppet DSL code to execute

  • :node (RSpecSystem::Node)

    node to execute DSL on

  • :debug (Boolean)

    true if debugging required

  • :trace (Boolean)

    true if trace required

Yields:

  • (result)

    yields result when called as a block

Yield Parameters:

  • result (Hash)

    a hash containing :exit_code, :stdout and :stderr

Returns:

  • (Hash)

    a hash of results



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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rspec-system-puppet/helpers.rb', line 189

def puppet_apply(opts)
  if opts.is_a?(String)
    opts = {:code => opts}
  end

  # Defaults
  opts = {
    :node => rspec_system_node_set.default_node,
    :debug => false,
    :trace => true,
  }.merge(opts)

  code = opts[:code]
  node = opts[:node]

  raise 'Must provide code' unless code

  log.info("Copying DSL to remote host")
  file = Tempfile.new('rsp_puppet_apply')
  file.write(code)
  file.close

  remote_path = '/tmp/puppetapply.' + rand(1000000000).to_s
  r = system_rcp(:sp => file.path, :dp => remote_path, :d => node)
  file.unlink

  log.info("Cat file to see contents")
  system_run(:n => node, :c => "cat #{remote_path}")

  log.info("Now running puppet apply")
  cmd = "puppet apply --detailed-exitcodes"
  cmd += " --debug" if opts[:debug]
  cmd += " --trace" if opts[:trace]
  cmd += " #{remote_path}"
  result = system_run(:n => node, :c => cmd)

  if block_given?
    yield(result)
  else
    result
  end
end

#puppet_install(opts = {}) ⇒ Object

Basic helper to install puppet

Parameters:

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

    a hash of opts



10
11
12
13
14
15
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
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec-system-puppet/helpers.rb', line 10

def puppet_install(opts = {})
  # Grab facts from node
  facts = system_node.facts

  # Remove annoying mesg n from profile, otherwise on Debian we get:
  # stdin: is not a tty which messes with our tests later on.
  if facts['osfamily'] == 'Debian'
    log.info("Remove 'mesg n' from profile to stop noise")
    system_run("sed -i 's/^mesg n/# mesg n/' /root/.profile")
  end

  # Grab PL repository and install PL copy of puppet
  log.info "Starting installation of puppet from PL repos"
  if facts['osfamily'] == 'RedHat'
    if facts['operatingsystem'] == 'Fedora'
      # Fedora testing is probably the best for now
      system_run('sed -i "0,/RE/s/enabled=0/enabled=1/" /etc/yum.repos.d/fedora-updates-testing.repo')
    else
      if facts['operatingsystemrelease'] =~ /^6\./
        system_run('rpm -ivh http://yum.puppetlabs.com/el/6/products/x86_64/puppetlabs-release-6-6.noarch.rpm')
      else
        system_run('rpm -ivh http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm')
      end
    end
    system_run('yum install -y puppet')
  elsif facts['osfamily'] == 'Debian'
    system_run("wget http://apt.puppetlabs.com/puppetlabs-release-#{facts['lsbdistcodename']}.deb")
    system_run("dpkg -i puppetlabs-release-#{facts['lsbdistcodename']}.deb")
    system_run('apt-get update')
    system_run('apt-get install -y puppet')
  end

  # Prep modules dir
  log.info("Preparing modules dir")
  system_run('mkdir -p /etc/puppet/modules')

  pp = <<-EOS
host { 'puppet':
ip => '127.0.0.1',
}
  EOS
  puppet_apply(pp)
end

#puppet_master_installObject

Basic helper to install a puppet master

Parameters:

  • opts (Hash)

    a hash of opts



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec-system-puppet/helpers.rb', line 57

def puppet_master_install
  # Defaults etc.
  opts = {
    :node => rspec_system_node_set.default_node,
  }

  node = opts[:node]

  # Grab facts from node
  facts = system_node(:node => node).facts

  if facts['osfamily'] == 'RedHat'
    system_run(:n => node, :c => 'yum install -y puppet-server')
    if facts['operatingsystemrelease'] =~ /^5\./
      system_run(:n => node, :c => '/etc/init.d/puppetmaster start')
    else
      system_run(:n => node, :c => 'service puppetmaster start')
    end
  elsif facts['osfamily'] == 'Debian'
    system_run(:n => node, :c => 'apt-get install -y puppetmaster')
    system_run(:n => node, :c => 'service puppetmaster start')
  end
end

#puppet_module_install(opts) ⇒ Object

Helper to copy a module onto a node from source

Parameters:

  • opts (Hash)

    a hash of opts



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rspec-system-puppet/helpers.rb', line 123

def puppet_module_install(opts)
  # Defaults etc.
  opts = {
    :node => rspec_system_node_set.default_node,
    :module_path => "/etc/puppet/modules",
  }.merge(opts)

  source = opts[:source]
  module_name = opts[:module_name]
  module_path = opts[:module_path]
  node = opts[:node]

  raise "Must provide :source and :module_name parameters" unless source && module_name

  log.info("Now transferring module onto node")
  system_rcp(:sp => source, :d => node, :dp => File.join(module_path, module_name))
end

#puppet_resource(opts) ⇒ Object

Runs puppet resource commands

Parameters:

  • opts (Hash)

    a hash of opts



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rspec-system-puppet/helpers.rb', line 144

def puppet_resource(opts)
  if opts.is_a?(String)
    opts = {:resource => opts}
  end

  # Defaults
  opts = {
    :node => rspec_system_node_set.default_node
  }.merge(opts)

  resource = opts[:resource]
  node = opts[:node]

  raise 'Must provide resource' unless resource

  log.info("Now running puppet resource")
  result = system_run(:n => node, :c => "puppet resource #{resource}")

  if block_given?
    yield(result)
  else
    result
  end
end