vagrant/plugins/provisioners/chef/provisioner/base.rb

92 lines
3.2 KiB
Ruby
Raw Normal View History

2012-04-19 04:53:19 +00:00
require 'tempfile'
2013-01-14 00:41:32 +00:00
require "vagrant/util/counter"
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-11-07 05:21:36 +00:00
class Base < Vagrant.plugin("2", :provisioner)
2013-01-14 00:41:32 +00:00
class ChefError < Vagrant::Errors::VagrantError
error_namespace("vagrant.provisioners.chef")
end
2012-04-19 04:53:19 +00:00
include Vagrant::Util::Counter
2013-01-14 00:41:32 +00:00
def initialize(machine, config)
2012-04-19 04:53:19 +00:00
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.
2013-01-14 00:41:32 +00:00
@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)
2013-01-14 00:41:32 +00:00
return binary if !@config.binary_path
return File.join(@config.binary_path, binary)
2012-04-19 04:53:19 +00:00
end
def chown_provisioning_folder
2013-01-14 00:41:32 +00:00
@machine.communicate.tap do |comm|
comm.sudo("mkdir -p #{@config.provisioning_path}")
comm.sudo("chown #{@machine.config.ssh.username} #{@config.provisioning_path}")
end
2012-04-19 04:53:19 +00:00
end
def setup_config(template, filename, template_vars)
config_file = Vagrant::Util::TemplateRenderer.render(template, {
2013-01-14 00:41:32 +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
2012-04-19 04:53:19 +00:00
}.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)
2013-01-14 00:41:32 +00:00
@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
2013-01-14 00:41:32 +00:00
@machine.env.ui.info I18n.t("vagrant.provisioners.chef.json")
2012-04-19 04:53:19 +00:00
# Get the JSON that we're going to expose to Chef
2013-01-14 00:41:32 +00:00
json = JSON.pretty_generate(@config.merged_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
2013-01-14 00:41:32 +00:00
@machine.communicate.upload(temp.path, File.join(@config.provisioning_path, "dna.json"))
2012-04-19 04:53:19 +00:00
end
end
end
end
end