Removed old VM code for VM creation. Lots of code removed!

This commit is contained in:
Mitchell Hashimoto 2010-02-15 00:04:13 -08:00
parent bf0aff45f6
commit 86524048ae
2 changed files with 0 additions and 321 deletions

View File

@ -12,12 +12,6 @@ module Vagrant
vm.execute!
end
# Bring up the virtual machine. Imports the base image and
# provisions it.
def up
new.create
end
# Finds a virtual machine by a given UUID and either returns
# a Vagrant::VM object or returns nil.
def find(uuid)
@ -57,27 +51,6 @@ module Vagrant
results
end
def create
share_folder("vagrant-root", Env.root_path, Vagrant.config.vm.project_directory)
# Create the provisioning object, prior to doing anything so it can
# set any configuration on the VM object prior to creation
provisioning = Provisioning.new(self)
# The path of righteousness
import
move_hd if Vagrant.config[:vm][:hd_location]
persist
setup_mac_address
forward_ports
setup_shared_folders
start
mount_shared_folders
# Once we're started, run the provisioning
provisioning.run
end
def destroy
if @vm.running?
logger.info "VM is running. Forcing immediate shutdown..."
@ -88,114 +61,6 @@ module Vagrant
@vm.destroy(:destroy_image => true)
end
def move_hd
error_and_exit(<<-error) unless @vm.powered_off?
The virtual machine must be powered off to move its disk.
error
old_image = hd.image.dup
new_image_file = Vagrant.config[:vm][:hd_location] + old_image.filename
logger.info "Cloning current VM Disk to new location (#{ new_image_file })..."
# TODO image extension default?
new_image = hd.image.clone(new_image_file , Vagrant.config[:vm][:disk_image_format], true)
hd.image = new_image
logger.info "Attaching new disk to VM ..."
@vm.save
logger.info "Destroying old VM Disk (#{ old_image.filename })..."
old_image.destroy(true)
end
def import
logger.info "Importing base VM (#{Vagrant.config[:vm][:base]})..."
@vm = VirtualBox::VM.import(File.expand_path(Vagrant.config[:vm][:base]))
end
def persist
logger.info "Persisting the VM UUID (#{@vm.uuid})..."
Env.persist_vm(@vm)
end
def setup_mac_address
logger.info "Matching MAC addresses..."
@vm.nics.first.macaddress = Vagrant.config[:vm][:base_mac]
@vm.save(true)
end
def forward_ports
logger.info "Forwarding ports..."
Vagrant.config.vm.forwarded_ports.each do |name, options|
logger.info "Forwarding \"#{name}\": #{options[:guestport]} => #{options[:hostport]}"
port = VirtualBox::ForwardedPort.new
port.name = name
port.hostport = options[:hostport]
port.guestport = options[:guestport]
@vm.forwarded_ports << port
end
@vm.save(true)
end
def setup_shared_folders
logger.info "Creating shared folders metadata..."
shared_folders.each do |name, hostpath, guestpath|
folder = VirtualBox::SharedFolder.new
folder.name = name
folder.hostpath = hostpath
@vm.shared_folders << folder
end
@vm.save(true)
end
def mount_shared_folders
logger.info "Mounting shared folders..."
Vagrant::SSH.execute do |ssh|
shared_folders.each do |name, hostpath, guestpath|
logger.info "-- #{name}: #{guestpath}"
ssh.exec!("sudo mkdir -p #{guestpath}")
ssh.exec!("sudo mount -t vboxsf #{name} #{guestpath}")
ssh.exec!("sudo chown #{Vagrant.config.ssh.username} #{guestpath}")
end
end
end
def start(sleep_interval = 5)
logger.info "Booting VM..."
@vm.start(:headless, true)
# Now we have to wait for the boot to be successful
logger.info "Waiting for VM to boot..."
Vagrant.config[:ssh][:max_tries].to_i.times do |i|
logger.info "Trying to connect (attempt ##{i+1} of #{Vagrant.config[:ssh][:max_tries]})..."
if Vagrant::SSH.up?
logger.info "VM booted and ready for use!"
return true
end
sleep sleep_interval
end
logger.info "Failed to connect to VM! Failed to boot?"
false
end
def shared_folders(clear=false)
@shared_folders = nil if clear
@shared_folders ||= []
end
def share_folder(name, hostpath, guestpath)
shared_folders << [name, hostpath, guestpath]
end
def saved?
@vm.saved?
end
@ -236,11 +101,6 @@ error
tar_path
end
# TODO need a better way to which controller is the hd
def hd
@vm.storage_controllers.first.devices.first
end
def powered_off?; @vm.powered_off? end
def export(filename); @vm.export(filename, {}, true) end

View File

