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_module_paths = @config.expanded_module_paths(root_path)
|
2013-05-01 01:31:06 +00:00
|
|
|
@manifest_file = File.join(manifests_guest_path, @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|
|
2013-05-01 01:27:33 +00:00
|
|
|
@module_paths << [path, File.join(config.temp_dir, "modules-#{i}")]
|
2013-01-14 00:22:47 +00:00
|
|
|
end
|
|
|
|
|
2013-09-01 20:32:49 +00:00
|
|
|
folder_opts = {}
|
2014-01-02 22:37:47 +00:00
|
|
|
folder_opts[:type] = @config.synced_folder_type if @config.synced_folder_type
|
|
|
|
folder_opts[:owner] = "root" if !@config.synced_folder_type
|
2013-09-01 20:32:49 +00:00
|
|
|
|
2013-01-14 00:22:47 +00:00
|
|
|
# Share the manifests directory with the guest
|
2013-11-26 06:39:20 +00:00
|
|
|
if @config.manifests_path[0].to_sym == :host
|
|
|
|
root_config.vm.synced_folder(
|
|
|
|
File.expand_path(@config.manifests_path[1], root_path),
|
|
|
|
manifests_guest_path, folder_opts)
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2013-01-14 00:22:47 +00:00
|
|
|
# Share the module paths
|
|
|
|
@module_paths.each do |from, to|
|
2013-09-01 20:32:49 +00:00
|
|
|
root_config.vm.synced_folder(from, to, folder_opts)
|
2013-01-14 00:22:47 +00:00
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
2013-01-14 00:22:47 +00:00
|
|
|
def provision
|
2014-04-12 21:24:43 +00:00
|
|
|
# If the machine has a wait for reboot functionality, then
|
|
|
|
# do that (primarily Windows)
|
|
|
|
if @machine.guest.capability?(:wait_for_reboot)
|
|
|
|
@machine.guest.capability(:wait_for_reboot)
|
|
|
|
end
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
# Check that the shared folders are properly shared
|
2013-11-26 06:46:10 +00:00
|
|
|
check = []
|
|
|
|
if @config.manifests_path[0] == :host
|
|
|
|
check << manifests_guest_path
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
@module_paths.each do |host_path, guest_path|
|
|
|
|
check << guest_path
|
|
|
|
end
|
|
|
|
|
2013-05-02 01:44:18 +00:00
|
|
|
# Make sure the temporary directory is properly set up
|
|
|
|
@machine.communicate.tap do |comm|
|
|
|
|
comm.sudo("mkdir -p #{config.temp_dir}")
|
|
|
|
comm.sudo("chmod 0777 #{config.temp_dir}")
|
|
|
|
end
|
|
|
|
|
2013-09-19 18:26:35 +00:00
|
|
|
verify_shared_folders(check)
|
|
|
|
|
|
|
|
# Verify Puppet is installed and run it
|
|
|
|
verify_binary("puppet")
|
|
|
|
|
2013-05-02 01:44:18 +00:00
|
|
|
# Upload Hiera configuration if we have it
|
|
|
|
@hiera_config_path = nil
|
|
|
|
if config.hiera_config_path
|
2013-11-26 06:39:20 +00:00
|
|
|
local_hiera_path = File.expand_path(config.hiera_config_path,
|
|
|
|
@machine.env.root_path)
|
2013-05-02 01:44:18 +00:00
|
|
|
@hiera_config_path = File.join(config.temp_dir, "hiera.yaml")
|
|
|
|
@machine.communicate.upload(local_hiera_path, @hiera_config_path)
|
|
|
|
end
|
|
|
|
|
2012-07-11 17:59:20 +00:00
|
|
|
run_puppet_apply
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def manifests_guest_path
|
2013-11-26 06:39:20 +00:00
|
|
|
if config.manifests_path[0] == :host
|
|
|
|
# The path is on the host, so point to where it is shared
|
|
|
|
File.join(config.temp_dir, "manifests")
|
|
|
|
else
|
|
|
|
# The path is on the VM, so just point directly to it
|
|
|
|
config.manifests_path[1]
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
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
|
|
|
|
|
2012-07-11 17:59:20 +00:00
|
|
|
def run_puppet_apply
|
2014-04-12 21:35:27 +00:00
|
|
|
default_module_path = "/etc/puppet/modules"
|
|
|
|
if windows?
|
|
|
|
default_module_path = "/ProgramData/PuppetLabs/puppet/etc/modules"
|
|
|
|
end
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
options = [config.options].flatten
|
2012-06-07 18:37:15 +00:00
|
|
|
module_paths = @module_paths.map { |_, to| to }
|
2013-03-19 22:23:37 +00:00
|
|
|
if !@module_paths.empty?
|
2014-02-24 16:09:24 +00:00
|
|
|
# Append the default module path
|
2014-04-12 21:35:27 +00:00
|
|
|
module_paths << default_module_path
|
2014-02-24 16:07:57 +00:00
|
|
|
|
2013-03-19 22:23:37 +00:00
|
|
|
# Add the command line switch to add the module path
|
2014-05-09 16:22:36 +00:00
|
|
|
module_path_sep = windows? ? ";" : ":"
|
|
|
|
options << "--modulepath '#{module_paths.join(module_path_sep)}'"
|
2013-03-19 22:23:37 +00:00
|
|
|
end
|
|
|
|
|
2013-05-02 01:44:18 +00:00
|
|
|
if @hiera_config_path
|
|
|
|
options << "--hiera_config=#{@hiera_config_path}"
|
|
|
|
end
|
|
|
|
|
2013-07-18 04:16:53 +00:00
|
|
|
if !@machine.env.ui.is_a?(Vagrant::UI::Colored)
|
2013-07-28 19:07:48 +00:00
|
|
|
options << "--color=false"
|
2013-07-18 04:16:53 +00:00
|
|
|
end
|
|
|
|
|
2013-09-01 05:24:30 +00:00
|
|
|
options << "--manifestdir #{manifests_guest_path}"
|
2013-05-02 01:44:18 +00:00
|
|
|
options << "--detailed-exitcodes"
|
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
|
|
|
|
|
2014-04-12 21:35:27 +00:00
|
|
|
# If we're on Windows, we need to use the PowerShell style
|
|
|
|
if windows?
|
2014-05-13 16:31:31 +00:00
|
|
|
facts.map! { |v| "$env:#{v};" }
|
2014-04-12 21:35:27 +00:00
|
|
|
end
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
facter = "#{facts.join(" ")} "
|
|
|
|
end
|
|
|
|
|
2014-04-12 21:35:27 +00:00
|
|
|
command = "#{facter}puppet apply #{options}"
|
2013-05-01 01:27:33 +00:00
|
|
|
if config.working_directory
|
2014-04-12 21:35:27 +00:00
|
|
|
if windows?
|
|
|
|
command = "cd #{config.working_directory}; if ($?) \{ #{command} \}"
|
|
|
|
else
|
|
|
|
command = "cd #{config.working_directory} && #{command}"
|
|
|
|
end
|
2013-05-01 01:27:33 +00:00
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info(I18n.t(
|
2014-04-12 21:35:27 +00:00
|
|
|
"vagrant.provisioners.puppet.running_puppet",
|
|
|
|
:manifest => config.manifest_file))
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2014-04-12 21:35:27 +00:00
|
|
|
@machine.communicate.sudo(command, good_exit: [0,2]) do |type, data|
|
2014-04-19 06:48:05 +00:00
|
|
|
if !data.chomp.empty?
|
|
|
|
@machine.ui.info(data.chomp)
|
2013-07-20 03:38:25 +00:00
|
|
|
end
|
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-09-05 23:24:30 +00:00
|
|
|
if !@machine.communicate.test("test -d #{folder}", sudo: true)
|
2012-04-19 04:53:19 +00:00
|
|
|
raise PuppetError, :missing_shared_folders
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-04-12 21:35:27 +00:00
|
|
|
|
|
|
|
def windows?
|
|
|
|
@machine.config.vm.communicator == :winrm
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|