Skip to content

Commit

Permalink
Merge pull request #1 from ChrisLundquist/formatting
Browse files Browse the repository at this point in the history
Formatting
  • Loading branch information
neillturner committed Mar 24, 2014
2 parents e0070f5 + 4be2bf7 commit c0aba96
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 184 deletions.
344 changes: 173 additions & 171 deletions lib/kitchen/provisioner/puppet_apply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ 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'

Expand All @@ -54,204 +54,206 @@ 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
command = commands.join(' && ')
debug(command)
command
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_flag,
puppet_verbose_flag,
puppet_debug_flag,
].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] ? "=#{config[:puppet_version]}" : nil
end

FileUtils.cp_r(hiera_config, File.join(sandbox_path, 'hiera.yaml'))
end
def puppet_noop_flag
config[:puppet_noop] ? '--noop' : nil
end

def puppet_debug_flag
config[:puppet_debug] ? '-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_flag
config[:puppet_verbose] ? '-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] ? nil : "#{sudo('apt-get')} update"
end

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

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
end
Loading

0 comments on commit c0aba96

Please sign in to comment.