diff --git a/plugins/providers/hyperv/action/delete_vm.rb b/plugins/providers/hyperv/action/delete_vm.rb index 6f8608b98..2077a7a41 100644 --- a/plugins/providers/hyperv/action/delete_vm.rb +++ b/plugins/providers/hyperv/action/delete_vm.rb @@ -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 diff --git a/plugins/providers/hyperv/action/import.rb b/plugins/providers/hyperv/action/import.rb index b2635d6d2..db5100762 100644 --- a/plugins/providers/hyperv/action/import.rb +++ b/plugins/providers/hyperv/action/import.rb @@ -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) diff --git a/plugins/providers/hyperv/action/read_guest_ip.rb b/plugins/providers/hyperv/action/read_guest_ip.rb index 92bfbdc8d..3f311ddb7 100644 --- a/plugins/providers/hyperv/action/read_guest_ip.rb +++ b/plugins/providers/hyperv/action/read_guest_ip.rb @@ -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? diff --git a/plugins/providers/hyperv/action/read_state.rb b/plugins/providers/hyperv/action/read_state.rb index c56bd44c4..154e88a82 100644 --- a/plugins/providers/hyperv/action/read_state.rb +++ b/plugins/providers/hyperv/action/read_state.rb @@ -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 diff --git a/plugins/providers/hyperv/action/resume_vm.rb b/plugins/providers/hyperv/action/resume_vm.rb index bd3c09dff..b872ece4c 100644 --- a/plugins/providers/hyperv/action/resume_vm.rb +++ b/plugins/providers/hyperv/action/resume_vm.rb @@ -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 diff --git a/plugins/providers/hyperv/action/start_instance.rb b/plugins/providers/hyperv/action/start_instance.rb index 17c5b9d57..10b6bceb3 100644 --- a/plugins/providers/hyperv/action/start_instance.rb +++ b/plugins/providers/hyperv/action/start_instance.rb @@ -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 diff --git a/plugins/providers/hyperv/action/stop_instance.rb b/plugins/providers/hyperv/action/stop_instance.rb index 132ea8624..59aeef947 100644 --- a/plugins/providers/hyperv/action/stop_instance.rb +++ b/plugins/providers/hyperv/action/stop_instance.rb @@ -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 diff --git a/plugins/providers/hyperv/action/suspend_vm.rb b/plugins/providers/hyperv/action/suspend_vm.rb index f3c8a1c34..d161a951a 100644 --- a/plugins/providers/hyperv/action/suspend_vm.rb +++ b/plugins/providers/hyperv/action/suspend_vm.rb @@ -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 diff --git a/plugins/providers/hyperv/action/wait_for_ip_address.rb b/plugins/providers/hyperv/action/wait_for_ip_address.rb index ad7e80e98..2ea8f6be7 100644 --- a/plugins/providers/hyperv/action/wait_for_ip_address.rb +++ b/plugins/providers/hyperv/action/wait_for_ip_address.rb @@ -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 diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index 0ff8bad4e..277a53266 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -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) diff --git a/plugins/providers/hyperv/provider.rb b/plugins/providers/hyperv/provider.rb index d9a615f87..54a459666 100644 --- a/plugins/providers/hyperv/provider.rb +++ b/plugins/providers/hyperv/provider.rb @@ -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) diff --git a/plugins/providers/hyperv/scripts/start_vm.ps1 b/plugins/providers/hyperv/scripts/start_vm.ps1 index 65971fc84..937ce75ed 100644 --- a/plugins/providers/hyperv/scripts/start_vm.ps1 +++ b/plugins/providers/hyperv/scripts/start_vm.ps1 @@ -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