Added a `--provision-with` flag to specify provisioners to use. [GH-367]

This commit is contained in:
Mitchell Hashimoto 2012-01-10 22:03:22 -08:00
parent 566a751d6f
commit bb2a473549
7 changed files with 74 additions and 13 deletions

View File

@ -37,6 +37,8 @@
host will be created if it doesn't exist.
- Added `--force` flag to `box add`, which will overwite any existing boxes
if they exist. [GH-631]
- Added `--provision-with` to `up` which configures what provisioners run,
by shortcut. [GH-367]
- Removed Thor as a dependency for the command line interfaces. This resulted
in general speed increases across all command line commands.
- Linux uses `shutdown -h` instead of `halt` to hopefully more consistently

View File

@ -1,14 +1,18 @@
require "log4r"
module Vagrant
module Action
module VM
class Provision
def initialize(app, env)
@logger = Log4r::Logger.new("vagrant::action::vm::provision")
@app = app
@env = env
@env["provision.enabled"] = true if !@env.has_key?("provision.enabled")
@env["provision.enabled"] = true if !env.has_key?("provision.enabled")
end
def call(env)
@env = env
provisioners = nil
# We set this here so that even if this value is changed in the future,
@ -34,9 +38,23 @@ module Vagrant
end
def enabled_provisioners
@env[:vm].config.vm.provisioners.map do |provisioner|
provisioner.provisioner.new(@env, provisioner.config)
enabled = []
@env[:vm].config.vm.provisioners.each do |provisioner|
if @env["provision.types"]
# If we've specified types of provisioners to enable, then we
# only use those provisioners, and skip any that we haven't
# specified.
if !@env["provision.types"].include?(provisioner.shortcut.to_s)
@logger.debug("Skipping provisioner: #{provisioner.shortcut}")
next
end
end
enabled << provisioner.provisioner.new(@env, provisioner.config)
end
# Return the enable provisioners
enabled
end
end
end

View File

@ -4,7 +4,10 @@ module Vagrant
module Command
class Up < Base
def execute
options = { :provision => true }
options = {
:provision => true,
:provisioners => nil
}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant up [vm-name] [--[no-]provision] [-h]"
@ -14,22 +17,33 @@ module Vagrant
opts.on("--[no-]provision", "Enable or disable provisioning") do |p|
options[:provision] = p
end
opts.on("--provision-with x,y,z", Array,
"Enable only certain provisioners, by type.") do |list|
options[:provisioners] = list
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
# Parameters to send to actions
action_params = {
"provision.enabled" => options[:provision],
"provision.types" => options[:provisioners]
}
# Go over each VM and bring it up
@logger.debug("'Up' each target VM...")
with_target_vms(argv[0]) do |vm|
if vm.created?
@logger.info("Booting: #{vm.name}")
vm.ui.info I18n.t("vagrant.commands.up.vm_created")
vm.start("provision.enabled" => options[:provision])
vm.start(action_params)
else
@logger.info("Creating: #{vm.name}")
vm.up("provision.enabled" => options[:provision])
vm.up(action_params)
end
end
end

View File

@ -82,13 +82,15 @@ module Vagrant
env[:vm].channel.upload(path.to_s, config.upload_path)
# Execute it with sudo
env[:vm].channel.sudo(command) do |ch, type, data|
# Output the data with the proper color based on the stream.
color = type == :stdout ? :green : :red
env[:vm].channel.sudo(command) do |type, data|
if [:stderr, :stdout].include?(type)
# Output the data with the proper color based on the stream.
color = type == :stdout ? :green : :red
# Note: Be sure to chomp the data to avoid the newlines that the
# Chef outputs.
env[:ui].info(data.chomp, :color => color, :prefix => false)
# Note: Be sure to chomp the data to avoid the newlines that the
# Chef outputs.
env[:ui].info(data.chomp, :color => color, :prefix => false)
end
end
end
end

View File

@ -24,4 +24,21 @@ vf
result_file = environment.workdir.join("results")
result_file.exist?.should_not be
end
it "only uses the provisioners specified with `--provision-with`" do
# Skeleton for multiple provisioners
environment.skeleton!("provisioner_multi")
# Setup the basic environment
require_box("default")
assert_execute("vagrant", "box", "add", "base", box_path("default"))
# Bring up the VM, only using the shell provisioner
assert_execute("vagrant", "up", "--provision-with", "shell")
# Verify the file the shell provisioner made is there, but not the
# Chef one.
environment.workdir.join("shell").exist?.should be
environment.workdir.join("chef_solo").exist?.should_not be
end
end

View File

@ -0,0 +1,3 @@
# Chef Solo Basic Skeleton
This is a skeleton that contains a basic chef solo setup.

View File

@ -0,0 +1,5 @@
# Just create a file
file "/vagrant/chef_solo" do
mode 0644
content "success"
end