Class: Kitchen::Provisioner::PuppetApply

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/provisioner/puppet_apply.rb

Overview

Puppet Apply provisioner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tmp_dirObject

Returns the value of attribute tmp_dir.



40
41
42
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 40

def tmp_dir
  @tmp_dir
end

Instance Method Details

#calculate_path(path, type = :directory) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 136

def calculate_path(path, type = :directory)
  base = config[:test_base_path]
  candidates = []
  candidates << File.join(base, instance.suite.name, 'puppet', path)
  candidates << File.join(base, instance.suite.name, path)
  candidates << File.join(base, path)
  candidates << File.join(Dir.pwd, path)

  candidates.find do |c|
    type == :directory ? File.directory?(c) : File.file?(c)
  end
end

#cleanup_sandboxObject



363
364
365
366
367
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 363

def cleanup_sandbox
  return if sandbox_path.nil?
  debug("Cleaning up local sandbox in #{sandbox_path}")
  FileUtils.rmtree(sandbox_path)
end

#create_sandboxObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 348

def create_sandbox
  super
  debug("Creating local sandbox in #{sandbox_path}")
  yield if block_given?
  prepare_modules
  prepare_manifests
  prepare_files
  prepare_facts
  prepare_puppet_config
  prepare_hiera_config
  prepare_fileserver_config
  prepare_hiera_data
  info('Finished Preparing files for transfer')
end

#init_commandObject



338
339
340
341
342
343
344
345
346
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 338

def init_command
  dirs = %w(modules manifests files hiera hiera.yaml)
  .map { |dir| File.join(config[:root_path], dir) }.join(' ')
  cmd = "#{sudo('rm')} -rf #{dirs} #{hiera_data_remote_path} /etc/hiera.yaml #{puppet_dir}/hiera.yaml #{puppet_dir}/fileserver.conf;"
  cmd += config[:puppet_environment] ? "#{sudo('rm')} -f #{File.join(puppet_dir, config[:puppet_environment])};" : ''
  cmd += " mkdir -p #{config[:root_path]} #{puppet_dir}"
  debug(cmd)
  cmd
end

#install_busserObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 287

def install_busser
  return unless config[:require_chef_for_busser]
  <<-INSTALL
    #{Util.shell_helpers}
    # install chef omnibus so that busser works as this is needed to run tests :(
    # TODO: work out how to install enough ruby
    # and set busser: { :ruby_bindir => '/usr/bin/ruby' } so that we dont need the
    # whole chef client
    if [ ! -d "/opt/chef" ]
    then
      echo '-----> Installing Chef Omnibus to install busser to run tests'
      do_download #{chef_url} /tmp/install.sh
      #{sudo('sh')} /tmp/install.sh
    fi
  INSTALL
end

#install_commandObject

TODO: refactor for smaller cyclomatic complexity and perceived complexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 151

def install_command
  return unless config[:require_puppet_collections] || config[:require_puppet_repo]
  if config[:require_puppet_collections]
    install_command_collections
  else
    case puppet_platform
    when 'debian', 'ubuntu'
      info("Installing puppet on #{puppet_platform}")
      <<-INSTALL
        if [ ! $(which puppet) ]; then
          #{sudo('apt-get')} -y install wget
          #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
          #{sudo('dpkg')} -i #{puppet_apt_repo_file}
          #{update_packages_debian_cmd}
          #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
          #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
        fi
        #{install_eyaml}
        #{install_deep_merge}
        #{install_busser}
      INSTALL
    when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
      info("Installing puppet from yum on #{puppet_platform}")
      <<-INSTALL
        if [ ! $(which puppet) ]; then
          #{install_puppet_yum_repo}
        fi
        #{install_eyaml}
        #{install_deep_merge}
        #{install_busser}
      INSTALL
    else
      info('Installing puppet, will try to determine platform os')
      <<-INSTALL
        if [ ! $(which puppet) ]; then
          if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
              #{install_puppet_yum_repo}
          else
            if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
               #{install_puppet_yum_repo}
            else
              #{sudo('apt-get')} -y install wget
              #{sudo('wget')} #{wget_proxy_parm} #{puppet_apt_repo}
              #{sudo('dpkg')} -i #{puppet_apt_repo_file}
              #{update_packages_debian_cmd}
              #{sudo_env('apt-get')} -y install puppet-common#{puppet_debian_version}
              #{sudo_env('apt-get')} -y install puppet#{puppet_debian_version}
            fi
          fi
        fi
        #{install_eyaml}
        #{install_deep_merge}
        #{install_busser}
      INSTALL
    end
  end
