class Kitchen::Provisioner::PuppetAgent

Puppet Agent provisioner.

Attributes

tmp_dir[RW]

Public Instance Methods

calculate_path(path, type = :directory) click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 87
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_sandbox() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 203
def cleanup_sandbox
  return if sandbox_path.nil?
  debug("Cleaning up local sandbox in #{sandbox_path}")
  FileUtils.rmtree(sandbox_path)
end
create_sandbox() { || ... } click to toggle source
Calls superclass method
# File lib/kitchen/provisioner/puppet_agent.rb, line 193
def create_sandbox
  super
  debug("Creating local sandbox in #{sandbox_path}")

  yield if block_given?

  prepare_puppet_config
  info('Finished Preparing files for transfer')
end
init_command() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 191
def init_command; end
install_busser() click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/kitchen/provisioner/puppet_agent.rb, line 174
      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_command() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/kitchen/provisioner/puppet_agent.rb, line 101
      def install_command
        return unless config[:require_puppet_omnibus] || config[:require_puppet_repo]
        if config[:require_puppet_omnibus]
          info('Installing puppet using puppet omnibus')
          version = ''
          version = "-v #{config[:puppet_version]}" if config[:puppet_version]
          <<-INSTALL
            #{Util.shell_helpers}

            if [ ! -d "#{config[:puppet_omnibus_remote_path]}" ]; then
              echo "-----> Installing Puppet Omnibus"
              do_download #{config[:puppet_omnibus_url]} /tmp/puppet_install.sh
              #{sudo('sh')} /tmp/puppet_install.sh #{version}
            fi
            #{install_busser}
          INSTALL
        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 facter#{facter_debian_version}
                #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
                #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
              fi
              #{install_busser}
            INSTALL
          when 'redhat', 'centos', 'fedora', 'oracle', 'amazon'
            info("Installing puppet on #{puppet_platform}")
            <<-INSTALL
              if [ ! $(which puppet) ]; then
                #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
                #{update_packages_redhat_cmd}
                #{sudo('yum')} -y install puppet#{puppet_redhat_version}
              fi
              #{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
                  #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
                  #{update_packages_redhat_cmd}
                  #{sudo('yum')} -y install puppet#{puppet_redhat_version}
                else
                  if [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then
                    #{sudo('rpm')} -ivh #{proxy_parm} #{puppet_yum_repo}
                    #{update_packages_redhat_cmd}
                    #{sudo('yum')} -y install puppet#{puppet_redhat_version}
                  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('apt-get')} -y install facter#{facter_debian_version}
                    #{sudo('apt-get')} -y install puppet-common#{puppet_debian_version}
                    #{sudo('apt-get')} -y install puppet#{puppet_debian_version}
                  fi
                fi
              fi
              #{install_busser}
            INSTALL
          end
        end
      end
prepare_command() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 209
def prepare_command
  commands = []

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

  command = commands.join(' && ')
  debug(command)
  command
end
run_command() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 225
def run_command
  return config[:puppet_agent_command] unless config[:puppet_agent_command].nil?
  [
    custom_facts,
    sudo_env('puppet'),
    'agent',
    puppet_server_flag,
    "--waitforcert=#{config[:puppet_waitforcert]}",
    puppet_masterport_flag,
    puppet_certname_flag,
    puppet_digest_flag,
    puppet_detailed_exitcodes_flag,
    puppet_logdest_flag,
    puppet_test_flag,
    puppet_onetime_flag,
    puppet_no_daemonize_flag,
    puppet_noop_flag,
    puppet_environment_flag,
    puppet_verbose_flag,
    puppet_debug_flag,
    puppet_whitelist_exit_code
  ].compact.join(' ')
end

Protected Instance Methods

chef_url() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 415
def chef_url
  config[:chef_bootstrap_url]
end
custom_facts() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 308
def custom_facts
  return nil if config[:custom_facts].none?
  bash_vars = config[:custom_facts].map { |k, v| "FACTER_#{k}=#{v}" }.join(' ')
  bash_vars = "export #{bash_vars};"
  debug(bash_vars)
  bash_vars
end
facter_debian_version() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 265
def facter_debian_version
  config[:facter_version] ? "=#{config[:facter_version]}" : nil
end
gem_proxy_parm() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 376
def gem_proxy_parm
  p = http_proxy ? "--http-proxy #{http_proxy}" : nil
  n = no_proxy ? "--no-http-proxy #{no_proxy}" : nil
  p ? "#{p} #{n}" : nil
end
http_proxy() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 389
def http_proxy
  config[:http_proxy]
end
https_proxy() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 393
def https_proxy
  config[:https_proxy]
end
load_needed_dependencies!() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 251
def load_needed_dependencies!; end
no_proxy() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 397
def no_proxy
  config[:no_proxy]
end
powershell?() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 401
def powershell?
  powershell_shell? || !(puppet_platform =~ /^windows.*/).nil?
end
prepare_puppet_config() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 419
def prepare_puppet_config
  return unless puppet_config

  info('Preparing puppet.conf')
  debug("Using puppet config from #{puppet_config}")

  FileUtils.cp_r(puppet_config, File.join(sandbox_path, 'puppet.conf'))
