Some work in progress environment support

This commit is contained in:
Ben Hines 2014-10-19 21:28:38 -07:00
parent 1a6805d469
commit 16870d72d1
5 changed files with 171 additions and 71 deletions

79
Vagrantfile vendored
View File

@ -1,54 +1,35 @@
# This Vagrantfile can be used to develop Vagrant. Note that VirtualBox
# doesn't run in VirtualBox so you can't actually _run_ Vagrant within
# the VM created by this Vagrantfile, but you can use it to develop the
# Ruby, run unit tests, etc.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
["virtualbox", "vmware_fusion", "vmware_workstation"].each do |provider|
config.vm.provider provider do |v, override|
v.memory = "1024"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "centos7"
config.vm.define :centos7 do |centos7|
config.vm.network "private_network", ip: "192.168.11.3"
config.vm.hostname = "centos7"
# config.vm.network "public_network"
# config.ssh.forward_agent = true
config.vm.synced_folder "../puppet", "/puppet"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
# Use VBoxManage to customize the VM. For example to change memory:
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
config.vm.provision "puppet" do |puppet|
puppet.environmentpath = "../puppet/environments"
puppet.environment = "testenv"
# puppet.manifests_path = "../puppet/manifests"
# puppet.manifest_file = "site.pp"
puppet.module_path = [ "../puppet/modules/public", "../puppet/modules/private" ]
# puppet.options = "--debug --verbose"
end
# Deprecated method:
#puppet apply --debug --verbose --modulepath '/puppet/modules/private:/puppet/modules/public:/etc/puppet/modules'
#--manifestdir /tmp/vagrant-puppet-1/manifests --detailed-exitcodes /tmp/vagrant-puppet-1/manifests/site.pp
end
config.vm.provision "shell", inline: $shell
end
$shell = <<-CONTENTS
MARKER_FILE="/usr/local/etc/vagrant_provision_marker"
# Only provision once
if [ -f "${MARKER_FILE}" ]; then
exit 0
fi
# Update apt
apt-get update
# Install basic dependencies
apt-get install -y build-essential bsdtar curl
# Install RVM
su -l -c 'curl -L https://get.rvm.io | bash -s stable' vagrant
# Add the vagrant user to the RVM group
#usermod -a -G rvm vagrant
# Install some Rubies
su -l -c 'rvm install 2.1.1' vagrant
su -l -c 'rvm --default use 2.1.1' vagrant
# Output the Ruby version (for sanity)
su -l -c 'ruby --version' vagrant
# Install Git
apt-get install -y git
# Automatically move into the shared folder, but only add the command
# if it's not already there.
grep -q 'cd /vagrant' /home/vagrant/.bash_profile || echo 'cd /vagrant' >> /home/vagrant/.bash_profile
# Touch the marker file so we don't do this again
touch ${MARKER_FILE}
CONTENTS

View File

