Move commands into plugins

This commit is contained in:
Mitchell Hashimoto 2012-04-19 13:59:48 -07:00
parent 661f20bb91
commit 1489854d70
43 changed files with 660 additions and 270 deletions

View File

@ -106,14 +106,6 @@ module Vagrant
# Raise an error that the plugin version is invalid
raise ArgumentError, "Invalid plugin version API: #{version}"
end
# Global registry of commands that are available via the CLI.
#
# This registry is used to look up the sub-commands that are available
# to Vagrant.
def self.commands
@commands ||= Registry.new
end
end
# # Default I18n to load the en locale
@ -131,19 +123,3 @@ Vagrant.source_root.join("plugins").each_child do |directory|
# Load the plugin!
load(plugin_file)
end
# Register the built-in commands
Vagrant.commands.register(:box) { Vagrant::Command::Box }
Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy }
Vagrant.commands.register(:gem) { Vagrant::Command::Gem }
Vagrant.commands.register(:halt) { Vagrant::Command::Halt }
Vagrant.commands.register(:init) { Vagrant::Command::Init }
Vagrant.commands.register(:package) { Vagrant::Command::Package }
Vagrant.commands.register(:provision) { Vagrant::Command::Provision }
Vagrant.commands.register(:reload) { Vagrant::Command::Reload }
Vagrant.commands.register(:resume) { Vagrant::Command::Resume }
Vagrant.commands.register(:ssh) { Vagrant::Command::SSH }
Vagrant.commands.register(:"ssh-config") { Vagrant::Command::SSHConfig }
Vagrant.commands.register(:status) { Vagrant::Command::Status }
Vagrant.commands.register(:suspend) { Vagrant::Command::Suspend }
Vagrant.commands.register(:up) { Vagrant::Command::Up }

View File

@ -31,7 +31,14 @@ module Vagrant
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = Vagrant.commands.get(@sub_command.to_sym) if @sub_command
command_class = nil
Vagrant.plugin("1").registered.each do |plugin|
if plugin.command.has_key?(@sub_command.to_sym)
command_class = plugin.command.get(@sub_command.to_sym)
break
end
end
if !command_class || !@sub_command
help
return 0
@ -60,7 +67,11 @@ module Vagrant
# Add the available subcommands as separators in order to print them
# out as well.
keys = []
Vagrant.commands.each { |key, value| keys << key.to_s }
Vagrant.plugin("1").registered.each do |plugin|
plugin.command.each do |key, _|
keys << key
end
end
keys.sort.each do |key|
opts.separator " #{key}"

View File

@ -1,24 +1,5 @@
module Vagrant
module Command
autoload :Base, 'vagrant/command/base'
autoload :Box, 'vagrant/command/box'
autoload :BoxAdd, 'vagrant/command/box_add'
autoload :BoxRemove, 'vagrant/command/box_remove'
autoload :BoxRepackage, 'vagrant/command/box_repackage'
autoload :BoxList, 'vagrant/command/box_list'
autoload :Destroy, 'vagrant/command/destroy'
autoload :Gem, 'vagrant/command/gem'
autoload :Halt, 'vagrant/command/halt'
autoload :Init, 'vagrant/command/init'
autoload :Package, 'vagrant/command/package'
autoload :Provision, 'vagrant/command/provision'
autoload :Reload, 'vagrant/command/reload'
autoload :Resume, 'vagrant/command/resume'
autoload :SSH, 'vagrant/command/ssh'
autoload :SSHConfig, 'vagrant/command/ssh_config'
autoload :Status, 'vagrant/command/status'
autoload :Suspend, 'vagrant/command/suspend'
autoload :Up, 'vagrant/command/up'
end
end

View File

@ -1,58 +0,0 @@
require 'optparse'
module Vagrant
module Command
class Box < Base
def initialize(argv, env)
super
@main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
@subcommands = Registry.new
@subcommands.register(:add) { Vagrant::Command::BoxAdd }
@subcommands.register(:remove) { Vagrant::Command::BoxRemove }
@subcommands.register(:repackage) { Vagrant::Command::BoxRepackage }
@subcommands.register(:list) { Vagrant::Command::BoxList }
end
def execute
if @main_args.include?("-h") || @main_args.include?("--help")
# Print the help for all the box commands.
return help
end
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
return help if !command_class || !@sub_command
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class
command_class.new(@sub_args, @env).execute
end
# Prints the help out for this command
def help
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box <command> [<args>]"
opts.separator ""
opts.separator "Available subcommands:"
# Add the available subcommands as separators in order to print them
# out as well.
keys = []
@subcommands.each { |key, value| keys << key.to_s }
keys.sort.each do |key|
opts.separator " #{key}"
end
opts.separator ""
opts.separator "For help on any individual command run `vagrant box COMMAND -h`"
end
@env.ui.info(opts.help, :prefix => false)
end
end
end
end

