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| ... } ⇒ RSpecSystem::Result

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 (RSpecSystem::Result)

    result containing :facts, :exit_code, :stdout and :stderr

Returns:

  • (RSpecSystem::Result)

    a hash of results



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rspec-system-puppet/helpers.rb', line 257

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| ... } ⇒ RSpecSystem::Result

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 (RSpecSystem::Result)

    a hash containing :exit_code, :stdout and :stderr

Returns:

  • (RSpecSystem::Result)

    results containing keys :exit_code, :stdout and :stderr



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rspec-system-puppet/helpers.rb', line 112

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| ... } ⇒ RSpecSystem::Result

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 (RSpecSystem::Result)

    a hash containing :exit_code, :stdout and :stderr

Returns:

  • (RSpecSystem::Result)

    results containing keys :exit_code, :stdout and :stderr



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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rspec-system-puppet/helpers.rb', line 206

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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')

  # Create alias for puppet
  pp = <<-EOS
host { 'puppet':
ip => '127.0.0.1',
}
  EOS
  puppet_apply(pp)

  # Create hiera.yaml
  file = Tempfile.new('hierayaml')
  begin
    file.write(<<-EOS)
---
:logger: noop
    EOS
    file.close
    system_rcp(:sp => file.path, :dp => '/etc/puppet/hiera.yaml')
  ensure
    file.unlink
  end
end

#puppet_master_installObject

Basic helper to install a puppet master

Parameters:

  • opts (Hash)

    a hash of opts



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rspec-system-puppet/helpers.rb', line 71

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rspec-system-puppet/helpers.rb', line 137

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) {|result| ... } ⇒ RSpecSystem::Result

Runs puppet resource commands

Parameters:

  • opts (Hash)

    a hash of opts

Yields:

  • (result)

    yields result when called as a block

Yield Parameters:

  • result (RSpecSystem::Result)

    a hash containing :exit_code, :stdout and :stderr

Returns:

  • (RSpecSystem::Result)

    results containing keys :exit_code, :stdout and :stderr



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rspec-system-puppet/helpers.rb', line 161

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