@ -10,6 +10,8 @@ module VagrantPlugins
attr_accessor :hiera_config_path
attr_accessor :manifest_file
attr_accessor :manifests_path
attr_accessor :environment
attr_accessor :environmentpath
attr_accessor :module_path
attr_accessor :options
attr_accessor :synced_folder_type
@ -22,6 +24,8 @@ module VagrantPlugins
@hiera_config_path = UNSET_VALUE
@manifest_file = UNSET_VALUE
@manifests_path = UNSET_VALUE
@environment = UNSET_VALUE
@environmentpath = UNSET_VALUE
@module_path = UNSET_VALUE
@options = []
@facter = {}
@ -45,24 +49,45 @@ module VagrantPlugins
def merge(other)
super.tap do |result|
result.facter = @facter.merge(other.facter)
result.environmentpath = @facter.merge(other.environmentpath)
result.environment = @facter.merge(other.environment)
end
end
def finalize!
super
if @manifests_path == UNSET_VALUE
@manifests_path = [:host, "manifests"]
end
if @environmentpath == UNSET_VALUE
if @manifests_path == UNSET_VALUE
if 1 #If puppet 3.4+
puts "Puppet 3.4+, manifests_path is unset and environmentpath is unset, presuming an environment"
@environmentpath = [:host, "environments"]
else
@manifests_path = [:host, "manifests"]
end
end
if @manifests_path && !@manifests_path.is_a?(Array)
@manifests_path = [:host, @manifests_path]
if @manifests_path && !@manifests_path.is_a?(Array)
@manifests_path = [:host, @manifests_path]
end
else
if @environmentpath && !@environmentpath.is_a?(Array)
@environmentpath = [:host, @environmentpath]
end
end
@manifests_path[0] = @manifests_path[0].to_sym
@hiera_config_path = nil if @hiera_config_path == UNSET_VALUE
@manifest_file = "default.pp" if @manifest_file == UNSET_VALUE
if @environmentpath == UNSET_VALUE
@manifests_path[0] = @manifests_path[0].to_sym
@environmentpath = nil
@manifest_file = "default.pp" if @manifest_file == UNSET_VALUE
else
@environmentpath[0] = @environmentpath[0].to_sym
@environment = "production" if @environment == UNSET_VALUE
@manifest_file = nil
end
@module_path = nil if @module_path == UNSET_VALUE
@synced_folder_type = nil if @synced_folder_type == UNSET_VALUE
@temp_dir = nil if @temp_dir == UNSET_VALUE
@ -96,8 +121,13 @@ module VagrantPlugins
# Calculate the manifests and module paths based on env
this_expanded_module_paths = expanded_module_paths(machine.env.root_path)
if environmentpath != UNSET_VALUE && manifests_path != UNSET_VALUE
errors << "You may not specify both environmentpath and manifests_path. Please specify environment and environmentpath only"
end
# Manifests path/file validation
if manifests_path[0].to_sym == :host
puts "manifests_path is #{manifests_path}"
if manifests_path != UNSET_VALUE && manifests_path[0].to_sym == :host
expanded_path = Pathname.new(manifests_path[1]).
expand_path(machine.env.root_path)
if !expanded_path.directory?
@ -110,8 +140,29 @@ module VagrantPlugins
manifest: expanded_manifest_file.to_s)
end
end
end
# Environments path/file validation
if environmentpath != UNSET_VALUE && environmentpath[0].to_sym == :host
expanded_path = Pathname.new(environmentpath[1]).
expand_path(machine.env.root_path)
if !expanded_path.directory?
errors << I18n.t("vagrant.provisioners.puppet.environmentpath_missing",
path: expanded_path.to_s)
else
expanded_environment_file = expanded_path.join(environment)
if !expanded_environment_file.file? && !expanded_environment_file.directory?
errors << I18n.t("vagrant.provisioners.puppet.environment_missing",
environment: environment.to_s,
environmentpath: expanded_path.to_s)
end
end
end
if environmentpath == UNSET_VALUE && manifests_path == UNSET_VALUE
errors << "Please specify either a Puppet environmentpath + environment (preferred) or manifests_path (deprecated)."
end
# Module paths validation
this_expanded_module_paths.each do |path|
if !path.directory?

View File

