Class: Chef::Knife::VsphereVmVmdkAdd

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

Overview

Lists all known virtual machines in the configured datacenter

Instance Method Summary collapse

Methods inherited from BaseVsphereCommand

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

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/vsphere_vm_vmdk_add.rb', line 20

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

  size = @name_args[1]
  if size.nil?
    ui.fatal "You need a VMDK size!"
    show_usage
    exit 1
  end

  vim = get_vim_connection
  vdm = vim.serviceContent.virtualDiskManager
  vm = get_vm(vmname)

  vmdk_datastore = vm.datastore[0]
  vmdk_fileName = "#{vmname}/#{vmname}_1.vmdk"
  vmdk_name = "[#{vmdk_datastore.name}] #{vmdk_fileName}"
  vmdk_size_kb = size.to_i * 1024 * 1024
  vmdk_type = get_config(:vmdk_type)
  vmdk_type = "preallocated" if vmdk_type == "thick"
  
  vmdk_spec = RbVmomi::VIM::FileBackedVirtualDiskSpec(
    :adapterType => "lsiLogic",
    :capacityKb => vmdk_size_kb,
    :diskType => vmdk_type
  )

  ui.info "Creating VMDK"
  ui.info "#{ui.color "Capacity:", :cyan} #{size} GB"
  ui.info "#{ui.color "Disk:", :cyan} #{vmdk_name}"

  if get_config(:noop)
    ui.info "#{ui.color "Skipping disk creation process because --noop specified.", :red}"
  else
    vdm.CreateVirtualDisk_Task(
      :datacenter => get_datacenter,
      :name => vmdk_name,
      :spec => vmdk_spec
    ).wait_for_completion
  end

  ui.info "Attaching VMDK to #{vmname}"

  controller = find_device(vm,"SCSI controller 0")

  vmdk_backing = RbVmomi::VIM::VirtualDiskFlatVer2BackingInfo(
    :datastore => vmdk_datastore,
    :diskMode => "persistent",
    :fileName => vmdk_name,
  )

  device = RbVmomi::VIM::VirtualDisk(
    :backing => vmdk_backing,
    :capacityInKB => vmdk_size_kb,
    :controllerKey => controller.key,
    :key => -1,
    :unitNumber => controller.device.size + 1
  )

  device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
    :device => device,
    :operation => RbVmomi::VIM::VirtualDeviceConfigSpecOperation("add")
  )

  vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
    :deviceChange => [device_config_spec]
  )

  if get_config(:noop)
    ui.info "#{ui.color "Skipping disk attaching process because --noop specified.", :red}"
  else
    vm.ReconfigVM_Task(:spec => vm_config_spec).wait_for_completion
  end
end