vagrant/plugins/provisioners/puppet/provisioner/puppet.rb

118 lines
3.7 KiB
Ruby
Raw Normal View History

2012-04-19 04:53:19 +00:00
require "log4r"
module VagrantPlugins
module Puppet
module Provisioner
class PuppetError < Vagrant::Errors::VagrantError
error_namespace("vagrant.provisioners.puppet")
end
2012-11-07 05:21:36 +00:00
class Puppet < Vagrant.plugin("2", :provisioner)
2013-01-14 00:22:47 +00:00
def initialize(machine, config)
2012-04-19 04:53:19 +00:00
super
@logger = Log4r::Logger.new("vagrant::provisioners::puppet")
end
2013-01-14 00:22:47 +00:00
def configure(root_config)
2012-04-19 04:53:19 +00:00
# Calculate the paths we're going to use based on the environment
2013-01-14 00:22:47 +00:00
root_path = @machine.env.root_path
@expanded_manifests_path = @config.expanded_manifests_path(root_path)
@expanded_module_paths = @config.expanded_module_paths(root_path)
@manifest_file = @config.manifest_file
2013-01-14 00:22:47 +00:00
# Setup the module paths
@module_paths = []
@expanded_module_paths.each_with_index do |path, i|
@module_paths << [path, File.join(config.pp_path, "modules-#{i}")]
end
# Share the manifests directory with the guest
2013-01-23 17:44:53 +00:00
root_config.vm.synced_folder(
@expanded_manifests_path, manifests_guest_path)
2012-04-19 04:53:19 +00:00
2013-01-14 00:22:47 +00:00
# Share the module paths
count = 0
@module_paths.each do |from, to|
# Sorry for the cryptic key here, but VirtualBox has a strange limit on
# maximum size for it and its something small (around 10)
2013-01-23 17:44:53 +00:00
root_config.vm.synced_folder(from, to)
2013-01-14 00:22:47 +00:00
count += 1
end
2012-04-19 04:53:19 +00:00
end
2013-01-14 00:22:47 +00:00
def provision
2012-04-19 04:53:19 +00:00
# Check that the shared folders are properly shared
check = [manifests_guest_path]
@module_paths.each do |host_path, guest_path|
check << guest_path
end
verify_shared_folders(check)
# Verify Puppet is installed and run it
verify_binary("puppet")
run_puppet_apply
2012-04-19 04:53:19 +00:00
end
def manifests_guest_path
File.join(config.pp_path, "manifests")
end
def verify_binary(binary)
2013-01-14 00:22:47 +00:00
@machine.communicate.sudo(
"which #{binary}",
:error_class => PuppetError,
:error_key => :not_detected,
:binary => binary)
2012-04-19 04:53:19 +00:00
end
def run_puppet_apply
2012-04-19 04:53:19 +00:00
options = [config.options].flatten
module_paths = @module_paths.map { |_, to| to }
if !@module_paths.empty?
# Prepend the default module path
module_paths.unshift("/etc/puppet/modules")
# Add the command line switch to add the module path
options << "--modulepath '#{module_paths.join(':')}'"
end
2012-04-19 04:53:19 +00:00
options << @manifest_file
options = options.join(" ")
# Build up the custom facts if we have any
facter = ""
if !config.facter.empty?
facts = []
config.facter.each do |key, value|
facts << "FACTER_#{key}='#{value}'"
end
facter = "#{facts.join(" ")} "
end
command = "cd #{manifests_guest_path} && #{facter}puppet apply #{options} --detailed-exitcodes || [ $? -eq 2 ]"
2012-04-19 04:53:19 +00:00
2013-01-14 00:22:47 +00:00
@machine.env.ui.info I18n.t("vagrant.provisioners.puppet.running_puppet",
:manifest => @manifest_file)
2012-04-19 04:53:19 +00:00
2013-01-14 00:22:47 +00:00
@machine.communicate.sudo(command) do |type, data|
data.chomp!
2013-01-14 00:22:47 +00:00
@machine.env.ui.info(data, :prefix => false) if !data.empty?
2012-04-19 04:53:19 +00:00
end
end
def verify_shared_folders(folders)
folders.each do |folder|
@logger.debug("Checking for shared folder: #{folder}")
2013-01-14 00:22:47 +00:00
if !@machine.communicate.test("test -d #{folder}")
2012-04-19 04:53:19 +00:00
raise PuppetError, :missing_shared_folders
end
end
end
end
end
end
end