2014-02-15 23:29:16 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
require_relative "driver"
|
|
|
|
require_relative "plugin"
|
|
|
|
|
2014-02-16 01:06:26 +00:00
|
|
|
require "vagrant/util/platform"
|
2014-02-16 00:28:11 +00:00
|
|
|
require "vagrant/util/powershell"
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module HyperV
|
|
|
|
class Provider < Vagrant.plugin("2", :provider)
|
2014-02-16 00:28:11 +00:00
|
|
|
attr_reader :driver
|
|
|
|
|
2014-04-10 17:04:03 +00:00
|
|
|
def self.usable?(raise_error=false)
|
2014-02-16 01:06:26 +00:00
|
|
|
if !Vagrant::Util::Platform.windows?
|
|
|
|
raise Errors::WindowsRequired
|
|
|
|
end
|
|
|
|
|
2015-12-10 15:04:39 +00:00
|
|
|
if !Vagrant::Util::Platform.windows_admin? and
|
|
|
|
!Vagrant::Util::Platform.windows_hyperv_admin?
|
|
|
|
raise Errors::AdminRequired
|
2014-02-16 01:06:26 +00:00
|
|
|
end
|
|
|
|
|
2014-02-16 00:28:11 +00:00
|
|
|
if !Vagrant::Util::PowerShell.available?
|
|
|
|
raise Errors::PowerShellRequired
|
|
|
|
end
|
2014-03-06 16:51:07 +00:00
|
|
|
|
2014-04-10 17:04:03 +00:00
|
|
|
true
|
2014-04-10 17:45:48 +00:00
|
|
|
rescue Errors::HyperVError
|
2014-04-10 17:04:03 +00:00
|
|
|
raise if raise_error
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(machine)
|
|
|
|
@machine = machine
|
|
|
|
|
2014-03-06 16:51:07 +00:00
|
|
|
# This method will load in our driver, so we call it now to
|
|
|
|
# initialize it.
|
|
|
|
machine_id_changed
|
|
|
|
end
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
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
|
|
|
|
|
2014-03-06 16:58:31 +00:00
|
|
|
def machine_id_changed
|
|
|
|
@driver = Driver.new(@machine.id)
|
|
|
|
end
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
def state
|
2014-02-16 19:31:33 +00:00
|
|
|
state_id = nil
|
|
|
|
state_id = :not_created if !@machine.id
|
|
|
|
|
|
|
|
if !state_id
|
|
|
|
# Run a custom action we define called "read_state" which does
|
|
|
|
# what it says. It puts the state in the `:machine_state_id`
|
|
|
|
# key in the environment.
|
|
|
|
env = @machine.action(:read_state)
|
|
|
|
state_id = env[:machine_state_id]
|
|
|
|
end
|
2014-02-15 23:29:16 +00:00
|
|
|
|
|
|
|
# Get the short and long description
|
2014-02-26 19:19:11 +00:00
|
|
|
short = state_id.to_s
|
2014-02-15 23:29:16 +00:00
|
|
|
long = ""
|
|
|
|
|
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
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
# Return the MachineState object
|
|
|
|
Vagrant::MachineState.new(state_id, short, long)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
id = @machine.id.nil? ? "new" : @machine.id
|
|
|
|
"Hyper-V (#{id})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def ssh_info
|
2014-03-06 20:27:05 +00:00
|
|
|
# We can only SSH into a running machine
|
|
|
|
return nil if state.id != :running
|
|
|
|
|
|
|
|
# Read the IP of the machine using Hyper-V APIs
|
|
|
|
network = @driver.read_guest_ip
|
|
|
|
return nil if !network["ip"]
|
2014-03-06 19:01:19 +00:00
|
|
|
|
2014-03-06 20:27:05 +00:00
|
|
|
{
|
|
|
|
host: network["ip"],
|
|
|
|
port: 22,
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|