Merge pull request #3068 from MSOpenTech/driver-methods
provider/hyperv: move to Driver based model
This commit is contained in:
parent
c9623a2a3f
commit
d1dc010073
|
@ -8,8 +8,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
env[:ui].info("Deleting the machine...")
|
||||
options = { VmId: env[:machine].id }
|
||||
env[:machine].provider.driver.execute('delete_vm.ps1', options)
|
||||
env[:machine].provider.driver.delete_vm
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -78,8 +78,7 @@ module VagrantPlugins
|
|||
options[:switchname] = switch if switch
|
||||
|
||||
env[:ui].detail("Creating and registering the VM...")
|
||||
server = env[:machine].provider.driver.execute(
|
||||
'import_vm.ps1', options)
|
||||
server = env[:machine].provider.driver.import(options)
|
||||
env[:ui].detail("Successfully imported a VM with name: #{server['name']}")
|
||||
env[:machine].id = server["id"]
|
||||
@app.call(env)
|
||||
|
|
|
@ -26,8 +26,7 @@ module VagrantPlugins
|
|||
begin
|
||||
Timeout.timeout(120) do
|
||||
begin
|
||||
options = { VmId: env[:machine].id }
|
||||
network_info = env[:machine].provider.driver.execute('get_network_config.ps1', options)
|
||||
network_info = env[:machine].provider.driver.read_guest_ip
|
||||
host_ip = network_info["ip"]
|
||||
sleep 10 if host_ip.empty?
|
||||
end while host_ip.empty?
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
require "log4r"
|
||||
|
||||
module VagrantPlugins
|
||||
|
@ -11,9 +12,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
if env[:machine].id
|
||||
options = { VmId: env[:machine].id }
|
||||
response = env[:machine].provider.driver.execute(
|
||||
"get_vm_status.ps1", options)
|
||||
response = env[:machine].provider.driver.get_current_state
|
||||
env[:machine_state_id] = response["state"].downcase.to_sym
|
||||
|
||||
# If the machine isn't created, then our ID is stale, so just
|
||||
|
|
|
@ -8,8 +8,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
env[:ui].info("Resuming the machine...")
|
||||
options = { VmId: env[:machine].id }
|
||||
env[:machine].provider.driver.execute("resume_vm.ps1", options)
|
||||
env[:machine].provider.driver.resume
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,9 +8,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
env[:ui].output('Starting the machine...')
|
||||
options = { vm_id: env[:machine].id }
|
||||
env[:machine].provider.driver.execute('start_vm.ps1', options)
|
||||
|
||||
env[:machine].provider.driver.start
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,8 +8,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
env[:ui].info("Stopping the machine...")
|
||||
options = { VmId: env[:machine].id }
|
||||
env[:machine].provider.driver.execute('stop_vm.ps1', options)
|
||||
env[:machine].provider.driver.stop
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,8 +8,7 @@ module VagrantPlugins
|
|||
|
||||
def call(env)
|
||||
env[:ui].info("Suspending the machine...")
|
||||
options = { VmId: env[:machine].id }
|
||||
env[:machine].provider.driver.execute("suspend_vm.ps1", options)
|
||||
env[:machine].provider.driver.suspend
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,8 +23,7 @@ module VagrantPlugins
|
|||
return if env[:interrupted]
|
||||
|
||||
# Try to get the IP
|
||||
network_info = env[:machine].provider.driver.execute(
|
||||
"get_network_config.ps1", VmId: env[:machine].id)
|
||||
network_info = env[:machine].provider.driver.read_guest_ip
|
||||
guest_ip = network_info["ip"]
|
||||
|
||||
if guest_ip
|
||||
|
|
|
@ -9,6 +9,12 @@ module VagrantPlugins
|
|||
class Driver
|
||||
ERROR_REGEXP = /===Begin-Error===(.+?)===End-Error===/m
|
||||
OUTPUT_REGEXP = /===Begin-Output===(.+?)===End-Output===/m
|
||||
attr_reader :vm_id, :machine
|
||||
|
||||
def initialize(machine)
|
||||
@vm_id = machine.id
|
||||
@machine = machine
|
||||
end
|
||||
|
||||
def execute(path, options)
|
||||
r = execute_powershell(path, options)
|
||||
|
@ -39,6 +45,38 @@ module VagrantPlugins
|
|||
return JSON.parse(output_match[1])
|
||||
end
|
||||
|
||||
def get_current_state
|
||||
execute('get_vm_status.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def delete_vm
|
||||
execute('delete_vm.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def read_guest_ip
|
||||
execute('get_network_config.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def resume
|
||||
execute('resume_vm.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def start
|
||||
execute('start_vm.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def stop
|
||||
execute('stop_vm.ps1', { VmId: vm_id })
|
||||
end
|
||||
|
||||
def suspend
|
||||
execute("suspend_vm.ps1", { VmId: vm_id })
|
||||
end
|
||||
|
||||
def import(options)
|
||||
execute('import_vm.ps1', options)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def execute_powershell(path, options, &block)
|
||||
|
|
|
@ -12,7 +12,6 @@ module VagrantPlugins
|
|||
attr_reader :driver
|
||||
|
||||
def initialize(machine)
|
||||
@driver = Driver.new
|
||||
@machine = machine
|
||||
|
||||
if !Vagrant::Util::Platform.windows?
|
||||
|
@ -26,6 +25,16 @@ module VagrantPlugins
|
|||
if !Vagrant::Util::PowerShell.available?
|
||||
raise Errors::PowerShellRequired
|
||||
end
|
||||
|
||||
# This method will load in our driver, so we call it now to
|
||||
# initialize it.
|
||||
machine_id_changed
|
||||
end
|
||||
|
||||
# If the machine ID changed, then we need to rebuild our underlying
|
||||
# driver.
|
||||
def machine_id_changed
|
||||
@driver = Driver.new(@machine)
|
||||
end
|
||||
|
||||
def action(name)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
param (
|
||||
[string]$vm_id = $(throw "-vm_id is required.")
|
||||
[string]$VmId = $(throw "-VmId is required.")
|
||||
)
|
||||
|
||||
# Include the following modules
|
||||
|
@ -9,7 +9,7 @@ $modules += $presentDir + "\utils\write_messages.ps1"
|
|||
forEach ($module in $modules) { . $module }
|
||||
|
||||
try {
|
||||
$vm = Get-VM -Id $vm_id -ErrorAction "stop"
|
||||
$vm = Get-VM -Id $VmId -ErrorAction "stop"
|
||||
Start-VM $vm
|
||||
$state = $vm.state
|
||||
$status = $vm.status
|
||||
|
|
Loading…
Reference in New Issue