2012-04-19 04:53:19 +00:00
|
|
|
require 'pathname'
|
2013-08-29 18:26:21 +00:00
|
|
|
require 'vagrant/util/subprocess'
|
2012-04-19 04:53:19 +00:00
|
|
|
|
|
|
|
require File.expand_path("../base", __FILE__)
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module Chef
|
|
|
|
module Provisioner
|
|
|
|
# This class implements provisioning via chef-client, allowing provisioning
|
|
|
|
# with a chef server.
|
|
|
|
class ChefClient < Base
|
2013-01-14 00:41:32 +00:00
|
|
|
def configure(root_config)
|
|
|
|
raise ChefError, :server_validation_key_required if @config.validation_key_path.nil?
|
2012-04-19 04:53:19 +00:00
|
|
|
raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path)
|
2013-01-14 00:41:32 +00:00
|
|
|
raise ChefError, :server_url_required if @config.chef_server_url.nil?
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
2013-01-14 00:51:16 +00:00
|
|
|
def provision
|
2012-04-19 04:53:19 +00:00
|
|
|
verify_binary(chef_binary_path("chef-client"))
|
|
|
|
chown_provisioning_folder
|
|
|
|
create_client_key_folder
|
|
|
|
upload_validation_key
|
2014-01-17 02:03:38 +00:00
|
|
|
upload_encrypted_data_bag_secret
|
2012-04-19 04:53:19 +00:00
|
|
|
setup_json
|
|
|
|
setup_server_config
|
|
|
|
run_chef_client
|
2014-01-17 02:03:38 +00:00
|
|
|
delete_encrypted_data_bag_secret
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
2013-08-29 18:13:43 +00:00
|
|
|
def cleanup
|
2013-10-15 03:12:49 +00:00
|
|
|
delete_from_chef_server('client') if @config.delete_client
|
|
|
|
delete_from_chef_server('node') if @config.delete_node
|
2013-08-29 18:13:43 +00:00
|
|
|
end
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
def create_client_key_folder
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info I18n.t("vagrant.provisioners.chef.client_key_folder")
|
2013-01-14 00:41:32 +00:00
|
|
|
path = Pathname.new(@config.client_key_path)
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2013-01-14 00:41:32 +00:00
|
|
|
@machine.communicate.sudo("mkdir -p #{path.dirname}")
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_validation_key
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info I18n.t("vagrant.provisioners.chef.upload_validation_key")
|
2013-01-14 00:41:32 +00:00
|
|
|
@machine.communicate.upload(validation_key_path, guest_validation_key_path)
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_server_config
|
|
|
|
setup_config("provisioners/chef_client/client", "client.rb", {
|
2013-01-14 00:41:32 +00:00
|
|
|
:chef_server_url => @config.chef_server_url,
|
|
|
|
:validation_client_name => @config.validation_client_name,
|
2012-04-19 04:53:19 +00:00
|
|
|
:validation_key => guest_validation_key_path,
|
2013-01-14 00:41:32 +00:00
|
|
|
:client_key => @config.client_key_path,
|
2012-04-19 04:53:19 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_chef_client
|
2013-04-19 16:11:43 +00:00
|
|
|
if @config.run_list && @config.run_list.empty?
|
|
|
|
@machine.ui.warn(I18n.t("vagrant.chef_run_list_empty"))
|
|
|
|
end
|
|
|
|
|
2014-04-12 21:46:57 +00:00
|
|
|
if windows?
|
|
|
|
# This re-establishes our symbolic links if they were
|
|
|
|
# created between now and a reboot
|
|
|
|
@machine.communicate.execute(
|
|
|
|
"& net use a-non-existant-share",
|
|
|
|
error_check: false)
|
|
|
|
end
|
|
|
|
|
2014-04-12 22:00:28 +00:00
|
|
|
command = build_command(:client)
|
2012-04-19 04:53:19 +00:00
|
|
|
|
2013-01-14 00:41:32 +00:00
|
|
|
@config.attempts.times do |attempt|
|
2012-04-19 04:53:19 +00:00
|
|
|
if attempt == 0
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info I18n.t("vagrant.provisioners.chef.running_client")
|
2012-04-19 04:53:19 +00:00
|
|
|
else
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info I18n.t("vagrant.provisioners.chef.running_client_again")
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
2013-01-14 00:41:32 +00:00
|
|
|
exit_status = @machine.communicate.sudo(command, :error_check => false) do |type, data|
|
2012-04-19 04:53:19 +00:00
|
|
|
# Output the data with the proper color based on the stream.
|
|
|
|
color = type == :stdout ? :green : :red
|
2014-04-19 06:48:05 +00:00
|
|
|
|
|
|
|
data = data.chomp
|
|
|
|
next if data.empty?
|
|
|
|
|
|
|
|
@machine.ui.info(data, :color => color)
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# There is no need to run Chef again if it converges
|
|
|
|
return if exit_status == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we reached this point then Chef never converged! Error.
|
|
|
|
raise ChefError, :no_convergence
|
|
|
|
end
|
|
|
|
|
|
|
|
def validation_key_path
|
2013-01-14 00:41:32 +00:00
|
|
|
File.expand_path(@config.validation_key_path, @machine.env.root_path)
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def guest_validation_key_path
|
2013-01-14 00:41:32 +00:00
|
|
|
File.join(@config.provisioning_path, "validation.pem")
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
2012-12-17 16:49:27 +00:00
|
|
|
|
|
|
|
def delete_from_chef_server(deletable)
|
2013-12-13 03:51:22 +00:00
|
|
|
node_name = @config.node_name || @machine.config.vm.hostname
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.info(I18n.t(
|
2013-08-29 18:13:43 +00:00
|
|
|
"vagrant.provisioners.chef.deleting_from_server",
|
|
|
|
deletable: deletable, name: node_name))
|
2013-08-29 18:26:21 +00:00
|
|
|
|
|
|
|
command = ["knife", deletable, "delete", "--yes", node_name]
|
|
|
|
r = Vagrant::Util::Subprocess.execute(*command)
|
|
|
|
if r.exit_code != 0
|
2014-04-19 06:48:05 +00:00
|
|
|
@machine.ui.error(I18n.t(
|
2013-08-29 18:26:21 +00:00
|
|
|
"vagrant.chef_client_cleanup_failed",
|
|
|
|
deletable: deletable,
|
|
|
|
stdout: r.stdout,
|
|
|
|
stderr: r.stderr))
|
|
|
|
end
|
2012-12-17 16:49:27 +00:00
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|