Move all commands to the Vagrant::Command class. Document all commands.
This commit is contained in:
parent
aa2d3d58db
commit
6c9c09c1b1
|
@ -23,6 +23,6 @@ Destroys the vagrant environment.
|
|||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::VM.down
|
||||
Vagrant::Commands.down
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env ruby
|
||||
begin
|
||||
require File.expand_path('../../.bundle/environment', __FILE__)
|
||||
rescue LoadError
|
||||
# Fallback on rubygems
|
||||
require "rubygems"
|
||||
end
|
||||
|
||||
require 'git-style-binary/command'
|
||||
|
||||
# Get library
|
||||
libdir = File.join(File.dirname(__FILE__), '..', 'lib')
|
||||
$:.unshift(libdir) unless $:.include?(libdir)
|
||||
require 'vagrant'
|
||||
|
||||
GitStyleBinary.command do
|
||||
short_desc "initializes directory for vagrant use"
|
||||
banner <<-EOS
|
||||
Usage: #{command.full_name} #{all_options_string}
|
||||
|
||||
Creates the initial files required for using vagrant.
|
||||
|
||||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::Commands.init
|
||||
end
|
||||
end
|
|
@ -23,6 +23,6 @@ Resumes the vagrant environment.
|
|||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::VM.resume
|
||||
Vagrant::Commands.resume
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,6 +23,6 @@ Opens an SSH connection into the created VM.
|
|||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::VM.ssh
|
||||
Vagrant::Commands.ssh
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,6 +23,6 @@ Suspends the vagrant environment.
|
|||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::VM.suspend
|
||||
Vagrant::Commands.suspend
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,6 +23,6 @@ Create the vagrant environment.
|
|||
EOS
|
||||
|
||||
run do |command|
|
||||
Vagrant::VM.up
|
||||
Vagrant::Commands.up
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,7 @@ require 'net/ssh'
|
|||
require 'net/scp'
|
||||
require 'vagrant/busy'
|
||||
require 'vagrant/util'
|
||||
require 'vagrant/commands'
|
||||
require 'vagrant/config'
|
||||
require 'vagrant/env'
|
||||
require 'vagrant/provisioning'
|
||||
|
@ -20,4 +21,3 @@ require 'vagrant/vm'
|
|||
# TODO: Make this configurable
|
||||
log_output = ENV['VAGRANT_ENV'] == 'test' ? nil : STDOUT
|
||||
VAGRANT_LOGGER = Vagrant::Logger.new(log_output)
|
||||
Vagrant::Env.load! unless ENV['VAGRANT_ENV'] == 'test'
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
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
|
||||
extend Vagrant::Util
|
||||
|
||||
class <<self
|
||||
# TODO: Coming soon to a theatre near you.
|
||||
def init
|
||||
|
||||
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
|
||||
Env.load!
|
||||
VM.up
|
||||
end
|
||||
|
||||
# Tear down 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 down
|
||||
Env.load!
|
||||
Env.require_persisted_vm
|
||||
Env.persisted_vm.destroy
|
||||
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.load!
|
||||
Env.require_persisted_vm
|
||||
SSH.connect
|
||||
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.load!
|
||||
Env.require_persisted_vm
|
||||
error_and_exit(<<-error) if Env.persisted_vm.saved?
|
||||
The vagrant virtual environment you are trying to suspend is already in a
|
||||
suspended state.
|
||||
error
|
||||
Env.persisted_vm.save_state(true)
|
||||
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.load!
|
||||
Env.require_persisted_vm
|
||||
error_and_exit(<<-error) unless Env.persisted_vm.saved?
|
||||
The vagrant virtual environment you are trying to resume is not in a
|
||||
suspended state.
|
||||
error
|
||||
Env.persisted_vm.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,39 +13,6 @@ module Vagrant
|
|||
new.create
|
||||
end
|
||||
|
||||
# Tear down a virtual machine.
|
||||
def down
|
||||
Env.require_persisted_vm
|
||||
Env.persisted_vm.destroy
|
||||
end
|
||||
|
||||
# SSHs into the VM and replaces the ruby process with the SSH process
|
||||
def ssh
|
||||
Env.require_persisted_vm
|
||||
SSH.connect
|
||||
end
|
||||
|
||||
# Save the state of the current vagrant environment to disk
|
||||
def suspend
|
||||
Env.require_persisted_vm
|
||||
error_and_exit(<<-error) if Env.persisted_vm.saved?
|
||||
The vagrant virtual environment you are trying to suspend is already in a
|
||||
suspended state.
|
||||
error
|
||||
logger.info "Saving VM state..."
|
||||
Env.persisted_vm.save_state(true)
|
||||
end
|
||||
|
||||
# Resume the current vagrant environment from disk
|
||||
def resume
|
||||
Env.require_persisted_vm
|
||||
error_and_exit(<<-error) unless Env.persisted_vm.saved?
|
||||
The vagrant virtual environment you are trying to resume is not in a
|
||||
suspended state.
|
||||
error
|
||||
Env.persisted_vm.start
|
||||
end
|
||||
|
||||
# Finds a virtual machine by a given UUID and either returns
|
||||
# a Vagrant::VM object or returns nil.
|
||||
def find(uuid)
|
||||
|
@ -202,8 +169,9 @@ error
|
|||
@vm.saved?
|
||||
end
|
||||
|
||||
def save_state(errs)
|
||||
@vm.save_state(errs)
|
||||
def save_state
|
||||
logger.info "Saving VM state..."
|
||||
@vm.save_state(true)
|
||||
end
|
||||
|
||||
# TODO need a better way to which controller is the hd
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class CommandsTest < Test::Unit::TestCase
|
||||
setup do
|
||||
Vagrant::Env.stubs(:load!)
|
||||
|
||||
@persisted_vm = mock("persisted_vm")
|
||||
Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm)
|
||||
end
|
||||
|
||||
context "up" do
|
||||
setup do
|
||||
Vagrant::VM.stubs(:up)
|
||||
end
|
||||
|
||||
should "require load the environment" do
|
||||
Vagrant::Env.expects(:load!).once
|
||||
Vagrant::Commands.up
|
||||
end
|
||||
|
||||
should "call up on VM" do
|
||||
Vagrant::VM.expects(:up).once
|
||||
Vagrant::Commands.up
|
||||
end
|
||||
end
|
||||
|
||||
context "down" do
|
||||
setup do
|
||||
@persisted_vm.stubs(:destroy)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::Commands.down
|
||||
end
|
||||
|
||||
should "destroy the persisted VM and the VM image" do
|
||||
@persisted_vm.expects(:destroy).once
|
||||
Vagrant::Commands.down
|
||||
end
|
||||
end
|
||||
|
||||
context "ssh" do
|
||||
setup do
|
||||
Vagrant::SSH.stubs(:connect)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::Commands.ssh
|
||||
end
|
||||
|
||||
should "connect to SSH" do
|
||||
Vagrant::SSH.expects(:connect).once
|
||||
Vagrant::Commands.ssh
|
||||
end
|
||||
end
|
||||
|
||||
context "suspend" do
|
||||
setup do
|
||||
@persisted_vm.stubs(:save_state)
|
||||
@persisted_vm.stubs(:saved?).returns(false)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::Commands.suspend
|
||||
end
|
||||
|
||||
should "error and exit if the VM is already saved" do
|
||||
@persisted_vm.expects(:saved?).returns(true)
|
||||
Vagrant::Commands.expects(:error_and_exit).once
|
||||
@persisted_vm.expects(:save_state).never
|
||||
Vagrant::Commands.suspend
|
||||
end
|
||||
|
||||
should "save the state of the VM" do
|
||||
@persisted_vm.expects(:save_state).once
|
||||
Vagrant::Commands.suspend
|
||||
end
|
||||
end
|
||||
|
||||
context "resume" do
|
||||
setup do
|
||||
@persisted_vm.stubs(:start)
|
||||
@persisted_vm.stubs(:saved?).returns(true)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::Commands.resume
|
||||
end
|
||||
|
||||
should "error and exit if the VM is not already saved" do
|
||||
@persisted_vm.expects(:saved?).returns(false)
|
||||
Vagrant::Commands.expects(:error_and_exit).once
|
||||
@persisted_vm.expects(:save_state).never
|
||||
Vagrant::Commands.resume
|
||||
end
|
||||
|
||||
should "save the state of the VM" do
|
||||
@persisted_vm.expects(:start).once
|
||||
Vagrant::Commands.resume
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,38 +11,6 @@ class VMTest < Test::Unit::TestCase
|
|||
Net::SSH.stubs(:start)
|
||||
end
|
||||
|
||||
context "vagrant ssh" do
|
||||
setup do
|
||||
Vagrant::SSH.stubs(:connect)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::VM.ssh
|
||||
end
|
||||
|
||||
should "connect to SSH" do
|
||||
Vagrant::SSH.expects(:connect).once
|
||||
Vagrant::VM.ssh
|
||||
end
|
||||
end
|
||||
|
||||
context "vagrant down" do
|
||||
setup do
|
||||
@persisted_vm.stubs(:destroy)
|
||||
end
|
||||
|
||||
should "require a persisted VM" do
|
||||
Vagrant::Env.expects(:require_persisted_vm).once
|
||||
Vagrant::VM.down
|
||||
end
|
||||
|
||||
should "destroy the persisted VM and the VM image" do
|
||||
@persisted_vm.expects(:destroy).once
|
||||
Vagrant::VM.down
|
||||
end
|
||||
end
|
||||
|
||||
context "vagrant up" do
|
||||
should "create a Vagrant::VM instance and call create" do
|
||||
inst = mock("instance")
|
||||
|
@ -174,47 +142,6 @@ class VMTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "suspending and resuming a vm" do
|
||||
should "put the vm in a suspended state" do
|
||||
saved_state_expectation(false)
|
||||
save_expectation
|
||||
Vagrant::VM.suspend
|
||||
end
|
||||
|
||||
should "results in an error and exit if the vm is already in a saved state" do
|
||||
saved_state_expectation(true)
|
||||
save_expectation
|
||||
Vagrant::VM.expects(:error_and_exit)
|
||||
Vagrant::VM.suspend
|
||||
end
|
||||
|
||||
should "start a vm in a suspended state" do
|
||||
saved_state_expectation(true)
|
||||
start_expectation
|
||||
Vagrant::VM.resume
|
||||
end
|
||||
|
||||
should "results in an error and exit if the vm is not in a saved state" do
|
||||
saved_state_expectation(false)
|
||||
start_expectation
|
||||
# TODO research the matter of mocking exit
|
||||
Vagrant::VM.expects(:error_and_exit)
|
||||
Vagrant::VM.resume
|
||||
end
|
||||
|
||||
def saved_state_expectation(saved)
|
||||
@persisted_vm.expects(:saved?).returns(saved)
|
||||
end
|
||||
|
||||
def save_expectation
|
||||
@persisted_vm.expects(:save_state).with(true)
|
||||
end
|
||||
|
||||
def start_expectation
|
||||
Vagrant::Env.persisted_vm.expects(:start).once.returns(true)
|
||||
end
|
||||
end
|
||||
|
||||
context "shared folders" do
|
||||
setup do
|
||||
@mock_vm = mock("mock_vm")
|
||||
|
@ -268,6 +195,18 @@ class VMTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "saving the state" do
|
||||
should "check if a VM is saved" do
|
||||
@mock_vm.expects(:saved?).returns("foo")
|
||||
assert_equal "foo", @vm.saved?
|
||||
end
|
||||
|
||||
should "save state with errors raised" do
|
||||
@mock_vm.expects(:save_state).with(true).once
|
||||
@vm.save_state
|
||||
end
|
||||
end
|
||||
|
||||
context "creating a new vm with a specified disk storage location" do
|
||||
should "error and exit of the vm is not powered off" do
|
||||
# Exit does not prevent method from proceeding in test, so we must set expectations
|
||||
|
|
Loading…
Reference in New Issue