View File

@ -1,37 +0,0 @@
require 'optparse'
module Vagrant
module Command
class BoxAdd < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box add <name> <url>"
opts.separator ""
opts.on("-f", "--force", "Overwrite an existing box if it exists.") do |f|
options[:force] = f
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 2
# If we're force adding, then be sure to destroy any existing box if it
# exists.
if options[:force]
existing = @env.boxes.find(argv[0])
existing.destroy if existing
end
@env.boxes.add(argv[0], argv[1])
# Success, exit status 0
0
end
end
end
end

View File

@ -1,28 +0,0 @@
require 'optparse'
module Vagrant
module Command
class BoxList < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box list"
end
# Parse the options
argv = parse_options(opts)
return if !argv
boxes = @env.boxes.sort
if boxes.empty?
return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false)
end
boxes.each { |b| @env.ui.info(b.name, :prefix => false) }
# Success, exit status 0
0
end
end
end
end

View File

@ -1,27 +0,0 @@
require 'optparse'
module Vagrant
module Command
class BoxRemove < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box remove <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 1
b = @env.boxes.find(argv[0])
raise Errors::BoxNotFound, :name => argv[0] if !b
b.destroy
# Success, exit status 0
0
end
end
end
end

View File

@ -1,27 +0,0 @@
require 'optparse'
module Vagrant
module Command
class BoxRepackage < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box repackage <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 1
b = @env.boxes.find(argv[0])
raise Errors::BoxNotFound, :name => argv[0] if !b
b.repackage
# Success, exit status 0
0
end
end
end
end

View File

@ -40,6 +40,21 @@ module Vagrant
get_or_set(:description, value)
end
# Defines additional command line commands available by key. The key
# becomes the subcommand, so if you register a command "foo" then
# "vagrant foo" becomes available.
#
# @param [String] name Subcommand key.
def self.command(name=UNSET_VALUE, &block)
data[:command] ||= Registry.new
# Register a new command class only if a name was given.
data[:command].register(name.to_sym, &block) if name != UNSET_VALUE
# Return the registry
data[:command]
end
# Defines additional configuration keys to be available in the
# Vagrantfile. The configuration class should be returned by a
# block passed to this method. This is done to ensure that the class

View File

@ -0,0 +1,39 @@
require 'optparse'
module VagrantPlugins
module CommandBox
module Command
class Add < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box add <name> <url>"
opts.separator ""
opts.on("-f", "--force", "Overwrite an existing box if it exists.") do |f|
options[:force] = f
end
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 2
# If we're force adding, then be sure to destroy any existing box if it
# exists.
if options[:force]
existing = @env.boxes.find(argv[0])
existing.destroy if existing
end
@env.boxes.add(argv[0], argv[1])
# Success, exit status 0
0
end
end
end
end
end

View File

@ -0,0 +1,30 @@
require 'optparse'
module VagrantPlugins
module CommandBox
module Command
class List < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box list"
end
# Parse the options
argv = parse_options(opts)
return if !argv
boxes = @env.boxes.sort
if boxes.empty?
return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false)
end
boxes.each { |b| @env.ui.info(b.name, :prefix => false) }
# Success, exit status 0
0
end
end
end
end
end

View File

@ -0,0 +1,29 @@
require 'optparse'
module VagrantPlugins
module CommandBox
module Command
class Remove < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box remove <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 1
b = @env.boxes.find(argv[0])
raise Vagrant::Errors::BoxNotFound, :name => argv[0] if !b
b.destroy
# Success, exit status 0
0
end
end
end
end
end

View File

@ -0,0 +1,29 @@
require 'optparse'
module VagrantPlugins
module CommandBox
module Command
class Repackage < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box repackage <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 1
b = @env.boxes.find(argv[0])
raise Vagrant::Errors::BoxNotFound, :name => argv[0] if !b
b.repackage
# Success, exit status 0
0
end
end
end
end
end

View File

