2012-04-19 04:53:19 +00:00
|
|
|
require 'tempfile'
|
|
|
|
|
2012-06-28 15:29:48 +00:00
|
|
|
require "vagrant/util/template_renderer"
|
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Chef
|
|
|
|
module Provisioner
|
|
|
|
# This class is a base class where the common functionality shared between
|
|
|
|
# chef-solo and chef-client provisioning are stored. This is **not an actual
|
|
|
|
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
|
2012-06-26 23:04:51 +00:00
|
|
|
class Base < Vagrant.plugin("1", :provisioner)
|
2012-04-19 04:53:19 +00:00
|
|
|
include Vagrant::Util::Counter
|
|
|
|
|
|
|
|
def initialize(env, config)
|
|
|
|
super
|
|
|
|
|
|
|
|
config.provisioning_path ||= "/tmp/vagrant-chef-#{get_and_update_counter(:provisioning_path)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_binary(binary)
|
|
|
|
# Checks for the existence of chef binary and error if it
|
|
|
|
# doesn't exist.
|
2012-08-21 23:57:17 +00:00
|
|
|
env[:machine].communicate.sudo("which #{binary}",
|
|
|
|
:error_class => ChefError,
|
|
|
|
:error_key => :chef_not_detected,
|
|
|
|
:binary => binary)
|
2012-04-19 04:53:19 +00:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
def chown_provisioning_folder
|
2012-08-21 23:57:17 +00:00
|
|
|
env[:machine].communicate.tap do |comm|
|
|
|
|
comm.sudo("mkdir -p #{config.provisioning_path}")
|
|
|
|
comm.sudo("chown #{env[:machine].config.ssh.username} #{config.provisioning_path}")
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_config(template, filename, template_vars)
|
2012-06-28 15:29:48 +00:00
|
|
|
config_file = Vagrant::Util::TemplateRenderer.render(template, {
|
2012-04-19 04:53:19 +00:00
|
|
|
: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
|
|
|
|
# can upload it
|
|
|
|
temp = Tempfile.new("vagrant")
|
|
|
|
temp.write(config_file)
|
|
|
|
temp.close
|
|
|
|
|
|
|
|
remote_file = File.join(config.provisioning_path, filename)
|
2012-08-21 23:57:17 +00:00
|
|
|
env[:machine].communicate.tap do |comm|
|
|
|
|
comm.sudo("rm #{remote_file}", :error_check => false)
|
|
|
|
comm.upload(temp.path, remote_file)
|
|
|
|
end
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_json
|
|
|
|
env[:ui].info I18n.t("vagrant.provisioners.chef.json")
|
|
|
|
|
2012-05-02 05:03:54 +00:00
|
|
|
# Get the JSON that we're going to expose to Chef
|
|
|
|
json = config.merged_json.to_json
|
2012-04-19 04:53:19 +00:00
|
|
|
|
|
|
|
# Create a temporary file to store the data so we
|
|
|
|
# can upload it
|
|
|
|
temp = Tempfile.new("vagrant")
|
|
|
|
temp.write(json)
|
|
|
|
temp.close
|
|
|
|
|
2012-08-21 23:57:17 +00:00
|
|
|
env[:machine].communicate.upload(temp.path, File.join(config.provisioning_path, "dna.json"))
|
2012-04-19 04:53:19 +00:00
|
|
|
end
|
|
|
|
|
2012-05-01 04:07:09 +00:00
|
|
|
class ChefError < Vagrant::Errors::VagrantError
|
2012-04-19 04:53:19 +00:00
|
|
|
error_namespace("vagrant.provisioners.chef")
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is the configuration which is available through `config.chef`
|
2012-06-26 23:02:44 +00:00
|
|
|
class Config < Vagrant.plugin("1", :config)
|
2012-04-19 04:53:19 +00:00
|
|
|
# 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_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
|
|
|
|
end
|