Puppet uses the new provisioner API
This commit is contained in:
parent
5c9f27626c
commit
51a227ae7e
|
@ -0,0 +1,65 @@
|
|||
module VagrantPlugins
|
||||
module Puppet
|
||||
module Config
|
||||
class Puppet < Vagrant.plugin("2", :config)
|
||||
attr_accessor :manifest_file
|
||||
attr_accessor :manifests_path
|
||||
attr_accessor :module_path
|
||||
attr_accessor :pp_path
|
||||
attr_accessor :options
|
||||
attr_accessor :facter
|
||||
|
||||
def manifest_file; @manifest_file || "default.pp"; end
|
||||
def manifests_path; @manifests_path || "manifests"; end
|
||||
def pp_path; @pp_path || "/tmp/vagrant-puppet"; end
|
||||
def options; @options ||= []; end
|
||||
def facter; @facter ||= {}; end
|
||||
|
||||
# Returns the manifests path expanded relative to the root path of the
|
||||
# environment.
|
||||
def expanded_manifests_path(root_path)
|
||||
Pathname.new(manifests_path).expand_path(root_path)
|
||||
end
|
||||
|
||||
# Returns the module paths as an array of paths expanded relative to the
|
||||
# root path.
|
||||
def expanded_module_paths(root_path)
|
||||
return [] if !module_path
|
||||
|
||||
# Get all the paths and expand them relative to the root path, returning
|
||||
# the array of expanded paths
|
||||
paths = module_path
|
||||
paths = [paths] if !paths.is_a?(Array)
|
||||
paths.map do |path|
|
||||
Pathname.new(path).expand_path(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
# Calculate the manifests and module paths based on env
|
||||
this_expanded_manifests_path = expanded_manifests_path(env.root_path)
|
||||
this_expanded_module_paths = expanded_module_paths(env.root_path)
|
||||
|
||||
# Manifests path/file validation
|
||||
if !this_expanded_manifests_path.directory?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.manifests_path_missing",
|
||||
:path => this_expanded_manifests_path))
|
||||
else
|
||||
expanded_manifest_file = this_expanded_manifests_path.join(manifest_file)
|
||||
if !expanded_manifest_file.file?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.manifest_missing",
|
||||
:manifest => expanded_manifest_file.to_s))
|
||||
end
|
||||
end
|
||||
|
||||
# Module paths validation
|
||||
this_expanded_module_paths.each do |path|
|
||||
if !path.directory?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.module_path_missing", :path => path))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
module VagrantPlugins
|
||||
module Puppet
|
||||
module Config
|
||||
class PuppetServer < Vagrant.plugin("2", :config)
|
||||
attr_accessor :puppet_server
|
||||
attr_accessor :puppet_node
|
||||
attr_accessor :options
|
||||
attr_accessor :facter
|
||||
|
||||
def facter; @facter ||= {}; end
|
||||
def puppet_server; @puppet_server || "puppet"; end
|
||||
def options; @options ||= []; end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -9,12 +9,22 @@ module VagrantPlugins
|
|||
Puppet either using `puppet apply` or a Puppet server.
|
||||
DESC
|
||||
|
||||
provisioner("puppet") do
|
||||
config(:puppet, :provisioner) do
|
||||
require File.expand_path("../config/puppet", __FILE__)
|
||||
Config::Puppet
|
||||
end
|
||||
|
||||
config(:puppet_server, :provisioner) do
|
||||
require File.expand_path("../config/puppet_server", __FILE__)
|
||||
Config::PuppetServer
|
||||
end
|
||||
|
||||
provisioner(:puppet) do
|
||||
require File.expand_path("../provisioner/puppet", __FILE__)
|
||||
Provisioner::Puppet
|
||||
end
|
||||
|
||||
provisioner("puppet_server") do
|
||||
provisioner(:puppet_server) do
|
||||
require File.expand_path("../provisioner/puppet_server", __FILE__)
|
||||
Provisioner::PuppetServer
|
||||
end
|
||||
|
|
|
@ -8,88 +8,40 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
class Puppet < Vagrant.plugin("2", :provisioner)
|
||||
class Config < Vagrant.plugin("2", :config)
|
||||
attr_accessor :manifest_file
|
||||
attr_accessor :manifests_path
|
||||
attr_accessor :module_path
|
||||
attr_accessor :pp_path
|
||||
attr_accessor :options
|
||||
attr_accessor :facter
|
||||
|
||||
def manifest_file; @manifest_file || "default.pp"; end
|
||||
def manifests_path; @manifests_path || "manifests"; end
|
||||
def pp_path; @pp_path || "/tmp/vagrant-puppet"; end
|
||||
def options; @options ||= []; end
|
||||
def facter; @facter ||= {}; end
|
||||
|
||||
# Returns the manifests path expanded relative to the root path of the
|
||||
# environment.
|
||||
def expanded_manifests_path(root_path)
|
||||
Pathname.new(manifests_path).expand_path(root_path)
|
||||
end
|
||||
|
||||
# Returns the module paths as an array of paths expanded relative to the
|
||||
# root path.
|
||||
def expanded_module_paths(root_path)
|
||||
return [] if !module_path
|
||||
|
||||
# Get all the paths and expand them relative to the root path, returning
|
||||
# the array of expanded paths
|
||||
paths = module_path
|
||||
paths = [paths] if !paths.is_a?(Array)
|
||||
paths.map do |path|
|
||||
Pathname.new(path).expand_path(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
def validate(env, errors)
|
||||
# Calculate the manifests and module paths based on env
|
||||
this_expanded_manifests_path = expanded_manifests_path(env.root_path)
|
||||
this_expanded_module_paths = expanded_module_paths(env.root_path)
|
||||
|
||||
# Manifests path/file validation
|
||||
if !this_expanded_manifests_path.directory?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.manifests_path_missing",
|
||||
:path => this_expanded_manifests_path))
|
||||
else
|
||||
expanded_manifest_file = this_expanded_manifests_path.join(manifest_file)
|
||||
if !expanded_manifest_file.file?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.manifest_missing",
|
||||
:manifest => expanded_manifest_file.to_s))
|
||||
end
|
||||
end
|
||||
|
||||
# Module paths validation
|
||||
this_expanded_module_paths.each do |path|
|
||||
if !path.directory?
|
||||
errors.add(I18n.t("vagrant.provisioners.puppet.module_path_missing", :path => path))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.config_class
|
||||
Config
|
||||
end
|
||||
|
||||
def initialize(env, config)
|
||||
def initialize(machine, config)
|
||||
super
|
||||
|
||||
@logger = Log4r::Logger.new("vagrant::provisioners::puppet")
|
||||
end
|
||||
|
||||
def prepare
|
||||
def configure(root_config)
|
||||
# Calculate the paths we're going to use based on the environment
|
||||
@expanded_manifests_path = config.expanded_manifests_path(env[:root_path])
|
||||
@expanded_module_paths = config.expanded_module_paths(env[:root_path])
|
||||
@manifest_file = File.join(manifests_guest_path, config.manifest_file)
|
||||
root_path = @machine.env.root_path
|
||||
@expanded_manifests_path = @config.expanded_manifests_path(root_path)
|
||||
@expanded_module_paths = @config.expanded_module_paths(root_path)
|
||||
@manifest_file = File.join(manifests_guest_path, @config.manifest_file)
|
||||
|
||||
set_module_paths
|
||||
share_manifests
|
||||
share_module_paths
|
||||
# Setup the module paths
|
||||
@module_paths = []
|
||||
@expanded_module_paths.each_with_index do |path, i|
|
||||
@module_paths << [path, File.join(config.pp_path, "modules-#{i}")]
|
||||
end
|
||||
|
||||
# Share the manifests directory with the guest
|
||||
root_config.vm.share_folder(
|
||||
"manifests", manifests_guest_path, @expanded_manifests_path)
|
||||
|
||||
# Share the module paths
|
||||
count = 0
|
||||
@module_paths.each do |from, to|
|
||||
# Sorry for the cryptic key here, but VirtualBox has a strange limit on
|
||||
# maximum size for it and its something small (around 10)
|
||||
root_config.vm.share_folder("v-pp-m#{count}", to, from)
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
|
||||
def provision!
|
||||
def provision
|
||||
# Check that the shared folders are properly shared
|
||||
check = [manifests_guest_path]
|
||||
@module_paths.each do |host_path, guest_path|
|
||||
|
@ -103,36 +55,16 @@ module VagrantPlugins
|
|||
run_puppet_apply
|
||||
end
|
||||
|
||||
def share_manifests
|
||||
env[:machine].config.vm.share_folder("manifests", manifests_guest_path, @expanded_manifests_path)
|
||||
end
|
||||
|
||||
def share_module_paths
|
||||
count = 0
|
||||
@module_paths.each do |from, to|
|
||||
# Sorry for the cryptic key here, but VirtualBox has a strange limit on
|
||||
# maximum size for it and its something small (around 10)
|
||||
env[:machine].config.vm.share_folder("v-pp-m#{count}", to, from)
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
|
||||
def set_module_paths
|
||||
@module_paths = []
|
||||
@expanded_module_paths.each_with_index do |path, i|
|
||||
@module_paths << [path, File.join(config.pp_path, "modules-#{i}")]
|
||||
end
|
||||
end
|
||||
|
||||
def manifests_guest_path
|
||||
File.join(config.pp_path, "manifests")
|
||||
end
|
||||
|
||||
def verify_binary(binary)
|
||||
env[:machine].communicate.sudo("which #{binary}",
|
||||
:error_class => PuppetError,
|
||||
:error_key => :not_detected,
|
||||
:binary => binary)
|
||||
@machine.communicate.sudo(
|
||||
"which #{binary}",
|
||||
:error_class => PuppetError,
|
||||
:error_key => :not_detected,
|
||||
:binary => binary)
|
||||
end
|
||||
|
||||
def run_puppet_apply
|
||||
|
@ -155,19 +87,19 @@ module VagrantPlugins
|
|||
|
||||
command = "cd #{manifests_guest_path} && #{facter}puppet apply #{options} --detailed-exitcodes || [ $? -eq 2 ]"
|
||||
|
||||
env[:ui].info I18n.t("vagrant.provisioners.puppet.running_puppet",
|
||||
:manifest => @manifest_file)
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.puppet.running_puppet",
|
||||
:manifest => @manifest_file)
|
||||
|
||||
env[:machine].communicate.sudo(command) do |type, data|
|
||||
@machine.communicate.sudo(command) do |type, data|
|
||||
data.chomp!
|
||||
env[:ui].info(data, :prefix => false) if !data.empty?
|
||||
@machine.env.ui.info(data, :prefix => false) if !data.empty?
|
||||
end
|
||||
end
|
||||
|
||||
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 PuppetError, :missing_shared_folders
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,31 +6,17 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
class PuppetServer < Vagrant.plugin("2", :provisioner)
|
||||
class Config < Vagrant.plugin("2", :config)
|
||||
attr_accessor :puppet_server
|
||||
attr_accessor :puppet_node
|
||||
attr_accessor :options
|
||||
attr_accessor :facter
|
||||
|
||||
def facter; @facter ||= {}; end
|
||||
def puppet_server; @puppet_server || "puppet"; end
|
||||
def options; @options ||= []; end
|
||||
end
|
||||
|
||||
def self.config_class
|
||||
Config
|
||||
end
|
||||
|
||||
def provision!
|
||||
def provision
|
||||
verify_binary("puppet")
|
||||
run_puppet_agent
|
||||
end
|
||||
|
||||
def verify_binary(binary)
|
||||
env[:vm].channel.sudo("which #{binary}",
|
||||
:error_class => PuppetServerError,
|
||||
:error_key => :not_detected,
|
||||
:binary => binary)
|
||||
@machine.communicate.sudo(
|
||||
"which #{binary}",
|
||||
:error_class => PuppetServerError,
|
||||
:error_key => :not_detected,
|
||||
:binary => binary)
|
||||
end
|
||||
|
||||
def run_puppet_agent
|
||||
|
@ -43,13 +29,13 @@ module VagrantPlugins
|
|||
if config.puppet_node
|
||||
# If a node name is given, we use that directly for the certname
|
||||
cn = config.puppet_node
|
||||
elsif env[:vm].config.vm.host_name
|
||||
elsif @machine.config.vm.host_name
|
||||
# If a host name is given, we explicitly set the certname to
|
||||
# nil so that the hostname becomes the cert name.
|
||||
cn = nil
|
||||
else
|
||||
# Otherwise, we default to the name of the box.
|
||||
cn = env[:vm].config.vm.box
|
||||
cn = @machine.config.vm.box
|
||||
end
|
||||
|
||||
# Add the certname option if there is one
|
||||
|
@ -69,10 +55,10 @@ module VagrantPlugins
|
|||
|
||||
command = "#{facter}puppet agent #{options} --server #{config.puppet_server} --detailed-exitcodes || [ $? -eq 2 ]"
|
||||
|
||||
env[:ui].info I18n.t("vagrant.provisioners.puppet_server.running_puppetd")
|
||||
env[:vm].channel.sudo(command) do |type, data|
|
||||
@machine.env.ui.info I18n.t("vagrant.provisioners.puppet_server.running_puppetd")
|
||||
@machine.communicate.sudo(command) do |type, data|
|
||||
data.chomp!
|
||||
env[:ui].info(data, :prefix => false) if !data.empty?
|
||||
@machine.env.ui.info(data, :prefix => false) if !data.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue