From c8634571571ad5164c4602aa6fed646210683cb5 Mon Sep 17 00:00:00 2001 From: Andrew Clay Shafer Date: Sat, 22 May 2010 01:39:31 -0600 Subject: [PATCH] Added provision command Will rerun provisioner on running vms --- lib/vagrant/commands/provision.rb | 31 +++++++++++ lib/vagrant/vm.rb | 4 ++ test/vagrant/commands/provision_test.rb | 68 +++++++++++++++++++++++++ test/vagrant/vm_test.rb | 7 +++ 4 files changed, 110 insertions(+) create mode 100644 lib/vagrant/commands/provision.rb create mode 100644 test/vagrant/commands/provision_test.rb diff --git a/lib/vagrant/commands/provision.rb b/lib/vagrant/commands/provision.rb new file mode 100644 index 000000000..ddceef8df --- /dev/null +++ b/lib/vagrant/commands/provision.rb @@ -0,0 +1,31 @@ +module Vagrant + class Commands + #run the provisioner on a running vm + class Provision < Base + Base.subcommand "provision", self + description "Run the provisioner" + + def execute(args=[]) + all_or_single(args, :provision) + end + + def provision_single(name) + vm = env.vms[name.to_sym] + if vm.nil? + error_and_exit(:unknown_vm, :vm => name) + return # for tests + end + + if vm.vm.running? + vm.provision + else + vm.env.logger.info "VM '#{name}' not running. Ignoring provision request." + end + end + + def options_spec(opts) + opts.banner = "Usage: vagrant provision" + end + end + end +end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index e77cb17dc..01b0cee6e 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -117,6 +117,10 @@ module Vagrant execute!(Actions::VM::Reload) end + def provision + execute!(Actions::VM::Provision) + end + def destroy execute!(Actions::VM::Down) end diff --git a/test/vagrant/commands/provision_test.rb b/test/vagrant/commands/provision_test.rb new file mode 100644 index 000000000..733dfe017 --- /dev/null +++ b/test/vagrant/commands/provision_test.rb @@ -0,0 +1,68 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class CommandsProvisionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Commands::Provision + @env = mock_environment + @instance = @klass.new(@env) + end + + context "executing" do + should "provision all if no name is given" do + @instance.expects(:all_or_single).with([], :provision).once + @instance.execute + end + + should "provision single if a name is given" do + @instance.expects(:provision_single).with("foo").once + @instance.execute(["foo"]) + end + end + + context "provisioning all" do + should "reprovision each VM" do + vms = { :foo => nil, :bar => nil, :baz => nil } + @env.stubs(:vms).returns(vms) + + vms.each do |name, value| + @instance.expects(:provision_single).with(name).once + end + + @instance.execute + end + end + + context "provisioning a single VM" do + setup do + @foo_vm = mock("vm") + @foo_vm.stubs(:env).returns(@env) + + @vm_for_real = mock("vm for real") + @foo_vm.stubs(:vm).returns(@vm_for_real) + vms = { :foo => @foo_vm } + @env.stubs(:vms).returns(vms) + end + + should "error and exit if the VM doesn't exist" do + @env.stubs(:vms).returns({}) + @instance.expects(:error_and_exit).with(:unknown_vm, :vm => 'foo').once + @instance.execute(["foo"]) + end + + should "reload if it's running" do + @vm_for_real.stubs(:running?).returns(true) + @foo_vm.expects(:provision).once + @instance.execute(["foo"]) + end + + should "do log to info if it's not running" do + logger = mock("logger") + logger.expects(:info) + @env.stubs(:logger).returns(logger) + @vm_for_real.stubs(:running?).returns(false) + @foo_vm.expects(:provision).never + @instance.execute(["foo"]) + end + end + +end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 9158dcb9b..339236d7c 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -188,6 +188,13 @@ class VMTest < Test::Unit::TestCase end end + context "provisioning" do + should "execute the provision action" do + @vm.expects(:execute!).with(Vagrant::Actions::VM::Provision).once + @vm.provision + end + end + context "destroying" do should "execute the down action" do @vm.expects(:execute!).with(Vagrant::Actions::VM::Down).once