Remove old commands files

This commit is contained in:
Mitchell Hashimoto 2010-04-13 17:25:43 -07:00
parent 368e468620
commit 1a4f934465
2 changed files with 0 additions and 622 deletions

View File

@ -1,244 +0,0 @@
module Vagrant
# Contains all the command-line commands invoked by the
# binaries. Having them all in one location assists with
# documentation and also takes the commands out of some of
# the other classes.
class Commands
include Vagrant::Util
# The environment which these commands will act on
attr_reader :env
# Initialize a new {Commands} instance for the given environment.
# This merely prepares the {env} variable.
def initialize(env)
@env = env
end
# Bring up a vagrant instance. This handles everything from importing
# the base VM, setting up shared folders, forwarded ports, etc to
# provisioning the instance with chef. {up} also starts the instance,
# running it in the background.
def up
if env.vm
logger.info "VM already created. Starting VM if its not already running..."
env.vm.start
else
env.require_box
env.create_vm.execute!(Actions::VM::Up)
end
end
# Destroys a vagrant instance. This not only shuts down the instance
# (if its running), but also deletes it from the system, including the
# hard disks associated with it.
#
# This command requires that an instance already be brought up with
# `vagrant up`.
def destroy
env.require_persisted_vm
env.vm.destroy
end
# Reload the environment. This is almost equivalent to the {up} command
# except that it doesn't import the VM and do the initialize bootstrapping
# of the instance. Instead, it forces a shutdown (if its running) of the
# VM, updates the metadata (shared folders, forwarded ports), restarts
# the VM, and then reruns the provisioning if enabled.
def reload
env.require_persisted_vm
env.vm.execute!(Actions::VM::Reload)
end
# SSH into the vagrant instance. This will setup an SSH connection into
# the vagrant instance, replacing the running ruby process with the SSH
# connection.
#
# This command requires that an instance already be brought up with
# `vagrant up`.
def ssh
env.require_persisted_vm
env.ssh.connect
end
# Outputs a valid entry for .ssh/config which can be used to connect
# to this environment.
def ssh_config(opts={})
env.require_root_path
puts TemplateRenderer.render("ssh_config", {
:host_key => opts[:host] || "vagrant",
:ssh_user => env.config.ssh.username,
:ssh_port => env.ssh.port,
:private_key_path => env.config.ssh.private_key_path
})
end
# Halts a running vagrant instance. This forcibly halts the instance;
# it is the equivalent of pulling the power on a machine. The instance
# can be restarted again with {up}.
#
# This command requires than an instance already be brought up with
# `vagrant up`.
def halt
env.require_persisted_vm
env.vm.execute!(Actions::VM::Halt)
end
# Suspend a running vagrant instance. This suspends the instance, saving
# the state of the VM and "pausing" it. The instance can be resumed
# again with {resume}.
#
# This command requires that an instance already be brought up with
# `vagrant up`.
def suspend
env.require_persisted_vm
env.vm.suspend
end
# Resume a running vagrant instance. This resumes an already suspended
# instance (from {suspend}).
#
# This command requires that an instance already be brought up with
# `vagrant up`.
def resume
env.require_persisted_vm
env.vm.resume
end
# Export and package the current vm
#
# This command requires that an instance be powered off
def package(out_path=nil, opts={})
opts[:include] ||= []
if !opts[:base]
# Packaging a pre-existing environment
env.require_persisted_vm
else
# Packaging a base box; that is a VM not tied to a specific
# vagrant environment
vm = VM.find(opts[:base])
vm.env = env if vm
env.vm = vm
error_and_exit(:vm_base_not_found, :name => opts[:base]) unless vm
end
error_and_exit(:vm_power_off_to_package) unless env.vm.powered_off?
env.vm.package(out_path, opts[:include])
end
# Manages the `vagrant box` command, allowing the user to add
# and remove boxes. This single command, given an array, determines
# which action to take and calls the respective action method
# (see {box_add} and {box_remove})
def box(argv)
sub_commands = ["list", "add", "remove"]
if !sub_commands.include?(argv[0])
error_and_exit(:command_box_invalid)
end
send("box_#{argv[0]}", env, *argv[1..-1])
end
# Lists all added boxes
def box_list(env)
boxes = Box.all(env).sort
wrap_output do
if !boxes.empty?
puts "Installed Vagrant Boxes:\n\n"
boxes.each do |box|
Kernel.puts box
end
else
Kernel.puts "No Vagrant Boxes Added!"
end
end
end
# Adds a box to the local filesystem, given a URI.
def box_add(env, name, path)
Box.add(env, name, path)
end
# Removes a box.
def box_remove(env, name)
box = Box.find(env, name)
if box.nil?
error_and_exit(:box_remove_doesnt_exist)
return # for tests
end
box.destroy
end
# Outputs the status of the current environment. This command outputs
# useful information such as whether or not the environment is created
# and if its running, suspended, etc.
def status
wrap_output do
if !env.vm
puts <<-msg
The environment has not yet been created. Run `vagrant up` to create the
environment.
msg
else
additional_msg = ""
if env.vm.vm.running?
additional_msg = <<-msg
To stop this VM, you can run `vagrant halt` to shut it down forcefully,
or you can run `vagrant suspend` to simply suspend the virtual machine.
In either case, to restart it again, simply run a `vagrant up`.
msg
elsif env.vm.vm.saved?
additional_msg = <<-msg
To resume this VM, simply run `vagrant up`.
msg
elsif env.vm.vm.powered_off?
additional_msg = <<-msg
To restart this VM, simply run `vagrant up`.
msg
end
if !additional_msg.empty?
additional_msg.chomp!
additional_msg = "\n\n#{additional_msg}"
end
puts <<-msg
The environment has been created. The status of the current environment's
virtual machine is: "#{env.vm.vm.state}."#{additional_msg}
msg
end
end
end
class << self
# Initializes a directory for use with vagrant. This command copies an
# initial `Vagrantfile` into the current working directory so you can
# begin using vagrant. The configuration file contains some documentation
# to get you started.
def init(default_box=nil)
rootfile_path = File.join(Dir.pwd, Environment::ROOTFILE_NAME)
if File.exist?(rootfile_path)
error_and_exit(:rootfile_already_exists)
end
# Copy over the rootfile template into this directory
default_box ||= "base"
File.open(rootfile_path, 'w+') do |f|
f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_box))
end
end
# Runs a command in the current environment by loading the environment
# of the current working directory prior to executing.
def execute(command, *args)
env = Environment.load!
env.commands.send(command, *args)
end
end
end
end

