From 2a7a89c9b7684f7fe9cacf54b63a96e92aafef29 Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Fri, 21 Mar 2014 14:16:54 -0700 Subject: [PATCH 1/5] whitespace and formatting --- lib/kitchen/provisioner/puppet_apply.rb | 345 +++++++++---------- lib/kitchen/provisioner/puppet_apply_spec.rb | 2 +- 2 files changed, 173 insertions(+), 174 deletions(-) diff --git a/lib/kitchen/provisioner/puppet_apply.rb b/lib/kitchen/provisioner/puppet_apply.rb index 4bb31a3..4725b9d 100644 --- a/lib/kitchen/provisioner/puppet_apply.rb +++ b/lib/kitchen/provisioner/puppet_apply.rb @@ -25,25 +25,25 @@ module Provisioner # Puppet Apply provisioner. # - class PuppetApply < Base + class PuppetApply < Base attr_accessor :tmp_dir default_config :require_puppet_omnibus, false # TODO use something like https://github.com/fnichol/omnibus-puppet - default_config :puppet_omnibus_url, nil + default_config :puppet_omnibus_url, nil default_config :puppet_version, nil - default_config :puppet_apt_repo, "http://apt.puppetlabs.com/puppetlabs-release-precise.deb" + default_config :puppet_apt_repo, "http://apt.puppetlabs.com/puppetlabs-release-precise.deb" default_config :manifest, 'site.pp' default_config :manifests_path do |provisioner| provisioner.calculate_path('manifests') or - raise 'No manifests_path detected. Please specify one in .kitchen.yml' + raise 'No manifests_path detected. Please specify one in .kitchen.yml' end default_config :modules_path do |provisioner| provisioner.calculate_path('modules') or - raise 'No modules_path detected. Please specify one in .kitchen.yml' + raise 'No modules_path detected. Please specify one in .kitchen.yml' end default_config :hiera_data_path do |provisioner| @@ -54,204 +54,203 @@ class PuppetApply < Base provisioner.calculate_path('hiera.yaml', :file) end - default_config :puppet_debug, false + default_config :puppet_debug, false default_config :puppet_verbose, false default_config :puppet_noop, false - default_config :update_packages, true - - def install_command + default_config :update_packages, true + + def install_command info('installing puppet') - <<-INSTALL - if [ ! $(which puppet) ]; then + <<-INSTALL + if [ ! $(which puppet) ]; then #{sudo('wget')} #{puppet_apt_repo} #{sudo('dpkg')} -i #{puppet_apt_repo_file} #{update_packages_cmd} #{sudo('apt-get')} install -y puppet#{puppet_version} - fi - INSTALL - end - - def init_command - dirs = %w{modules manifests hiera hiera.yaml}. - map { |dir| File.join(config[:root_path], dir) }.join(" ") - cmd = "#{sudo('rm')} -rf #{dirs} /var/lib/hiera /etc/hiera.yaml /etc/puppet/hiera.yaml; mkdir -p #{config[:root_path]}" - debug(cmd) - cmd - end - - def create_sandbox - super - debug("Creating local sandbox in #{sandbox_path}") - - yield if block_given? - - prepare_modules - prepare_manifests - prepare_hiera_config - prepare_hiera_data - info('Finished Preparing files for transfer') - - end + fi + INSTALL + end - def cleanup_sandbox - return if sandbox_path.nil? - debug("Cleaning up local sandbox in #{sandbox_path}") - FileUtils.rmtree(sandbox_path) - end + def init_command + dirs = %w{modules manifests hiera hiera.yaml}. + map { |dir| File.join(config[:root_path], dir) }.join(" ") + cmd = "#{sudo('rm')} -rf #{dirs} /var/lib/hiera /etc/hiera.yaml /etc/puppet/hiera.yaml; mkdir -p #{config[:root_path]}" + debug(cmd) + cmd + end + + def create_sandbox + super + debug("Creating local sandbox in #{sandbox_path}") - def prepare_command - commands = [] + yield if block_given? - if hiera_config - commands << [ - sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/', - ].join(' ') + prepare_modules + prepare_manifests + prepare_hiera_config + prepare_hiera_data + info('Finished Preparing files for transfer') - commands << [ - sudo('cp'), File.join(config[:root_path],'hiera.yaml'), '/etc/puppet/', - ].join(' ') end - if hiera_data - commands << [ - sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/' - ].join(' ') + def cleanup_sandbox + return if sandbox_path.nil? + debug("Cleaning up local sandbox in #{sandbox_path}") + FileUtils.rmtree(sandbox_path) end - debug(commands.join(' && ')) - commands.join(' && ') - end - def run_command - [ - "#{custom_facts}", - sudo('puppet'), - 'apply', - File.join(config[:root_path], 'manifests', manifest), - "--modulepath=#{File.join(config[:root_path], 'modules')}", - "--manifestdir=#{File.join(config[:root_path], 'manifests')}", - "#{puppet_noop}", - "#{puppet_verbose}", - "#{puppet_debug}", - ].join(" ") - end + def prepare_command + commands = [] + + 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'), '/etc/puppet/', + ].join(' ') + end + + if hiera_data + commands << [ + sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/' + ].join(' ') + end + debug(commands.join(' && ')) + commands.join(' && ') + end - protected - - def load_needed_dependencies! - if File.exists?(puppetfile) - debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet") - Puppet::Librarian.load!(logger) + def run_command + [ + "#{custom_facts}", + sudo('puppet'), + 'apply', + File.join(config[:root_path], 'manifests', manifest), + "--modulepath=#{File.join(config[:root_path], 'modules')}", + "--manifestdir=#{File.join(config[:root_path], 'manifests')}", + "#{puppet_noop}", + "#{puppet_verbose}", + "#{puppet_debug}", + ].join(" ") end - end - - def tmpmodules_dir - File.join(sandbox_path, 'modules') - end - - def puppetfile - File.join(config[:kitchen_root], "Puppetfile") - end - def manifest - config[:manifest] - end + protected - def manifests - config[:manifests_path] - end + def load_needed_dependencies! + if File.exists?(puppetfile) + debug("Puppetfile found at #{puppetfile}, loading Librarian-Puppet") + Puppet::Librarian.load!(logger) + end + end - def modules - config[:modules_path] - end + def tmpmodules_dir + File.join(sandbox_path, 'modules') + end - def hiera_config - config[:hiera_config_path] - end + def puppetfile + File.join(config[:kitchen_root], "Puppetfile") + end - def hiera_data - config[:hiera_data_path] - end + def manifest + config[:manifest] + end - def puppet_version - config[:puppet_version] == nil ? nil : "=#{config[:puppet_version]}" - end - - def puppet_noop - config[:puppet_noop].to_s.downcase == 'true' ? "--noop" : nil - end - - def puppet_debug - config[:puppet_debug].to_s.downcase == 'true' ? "-d" : nil - end - - def puppet_verbose - config[:puppet_verbose].to_s.downcase == 'true' ? "-v" : nil - end - - def update_packages_cmd - config[:update_packages].to_s.downcase == 'false' ? nil : "#{sudo('apt-get')} update" - end - - def custom_facts - return nil if config[:custom_facts] == nil - facts_array = config[:custom_facts].split(',').map { |f| f = "export FACTER_#{f}" } - return facts_array.join("; ")+";" - end - - def puppet_apt_repo - config[:puppet_apt_repo] - end - - def puppet_apt_repo_file - config[:puppet_apt_repo].split('/').last - end - - def prepare_manifests - info('Preparing manifests') - debug("Using manifests from #{manifests}") - - tmp_manifests_dir = File.join(sandbox_path, 'manifests') - FileUtils.mkdir_p(tmp_manifests_dir) - FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir) - end + def manifests + config[:manifests_path] + end - def prepare_modules - info('Preparing modules') - if File.exists?(puppetfile) - resolve_with_librarian - end - debug("Using modules from #{modules}") + def modules + config[:modules_path] + end - tmp_modules_dir = File.join(sandbox_path, 'modules') - FileUtils.mkdir_p(tmp_modules_dir) - FileUtils.cp_r(Dir.glob("#{modules}/*"), tmp_modules_dir) - end + def hiera_config + config[:hiera_config_path] + end - def prepare_hiera_config - return unless hiera_config + def hiera_data + config[:hiera_data_path] + end - info('Preparing hiera') - debug("Using hiera from #{hiera_config}") + def puppet_version + config[:puppet_version] == nil ? nil : "=#{config[:puppet_version]}" + end - FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml')) - end + def puppet_noop + config[:puppet_noop].to_s.downcase == 'true' ? "--noop" : nil + end + + def puppet_debug + config[:puppet_debug].to_s.downcase == 'true' ? "-d" : nil + end - def prepare_hiera_data - return unless hiera_data - info('Preparing hiera data') - debug("Using hiera data from #{hiera_data}") + def puppet_verbose + config[:puppet_verbose].to_s.downcase == 'true' ? "-v" : nil + end - tmp_hiera_dir = File.join(sandbox_path, 'hiera') - FileUtils.mkdir_p(tmp_hiera_dir) - FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir) - end - - def resolve_with_librarian - Kitchen.mutex.synchronize do - Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve + def update_packages_cmd + config[:update_packages].to_s.downcase == 'false' ? nil : "#{sudo('apt-get')} update" + end + + def custom_facts + return nil if config[:custom_facts] == nil + facts_array = config[:custom_facts].split(',').map { |f| f = "export FACTER_#{f}" } + return facts_array.join("; ")+";" + end + + def puppet_apt_repo + config[:puppet_apt_repo] + end + + def puppet_apt_repo_file + config[:puppet_apt_repo].split('/').last + end + + def prepare_manifests + info('Preparing manifests') + debug("Using manifests from #{manifests}") + + tmp_manifests_dir = File.join(sandbox_path, 'manifests') + FileUtils.mkdir_p(tmp_manifests_dir) + FileUtils.cp_r(Dir.glob("#{manifests}/*"), tmp_manifests_dir) + end + + def prepare_modules + info('Preparing modules') + if File.exists?(puppetfile) + resolve_with_librarian + end + debug("Using modules from #{modules}") + + tmp_modules_dir = File.join(sandbox_path, 'modules') + FileUtils.mkdir_p(tmp_modules_dir) + FileUtils.cp_r(Dir.glob("#{modules}/*"), tmp_modules_dir) + end + + def prepare_hiera_config + return unless hiera_config + + info('Preparing hiera') + debug("Using hiera from #{hiera_config}") + + FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml')) + end + + def prepare_hiera_data + return unless hiera_data + info('Preparing hiera data') + debug("Using hiera data from #{hiera_data}") + + tmp_hiera_dir = File.join(sandbox_path, 'hiera') + FileUtils.mkdir_p(tmp_hiera_dir) + FileUtils.cp_r(Dir.glob("#{hiera_data}/*"), tmp_hiera_dir) + end + + def resolve_with_librarian + Kitchen.mutex.synchronize do + Puppet::Librarian.new(puppetfile, tmpmodules_dir, logger).resolve + end end - end - end end -end \ No newline at end of file +end diff --git a/lib/kitchen/provisioner/puppet_apply_spec.rb b/lib/kitchen/provisioner/puppet_apply_spec.rb index 9fb58f3..99d49df 100644 --- a/lib/kitchen/provisioner/puppet_apply_spec.rb +++ b/lib/kitchen/provisioner/puppet_apply_spec.rb @@ -30,4 +30,4 @@ it "should give a sane run_command" do provisioner.run_command.must_match /puppet apply/ end -end \ No newline at end of file +end From efde78fe4d807d11684725549d9cd480b0b43003 Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Fri, 21 Mar 2014 14:28:30 -0700 Subject: [PATCH 2/5] DRY some things up --- lib/kitchen/provisioner/puppet_apply.rb | 31 +++++++++++++------------ 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/kitchen/provisioner/puppet_apply.rb b/lib/kitchen/provisioner/puppet_apply.rb index 4725b9d..618c3f0 100644 --- a/lib/kitchen/provisioner/puppet_apply.rb +++ b/lib/kitchen/provisioner/puppet_apply.rb @@ -117,21 +117,22 @@ def prepare_command sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/' ].join(' ') end - debug(commands.join(' && ')) - commands.join(' && ') + command = commands.join( ' && ') + debug(command) + command end def run_command [ - "#{custom_facts}", + custom_facts, sudo('puppet'), 'apply', File.join(config[:root_path], 'manifests', manifest), "--modulepath=#{File.join(config[:root_path], 'modules')}", "--manifestdir=#{File.join(config[:root_path], 'manifests')}", - "#{puppet_noop}", - "#{puppet_verbose}", - "#{puppet_debug}", + puppet_noop_flag, + puppet_verbose_flag, + puppet_debug_flag, ].join(" ") end @@ -149,7 +150,7 @@ def tmpmodules_dir end def puppetfile - File.join(config[:kitchen_root], "Puppetfile") + File.join(config[:kitchen_root], 'Puppetfile') end def manifest @@ -176,24 +177,24 @@ def puppet_version config[:puppet_version] == nil ? nil : "=#{config[:puppet_version]}" end - def puppet_noop - config[:puppet_noop].to_s.downcase == 'true' ? "--noop" : nil + def puppet_noop_flag + config[:puppet_noop] ? '--noop' : nil end - def puppet_debug - config[:puppet_debug].to_s.downcase == 'true' ? "-d" : nil + def puppet_debug_flag + config[:puppet_debug] ? '-d' : nil end - def puppet_verbose - config[:puppet_verbose].to_s.downcase == 'true' ? "-v" : nil + def puppet_verbose_flag + config[:puppet_verbose] ? '-v' : nil end def update_packages_cmd - config[:update_packages].to_s.downcase == 'false' ? nil : "#{sudo('apt-get')} update" + config[:update_packages] ? nil : "#{sudo('apt-get')} update" end def custom_facts - return nil if config[:custom_facts] == nil + return nil unless config[:custom_facts] facts_array = config[:custom_facts].split(',').map { |f| f = "export FACTER_#{f}" } return facts_array.join("; ")+";" end From 87e55733dd1182b75a245ebeba11572bbaf2ae1d Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Fri, 21 Mar 2014 14:44:41 -0700 Subject: [PATCH 3/5] rework custom_facts into a hash --- lib/kitchen/provisioner/puppet_apply.rb | 10 ++++++---- provisioner_options.md | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/kitchen/provisioner/puppet_apply.rb b/lib/kitchen/provisioner/puppet_apply.rb index 618c3f0..272d239 100644 --- a/lib/kitchen/provisioner/puppet_apply.rb +++ b/lib/kitchen/provisioner/puppet_apply.rb @@ -117,7 +117,7 @@ def prepare_command sudo('cp -r'), File.join(config[:root_path], 'hiera'), '/var/lib/' ].join(' ') end - command = commands.join( ' && ') + command = commands.join(' && ') debug(command) command end @@ -194,9 +194,11 @@ def update_packages_cmd end def custom_facts - return nil unless config[:custom_facts] - facts_array = config[:custom_facts].split(',').map { |f| f = "export FACTER_#{f}" } - return facts_array.join("; ")+";" + 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 def puppet_apt_repo diff --git a/provisioner_options.md b/provisioner_options.md index 6e7cd67..f3390f4 100644 --- a/provisioner_options.md +++ b/provisioner_options.md @@ -5,38 +5,38 @@ key | default value | Notes ----|---------------|-------- puppet_version | "latest"| desired version, affects apt installs puppet_apt_repo | "http://apt.puppetlabs.com/puppetlabs-release-precise.deb"| apt repo -manifests_path | | puppet repo manifests directory -manifest | 'site.pp' | manifest for puppet apply to run -modules_path | | puppet repo manifests directory +manifests_path | | puppet repo manifests directory +manifest | 'site.pp' | manifest for puppet apply to run +modules_path | | puppet repo manifests directory hiera_data_path | | puppet repo manifests directory -puppet_debug| false| Enable full debugging logging +puppet_debug| false| Enable full debugging logging puppet_verbose| false| Extra information logging puppet_noop| false| puppet runs in a no-op or dry-run mode update_packages| true| update OS packages before installing puppet -custom_facts| | fact1=aaa,fact2=bbb set the puppet facts before running puppet apply +custom_facts| Hash.new | Hash to set the puppet facts before running puppet apply ##Configuring Provisioner Options The provisioner can be configured globally or per suite, global settings act as defaults for all suites, you can then customise per suite, for example: - + --- driver: name: vagrant - + provisioner: name: puppet_apply manifests_path: /repository/puppet_repo/manifests modules_path: /repository/puppet_repo/modules-mycompany hiera_data_path: /repository/puppet_repo/hieradata - + platforms: - name: nocm_ubuntu-12.04 driver_plugin: vagrant driver_config: box: nocm_ubuntu-12.04 box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box - + suites: - name: default - - -in this example, vagrant will download a box for ubuntu 1204 with no configuration management installed, then install the latest puppet and puppet apply against the puppet repo from the /repository/puppet_repo directory using the defailt manifest site.pp \ No newline at end of file + + +in this example, vagrant will download a box for ubuntu 1204 with no configuration management installed, then install the latest puppet and puppet apply against the puppet repo from the /repository/puppet_repo directory using the defailt manifest site.pp From a6105336a35df85a925f85bc1fa4099ec9a3a1f4 Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Fri, 21 Mar 2014 14:46:15 -0700 Subject: [PATCH 4/5] whoops. fix autoformatter format --- lib/kitchen/provisioner/puppet_apply.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kitchen/provisioner/puppet_apply.rb b/lib/kitchen/provisioner/puppet_apply.rb index 272d239..667470f 100644 --- a/lib/kitchen/provisioner/puppet_apply.rb +++ b/lib/kitchen/provisioner/puppet_apply.rb @@ -38,12 +38,12 @@ class PuppetApply < Base default_config :manifests_path do |provisioner| provisioner.calculate_path('manifests') or - raise 'No manifests_path detected. Please specify one in .kitchen.yml' + raise 'No manifests_path detected. Please specify one in .kitchen.yml' end default_config :modules_path do |provisioner| provisioner.calculate_path('modules') or - raise 'No modules_path detected. Please specify one in .kitchen.yml' + raise 'No modules_path detected. Please specify one in .kitchen.yml' end default_config :hiera_data_path do |provisioner| From 4be2bf7455a3e67b3967f9aca249a9cecd82852b Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Fri, 21 Mar 2014 14:47:46 -0700 Subject: [PATCH 5/5] make puppet_version consistent with others --- lib/kitchen/provisioner/puppet_apply.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kitchen/provisioner/puppet_apply.rb b/lib/kitchen/provisioner/puppet_apply.rb index 667470f..9109b79 100644 --- a/lib/kitchen/provisioner/puppet_apply.rb +++ b/lib/kitchen/provisioner/puppet_apply.rb @@ -174,7 +174,7 @@ def hiera_data end def puppet_version - config[:puppet_version] == nil ? nil : "=#{config[:puppet_version]}" + config[:puppet_version] ? "=#{config[:puppet_version]}" : nil end def puppet_noop_flag