lib/vagrant-vcenter/action/read_state.rb in vagrant-vcenter-0.3.2 vs lib/vagrant-vcenter/action/read_state.rb in vagrant-vcenter-0.3.3
- old
+ new
@@ -1,7 +1,5 @@
-require 'log4r'
-
module VagrantPlugins
module VCenter
module Action
# This Class read the power state of the VM that Vagrant is managing.
class ReadState
@@ -19,24 +17,23 @@
if env[:machine].id.nil?
@logger.info('VM is not created yet')
return :not_created
end
- # FIXME: this part needs some cleanup
- config = env[:machine].provider_config
+ cfg = env[:machine].provider_config
- # FIXME: Raise a correct exception
- dc = config.vcenter_cnx.serviceInstance.find_datacenter(
- config.datacenter_name) or abort 'datacenter not found'
+ vm = cfg.vmfolder.findByUuid(env[:machine].id) or
+ fail Errors::VMNotFound,
+ :vm_name => env[:machine].name
- root_vm_folder = dc.vmFolder
-
- vm = root_vm_folder.findByUuid(env[:machine].id)
-
# @logger.debug("Current power state: #{vm.runtime.powerState}")
vm_name = env[:machine].name
- if vm.runtime.powerState == 'poweredOff'
+ if vm.nil?
+ # If the VM has been cloned/moved, the findByUuid call returns nil
+ @logger.info("VM [#{vm_name}] state is unknown")
+ return :unknown
+ elsif vm.runtime.powerState == 'poweredOff'
@logger.info("VM [#{vm_name}] is stopped")
return :stopped
elsif vm.runtime.powerState == 'poweredOn'
@logger.info("VM [#{vm_name}] is running")
return :running