end

#install_command_collectionsObject



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 209

def install_command_collections
  case puppet_platform
  when 'debian', 'ubuntu'
    info("Installing Puppet Collections on #{puppet_platform}")
    <<-INSTALL
    #{sudo('apt-get')} -y install wget
    #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
    #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
    INSTALL
  when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
    info("Installing Puppet Collections on #{puppet_platform}")
    <<-INSTALL
    #{Util.shell_helpers}
    if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
      echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
      #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
      #{sudo_env('yum')} -y install puppet
    fi
    #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
    #{install_deep_merge}
    #{install_busser}
    INSTALL
  else
    info('Installing Puppet Collections, will try to determine platform os')
    <<-INSTALL
      if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
        if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ]; then
           #{Util.shell_helpers}
           if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
             echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
             #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
             #{sudo_env('yum')} -y install puppet
           fi
        else
          if [ -f /etc/system-release ] || [ grep -q 'Amazon Linux' /etc/system-release ]; then
            #{Util.shell_helpers}
            if [ ! -d "#{config[:puppet_coll_remote_path]}" ]; then
              echo "-----> #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}"
              #{sudo_env('yum')} -y localinstall #{config[:puppet_yum_collections_repo]}
              #{sudo_env('yum')} -y install puppet
            fi
          else
            #{sudo('apt-get')} -y install wget
            #{sudo('wget')} #{wget_proxy_parm} #{config[:puppet_apt_collections_repo]}
            #{sudo('dpkg')} -i #{puppet_apt_coll_repo_file}
          fi
        fi
      fi
      #{install_eyaml("#{config[:puppet_coll_remote_path]}/puppet/bin/gem")}
      #{install_deep_merge}
      #{install_busser}
    INSTALL
  end
end

#install_deep_mergeObject



264
265
266
267
268
269
270
271
272
273
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 264