View File

@ -1,378 +0,0 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class CommandsTest < Test::Unit::TestCase
setup do
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
@env = mock_environment
@env.stubs(:vm).returns(@persisted_vm)
@env.stubs(:require_persisted_vm)
Vagrant::Environment.stubs(:load!).returns(@env)
end
context "instance methods" do
setup do
@commands = Vagrant::Commands.new(@env)
end
context "initialization" do
should "set up the environment variable" do
env = mock("env")
command = Vagrant::Commands.new(env)
assert_equal env, command.env
end
end
context "up" do
setup do
@new_vm = mock("vm")
@new_vm.stubs(:execute!)
@env.stubs(:vm).returns(nil)
@env.stubs(:require_box)
@env.stubs(:create_vm).returns(@new_vm)
end
should "require a box" do
@env.expects(:require_box).once
@commands.up
end
should "call the up action on VM if it doesn't exist" do
@new_vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once
@commands.up
end
should "call start on the persisted vm if it exists" do
@env.stubs(:vm).returns(@persisted_vm)
@persisted_vm.expects(:start).once
@env.expects(:create_vm).never
@commands.up
end
end
context "destroy" do
setup do
@persisted_vm.stubs(:destroy)
end
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.destroy
end
should "destroy the persisted VM and the VM image" do
@persisted_vm.expects(:destroy).once
@commands.destroy
end
end
context "reload" do
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.reload
end
should "call the `reload` action on the VM" do
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Reload).once
@commands.reload
end
end
context "ssh" do
setup do
@env.ssh.stubs(:connect)
end
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.ssh
end
should "connect to SSH" do
@env.ssh.expects(:connect).once
@commands.ssh
end
end
context "halt" do
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.halt
end
should "call the `halt` action on the VM" do
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once
@commands.halt
end
end
context "ssh-config" do
setup do
@ssh = mock("ssh")
@ssh.stubs(:port).returns(2197)
@env.stubs(:ssh).returns(@ssh)
@env.stubs(:require_root_path)
@commands.stubs(:puts)
@data = {
:host_key => "vagrant",
:ssh_user => @env.config.ssh.username,
:ssh_port => @env.ssh.port,
:private_key_path => @env.config.ssh.private_key_path
}
end
should "require root path" do
@env.expects(:require_root_path).once
@commands.ssh_config
end
should "output rendered template" do
result = mock("result")
Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data).returns(result)
@commands.expects(:puts).with(result).once
@commands.ssh_config
end
should "render with the given host name if given" do
opts = { :host => "foo" }
@data[:host_key] = opts[:host]
Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data)
@commands.ssh_config(opts)
end
end
context "suspend" do
setup do
@persisted_vm.stubs(:suspend)
@persisted_vm.stubs(:saved?).returns(false)
end
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.suspend
end
should "suspend the VM" do
@persisted_vm.expects(:suspend).once
@commands.suspend
end
end
context "resume" do
setup do
@persisted_vm.stubs(:resume)
@persisted_vm.stubs(:saved?).returns(true)
end
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@commands.resume
end
should "save the state of the VM" do
@persisted_vm.expects(:resume).once
@commands.resume
end
end
context "package" do
setup do
@persisted_vm.stubs(:package)
@persisted_vm.stubs(:powered_off?).returns(true)
end
context "with no base specified" do
should "require a persisted vm" do
@env.expects(:require_persisted_vm).once
@commands.package
end
end
context "with base specified" do
setup do
@vm = mock("vm")
Vagrant::VM.stubs(:find).with(@name).returns(@vm)
@vm.stubs(:env=).with(@env)
@env.stubs(:vm=)
@name = :bar
end
should "find the given base and set it on the env" do
Vagrant::VM.expects(:find).with(@name).returns(@vm)
@vm.expects(:env=).with(@env)
@env.expects(:vm=).with(@vm)
@commands.package("foo", { :base => @name })
end
should "error if the VM is not found" do
Vagrant::VM.expects(:find).with(@name).returns(nil)
@commands.expects(:error_and_exit).with(:vm_base_not_found, :name => @name).once
@commands.package("foo", { :base => @name })
end
end
context "shared (with and without base specified)" do
should "error and exit if the VM is not powered off" do
@persisted_vm.stubs(:powered_off?).returns(false)
@commands.expects(:error_and_exit).with(:vm_power_off_to_package).once
@persisted_vm.expects(:package).never
@commands.package
end
should "call package on the persisted VM" do
@persisted_vm.expects(:package).once
@commands.package
end
should "pass the out path and include_files to the package method" do
out_path = mock("out_path")
include_files = mock("include_files")
@persisted_vm.expects(:package).with(out_path, include_files).once
@commands.package(out_path, {
:include => include_files
})
end
should "default to an empty array when not include_files are specified" do
out_path = mock("out_path")
@persisted_vm.expects(:package).with(out_path, []).once
@commands.package(out_path)
end
end
end
context "box" do
setup do
@commands.stubs(:box_foo)
@commands.stubs(:box_add)
@commands.stubs(:box_remove)
end
should "error and exit if the first argument is not a valid subcommand" do
@commands.expects(:error_and_exit).with(:command_box_invalid).once
@commands.box(["foo"])
end
should "not error and exit if the first argument is a valid subcommand" do
commands = ["add", "remove", "list"]
commands.each do |command|
@commands.expects(:error_and_exit).never
@commands.expects("box_#{command}".to_sym).once
@commands.box([command])
end
end
should "forward any additional arguments" do
@commands.expects(:box_add).with(@env, 1,2,3).once
@commands.box(["add",1,2,3])
end
end
context "box list" do
setup do
@boxes = ["foo", "bar"]
Vagrant::Box.stubs(:all).returns(@boxes)
@commands.stubs(:wrap_output)
end
should "call all on box and sort the results" do
@all = mock("all")
@all.expects(:sort).returns(@boxes)
Vagrant::Box.expects(:all).with(@env).returns(@all)
@commands.box_list(@env)
end
end
context "box add" do
setup do
@name = "foo"
@path = "bar"
end
should "execute the add action with the name and path" do
Vagrant::Box.expects(:add).with(@env, @name, @path).once
@commands.box_add(@env, @name, @path)
end
end
context "box remove" do
setup do
@name = "foo"
end
should "error and exit if the box doesn't exist" do
Vagrant::Box.expects(:find).returns(nil)
@commands.expects(:error_and_exit).with(:box_remove_doesnt_exist).once
@commands.box_remove(@env, @name)
end
should "call destroy on the box if it exists" do
@box = mock("box")
Vagrant::Box.expects(:find).with(@env, @name).returns(@box)
@box.expects(:destroy).once
@commands.box_remove(@env, @name)
end
end
end
context "class methods" do
context "init" do
setup do
@file = mock("file")
@file.stubs(:write)
File.stubs(:open).yields(@file)
@rootfile_path = File.join(Dir.pwd, Vagrant::Environment::ROOTFILE_NAME)
Vagrant::Util::TemplateRenderer.stubs(:render)
end
should "error and exit if a rootfile already exists" do
File.expects(:exist?).with(@rootfile_path).returns(true)
Vagrant::Commands.expects(:error_and_exit).with(:rootfile_already_exists).once
Vagrant::Commands.init
end
should "write to the rootfile path using the template renderer" do
result = "foo"
Vagrant::Util::TemplateRenderer.expects(:render).returns(result).once
@file.expects(:write).with(result).once
File.expects(:open).with(@rootfile_path, 'w+').yields(@file)
Vagrant::Commands.init
end
should "use the given base box if given" do
box = "zooo"
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => box)
Vagrant::Commands.init(box)
end
should "use the default `base` if no box is given" do
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base")
Vagrant::Commands.init
end
end
context "executing commands in the current environment" do
should "load the environment then send the command to the commands instance" do
method = :foo
args = [1,2,3]
Vagrant::Environment.expects(:load!).returns(@env)
@env.commands.expects(:send).with(method, *args).once
Vagrant::Commands.execute(method, *args)
end
end
end
end