From 6c7e88c3ec2a8d14808be5dfec56d3a306907aee Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 17 Dec 2011 11:14:56 -0800 Subject: [PATCH] `vagrant halt` --- lib/vagrant.rb | 1 + lib/vagrant/command.rb | 1 + lib/vagrant/command/halt.rb | 32 +++++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 33df916c8..e5691e9df 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -95,6 +95,7 @@ I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_ro # Register the built-in commands Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy } +Vagrant.commands.register(:halt) { Vagrant::Command::Halt } Vagrant.commands.register(:up) { Vagrant::Command::Up } # Register the built-in config keys diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index 6142f8beb..468f9215a 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -3,6 +3,7 @@ module Vagrant autoload :Base, 'vagrant/command/base' autoload :Destroy, 'vagrant/command/destroy' + autoload :Halt, 'vagrant/command/halt' autoload :Up, 'vagrant/command/up' end end diff --git a/lib/vagrant/command/halt.rb b/lib/vagrant/command/halt.rb index 3b4d6f85a..b938d99de 100644 --- a/lib/vagrant/command/halt.rb +++ b/lib/vagrant/command/halt.rb @@ -1,15 +1,33 @@ +require 'optparse' + module Vagrant module Command - class HaltCommand < NamedBase - class_option :force, :type => :boolean, :default => false, :aliases => "-f" - register "halt", "Halt the running VMs in the environment" - + class Halt < Base def execute - target_vms.each do |vm| + options = {} + + opts = OptionParser.new do |opts| + opts.banner = "Usage: vagrant halt [vm-name] [--force] [-h]" + + opts.separator "" + + opts.on("-f", "--force", "Force shut down (equivalent of pulling power)") do |f| + options[:force] = f + end + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + @logger.debug("Halt command: #{argv.inspect} #{options.inspect}") + with_target_vms(argv[0]) do |vm| if vm.created? - vm.halt(options) + @logger.info("Halting #{vm.name}") + vm.halt(:force => options[:force]) else - vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created") + @logger.info("Not halting #{vm.name}, since not created.") + vm.ui.info I18n.t("vagrant.commands.common.vm_not_created") end end end