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.
|
Puppet either using `puppet apply` or a Puppet server.
|
||||||
DESC
|
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__)
|
require File.expand_path("../provisioner/puppet", __FILE__)
|
||||||
Provisioner::Puppet
|
Provisioner::Puppet
|
||||||
end
|
end
|
||||||
|
|
||||||
provisioner("puppet_server") do
|
provisioner(:puppet_server) do
|
||||||
require File.expand_path("../provisioner/puppet_server", __FILE__)
|
require File.expand_path("../provisioner/puppet_server", __FILE__)
|
||||||
Provisioner::PuppetServer
|
Provisioner::PuppetServer
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,88 +8,40 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
|
|
||||||
class Puppet < Vagrant.plugin("2", :provisioner)
|
class Puppet < Vagrant.plugin("2", :provisioner)
|
||||||
class Config < Vagrant.plugin("2", :config)
|
def initialize(machine, 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)
|
|
||||||
super
|
super
|
||||||
|
|
||||||
@logger = Log4r::Logger.new("vagrant::provisioners::puppet")
|
@logger = Log4r::Logger.new("vagrant::provisioners::puppet")
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare
|
def configure(root_config)
|
||||||
# Calculate the paths we're going to use based on the environment
|
# Calculate the paths we're going to use based on the environment
|
||||||
@expanded_manifests_path = config.expanded_manifests_path(env[:root_path])
|
root_path = @machine.env.root_path
|
||||||
@expanded_module_paths = config.expanded_module_paths(env[:root_path])
|
@expanded_manifests_path = @config.expanded_manifests_path(root_path)
|
||||||
@manifest_file = File.join(manifests_guest_path, config.manifest_file)
|
@expanded_module_paths = @config.expanded_module_paths(root_path)
|
||||||
|
@manifest_file = File.join(manifests_guest_path, @config.manifest_file)
|
||||||
|
|
||||||
set_module_paths
|
# Setup the module paths
|
||||||
share_manifests
|
@module_paths = []
|
||||||
share_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
|
end
|
||||||
|
|
||||||
def provision!
|
def provision
|
||||||
# Check that the shared folders are properly shared
|
# Check that the shared folders are properly shared
|
||||||
check = [manifests_guest_path]
|
check = [manifests_guest_path]
|
||||||
@module_paths.each do |host_path, guest_path|
|
@module_paths.each do |host_path, guest_path|
|
||||||
|
@ -103,36 +55,16 @@ module VagrantPlugins
|
||||||
run_puppet_apply
|
run_puppet_apply
|
||||||
end
|
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
|
def manifests_guest_path
|
||||||
File.join(config.pp_path, "manifests")
|
File.join(config.pp_path, "manifests")
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_binary(binary)
|
def verify_binary(binary)
|
||||||
env[:machine].communicate.sudo("which #{binary}",
|
@machine.communicate.sudo(
|
||||||
:error_class => PuppetError,
|
"which #{binary}",
|
||||||
:error_key => :not_detected,
|
:error_class => PuppetError,
|
||||||
:binary => binary)
|
:error_key => :not_detected,
|
||||||
|
:binary => binary)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_puppet_apply
|
def run_puppet_apply
|
||||||
|
@ -155,19 +87,19 @@ module VagrantPlugins
|
||||||
|
|
||||||
command = "cd #{manifests_guest_path} && #{facter}puppet apply #{options} --detailed-exitcodes || [ $? -eq 2 ]"
|
command = "cd #{manifests_guest_path} && #{facter}puppet apply #{options} --detailed-exitcodes || [ $? -eq 2 ]"
|
||||||
|
|
||||||
env[:ui].info I18n.t("vagrant.provisioners.puppet.running_puppet",
|
@machine.env.ui.info I18n.t("vagrant.provisioners.puppet.running_puppet",
|
||||||
:manifest => @manifest_file)
|
:manifest => @manifest_file)
|
||||||
|
|
||||||
env[:machine].communicate.sudo(command) do |type, data|
|
@machine.communicate.sudo(command) do |type, data|
|
||||||
data.chomp!
|
data.chomp!
|
||||||
env[:ui].info(data, :prefix => false) if !data.empty?
|
@machine.env.ui.info(data, :prefix => false) if !data.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_shared_folders(folders)
|
def verify_shared_folders(folders)
|
||||||
folders.each do |folder|
|
folders.each do |folder|
|
||||||
@logger.debug("Checking for shared folder: #{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
|
raise PuppetError, :missing_shared_folders
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,31 +6,17 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
|
|
||||||
class PuppetServer < Vagrant.plugin("2", :provisioner)
|
class PuppetServer < Vagrant.plugin("2", :provisioner)
|
||||||
class Config < Vagrant.plugin("2", :config)
|
def provision
|
||||||
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!
|
|
||||||
verify_binary("puppet")
|
verify_binary("puppet")
|
||||||
run_puppet_agent
|
run_puppet_agent
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_binary(binary)
|
def verify_binary(binary)
|
||||||
env[:vm].channel.sudo("which #{binary}",
|
@machine.communicate.sudo(
|
||||||
:error_class => PuppetServerError,
|
"which #{binary}",
|
||||||
:error_key => :not_detected,
|
:error_class => PuppetServerError,
|
||||||
:binary => binary)
|
:error_key => :not_detected,
|
||||||
|
:binary => binary)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_puppet_agent
|
def run_puppet_agent
|
||||||
|
@ -43,13 +29,13 @@ module VagrantPlugins
|
||||||
if config.puppet_node
|
if config.puppet_node
|
||||||
# If a node name is given, we use that directly for the certname
|
# If a node name is given, we use that directly for the certname
|
||||||
cn = config.puppet_node
|
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
|
# If a host name is given, we explicitly set the certname to
|
||||||
# nil so that the hostname becomes the cert name.
|
# nil so that the hostname becomes the cert name.
|
||||||
cn = nil
|
cn = nil
|
||||||
else
|
else
|
||||||
# Otherwise, we default to the name of the box.
|
# Otherwise, we default to the name of the box.
|
||||||
cn = env[:vm].config.vm.box
|
cn = @machine.config.vm.box
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add the certname option if there is one
|
# 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 ]"
|
command = "#{facter}puppet agent #{options} --server #{config.puppet_server} --detailed-exitcodes || [ $? -eq 2 ]"
|
||||||
|
|
||||||
env[:ui].info I18n.t("vagrant.provisioners.puppet_server.running_puppetd")
|
@machine.env.ui.info I18n.t("vagrant.provisioners.puppet_server.running_puppetd")
|
||||||
env[:vm].channel.sudo(command) do |type, data|
|
@machine.communicate.sudo(command) do |type, data|
|
||||||
data.chomp!
|
data.chomp!
|
||||||
env[:ui].info(data, :prefix => false) if !data.empty?
|
@machine.env.ui.info(data, :prefix => false) if !data.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue