2012-08-15 03:27:28 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2012-07-15 18:17:58 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
2012-11-07 05:20:55 +00:00
|
|
|
class Provider < Vagrant.plugin("2", :provider)
|
2012-08-04 18:16:31 +00:00
|
|
|
attr_reader :driver
|
|
|
|
|
2014-04-10 17:02:27 +00:00
|
|
|
def self.usable?(raise_error=false)
|
|
|
|
# Instantiate the driver, which will determine the VirtualBox
|
|
|
|
# version and all that, which checks for VirtualBox being present
|
|
|
|
Driver::Meta.new
|
|
|
|
true
|
|
|
|
rescue Vagrant::Errors::VirtualBoxInvalidVersion
|
|
|
|
raise if raise_error
|
|
|
|
return false
|
|
|
|
rescue Vagrant::Errors::VirtualBoxNotDetected
|
|
|
|
raise if raise_error
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-07-25 04:32:38 +00:00
|
|
|
def initialize(machine)
|
2012-08-15 03:27:28 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::provider::virtualbox")
|
2012-07-25 04:32:38 +00:00
|
|
|
@machine = machine
|
2012-08-15 03:27:28 +00:00
|
|
|
|
2012-08-15 05:38:41 +00:00
|
|
|
# This method will load in our driver, so we call it now to
|
|
|
|
# initialize it.
|
|
|
|
machine_id_changed
|
2012-07-25 04:32:38 +00:00
|
|
|
end
|
|
|
|
|
2012-07-27 05:39:27 +00:00
|
|
|
# @see Vagrant::Plugin::V1::Provider#action
|
|
|
|
def action(name)
|
|
|
|
# Attempt to get the action method from the Action class if it
|
|
|
|
# exists, otherwise return nil to show that we don't support the
|
|
|
|
# given action.
|
|
|
|
action_method = "action_#{name}"
|
|
|
|
return Action.send(action_method) if Action.respond_to?(action_method)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-08-15 05:38:41 +00:00
|
|
|
# If the machine ID changed, then we need to rebuild our underlying
|
|
|
|
# driver.
|
|
|
|
def machine_id_changed
|
|
|
|
id = @machine.id
|
|
|
|
|
|
|
|
begin
|
|
|
|
@logger.debug("Instantiating the driver for machine ID: #{@machine.id.inspect}")
|
|
|
|
@driver = Driver::Meta.new(id)
|
|
|
|
rescue Driver::Meta::VMNotFound
|
|
|
|
# The virtual machine doesn't exist, so we probably have a stale
|
|
|
|
# ID. Just clear the id out of the machine and reload it.
|
|
|
|
@logger.debug("VM not found! Clearing saved machine ID and reloading.")
|
|
|
|
id = nil
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-05 20:45:24 +00:00
|
|
|
# Returns the SSH info for accessing the VirtualBox VM.
|
|
|
|
def ssh_info
|
2014-04-28 00:26:24 +00:00
|
|
|
# If the VM is not running that we can't possibly SSH into it
|
|
|
|
return nil if state.id != :running
|
2012-08-05 20:45:24 +00:00
|
|
|
|
|
|
|
# Return what we know. The host is always "127.0.0.1" because
|
|
|
|
# VirtualBox VMs are always local. The port we try to discover
|
|
|
|
# by reading the forwarded ports.
|
|
|
|
return {
|
|
|
|
:host => "127.0.0.1",
|
|
|
|
:port => @driver.ssh_port(@machine.config.ssh.guest_port)
|
|
|
|
}
|
2012-07-27 05:39:27 +00:00
|
|
|
end
|
|
|
|
|
2012-07-25 04:32:38 +00:00
|
|
|
# Return the state of VirtualBox virtual machine by actually
|
|
|
|
# querying VBoxManage.
|
2012-07-27 05:39:27 +00:00
|
|
|
#
|
|
|
|
# @return [Symbol]
|
2012-07-25 04:32:38 +00:00
|
|
|
def state
|
2013-01-21 17:34:04 +00:00
|
|
|
# Determine the ID of the state here.
|
|
|
|
state_id = nil
|
|
|
|
state_id = :not_created if !@driver.uuid
|
|
|
|
state_id = @driver.read_state if !state_id
|
|
|
|
state_id = :unknown if !state_id
|
|
|
|
|
|
|
|
# Translate into short/long descriptions
|
2013-01-21 17:35:46 +00:00
|
|
|
short = state_id.to_s.gsub("_", " ")
|
|
|
|
long = I18n.t("vagrant.commands.status.#{state_id}")
|
2013-01-21 17:34:04 +00:00
|
|
|
|
2014-05-06 04:52:28 +00:00
|
|
|
# If we're not created, then specify the special ID flag
|
|
|
|
if state_id == :not_created
|
|
|
|
state_id = Vagrant::MachineState::NOT_CREATED_ID
|
|
|
|
end
|
|
|
|
|
2013-01-21 17:34:04 +00:00
|
|
|
# Return the state
|
|
|
|
Vagrant::MachineState.new(state_id, short, long)
|
2012-07-25 04:32:38 +00:00
|
|
|
end
|
2012-08-05 20:45:24 +00:00
|
|
|
|
|
|
|
# Returns a human-friendly string version of this provider which
|
|
|
|
# includes the machine's ID that this provider represents, if it
|
|
|
|
# has one.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def to_s
|
|
|
|
id = @machine.id ? @machine.id : "new VM"
|
|
|
|
"VirtualBox (#{id})"
|
|
|
|
end
|
2012-07-15 18:17:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|