end
proxy_parm() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 372
def proxy_parm
  http_proxy ? "--httpproxy #{URI.parse(http_proxy).host.downcase} --httpport #{URI.parse(http_proxy).port} " : nil
end
puppet_apt_repo() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 360
def puppet_apt_repo
  config[:puppet_apt_repo]
end
puppet_apt_repo_file() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 364
def puppet_apt_repo_file
  config[:puppet_apt_repo].split('/').last
end
puppet_certname_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 352
def puppet_certname_flag
  config[:puppet_certname] ? "--certname=#{config[:puppet_certname]}" : nil
end
puppet_config() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 257
def puppet_config
  config[:puppet_config_path]
end
puppet_debian_version() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 261
def puppet_debian_version
  config[:puppet_version] ? "=#{config[:puppet_version]}" : nil
end
puppet_debug_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 281
def puppet_debug_flag
  config[:puppet_debug] ? '-d' : nil
end
puppet_detailed_exitcodes_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 324
def puppet_detailed_exitcodes_flag
  config[:puppet_detailed_exitcodes] ? '--detailed-exitcodes' : nil
end
puppet_digest_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 356
def puppet_digest_flag
  config[:puppet_digest] ? "--digest=#{config[:puppet_digest]}" : nil
end
puppet_environment_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 253
def puppet_environment_flag
  config[:puppet_environment] ? "--environment=\"#{config[:puppet_environment]}\"" : nil
end
puppet_logdest_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 328
def puppet_logdest_flag
  config[:puppet_logdest] ? "--logdest=#{config[:puppet_logdest]}" : nil
end
puppet_masterport_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 320
def puppet_masterport_flag
  config[:puppet_masterport] ? "--masterport=#{config[:puppet_masterport]}" : nil
end
puppet_no_daemonize() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 344
def puppet_no_daemonize
  config[:puppet_no_daemonize]
end
puppet_no_daemonize_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 340
def puppet_no_daemonize_flag
  config[:puppet_no_daemonize] ? '--no-daemonize' : nil
end
puppet_noop_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 277
def puppet_noop_flag
  config[:puppet_noop] ? '--noop' : nil
end
puppet_onetime_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 336
def puppet_onetime_flag
  config[:puppet_onetime] ? '--onetime' : nil
end
puppet_platform() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 289
def puppet_platform
  config[:puppet_platform].to_s.downcase
end
puppet_redhat_version() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 269
def puppet_redhat_version
  if puppet_platform == 'amazon'
    config[:puppet_version]
  else
    config[:puppet_version] ? "-#{config[:puppet_version]}" : nil
  end
end
puppet_server() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 348
def puppet_server
  config[:puppet_server]
end
puppet_server_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 316
def puppet_server_flag
  config[:puppet_server] ? "--server=#{config[:puppet_server]}" : nil
end
puppet_test_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 332
def puppet_test_flag
  config[:puppet_test] ? '--test' : nil
end
puppet_verbose_flag() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 285
def puppet_verbose_flag
  config[:puppet_verbose] ? '-v' : nil
end
puppet_whitelist_exit_code() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 405
def puppet_whitelist_exit_code
  if config[:puppet_whitelist_exit_code].nil?
    powershell? ? '; exit $LASTEXITCODE' : nil
  elsif powershell?
    "; if(@(#{[config[:puppet_whitelist_exit_code]].join(', ')}) -contains $LASTEXITCODE) {exit 0} else {exit $LASTEXITCODE}"
  else
    '; RC=$?; [ ' + [config[:puppet_whitelist_exit_code]].flatten.map { |x| "\$RC -eq #{x}" }.join(' -o ') + ' ] && exit 0; exit $RC'
  end
end
puppet_yum_repo() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 368
def puppet_yum_repo
  config[:puppet_yum_repo]
end
sudo_env(pm) click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 301
def sudo_env(pm)
  s = https_proxy ? "https_proxy=#{https_proxy}" : nil
  p = http_proxy ? "http_proxy=#{http_proxy}" : nil
  n = no_proxy ? "no_proxy=#{no_proxy}" : nil
  p || s ? "#{sudo('env')} #{p} #{s} #{n} #{pm}" : sudo(pm).to_s
end
update_packages_debian_cmd() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 293
def update_packages_debian_cmd
  config[:update_package_repos] ? "#{sudo_env('apt-get')} update" : nil
end
update_packages_redhat_cmd() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 297
def update_packages_redhat_cmd
  config[:update_package_repos] ? "#{sudo_env('yum')} makecache" : nil
end
wget_proxy_parm() click to toggle source
# File lib/kitchen/provisioner/puppet_agent.rb, line 382
def wget_proxy_parm
  s = https_proxy ? "-e https_proxy=#{https_proxy}" : nil
  p = http_proxy ? "-e http_proxy=#{http_proxy}" : nil
  n = no_proxy ? "-e no_proxy=#{no_proxy}" : nil
  p || s ? "-e use_proxy=yes #{p} #{s} #{n}" : nil
end