Chef uses the new provisioner API
This commit is contained in:
parent
51a227ae7e
commit
0c8391aedd
|
@ -0,0 +1,71 @@
|
|||
module VagrantPlugins
|
||||
module Chef
|
||||
module Config
|
||||
class Base < Vagrant.plugin("2", :config)
|
||||
# Shared config
|
||||
attr_accessor :node_name
|
||||
attr_accessor :provisioning_path
|
||||
attr_accessor :log_level
|
||||
attr_accessor :json
|
||||
attr_accessor :http_proxy
|
||||
attr_accessor :http_proxy_user
|
||||
attr_accessor :http_proxy_pass
|
||||
attr_accessor :https_proxy
|
||||
attr_accessor :https_proxy_user
|
||||
attr_accessor :https_proxy_pass
|
||||
attr_accessor :no_proxy
|
||||
attr_accessor :binary_path
|
||||
attr_accessor :binary_env
|
||||
attr_accessor :attempts
|
||||
attr_accessor :arguments
|
||||
attr_writer :run_list
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def attempts; @attempts || 1; end
|
||||
def json; @json ||= {}; end
|
||||
def log_level; @log_level || :info; end
|
||||
|
||||
# This returns the json that is merged with the defaults and the
|
||||
# user set data.
|
||||
def merged_json
|
||||
original = { :instance_role => "vagrant" }
|
||||
original[:run_list] = @run_list if @run_list
|
||||
original.merge(json || {})
|
||||
end
|
||||
|
||||
# Returns the run list, but also sets it up to be empty if it
|
||||
# hasn't been defined already.
|
||||
def run_list
|
||||
@run_list ||= []
|
||||
end
|
||||
|
||||
# Adds a recipe to the run list
|
||||
def add_recipe(name)
|
||||
name = "recipe[#{name}]" unless name =~ /^recipe\[(.+?)\]$/
|
||||
run_list << name
|
||||
end
|
||||
|
||||
# Adds a role to the run list
|
||||
def add_role(name)
|
||||
name = "role[#{name}]" unless name =~ /^role\[(.+?)\]$/
|
||||
run_list << name
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.vagrant_as_json_key")) if json.has_key?(:vagrant)
|
||||
end
|
||||
|
||||
def instance_variables_hash
|
||||
# Overridden so that the 'json' key could be removed, since its just
|
||||
# merged into the config anyways
|
||||
result = super
|
||||
result.delete("json")
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,35 @@
|
|||
require File.expand_path("../base", __FILE__)
|
||||
|
||||
module VagrantPlugins
|
||||
module Chef
|
||||
module Config
|
||||
class ChefClient < Base
|
||||
attr_accessor :chef_server_url
|
||||
attr_accessor :validation_key_path
|
||||
attr_accessor :validation_client_name
|
||||
attr_accessor :client_key_path
|
||||
attr_accessor :file_cache_path
|
||||
attr_accessor :file_backup_path
|
||||
attr_accessor :environment
|
||||
attr_accessor :encrypted_data_bag_secret_key_path
|
||||
attr_accessor :encrypted_data_bag_secret
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def validation_client_name; @validation_client_name || "chef-validator"; end
|
||||
def client_key_path; @client_key_path || "/etc/chef/client.pem"; end
|
||||
def file_cache_path; @file_cache_path || "/srv/chef/file_store"; end
|
||||
def file_backup_path; @file_backup_path || "/srv/chef/cache"; end
|
||||
def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.server_url_empty")) if !chef_server_url || chef_server_url.strip == ""
|
||||
errors.add(I18n.t("vagrant.config.chef.validation_key_path")) if !validation_key_path
|
||||
errors.add(I18n.t("vagrant.config.chef.run_list_empty")) if @run_list && @run_list.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
require File.expand_path("../base", __FILE__)
|
||||
|
||||
module VagrantPlugins
|
||||
module Chef
|
||||
module Config
|
||||
class ChefSolo < Base
|
||||
attr_accessor :cookbooks_path
|
||||
attr_accessor :roles_path
|
||||
attr_accessor :data_bags_path
|
||||
attr_accessor :recipe_url
|
||||
attr_accessor :nfs
|
||||
attr_accessor :encrypted_data_bag_secret_key_path
|
||||
attr_accessor :encrypted_data_bag_secret
|
||||
|
||||
def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end
|
||||
|
||||
def initialize
|
||||
super
|
||||
|
||||
@__default = ["cookbooks", [:vm, "cookbooks"]]
|
||||
end
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def cookbooks_path
|
||||
@cookbooks_path || _default_cookbook_path
|
||||
end
|
||||
|
||||
# This stores a reference to the default cookbook path which is used
|
||||
# later. Do not use this publicly. I apologize for not making it
|
||||
# "protected" but it has to be called by Vagrant internals later.
|
||||
def _default_cookbook_path
|
||||
@__default
|
||||
end
|
||||
|
||||
def nfs
|
||||
@nfs || false
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.cookbooks_path_empty")) if !cookbooks_path || [cookbooks_path].flatten.empty?
|
||||
errors.add(I18n.t("vagrant.config.chef.run_list_empty")) if !run_list || run_list.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -9,12 +9,22 @@ module VagrantPlugins
|
|||
Chef via `chef-solo` or `chef-client`.
|
||||
DESC
|
||||
|
||||
provisioner("chef_solo") do
|
||||
config(:chef_solo, :provisioner) do
|
||||
require File.expand_path("../config/chef_solo", __FILE__)
|
||||
Config::ChefSolo
|
||||
end
|
||||
|
||||
config(:chef_client, :provisioner) do
|
||||
require File.expand_path("../config/chef_client", __FILE__)
|
||||
Config::ChefClient
|
||||
end
|
||||
|
||||
provisioner(:chef_solo) do
|
||||
require File.expand_path("../provisioner/chef_solo", __FILE__)
|
||||
Provisioner::ChefSolo
|
||||
end
|
||||
|
||||
provisioner("chef_client") do
|
||||
provisioner(:chef_client) do
|
||||
require File.expand_path("../provisioner/chef_client", __FILE__)
|
||||
Provisioner::ChefClient
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'tempfile'
|
||||
|
||||
require "vagrant/util/counter"
|
||||
require "vagrant/util/template_renderer"
|
||||
|
||||
module VagrantPlugins
|
||||
|
@ -9,9 +10,13 @@ module VagrantPlugins
|
|||
# chef-solo and chef-client provisioning are stored. This is **not an actual
|
||||
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
|
||||
class Base < Vagrant.plugin("2", :provisioner)
|
||||
class ChefError < Vagrant::Errors::VagrantError
|
||||
error_namespace("vagrant.provisioners.chef")
|
||||
end
|
||||
|
||||
include Vagrant::Util::Counter
|
||||
|
||||
def initialize(env, config)
|
||||
def initialize(machine, config)
|
||||
super
|
||||
|
||||
config.provisioning_path ||= "/tmp/vagrant-chef-#{get_and_update_counter(:provisioning_path)}"
|
||||
|
@ -20,36 +25,37 @@ module VagrantPlugins
|
|||
def verify_binary(binary)
|
||||
# Checks for the existence of chef binary and error if it
|
||||
# doesn't exist.
|
||||
env[:machine].communicate.sudo("which #{binary}",
|
||||
:error_class => ChefError,
|
||||
:error_key => :chef_not_detected,
|
||||
:binary => binary)
|
||||
@machine.communicate.sudo(
|
||||
"which #{binary}",
|
||||
:error_class => ChefError,
|
||||
:error_key => :chef_not_detected,
|
||||
:binary => binary)
|
||||
end
|
||||
|
||||
# Returns the path to the Chef binary, taking into account the
|
||||
# `binary_path` configuration option.
|
||||
def chef_binary_path(binary)
|
||||
return binary if !config.binary_path
|
||||
return File.join(config.binary_path, binary)
|
||||
return binary if !@config.binary_path
|
||||
return File.join(@config.binary_path, binary)
|
||||
end
|
||||
|
||||
def chown_provisioning_folder
|
||||
env[:machine].communicate.tap do |comm|
|
||||
comm.sudo("mkdir -p #{config.provisioning_path}")
|
||||
comm.sudo("chown #{env[:machine].config.ssh.username} #{config.provisioning_path}")
|
||||
@machine.communicate.tap do |comm|
|
||||
comm.sudo("mkdir -p #{@config.provisioning_path}")
|
||||
comm.sudo("chown #{@machine.config.ssh.username} #{@config.provisioning_path}")
|
||||
end
|
||||
end
|
||||
|
||||
def setup_config(template, filename, template_vars)
|
||||
config_file = Vagrant::Util::TemplateRenderer.render(template, {
|
||||
:log_level => config.log_level.to_sym,
|
||||
:http_proxy => config.http_proxy,
|
||||
:http_proxy_user => config.http_proxy_user,
|
||||
:http_proxy_pass => config.http_proxy_pass,
|
||||
:https_proxy => config.https_proxy,
|
||||
:https_proxy_user => config.https_proxy_user,
|
||||
:https_proxy_pass => config.https_proxy_pass,
|
||||
:no_proxy => config.no_proxy
|
||||
:log_level => @config.log_level.to_sym,
|
||||
:http_proxy => @config.http_proxy,
|
||||
:http_proxy_user => @config.http_proxy_user,
|
||||
:http_proxy_pass => @config.http_proxy_pass,
|
||||
:https_proxy => @config.https_proxy,
|
||||
:https_proxy_user => @config.https_proxy_user,
|
||||
:https_proxy_pass => @config.https_proxy_pass,
|
||||
:no_proxy => @config.no_proxy
|
||||
}.merge(template_vars))
|
||||
|
||||
# Create a temporary file to store the data so we
|
||||
|
@ -59,17 +65,17 @@ module VagrantPlugins
|
|||
temp.close
|
||||
|
||||
remote_file = File.join(config.provisioning_path, filename)
|
||||
env[:machine].communicate.tap do |comm|
|
||||
@machine.communicate.tap do |comm|
|
||||
comm.sudo("rm #{remote_file}", :error_check => false)
|
||||
comm.upload(temp.path, remote_file)
|
||||
end
|
||||
end
|
||||
|
||||
def setup_json
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.json")
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.json")
|
||||
|
||||
# Get the JSON that we're going to expose to Chef
|
||||
json = JSON.pretty_generate(config.merged_json)
|
||||
json = JSON.pretty_generate(@config.merged_json)
|
||||
|
||||
# Create a temporary file to store the data so we
|
||||
# can upload it
|
||||
|
@ -77,78 +83,7 @@ module VagrantPlugins
|
|||
temp.write(json)
|
||||
temp.close
|
||||
|
||||
env[:machine].communicate.upload(temp.path, File.join(config.provisioning_path, "dna.json"))
|
||||
end
|
||||
|
||||
class ChefError < Vagrant::Errors::VagrantError
|
||||
error_namespace("vagrant.provisioners.chef")
|
||||
end
|
||||
|
||||
# This is the configuration which is available through `config.chef`
|
||||
class Config < Vagrant.plugin("2", :config)
|
||||
# Shared config
|
||||
attr_accessor :node_name
|
||||
attr_accessor :provisioning_path
|
||||
attr_accessor :log_level
|
||||
attr_accessor :json
|
||||
attr_accessor :http_proxy
|
||||
attr_accessor :http_proxy_user
|
||||
attr_accessor :http_proxy_pass
|
||||
attr_accessor :https_proxy
|
||||
attr_accessor :https_proxy_user
|
||||
attr_accessor :https_proxy_pass
|
||||
attr_accessor :no_proxy
|
||||
attr_accessor :binary_path
|
||||
attr_accessor :binary_env
|
||||
attr_accessor :attempts
|
||||
attr_accessor :arguments
|
||||
attr_writer :run_list
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def attempts; @attempts || 1; end
|
||||
def json; @json ||= {}; end
|
||||
def log_level; @log_level || :info; end
|
||||
|
||||
# This returns the json that is merged with the defaults and the
|
||||
# user set data.
|
||||
def merged_json
|
||||
original = { :instance_role => "vagrant" }
|
||||
original[:run_list] = @run_list if @run_list
|
||||
original.merge(json || {})
|
||||
end
|
||||
|
||||
# Returns the run list, but also sets it up to be empty if it
|
||||
# hasn't been defined already.
|
||||
def run_list
|
||||
@run_list ||= []
|
||||
end
|
||||
|
||||
# Adds a recipe to the run list
|
||||
def add_recipe(name)
|
||||
name = "recipe[#{name}]" unless name =~ /^recipe\[(.+?)\]$/
|
||||
run_list << name
|
||||
end
|
||||
|
||||
# Adds a role to the run list
|
||||
def add_role(name)
|
||||
name = "role[#{name}]" unless name =~ /^role\[(.+?)\]$/
|
||||
run_list << name
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.vagrant_as_json_key")) if json.has_key?(:vagrant)
|
||||
end
|
||||
|
||||
def instance_variables_hash
|
||||
# Overridden so that the 'json' key could be removed, since its just
|
||||
# merged into the config anyways
|
||||
result = super
|
||||
result.delete("json")
|
||||
result
|
||||
end
|
||||
@machine.communicate.upload(temp.path, File.join(@config.provisioning_path, "dna.json"))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,42 +8,10 @@ module VagrantPlugins
|
|||
# This class implements provisioning via chef-client, allowing provisioning
|
||||
# with a chef server.
|
||||
class ChefClient < Base
|
||||
class Config < Base::Config
|
||||
attr_accessor :chef_server_url
|
||||
attr_accessor :validation_key_path
|
||||
attr_accessor :validation_client_name
|
||||
attr_accessor :client_key_path
|
||||
attr_accessor :file_cache_path
|
||||
attr_accessor :file_backup_path
|
||||
attr_accessor :environment
|
||||
attr_accessor :encrypted_data_bag_secret_key_path
|
||||
attr_accessor :encrypted_data_bag_secret
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def validation_client_name; @validation_client_name || "chef-validator"; end
|
||||
def client_key_path; @client_key_path || "/etc/chef/client.pem"; end
|
||||
def file_cache_path; @file_cache_path || "/srv/chef/file_store"; end
|
||||
def file_backup_path; @file_backup_path || "/srv/chef/cache"; end
|
||||
def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.server_url_empty")) if !chef_server_url || chef_server_url.strip == ""
|
||||
errors.add(I18n.t("vagrant.config.chef.validation_key_path")) if !validation_key_path
|
||||
errors.add(I18n.t("vagrant.config.chef.run_list_empty")) if @run_list && @run_list.empty?
|
||||
end
|
||||
end
|
||||
|
||||
def self.config_class
|
||||
Config
|
||||
end
|
||||
|
||||
def prepare
|
||||
raise ChefError, :server_validation_key_required if config.validation_key_path.nil?
|
||||
def configure(root_config)
|
||||
raise ChefError, :server_validation_key_required if @config.validation_key_path.nil?
|
||||
raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path)
|
||||
raise ChefError, :server_url_required if config.chef_server_url.nil?
|
||||
raise ChefError, :server_url_required if @config.chef_server_url.nil?
|
||||
end
|
||||
|
||||
def provision!
|
||||
|
@ -51,63 +19,63 @@ module VagrantPlugins
|
|||
chown_provisioning_folder
|
||||
create_client_key_folder
|
||||
upload_validation_key
|
||||
upload_encrypted_data_bag_secret if config.encrypted_data_bag_secret_key_path
|
||||
upload_encrypted_data_bag_secret if @config.encrypted_data_bag_secret_key_path
|
||||
setup_json
|
||||
setup_server_config
|
||||
run_chef_client
|
||||
end
|
||||
|
||||
def create_client_key_folder
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.client_key_folder")
|
||||
path = Pathname.new(config.client_key_path)
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.client_key_folder")
|
||||
path = Pathname.new(@config.client_key_path)
|
||||
|
||||
env[:machine].communicate.sudo("mkdir -p #{path.dirname}")
|
||||
@machine.communicate.sudo("mkdir -p #{path.dirname}")
|
||||
end
|
||||
|
||||
def upload_validation_key
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.upload_validation_key")
|
||||
env[:machine].communicate.upload(validation_key_path, guest_validation_key_path)
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.upload_validation_key")
|
||||
@machine.communicate.upload(validation_key_path, guest_validation_key_path)
|
||||
end
|
||||
|
||||
def upload_encrypted_data_bag_secret
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
||||
env[:machine].communicate.upload(encrypted_data_bag_secret_key_path,
|
||||
config.encrypted_data_bag_secret)
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
||||
@machine.communicate.upload(encrypted_data_bag_secret_key_path,
|
||||
@config.encrypted_data_bag_secret)
|
||||
end
|
||||
|
||||
def setup_server_config
|
||||
setup_config("provisioners/chef_client/client", "client.rb", {
|
||||
:node_name => config.node_name,
|
||||
:chef_server_url => config.chef_server_url,
|
||||
:validation_client_name => config.validation_client_name,
|
||||
:node_name => @config.node_name,
|
||||
:chef_server_url => @config.chef_server_url,
|
||||
:validation_client_name => @config.validation_client_name,
|
||||
:validation_key => guest_validation_key_path,
|
||||
:client_key => config.client_key_path,
|
||||
:file_cache_path => config.file_cache_path,
|
||||
:file_backup_path => config.file_backup_path,
|
||||
:environment => config.environment,
|
||||
:encrypted_data_bag_secret => config.encrypted_data_bag_secret
|
||||
:client_key => @config.client_key_path,
|
||||
:file_cache_path => @config.file_cache_path,
|
||||
:file_backup_path => @config.file_backup_path,
|
||||
:environment => @config.environment,
|
||||
:encrypted_data_bag_secret => @config.encrypted_data_bag_secret
|
||||
})
|
||||
end
|
||||
|
||||
def run_chef_client
|
||||
command_env = config.binary_env ? "#{config.binary_env} " : ""
|
||||
command_args = config.arguments ? " #{config.arguments}" : ""
|
||||
command = "#{command_env}#{chef_binary_path("chef-client")} -c #{config.provisioning_path}/client.rb -j #{config.provisioning_path}/dna.json #{command_args}"
|
||||
command_env = @config.binary_env ? "#{@config.binary_env} " : ""
|
||||
command_args = @config.arguments ? " #{@config.arguments}" : ""
|
||||
command = "#{command_env}#{chef_binary_path("chef-client")} -c #{@config.provisioning_path}/client.rb -j #{@config.provisioning_path}/dna.json #{command_args}"
|
||||
|
||||
config.attempts.times do |attempt|
|
||||
@config.attempts.times do |attempt|
|
||||
if attempt == 0
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.running_client")
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.running_client")
|
||||
else
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.running_client_again")
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.running_client_again")
|
||||
end
|
||||
|
||||
exit_status = env[:machine].communicate.sudo(command, :error_check => false) do |type, data|
|
||||
exit_status = @machine.communicate.sudo(command, :error_check => false) do |type, data|
|
||||
# Output the data with the proper color based on the stream.
|
||||
color = type == :stdout ? :green : :red
|
||||
|
||||
# Note: Be sure to chomp the data to avoid the newlines that the
|
||||
# Chef outputs.
|
||||
env[:ui].info(data.chomp, :color => color, :prefix => false)
|
||||
@machine.env.ui.info(data.chomp, :color => color, :prefix => false)
|
||||
end
|
||||
|
||||
# There is no need to run Chef again if it converges
|
||||
|
@ -119,15 +87,15 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
def validation_key_path
|
||||
File.expand_path(config.validation_key_path, env[:root_path])
|
||||
File.expand_path(@config.validation_key_path, @machine.env.root_path)
|
||||
end
|
||||
|
||||
def encrypted_data_bag_secret_key_path
|
||||
File.expand_path(config.encrypted_data_bag_secret_key_path, env[:root_path])
|
||||
File.expand_path(@config.encrypted_data_bag_secret_key_path, @machine.env.root_path)
|
||||
end
|
||||
|
||||
def guest_validation_key_path
|
||||
File.join(config.provisioning_path, "validation.pem")
|
||||
File.join(@config.provisioning_path, "validation.pem")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
require "log4r"
|
||||
|
||||
require "vagrant/util/counter"
|
||||
|
||||
require File.expand_path("../base", __FILE__)
|
||||
|
||||
module VagrantPlugins
|
||||
|
@ -10,72 +12,26 @@ module VagrantPlugins
|
|||
extend Vagrant::Util::Counter
|
||||
include Vagrant::Util::Counter
|
||||
|
||||
class Config < Base::Config
|
||||
attr_accessor :cookbooks_path
|
||||
attr_accessor :roles_path
|
||||
attr_accessor :data_bags_path
|
||||
attr_accessor :recipe_url
|
||||
attr_accessor :nfs
|
||||
attr_accessor :encrypted_data_bag_secret_key_path
|
||||
attr_accessor :encrypted_data_bag_secret
|
||||
|
||||
def encrypted_data_bag_secret; @encrypted_data_bag_secret || "/tmp/encrypted_data_bag_secret"; end
|
||||
|
||||
def initialize
|
||||
super
|
||||
|
||||
@__default = ["cookbooks", [:vm, "cookbooks"]]
|
||||
end
|
||||
|
||||
# Provide defaults in such a way that they won't override the instance
|
||||
# variable. This is so merging continues to work properly.
|
||||
def cookbooks_path
|
||||
@cookbooks_path || _default_cookbook_path
|
||||
end
|
||||
|
||||
# This stores a reference to the default cookbook path which is used
|
||||
# later. Do not use this publicly. I apologize for not making it
|
||||
# "protected" but it has to be called by Vagrant internals later.
|
||||
def _default_cookbook_path
|
||||
@__default
|
||||
end
|
||||
|
||||
def nfs
|
||||
@nfs || false
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
super
|
||||
|
||||
errors.add(I18n.t("vagrant.config.chef.cookbooks_path_empty")) if !cookbooks_path || [cookbooks_path].flatten.empty?
|
||||
errors.add(I18n.t("vagrant.config.chef.run_list_empty")) if !run_list || run_list.empty?
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :cookbook_folders
|
||||
attr_reader :role_folders
|
||||
attr_reader :data_bags_folders
|
||||
|
||||
def self.config_class
|
||||
Config
|
||||
end
|
||||
|
||||
def initialize(env, config)
|
||||
def initialize(machine, config)
|
||||
super
|
||||
@logger = Log4r::Logger.new("vagrant::provisioners::chef_solo")
|
||||
end
|
||||
|
||||
def prepare
|
||||
@cookbook_folders = expanded_folders(config.cookbooks_path, "cookbooks")
|
||||
@role_folders = expanded_folders(config.roles_path, "roles")
|
||||
@data_bags_folders = expanded_folders(config.data_bags_path, "data_bags")
|
||||
def configure(root_config)
|
||||
@cookbook_folders = expanded_folders(@config.cookbooks_path, "cookbooks")
|
||||
@role_folders = expanded_folders(@config.roles_path, "roles")
|
||||
@data_bags_folders = expanded_folders(@config.data_bags_path, "data_bags")
|
||||
|
||||
share_folders("csc", @cookbook_folders)
|
||||
share_folders("csr", @role_folders)
|
||||
share_folders("csdb", @data_bags_folders)
|
||||
share_folders(root_config, "csc", @cookbook_folders)
|
||||
share_folders(root_config, "csr", @role_folders)
|
||||
share_folders(root_config, "csdb", @data_bags_folders)
|
||||
end
|
||||
|
||||
def provision!
|
||||
def provision
|
||||
# Verify that the proper shared folders exist.
|
||||
check = []
|
||||
[@cookbook_folders, @role_folders, @data_bags_folders].each do |folders|
|
||||
|
@ -91,7 +47,7 @@ module VagrantPlugins
|
|||
|
||||
verify_binary(chef_binary_path("chef-solo"))
|
||||
chown_provisioning_folder
|
||||
upload_encrypted_data_bag_secret if config.encrypted_data_bag_secret_key_path
|
||||
upload_encrypted_data_bag_secret if @config.encrypted_data_bag_secret_key_path
|
||||
setup_json
|
||||
setup_solo_config
|
||||
run_chef_solo
|
||||
|
@ -116,22 +72,22 @@ module VagrantPlugins
|
|||
remote_path = nil
|
||||
if type == :host
|
||||
# Get the expanded path that the host path points to
|
||||
local_path = File.expand_path(path, env[:root_path])
|
||||
local_path = File.expand_path(path, @machine.env.root_path)
|
||||
|
||||
# Super hacky but if we're expanded the default cookbook paths,
|
||||
# and one of the host paths doesn't exist, then just ignore it,
|
||||
# because that is fine.
|
||||
if paths.equal?(config._default_cookbook_path) && !File.directory?(local_path)
|
||||
if paths.equal?(@config._default_cookbook_path) && !File.directory?(local_path)
|
||||
@logger.info("'cookbooks' folder doesn't exist on defaults. Ignoring.")
|
||||
next
|
||||
end
|
||||
|
||||
# Path exists on the host, setup the remote path
|
||||
remote_path = "#{config.provisioning_path}/chef-solo-#{get_and_update_counter(:cookbooks_path)}"
|
||||
remote_path = "#{@config.provisioning_path}/chef-solo-#{get_and_update_counter(:cookbooks_path)}"
|
||||
else
|
||||
# Path already exists on the virtual machine. Expand it
|
||||
# relative to where we're provisioning.
|
||||
remote_path = File.expand_path(path, config.provisioning_path)
|
||||
remote_path = File.expand_path(path, @config.provisioning_path)
|
||||
|
||||
# Remove drive letter if running on a windows host. This is a bit
|
||||
# of a hack but is the most portable way I can think of at the moment
|
||||
|
@ -152,20 +108,20 @@ module VagrantPlugins
|
|||
|
||||
# Shares the given folders with the given prefix. The folders should
|
||||
# be of the structure resulting from the `expanded_folders` function.
|
||||
def share_folders(prefix, folders)
|
||||
def share_folders(root_config, prefix, folders)
|
||||
folders.each do |type, local_path, remote_path|
|
||||
if type == :host
|
||||
env[:machine].config.vm.share_folder(
|
||||
root_config.vm.share_folder(
|
||||
"v-#{prefix}-#{self.class.get_and_update_counter(:shared_folder)}",
|
||||
remote_path, local_path, :nfs => config.nfs)
|
||||
remote_path, local_path, :nfs => @config.nfs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def upload_encrypted_data_bag_secret
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
||||
env[:machine].communicate.upload(encrypted_data_bag_secret_key_path,
|
||||
config.encrypted_data_bag_secret)
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
|
||||
@machine.communicate.upload(encrypted_data_bag_secret_key_path,
|
||||
@config.encrypted_data_bag_secret)
|
||||
end
|
||||
|
||||
def setup_solo_config
|
||||
|
@ -174,35 +130,35 @@ module VagrantPlugins
|
|||
data_bags_path = guest_paths(@data_bags_folders).first
|
||||
|
||||
setup_config("provisioners/chef_solo/solo", "solo.rb", {
|
||||
:node_name => config.node_name,
|
||||
:provisioning_path => config.provisioning_path,
|
||||
:node_name => @config.node_name,
|
||||
:provisioning_path => @config.provisioning_path,
|
||||
:cookbooks_path => cookbooks_path,
|
||||
:recipe_url => config.recipe_url,
|
||||
:recipe_url => @config.recipe_url,
|
||||
:roles_path => roles_path,
|
||||
:data_bags_path => data_bags_path,
|
||||
:encrypted_data_bag_secret => config.encrypted_data_bag_secret,
|
||||
:encrypted_data_bag_secret => @config.encrypted_data_bag_secret,
|
||||
})
|
||||
end
|
||||
|
||||
def run_chef_solo
|
||||
command_env = config.binary_env ? "#{config.binary_env} " : ""
|
||||
command_args = config.arguments ? " #{config.arguments}" : ""
|
||||
command = "#{command_env}#{chef_binary_path("chef-solo")} -c #{config.provisioning_path}/solo.rb -j #{config.provisioning_path}/dna.json #{command_args}"
|
||||
command_env = @config.binary_env ? "#{@config.binary_env} " : ""
|
||||
command_args = @config.arguments ? " #{@config.arguments}" : ""
|
||||
command = "#{command_env}#{chef_binary_path("chef-solo")} -c #{@config.provisioning_path}/solo.rb -j #{@config.provisioning_path}/dna.json #{command_args}"
|
||||
|
||||
config.attempts.times do |attempt|
|
||||
@config.attempts.times do |attempt|
|
||||
if attempt == 0
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.running_solo")
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.running_solo")
|
||||
else
|
||||
env[:ui].info I18n.t("vagrant.provisioners.chef.running_solo_again")
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.running_solo_again")
|
||||
end
|
||||
|
||||
exit_status = env[:machine].communicate.sudo(command, :error_check => false) do |type, data|
|
||||
exit_status = @machine.communicate.sudo(command, :error_check => false) do |type, data|
|
||||
# Output the data with the proper color based on the stream.
|
||||
color = type == :stdout ? :green : :red
|
||||
|
||||
# Note: Be sure to chomp the data to avoid the newlines that the
|
||||
# Chef outputs.
|
||||
env[:ui].info(data.chomp, :color => color, :prefix => false)
|
||||
@machine.env.ui.info(data.chomp, :color => color, :prefix => false)
|
||||
end
|
||||
|
||||
# There is no need to run Chef again if it converges
|
||||
|
@ -216,14 +172,14 @@ module VagrantPlugins
|
|||
def verify_shared_folders(folders)
|
||||
folders.each do |folder|
|
||||
@logger.debug("Checking for shared folder: #{folder}")
|
||||
if !env[:machine].communicate.test("test -d #{folder}")
|
||||
if !@machine.communicate.test("test -d #{folder}")
|
||||
raise ChefError, :missing_shared_folders
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def encrypted_data_bag_secret_key_path
|
||||
File.expand_path(config.encrypted_data_bag_secret_key_path, env[:root_path])
|
||||
File.expand_path(@config.encrypted_data_bag_secret_key_path, @machine.env.root_path)
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
Loading…
Reference in New Issue