@ -0,0 +1,60 @@
require 'optparse'
module VagrantPlugins
module CommandBox
module Command
class Root < Vagrant::Command::Base
def initialize(argv, env)
super
@main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
@subcommands = Registry.new
@subcommands.register(:add) { Add }
@subcommands.register(:list) { List }
@subcommands.register(:remove) { Remove }
@subcommands.register(:repackage) { Repackage }
end
def execute
if @main_args.include?("-h") || @main_args.include?("--help")
# Print the help for all the box commands.
return help
end
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
return help if !command_class || !@sub_command
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class
command_class.new(@sub_args, @env).execute
end
# Prints the help out for this command
def help
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box <command> [<args>]"
opts.separator ""
opts.separator "Available subcommands:"
# Add the available subcommands as separators in order to print them
# out as well.
keys = []
@subcommands.each { |key, value| keys << key.to_s }
keys.sort.each do |key|
opts.separator " #{key}"
end
opts.separator ""
opts.separator "For help on any individual command run `vagrant box COMMAND -h`"
end
@env.ui.info(opts.help, :prefix => false)
end
end
end
end
end

View File

@ -0,0 +1,20 @@
require "vagrant"
module VagrantPlugins
module CommandBox
module Command
autoload :Root, File.expand_path("../command/root", __FILE__)
autoload :Add, File.expand_path("../command/add", __FILE__)
autoload :List, File.expand_path("../command/list", __FILE__)
autoload :Remove, File.expand_path("../command/remove", __FILE__)
autoload :Repackage, File.expand_path("../command/repackage", __FILE__)
end
class Plugin < Vagrant.plugin("1")
name "box command"
description "The `box` command gives you a way to manage boxes."
command("box") { Command::Root }
end
end
end

View File

@ -1,8 +1,6 @@
require 'optparse'
module Vagrant
module Command
class Destroy < Base
module VagrantPlugins
module CommandDestroy
class Command < Vagrant::Command::Base
def execute
options = {}
@ -34,10 +32,10 @@ module Vagrant
begin
choice = @env.ui.ask(I18n.t("vagrant.commands.destroy.confirmation",
:name => vm.name))
rescue Errors::UIExpectsTTY
rescue Vagrant::Errors::UIExpectsTTY
# We raise a more specific error but one which basically
# means the same thing.
raise Errors::DestroyRequiresForce
raise Vagrant::Errors::DestroyRequiresForce
end
do_destroy = choice.upcase == "Y"
end

View File

@ -0,0 +1,14 @@
require "vagrant"
module VagrantPlugins
module CommandDestroy
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "destroy command"
description "The `destroy` command destroys your virtual machines."
command("destroy") { Command }
end
end
end

View File

@ -3,10 +3,10 @@ require "rubygems/gem_runner"
require "vagrant/util/safe_puts"
module Vagrant
module Command
class Gem < Base
include Util::SafePuts
module VagrantPlugins
module CommandGem
class Command < Vagrant::Command::Base
include Vagrant::Util::SafePuts
def execute
# Bundler sets up its own custom gem load paths such that our

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandGem
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "gem command"
description <<-DESC
Provides an interface to RubyGems that can be used to install
RubyGems into the Vagrant environment.
DESC
command("gem") { Command }
end
end
end

View File

@ -1,8 +1,8 @@
require 'optparse'
module Vagrant
module Command
class Halt < Base
module VagrantPlugins
module CommandHalt
class Command < Vagrant::Command::Base
def execute
options = {}

View File

@ -0,0 +1,16 @@
require "vagrant"
module VagrantPlugins
module CommandHalt
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "halt command"
description <<-DESC
The `halt` command halts your virtual machine.
DESC
command("halt") { Command }
end
end
end

View File