@ -96,15 +96,6 @@ class VMTest < Test::Unit::TestCase
end
end
context "vagrant up" do
should "create a Vagrant::VM instance and call create" do
inst = mock("instance")
inst.expects(:create).once
Vagrant::VM.expects(:new).returns(inst)
Vagrant::VM.up
end
end
context "finding a VM" do
should "return nil if the VM is not found" do
VirtualBox::VM.expects(:find).returns(nil)
@ -124,23 +115,6 @@ class VMTest < Test::Unit::TestCase
@vm = Vagrant::VM.new(@mock_vm)
end
context "creating" do
should "create the VM in the proper order" do
prov = mock("prov")
create_seq = sequence("create_seq")
Vagrant::Provisioning.expects(:new).with(@vm).in_sequence(create_seq).returns(prov)
@vm.expects(:import).in_sequence(create_seq)
@vm.expects(:persist).in_sequence(create_seq)
@vm.expects(:setup_mac_address).in_sequence(create_seq)
@vm.expects(:forward_ports).in_sequence(create_seq)
@vm.expects(:setup_shared_folders).in_sequence(create_seq)
@vm.expects(:start).in_sequence(create_seq)
@vm.expects(:mount_shared_folders).in_sequence(create_seq)
prov.expects(:run).in_sequence(create_seq)
@vm.create
end
end
context "destroying" do
setup do
@mock_vm.stubs(:running?).returns(false)
@ -159,127 +133,6 @@ class VMTest < Test::Unit::TestCase
end
end
context "starting" do
setup do
@mock_vm.stubs(:start)
end
should "start the VM in headless mode" do
@mock_vm.expects(:start).with(:headless, true).once
Vagrant::SSH.expects(:up?).once.returns(true)
@vm.start(0)
end
should "repeatedly ping the SSH port and return false with no response" do
seq = sequence('pings')
Vagrant::SSH.expects(:up?).times(Vagrant.config[:ssh][:max_tries].to_i - 1).returns(false).in_sequence(seq)
Vagrant::SSH.expects(:up?).once.returns(true).in_sequence(seq)
assert @vm.start(0)
end
should "ping the max number of times then just return" do
Vagrant::SSH.expects(:up?).times(Vagrant.config[:ssh][:max_tries].to_i).returns(false)
assert !@vm.start(0)
end
end
context "importing" do
should "call import on VirtualBox::VM with the proper base" do
VirtualBox::VM.expects(:import).once
@vm.import
end
should "return the VM object" do
VirtualBox::VM.expects(:import).returns(@mock_vm).once
assert_equal @mock_vm, @vm.import
end
end
context "persisting" do
should "persist the VM with Env" do
@mock_vm.stubs(:uuid)
Vagrant::Env.expects(:persist_vm).with(@mock_vm).once
@vm.persist
end
end
context "setting up MAC address" do
should "match the mac address with the base" do
nic = mock("nic")
nic.expects(:macaddress=).once
@mock_vm.expects(:nics).returns([nic]).once
@mock_vm.expects(:save).with(true).once
@vm.setup_mac_address
end
end
context "forwarding ports" do
should "create a port forwarding for the VM" do
# TODO: Test the actual port value to make sure it has the
# correct attributes
forwarded_ports = mock("forwarded_ports")
forwarded_ports.expects(:<<)
@mock_vm.expects(:forwarded_ports).returns(forwarded_ports)
@mock_vm.expects(:save).with(true).once
@vm.forward_ports
end
end
context "shared folders" do
setup do
@mock_vm = mock("mock_vm")
@vm = Vagrant::VM.new(@mock_vm)
end
should "not have any shared folders initially" do
assert @vm.shared_folders.empty?
end
should "be able to add shared folders" do
@vm.share_folder("foo", "from", "to")
assert_equal 1, @vm.shared_folders.length
end
should "be able to clear shared folders" do
@vm.share_folder("foo", "from", "to")
assert !@vm.shared_folders.empty?
@vm.shared_folders(true)
assert @vm.shared_folders.empty?
end
should "add all shared folders to the VM with 'setup_shared_folders'" do
@vm.share_folder("foo", "from", "to")
@vm.share_folder("bar", "bfrom", "bto")
share_seq = sequence("share_seq")
shared_folders = mock("shared_folders")
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "foo" && sf.hostpath == "from" }
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.hostpath == "bfrom" }
@mock_vm.stubs(:shared_folders).returns(shared_folders)
@mock_vm.expects(:save).with(true).once
@vm.setup_shared_folders
end
should "mount all shared folders to the VM with `mount_shared_folders`" do
@vm.share_folder("foo", "from", "to")
@vm.share_folder("bar", "bfrom", "bto")
mount_seq = sequence("mount_seq")
ssh = mock("ssh")
@vm.shared_folders.each do |name, hostpath, guestpath|
ssh.expects(:exec!).with("sudo mkdir -p #{guestpath}").in_sequence(mount_seq)
ssh.expects(:exec!).with("sudo mount -t vboxsf #{name} #{guestpath}").in_sequence(mount_seq)
ssh.expects(:exec!).with("sudo chown #{Vagrant.config.ssh.username} #{guestpath}").in_sequence(mount_seq)
end
Vagrant::SSH.expects(:execute).yields(ssh)
@vm.mount_shared_folders
end
end
context "saving the state" do
should "check if a VM is saved" do
@mock_vm.expects(:saved?).returns("foo")
@ -291,40 +144,6 @@ class VMTest < Test::Unit::TestCase
@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
vm = move_hd_expectations
@mock_vm.expects(:powered_off?).returns(false)
vm.expects(:error_and_exit)
vm.move_hd
end
should "create assign a new disk image, and delete the old one" do
vm = move_hd_expectations
@mock_vm.expects(:powered_off?).returns(true)
vm.move_hd
end
def move_hd_expectations
image, hd = mock('image'), mock('hd')
Vagrant.config[:vm].expects(:hd_location).at_least_once.returns('/locations/')
image.expects(:clone).with(Vagrant.config[:vm][:hd_location] + 'foo', Vagrant.config[:vm][:disk_image_format], true).returns(image)
image.expects(:filename).twice.returns('foo')
image.expects(:destroy)
hd.expects(:image).twice.returns(image)
hd.expects(:image=).with(image)
@mock_vm.expects(:save)
vm = Vagrant::VM.new(@mock_vm)
vm.expects(:hd).times(3).returns(hd)
vm
end
end
end
context "packaging a vm" do