2015-01-06 18:56:28 +00:00
|
|
|
require "digest/md5"
|
2014-10-30 19:32:15 +00:00
|
|
|
require "tempfile"
|
|
|
|
|
2014-10-31 20:06:05 +00:00
|
|
|
require_relative "base"
|
|
|
|
|
2014-10-30 19:32:15 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Chef
|
|
|
|
module Provisioner
|
2014-10-31 20:06:05 +00:00
|
|
|
class ChefApply < Base
|
2014-10-30 19:32:15 +00:00
|
|
|
def provision
|
2014-10-31 20:06:05 +00:00
|
|
|
install_chef
|
|
|
|
verify_binary(chef_binary_path("chef-apply"))
|
|
|
|
|
2014-10-30 19:32:15 +00:00
|
|
|
command = "chef-apply"
|
2014-10-31 20:06:05 +00:00
|
|
|
command << " \"#{target_recipe_path}\""
|
|
|
|
command << " --log_level #{config.log_level}"
|
2014-10-30 19:32:15 +00:00
|
|
|
|
|
|
|
user = @machine.ssh_info[:username]
|
|
|
|
|
|
|
|
# Reset upload path permissions for the current ssh user
|
2015-05-02 14:47:11 +00:00
|
|
|
if windows?
|
|
|
|
@machine.communicate.sudo("mkdir ""#{config.upload_path}"" -f")
|
|
|
|
else
|
|
|
|
@machine.communicate.sudo("mkdir -p #{config.upload_path}")
|
|
|
|
@machine.communicate.sudo("chown -R #{user} #{config.upload_path}")
|
|
|
|
end
|
2014-10-30 19:32:15 +00:00
|
|
|
|
|
|
|
# Upload the recipe
|
|
|
|
upload_recipe
|
|
|
|
|
2014-10-31 20:06:05 +00:00
|
|
|
@machine.ui.info(I18n.t("vagrant.provisioners.chef.running_apply",
|
2014-10-30 19:32:15 +00:00
|
|
|
script: config.path)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Execute it with sudo
|
|
|
|
@machine.communicate.sudo(command) do |type, data|
|
|
|
|
if [:stderr, :stdout].include?(type)
|
|
|
|
# Output the data with the proper color based on the stream.
|
|
|
|
color = (type == :stdout) ? :green : :red
|
|
|
|
|
|
|
|
# Chomp the data to avoid the newlines that the Chef outputs
|
|
|
|
@machine.env.ui.info(data.chomp, color: color, prefix: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-31 20:06:05 +00:00
|
|
|
# The destination (on the guest) where the recipe will live
|
|
|
|
# @return [String]
|
2015-01-06 18:57:25 +00:00
|
|
|
def target_recipe_path
|
|
|
|
key = Digest::MD5.hexdigest(config.recipe)
|
2015-01-06 18:56:28 +00:00
|
|
|
File.join(config.upload_path, "recipe-#{key}.rb")
|
2014-10-31 20:06:05 +00:00
|
|
|
end
|
|
|
|
|
2014-10-30 19:32:15 +00:00
|
|
|
# Write the raw recipe contents to a tempfile and upload that to the
|
|
|
|
# machine.
|
|
|
|
def upload_recipe
|
|
|
|
# Write the raw recipe contents to a tempfile
|
|
|
|
file = Tempfile.new(["vagrant-chef-apply", ".rb"])
|
|
|
|
file.write(config.recipe)
|
|
|
|
file.rewind
|
|
|
|
|
|
|
|
# Upload the tempfile to the guest
|
2015-01-06 18:57:25 +00:00
|
|
|
@machine.communicate.upload(file.path, target_recipe_path)
|
2014-10-30 19:32:15 +00:00
|
|
|
ensure
|
|
|
|
# Delete our template
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|