2012-04-19 04:53:19 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Puppet
|
|
|
|
module Provisioner
|
|
|
|
class PuppetServerError < Vagrant::Errors::VagrantError
|
|
|
|
error_namespace("vagrant.provisioners.puppet_server")
|
|
|
|
end
|
|
|
|
|
2012-11-07 05:21:36 +00:00
|
|
|
class PuppetServer < Vagrant.plugin("2", :provisioner)
|
2013-01-14 00:22:47 +00:00
|
|
|
def provision
|
2014-04-12 21:41:10 +00:00
|
|
|
if @machine.config.vm.communicator == :winrm
|
|
|
|
raise Vagrant::Errors::ProvisionerWinRMUnsupported,
|
|
|
|
name: "puppet_server"
|
|
|
|
end
|
|
|
|
|
2012-07-11 17:59:20 +00:00
|
|
|
verify_binary("puppet")
|
|
|
|
run_puppet_agent
|
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 => PuppetServerError,
|
|
|
|
: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_agent
|
2012-04-19 04:53:19 +00:00
|
|
|
options = config.options
|
|
|
|
options = [options] if !options.is_a?(Array)
|
|
|
|
|
|
|
|
# Intelligently set the puppet node cert name based on certain
|
|
|
|
# external parameters.
|
|
|
|
cn = nil
|
|
|
|
if config.puppet_node
|
|
|
|
# If a node name is given, we use that directly for the certname
|
|
|
|
cn = config.puppet_node
|
2013-03-18 05:26:44 +00:00
|
|
|
elsif @machine.config.vm.hostname
|
2012-04-19 04:53:19 +00:00
|
|
|
# If a host name is given, we explicitly set the certname to
|
|
|
|
# nil so that the hostname becomes the cert name.
|
|
|
|
cn = nil
|
|
|
|
else
|
|
|
|
# Otherwise, we default to the name of the box.
|
2013-01-14 00:22:47 +00:00
|
|
|
cn = @machine.config.vm.box
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add the certname option if there is one
|
|
|
|
options += ["--certname", cn] if cn
|
2013-07-18 04:16:53 +00:00
|
|
|
|
2013-11-25 23:06:05 +00:00
|
|
|
# A shortcut to make things easier
|
|
|
|
comm = @machine.communicate
|
|
|
|
|
|
|
|
# If we have client certs specified, then upload them
|
|
|
|
if config.client_cert_path && config.client_private_key_path
|
|
|
|
@machine.ui.info(
|
|
|
|
I18n.t("vagrant.provisioners.puppet_server.uploading_client_cert"))
|
|
|
|
dirname = "/tmp/puppet-#{Time.now.to_i}-#{rand(1000)}"
|
|
|
|
comm.sudo("mkdir -p #{dirname}")
|
|
|
|
comm.sudo("mkdir -p #{dirname}/certs")
|
|
|
|
comm.sudo("mkdir -p #{dirname}/private_keys")
|
|
|
|
comm.sudo("chmod -R 0777 #{dirname}")
|
|
|
|
comm.upload(config.client_cert_path, "#{dirname}/certs/#{cn}.pem")
|
|
|
|
comm.upload(config.client_private_key_path,
|
|
|
|
"#{dirname}/private_keys/#{cn}.pem")
|
|
|
|
|
|
|
|
# Setup the options so that they point to our directories
|
|
|
|
options << "--certdir=#{dirname}/certs"
|
|
|
|
options << "--privatekeydir=#{dirname}/private_keys"
|
|
|
|
end
|
|
|
|
|
2013-07-18 04:16:53 +00:00
|
|
|
# Disable colors if we must
|
|
|
|
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
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
# 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
|
|
|
|
|
2013-11-25 23:18:35 +00:00
|
|
|
options = options.join(" ")
|
2013-11-25 23:28:17 +00:00
|
|
|
command = "#{facter}puppet agent --onetime --no-daemonize #{options} " +
|
|
|
|
"--server #{config.puppet_server} --detailed-exitcodes || [ $? -eq 2 ]"
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2013-11-25 23:06:05 +00:00
|
|
|
@machine.ui.info I18n.t("vagrant.provisioners.puppet_server.running_puppetd")
|
2013-01-14 00:22:47 +00:00
|
|
|
@machine.communicate.sudo(command) do |type, data|
|
2013-07-20 03:38:25 +00:00
|
|
|
if !data.empty?
|
2013-11-25 23:28:17 +00:00
|
|
|
@machine.ui.info(data, :new_line => false, :prefix => false)
|
2013-07-20 03:38:25 +00:00
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|