@ -18,7 +18,6 @@ module VagrantPlugins
# Calculate the paths we're going to use based on the environment
root_path = @machine.env.root_path
@expanded_module_paths = @config.expanded_module_paths(root_path)
@manifest_file = File.join(manifests_guest_path, @config.manifest_file)
# Setup the module paths
@module_paths = []
@ -30,11 +29,23 @@ module VagrantPlugins
folder_opts[:type] = @config.synced_folder_type if @config.synced_folder_type
folder_opts[:owner] = "root" if !@config.synced_folder_type
# Share the manifests directory with the guest
if @config.manifests_path[0].to_sym == :host
root_config.vm.synced_folder(
File.expand_path(@config.manifests_path[1], root_path),
manifests_guest_path, folder_opts)
if @config.environmentpath.is_a?(Array)
# Share the environments directory with the guest
if @config.environmentpath[0].to_sym == :host
root_config.vm.synced_folder(
File.expand_path(@config.environmentpath[1], root_path),
environments_guest_path, folder_opts)
end
parse_environment_metadata()
else
# Non-Environment mode
@manifest_file = File.join(manifests_guest_path, @config.manifest_file)
# Share the manifests directory with the guest
if @config.manifests_path[0].to_sym == :host
root_config.vm.synced_folder(
File.expand_path(@config.manifests_path[1], root_path),
manifests_guest_path, folder_opts)
end
end
# Share the module paths
@ -43,6 +54,24 @@ module VagrantPlugins
end
end
# For convenience, add in any module_paths from the Puppet environment.cfg to the vagrant module_paths
# This is needed because puppet apply does not read environment metadata (as of v3.6)
def parse_environment_metadata
environment_conf = File.join(environments_guest_path, @config.environment, "environment.conf")
if @machine.communicate.test("test -e #{environment_conf}", sudo: true)
conf = @machine.communicate.sudo("cat #{environment_conf}") do | type, data|
if type == :stdout
#modulepath = $basemodulepath:modules/private:modules/public
puts "got line #{data}"
end
end
puts "Found an environment cfg at: #{environment_conf} - #{conf}"
else
puts "env cfg not found, looked for #{environment_conf}"
end
end
def provision
# If the machine has a wait for reboot functionality, then
# do that (primarily Windows)
@ -52,9 +81,12 @@ module VagrantPlugins
# Check that the shared folders are properly shared
check = []
if @config.manifests_path[0] == :host
if @config.manifests_path.is_a?(Array) && @config.manifests_path[0] == :host
check << manifests_guest_path
end
if @config.environmentpath.is_a?(Array) && @config.environmentpath[0] == :host
check << environments_guest_path
end
@module_paths.each do |host_path, guest_path|
check << guest_path
end
@ -92,6 +124,16 @@ module VagrantPlugins
end
end
def environments_guest_path
if config.environmentpath[0] == :host
# The path is on the host, so point to where it is shared
File.join(config.temp_dir, "environments")
else
# The path is on the VM, so just point directly to it
config.environmentpath[1]
end
end
def verify_binary(binary)
@machine.communicate.sudo(
"which #{binary}",
@ -125,10 +167,18 @@ module VagrantPlugins
options << "--color=false"
end
options << "--manifestdir #{manifests_guest_path}"
options << "--detailed-exitcodes"
options << @manifest_file
if !config.environmentpath.empty?
options << "#{environments_guest_path}/#{@config.environment}/manifests"
options << "--environment #{@config.environment}"
else
options << "--manifestdir #{manifests_guest_path}"
options << @manifest_file
end
options = options.join(" ")
@machine.ui.info("Running ye puppet apply with options #{options}")
# Build up the custom facts if we have any
facter = ""
@ -155,9 +205,15 @@ module VagrantPlugins
end
end
@machine.ui.info(I18n.t(
"vagrant.provisioners.puppet.running_puppet",
manifest: config.manifest_file))
if !config.environmentpath.empty?
@machine.ui.info(I18n.t(
"vagrant.provisioners.puppet.running_puppet_env",
environment: config.environment))
else
@machine.ui.info(I18n.t(
"vagrant.provisioners.puppet.running_puppet",
manifest: config.manifest_file))
end
opts = {
elevated: true,

View File

@ -1733,12 +1733,23 @@ en:
installed on this guest. Puppet provisioning can not continue without
Puppet properly installed.
running_puppet: "Running Puppet with %{manifest}..."
running_puppet_env: "Running Puppet with environment %{environment}..."
manifest_missing: |-
The configured Puppet manifest is missing. Please specify a path to an
existing manifest:
%{manifest}
environment_missing: |-
The configured Puppet environment folder '%{environment}' was not found in the
specified environmentpath %{environmentpath}.
Please specify a path to an existing Puppet directory environment.
manifests_path_missing: "The manifests path specified for Puppet does not exist: %{path}"
manifest_missing: |-
The configured Puppet envrionment is missing. Please specify a path to an
existing envrionment file:
%{environment}
manifests_path_missing: "The environment path specified for Puppet does not exist: %{path}"
missing_shared_folders: |-
Shared folders that Puppet requires are missing on the virtual machine.
This is usually due to configuration changing after already booting the

View File

@ -15,7 +15,8 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "vagrant"
s.add_dependency "bundler", ">= 1.5.2", "< 1.7.0"
s.add_dependency "bundler", ">= 1.5.2"
#, "< 1.7.0"
s.add_dependency "childprocess", "~> 0.5.0"
s.add_dependency "erubis", "~> 2.7.0"
s.add_dependency "i18n", "~> 0.6.0"