Class: Chef::Knife::VsphereVmState

Inherits:
BaseVsphereCommand show all
Defined in:
lib/chef/knife/vsphere_vm_state.rb

Overview

Manage power state of a virtual machine

Instance Method Summary collapse

Methods inherited from BaseVsphereCommand

#fatal_exit, #find_all_in_folder, #find_datastore, #find_folder, #find_in_folder, #find_network, #find_pool, get_common_options, #get_config, #get_vim_connection

Instance Method Details

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/knife/vsphere_vm_state.rb', line 33

def run

  $stdout.sync = true

  vmname = @name_args[0]
  if vmname.nil?
    show_usage
    ui.fatal("You must specify a virtual machine name")
    exit 1
  end
 
  vim = get_vim_connection

  baseFolder = find_folder(get_config(:folder));

  vm = find_in_folder(baseFolder, RbVmomi::VIM::VirtualMachine, vmname) or
    abort "VM #{vmname} not found"

  state = vm.runtime.powerState

  if config[:state].nil?
    puts "VM #{vmname} is currently " + PowerStates[vm.runtime.powerState]
  else

    case config[:state]
    when 'on'
      if state == PsOn
        puts "Virtual machine #{vmname} was already powered on"
      else
        vm.PowerOnVM_Task.wait_for_completion
        puts "Powered on virtual machine #{vmname}"
      end
    when 'off'
      if state == PsOff
        puts "Virtual machine #{vmname} was already powered off"
      else
        vm.PowerOffVM_Task.wait_for_completion
        puts "Powered off virtual machine #{vmname}"
      end
    when 'suspend'
      if state == PowerStates['suspended']
        puts "Virtual machine #{vmname} was already suspended"
      else
        vm.SuspendVM_Task.wait_for_completion
        puts "Suspended virtual machine #{vmname}"
      end
    when 'reset'
      vm.ResetVM_Task.wait_for_completion
      puts "Reset virtual machine #{vmname}"
    end
  end
end