def install_deep_merge
  return unless config[:hiera_deep_merge]
  <<-INSTALL
    # Support for hash merge lookups to recursively merge hash keys
    if [[ $(#{sudo('gem')} list deep_merge -i) == 'false' ]]; then
      echo '-----> Installing deep_merge to provide deep_merge of hiera hashes'
      #{sudo('gem')} install #{gem_proxy_parm} --no-ri --no-rdoc deep_merge
    fi
  INSTALL
end

#install_eyaml(gem_cmd = 'gem') ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 275

def install_eyaml(gem_cmd = 'gem')
  return unless config[:hiera_eyaml]
  <<-INSTALL
    # A backend for Hiera that provides per-value asymmetric encryption of sensitive data
    if [[ $(#{sudo(gem_cmd)} list hiera-eyaml -i) == 'false' ]]; then
      echo '-----> Installing hiera-eyaml to provide encryption of hiera data'
      #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc highline -v 1.6.21
      #{sudo(gem_cmd)} install #{gem_proxy_parm} --no-ri --no-rdoc hiera-eyaml
    fi
  INSTALL
end

#install_puppet_yum_repoObject

/bin/wget -P /etc/pki/rpm-gpg/ yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs changed to curl



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 307

def install_puppet_yum_repo
  <<-INSTALL
    rhelversion=$(cat /etc/redhat-release | grep release\ 7)
    # For CentOS7/RHEL7 the rdo release contains puppetlabs repo, creating conflict. Create temp-repo
    #{sudo_env('curl')} -o /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
    if [ -n "$rhelversion" ]; then
    echo '[puppettemp-products]
    name=Puppet Labs Products - \$basearch
    baseurl=http://yum.puppetlabs.com/el/7/products/\$basearch
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
    enabled=0
    gpgcheck=1
    [puppettemp-deps]
    name=Puppet Labs Dependencies - \$basearch
    baseurl=http://yum.puppetlabs.com/el/7/dependencies/\$basearch
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
    enabled=0
    gpgcheck=1' >> /etc/yum.repos.d/puppettemp.repo
      #{update_packages_redhat_cmd}
      #{sudo_env('yum')} -y --enablerepo=puppettemp-products --enablerepo=puppettemp-deps install puppet#{puppet_redhat_version}
      # Clean up temporary puppet repo
      rm -rf /etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs
      rm -rf /etc/yum.repos.d/puppettemp.repo
    else
      #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
      #{update_packages_redhat_cmd}
      #{sudo_env('yum')} -y install puppet#{puppet_redhat_version}
    fi
  INSTALL
end

#prepare_commandObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 370

def prepare_command
  commands = []
  if puppet_git_init
    commands << [
      sudo('rm -rf'), '/etc/puppet'
    ].join(' ')

    commands << [
      sudo('git clone'), puppet_git_init, '/etc/puppet'
    ].join(' ')
  end

  if puppet_git_pr
    commands << [sudo('git'),
                 '--git-dir=/etc/puppet/.git/',
                 'fetch -f',
                 'origin',
                 "pull/#{puppet_git_pr}/head:pr_#{puppet_git_pr}"
                ].join(' ')

    commands << [sudo('git'), '--git-dir=/etc/puppet/.git/',
                 '--work-tree=/etc/puppet/',
                 'checkout',
                 "pr_#{puppet_git_pr}"
                ].join(' ')
  end

  if puppet_config
    commands << [
      sudo('cp'),
      File.join(config[:root_path], 'puppet.conf'),
      puppet_dir
    ].join(' ')
  end

  if hiera_config
    commands << [
      sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), '/etc/'
    ].join(' ')

    commands << [
      sudo('cp'), File.join(config[:root_path], 'hiera.yaml'), puppet_dir
    ].join(' ')
  end

  if fileserver_config
    commands << [
      sudo('cp'),
      File.join(config[:root_path], 'fileserver.conf'),
      puppet_dir
    ].join(' ')
  end

  if hiera_data && hiera_data_remote_path == '/var/lib/hiera'
    commands << [
      sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/'
    ].join(' ')
  end

  if hiera_data && hiera_data_remote_path != '/var/lib/hiera'
    commands << [
      sudo('mkdir -p'), hiera_data_remote_path
    ].join(' ')
    commands << [
      sudo('cp -r'), File.join(config[:root_path], 'hiera/*'), hiera_data_remote_path
    ].join(' ')
  end

  if hiera_eyaml
    commands << [
      sudo('mkdir -p'), hiera_eyaml_key_remote_path
    ].join(' ')
    commands << [
      sudo('cp -r'), File.join(config[:root_path], 'hiera_keys/*'), hiera_eyaml_key_remote_path
    ].join(' ')
  end

  if puppet_environment
    commands << [
      sudo('ln -s '),  config[:root_path], File.join(puppet_dir, config[:puppet_environment])
    ].join(' ')
  end

  command = commands.join(' && ')
  debug(command)
  command
end

#run_commandObject

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/kitchen/provisioner/puppet_apply.rb', line 459

def run_command
  if !config[:puppet_apply_command].nil?
    return config[:puppet_apply_command]
  else
    [
      custom_facts,
      facter_facts,
      puppet_cmd,
      'apply',
      File.join(config[:root_path], 'manifests', manifest),
      "--modulepath=#{File.join(config[:root_path], 'modules')}",
      puppet_manifestdir,
      "--fileserverconfig=#{File.join(config[:root_path], 'fileserver.conf')}",
      puppet_environment_flag,
      puppet_noop_flag,
      puppet_detailed_exitcodes_flag,
      puppet_verbose_flag,
      puppet_debug_flag,
      puppet_logdest_flag,
      remove_repo
    ].join(' ')
  end
end