diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 2e48f1161..992e5e685 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -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 } diff --git a/lib/vagrant/cli.rb b/lib/vagrant/cli.rb index bbaacb46c..71bc1e7ea 100644 --- a/lib/vagrant/cli.rb +++ b/lib/vagrant/cli.rb @@ -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}" diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index 417450d6c..6d1f5f87d 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -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 diff --git a/lib/vagrant/command/box.rb b/lib/vagrant/command/box.rb deleted file mode 100644 index e1866bead..000000000 --- a/lib/vagrant/command/box.rb +++ /dev/null @@ -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 []" - 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 diff --git a/lib/vagrant/command/box_add.rb b/lib/vagrant/command/box_add.rb deleted file mode 100644 index 82417e6cb..000000000 --- a/lib/vagrant/command/box_add.rb +++ /dev/null @@ -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 " - 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 diff --git a/lib/vagrant/command/box_list.rb b/lib/vagrant/command/box_list.rb deleted file mode 100644 index 4d3f40e06..000000000 --- a/lib/vagrant/command/box_list.rb +++ /dev/null @@ -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 diff --git a/lib/vagrant/command/box_remove.rb b/lib/vagrant/command/box_remove.rb deleted file mode 100644 index 77e828900..000000000 --- a/lib/vagrant/command/box_remove.rb +++ /dev/null @@ -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 " - 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 diff --git a/lib/vagrant/command/box_repackage.rb b/lib/vagrant/command/box_repackage.rb deleted file mode 100644 index f4357002e..000000000 --- a/lib/vagrant/command/box_repackage.rb +++ /dev/null @@ -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 " - 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 diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index 25225cdda..a04ec0053 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -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 diff --git a/plugins/command_box/command/add.rb b/plugins/command_box/command/add.rb new file mode 100644 index 000000000..609b4189d --- /dev/null +++ b/plugins/command_box/command/add.rb @@ -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 " + 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 diff --git a/plugins/command_box/command/list.rb b/plugins/command_box/command/list.rb new file mode 100644 index 000000000..029f3bb45 --- /dev/null +++ b/plugins/command_box/command/list.rb @@ -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 diff --git a/plugins/command_box/command/remove.rb b/plugins/command_box/command/remove.rb new file mode 100644 index 000000000..b10684bc1 --- /dev/null +++ b/plugins/command_box/command/remove.rb @@ -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 " + 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 diff --git a/plugins/command_box/command/repackage.rb b/plugins/command_box/command/repackage.rb new file mode 100644 index 000000000..0b58b9246 --- /dev/null +++ b/plugins/command_box/command/repackage.rb @@ -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 " + 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 diff --git a/plugins/command_box/command/root.rb b/plugins/command_box/command/root.rb new file mode 100644 index 000000000..f8ed23125 --- /dev/null +++ b/plugins/command_box/command/root.rb @@ -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 []" + 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 diff --git a/plugins/command_box/plugin.rb b/plugins/command_box/plugin.rb new file mode 100644 index 000000000..20938ed6b --- /dev/null +++ b/plugins/command_box/plugin.rb @@ -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 diff --git a/lib/vagrant/command/destroy.rb b/plugins/command_destroy/command.rb similarity index 90% rename from lib/vagrant/command/destroy.rb rename to plugins/command_destroy/command.rb index 7d3ca4cd6..49fb73a67 100644 --- a/lib/vagrant/command/destroy.rb +++ b/plugins/command_destroy/command.rb @@ -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 diff --git a/plugins/command_destroy/plugin.rb b/plugins/command_destroy/plugin.rb new file mode 100644 index 000000000..088802bcf --- /dev/null +++ b/plugins/command_destroy/plugin.rb @@ -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 diff --git a/lib/vagrant/command/gem.rb b/plugins/command_gem/command.rb similarity index 90% rename from lib/vagrant/command/gem.rb rename to plugins/command_gem/command.rb index 319883587..116186851 100644 --- a/lib/vagrant/command/gem.rb +++ b/plugins/command_gem/command.rb @@ -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 diff --git a/plugins/command_gem/plugin.rb b/plugins/command_gem/plugin.rb new file mode 100644 index 000000000..64960a918 --- /dev/null +++ b/plugins/command_gem/plugin.rb @@ -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 diff --git a/lib/vagrant/command/halt.rb b/plugins/command_halt/command.rb similarity index 91% rename from lib/vagrant/command/halt.rb rename to plugins/command_halt/command.rb index 401037eec..f62a41990 100644 --- a/lib/vagrant/command/halt.rb +++ b/plugins/command_halt/command.rb @@ -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 = {} diff --git a/plugins/command_halt/plugin.rb b/plugins/command_halt/plugin.rb new file mode 100644 index 000000000..4710fca26 --- /dev/null +++ b/plugins/command_halt/plugin.rb @@ -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 diff --git a/lib/vagrant/command/init.rb b/plugins/command_init/command.rb similarity index 85% rename from lib/vagrant/command/init.rb rename to plugins/command_init/command.rb index 37c44737d..b7ec11a59 100644 --- a/lib/vagrant/command/init.rb +++ b/plugins/command_init/command.rb @@ -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, diff --git a/plugins/command_init/plugin.rb b/plugins/command_init/plugin.rb new file mode 100644 index 000000000..862b896d0 --- /dev/null +++ b/plugins/command_init/plugin.rb @@ -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 diff --git a/lib/vagrant/command/package.rb b/plugins/command_package/command.rb similarity index 84% rename from lib/vagrant/command/package.rb rename to plugins/command_package/command.rb index a8c5f7180..ba68c56f6 100644 --- a/lib/vagrant/command/package.rb +++ b/plugins/command_package/command.rb @@ -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 diff --git a/plugins/command_package/plugin.rb b/plugins/command_package/plugin.rb new file mode 100644 index 000000000..df7d814f9 --- /dev/null +++ b/plugins/command_package/plugin.rb @@ -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 diff --git a/lib/vagrant/command/provision.rb b/plugins/command_provision/command.rb similarity index 91% rename from lib/vagrant/command/provision.rb rename to plugins/command_provision/command.rb index 89bfd43bf..6b6e28eec 100644 --- a/lib/vagrant/command/provision.rb +++ b/plugins/command_provision/command.rb @@ -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}") diff --git a/plugins/command_provision/plugin.rb b/plugins/command_provision/plugin.rb new file mode 100644 index 000000000..eff8161e3 --- /dev/null +++ b/plugins/command_provision/plugin.rb @@ -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 diff --git a/lib/vagrant/command/reload.rb b/plugins/command_reload/command.rb similarity index 90% rename from lib/vagrant/command/reload.rb rename to plugins/command_reload/command.rb index ac3d63b4b..b2bdbccff 100644 --- a/lib/vagrant/command/reload.rb +++ b/plugins/command_reload/command.rb @@ -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 diff --git a/plugins/command_reload/plugin.rb b/plugins/command_reload/plugin.rb new file mode 100644 index 000000000..9ad2056d0 --- /dev/null +++ b/plugins/command_reload/plugin.rb @@ -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 diff --git a/plugins/command_resume/command.rb b/plugins/command_resume/command.rb new file mode 100644 index 000000000..68e969aab --- /dev/null +++ b/plugins/command_resume/command.rb @@ -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 diff --git a/plugins/command_resume/plugin.rb b/plugins/command_resume/plugin.rb new file mode 100644 index 000000000..b42d13761 --- /dev/null +++ b/plugins/command_resume/plugin.rb @@ -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 diff --git a/lib/vagrant/command/ssh.rb b/plugins/command_ssh/command.rb similarity index 89% rename from lib/vagrant/command/ssh.rb rename to plugins/command_ssh/command.rb index cc2d780a3..e19117674 100644 --- a/lib/vagrant/command/ssh.rb +++ b/plugins/command_ssh/command.rb @@ -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]) diff --git a/plugins/command_ssh/plugin.rb b/plugins/command_ssh/plugin.rb new file mode 100644 index 000000000..55e5ff269 --- /dev/null +++ b/plugins/command_ssh/plugin.rb @@ -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 diff --git a/lib/vagrant/command/ssh_config.rb b/plugins/command_ssh_config/command.rb similarity index 76% rename from lib/vagrant/command/ssh_config.rb rename to plugins/command_ssh_config/command.rb index f8bcd2f53..a5e4701b2 100644 --- a/lib/vagrant/command/ssh_config.rb +++ b/plugins/command_ssh_config/command.rb @@ -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 diff --git a/plugins/command_ssh_config/plugin.rb b/plugins/command_ssh_config/plugin.rb new file mode 100644 index 000000000..ab67e0416 --- /dev/null +++ b/plugins/command_ssh_config/plugin.rb @@ -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 diff --git a/plugins/command_status/command.rb b/plugins/command_status/command.rb new file mode 100644 index 000000000..4c9d1621f --- /dev/null +++ b/plugins/command_status/command.rb @@ -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 diff --git a/plugins/command_status/plugin.rb b/plugins/command_status/plugin.rb new file mode 100644 index 000000000..70d8d6e0e --- /dev/null +++ b/plugins/command_status/plugin.rb @@ -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 diff --git a/plugins/command_suspend/command.rb b/plugins/command_suspend/command.rb new file mode 100644 index 000000000..317e55ca8 --- /dev/null +++ b/plugins/command_suspend/command.rb @@ -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 diff --git a/plugins/command_suspend/plugin.rb b/plugins/command_suspend/plugin.rb new file mode 100644 index 000000000..d5f30494f --- /dev/null +++ b/plugins/command_suspend/plugin.rb @@ -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 diff --git a/plugins/command_up/command.rb b/plugins/command_up/command.rb new file mode 100644 index 000000000..1b3fcbeaf --- /dev/null +++ b/plugins/command_up/command.rb @@ -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 diff --git a/plugins/command_up/plugin.rb b/plugins/command_up/plugin.rb new file mode 100644 index 000000000..6fd632c12 --- /dev/null +++ b/plugins/command_up/plugin.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v1_test.rb b/test/unit/vagrant/plugin/v1_test.rb index 4e0671305..91ae94bb2 100644 --- a/test/unit/vagrant/plugin/v1_test.rb +++ b/test/unit/vagrant/plugin/v1_test.rb @@ -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 diff --git a/test/unit/vagrant_test.rb b/test/unit/vagrant_test.rb index 8363a3505..a97ad657a 100644 --- a/test/unit/vagrant_test.rb +++ b/test/unit/vagrant_test.rb @@ -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