@ -2,9 +2,9 @@ require 'optparse'
require 'vagrant/util/template_renderer'
module Vagrant
module Command
class Init < Base
module VagrantPlugins
module CommandInit
class Command < Vagrant::Command::Base
def execute
options = {}
@ -17,7 +17,7 @@ module Vagrant
return if !argv
save_path = @env.cwd.join("Vagrantfile")
raise Errors::VagrantfileExistsError if save_path.exist?
raise Vagrant::Errors::VagrantfileExistsError if save_path.exist?
template_path = ::Vagrant.source_root.join("templates/commands/init/Vagrantfile")
contents = Vagrant::Util::TemplateRenderer.render(template_path,

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandInit
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "init command"
description <<-DESC
The `init` command sets up your working directory to be a
Vagrant-managed environment.
DESC
command("init") { Command }
end
end
end

View File

@ -1,8 +1,8 @@
require 'optparse'
module Vagrant
module Command
class Package < Base
module VagrantPlugins
module CommandPackage
class Command < Vagrant::Command::Base
def execute
options = {}
@ -47,15 +47,15 @@ module Vagrant
protected
def package_base(options)
vm = VM.new(options[:base], @env, @env.config.global, :base => true)
raise Errors::BaseVMNotFound, :name => options[:base] if !vm.created?
vm = Vagrant::VM.new(options[:base], @env, @env.config.global, :base => true)
raise Vagrant::Errors::BaseVMNotFound, :name => options[:base] if !vm.created?
@logger.debug("Packaging base VM: #{vm.name}")
package_vm(vm, options)
end
def package_target(name, options)
with_target_vms(name, :single_target => true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
@logger.debug("Packaging VM: #{vm.name}")
package_vm(vm, options)
end

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandPackage
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "package command"
description <<-DESC
The `package` command will take a previously existing Vagrant
environment and package it into a box file.
DESC
command("package") { Command }
end
end
end

View File

@ -1,8 +1,8 @@
require 'optparse'
module Vagrant
module Command
class Provision < Base
module VagrantPlugins
module CommandProvision
class Command < Vagrant::Command::Base
def execute
options = {}
@ -17,7 +17,7 @@ module Vagrant
# Go over each VM and provision!
@logger.debug("'provision' each target VM...")
with_target_vms(argv) do |vm|
if vm.created?
if vm.state == :running
@logger.info("Provisioning: #{vm.name}")

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandProvision
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "provision command"
description <<-DESC
The `provision` command provisions your virtual machine based on the
configuration of the Vagrantfile.
DESC
command("provision") { Command }
end
end
end

View File

@ -2,9 +2,9 @@ require 'optparse'
require 'vagrant/command/start_mixins'
module Vagrant
module Command
class Reload < Base
module VagrantPlugins
module CommandReload
class Command < Vagrant::Command::Base
include StartMixins
def execute

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandReload
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "reload command"
description <<-DESC
The `reload` command will halt, reconfigure your machine based on
the Vagrantfile, and bring it back up.
DESC
command("reload") { Command }
end
end
end

View File

@ -0,0 +1,33 @@
require 'optparse'
module VagrantPlugins
module CommandResume
class Command < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant resume [vm-name]"
end
# Parse the options
argv = parse_options(opts)
return if !argv
@logger.debug("'resume' each target VM...")
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Resume: #{vm.name}")
vm.resume
else
@logger.info("Not created: #{vm.name}. Not resuming.")
vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
end
end
# Success, exit status 0
0
end
end
end
end

View File

@ -0,0 +1,16 @@
require "vagrant"
module VagrantPlugins
module CommandResume
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "resume command"
description <<-DESC
The `resume` command resumes a suspend virtual machine.
DESC
command("resume") { Command }
end
end
end

View File

@ -1,8 +1,8 @@
require 'optparse'
module Vagrant
module Command
class SSH < Base
module VagrantPlugins
module CommandSSH
class Command < Vagrant::Command::Base
def execute
options = {}
@ -38,9 +38,9 @@ module Vagrant
# Execute the actual SSH
with_target_vms(argv, :single_target => true) do |vm|
# Basic checks that are required for proper SSH
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.state == :inaccessible
raise Errors::VMNotRunningError if vm.state != :running
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
raise Vagrant::Errors::VMInaccessible if !vm.state == :inaccessible
raise Vagrant::Errors::VMNotRunningError if vm.state != :running
if options[:command]
ssh_execute(vm, options[:command])

View File

@ -0,0 +1,16 @@
require "vagrant"
module VagrantPlugins
module CommandSSH
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "ssh command"
description <<-DESC
The `ssh` command provides SSH access to the virtual machine.
DESC
command("ssh") { Command }
end
end
end

View File

@ -2,10 +2,10 @@ require 'optparse'
require "vagrant/util/safe_puts"
module Vagrant
module Command
class SSHConfig < Base
include Util::SafePuts
module VagrantPlugins
module CommandSSHConfig
class Command < Vagrant::Command::Base
include Vagrant::Util::SafePuts
def execute
options = {}
@ -24,8 +24,8 @@ module Vagrant
return if !argv
with_target_vms(argv, :single_target => true) do |vm|
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.state == :inaccessible
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
raise Vagrant::Errors::VMInaccessible if !vm.state == :inaccessible
ssh_info = vm.ssh.info
variables = {
@ -40,7 +40,7 @@ module Vagrant
# Render the template and output directly to STDOUT
template = "commands/ssh_config/config"
safe_puts(Util::TemplateRenderer.render(template, variables))
safe_puts(Vagrant::Util::TemplateRenderer.render(template, variables))
end
# Success, exit status 0

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandSSHConfig
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "ssh-config command"
description <<-DESC
The `ssh-config` command dumps an OpenSSH compatible configuration
that can be used to quickly SSH into your virtual machine.
DESC
command("ssh-config") { Command }
end
end
end

View File

@ -0,0 +1,36 @@
require 'optparse'
module VagrantPlugins
module CommandStatus
class Command < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant status [vm-name]"
end
# Parse the options
argv = parse_options(opts)
return if !argv
state = nil
results = []
with_target_vms(argv) do |vm|
state = vm.state.to_s if !state
results << "#{vm.name.to_s.ljust(25)}#{vm.state.to_s.gsub("_", " ")}"
end
state = results.length == 1 ? state : "listing"
@env.ui.info(I18n.t("vagrant.commands.status.output",
:states => results.join("\n"),
:message => I18n.t("vagrant.commands.status.#{state}")),
:prefix => false)
# Success, exit status 0
0
end
end
end
end

View File

@ -0,0 +1,17 @@
require "vagrant"
module VagrantPlugins
module CommandStatus
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "status command"
description <<-DESC
The `status` command shows the status of all your virtual machines
in this environment.
DESC
command("status") { Command }
end
end
end

View File

@ -0,0 +1,33 @@
require 'optparse'
module VagrantPlugins
module CommandSuspend
class Command < Vagrant::Command::Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant suspend [vm-name]"
end
# Parse the options
argv = parse_options(opts)
return if !argv
@logger.debug("'suspend' each target VM...")
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Suspending: #{vm.name}")
vm.suspend
else
@logger.info("Not created: #{vm.name}. Not suspending.")
vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
end
end
# Success, exit status 0
0
end
end
end
end

View File

@ -0,0 +1,16 @@
require "vagrant"
module VagrantPlugins
module CommandSuspend
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "suspend command"
description <<-DESC
The `suspend` command suspends a running virtual machine.
DESC
command("suspend") { Command }
end
end
end

View File

@ -0,0 +1,40 @@
require 'optparse'
require 'vagrant/command/start_mixins'
module VagrantPlugins
module CommandUp
class Command < Vagrant::Command::Base
include StartMixins
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant up [vm-name] [--[no-]provision] [-h]"
opts.separator ""
build_start_options(opts, options)
end
# Parse the options
argv = parse_options(opts)
return if !argv
# Go over each VM and bring it up
@logger.debug("'Up' each target VM...")
with_target_vms(argv) do |vm|
if vm.created?
@logger.info("Booting: #{vm.name}")
vm.ui.info I18n.t("vagrant.commands.up.vm_created")
vm.start(options)
else
@logger.info("Creating: #{vm.name}")
vm.up(options)
end
end
# Success, exit status 0
0
end
end
end
end

View File

@ -0,0 +1,16 @@
require "vagrant"
module VagrantPlugins
module CommandUp
autoload :Command, File.expand_path("../command", __FILE__)
class Plugin < Vagrant.plugin("1")
name "up command"
description <<-DESC
The `up` command brings the virtual environment up and running.
DESC
command("up") { Command }
end
end
end

View File

@ -23,6 +23,34 @@ describe Vagrant::Plugin::V1 do
plugin.description.should == "bar"
end
describe "commands" do
it "should register command classes" do
plugin = Class.new(described_class) do
command("foo") { "bar" }
end
plugin.command[:foo].should == "bar"
end
it "should lazily register command classes" do
# Below would raise an error if the value of the command class was
# evaluated immediately. By asserting that this does not raise an
# error, we verify that the value is actually lazily loaded
plugin = nil
expect {
plugin = Class.new(described_class) do
command("foo") { raise StandardError, "FAIL!" }
end
}.to_not raise_error
# Now verify when we actually get the command key that
# a proper error is raised.
expect {
plugin.command[:foo]
}.to raise_error(StandardError)
end
end
describe "configuration" do
it "should register configuration classes" do
plugin = Class.new(described_class) do

View File

@ -5,10 +5,6 @@ describe Vagrant do
described_class.source_root.should == Pathname.new(File.expand_path("../../../", __FILE__))
end
it "has a registry for commands" do
described_class.commands.should be_a(Vagrant::Registry)
end
describe "plugin superclass" do
it "returns the proper class for version 1" do
described_class.plugin("1").should == Vagrant::Plugin::V1