class Chef::Knife::VsphereVmCdrom

VsphereVmCdrom extends the BaseVspherecommand

Public Instance Methods

run() click to toggle source

The main run method for vm_cdrom

# File lib/chef/knife/vsphere_vm_cdrom.rb, line 55
def run
  $stdout.sync = true

  vmname = @name_args[0]
  if vmname.nil?
    show_usage
    fatal_exit("You must specify a virtual machine name")
  end

  unless get_config(:attach) ^ get_config(:disconnect)
    fatal_exit("You must specify one of --attach or --disconnect")
  end

  fatal_exit "You must specify the name and path of an ISO with --iso" if get_config(:attach) && !get_config(:iso)
  fatal_exit "You must specify the datastore containing the ISO with --datastore" if get_config(:attach) && !get_config(:datastore)

  vm = get_vm_by_name(vmname, get_config(:folder)) || fatal_exit("Could not find #{vmname}")

  cdrom_obj = vm.config.hardware.device.find { |hw| hw.class == ::RbVmomi::VIM::VirtualCdrom }
  fatal_exit "Could not find a cd drive" unless cdrom_obj

  backing = if get_config(:attach)
              ::RbVmomi::VIM::VirtualCdromIsoBackingInfo(
                fileName: iso_path
              )
            else
              ::RbVmomi::VIM::VirtualCdromRemoteAtapiBackingInfo(deviceName: EMPTY_DEVICE_NAME)
            end

  vm.ReconfigVM_Task(
    spec: spec(cdrom_obj, backing)
  ).wait_for_completion
end

Private Instance Methods

iso_path() click to toggle source
# File lib/chef/knife/vsphere_vm_cdrom.rb, line 109
def iso_path
  "[#{get_config(:datastore)}] #{get_config(:iso)}"
end
spec(cd_device, backing) click to toggle source
# File lib/chef/knife/vsphere_vm_cdrom.rb, line 91
def spec(cd_device, backing)
  ::RbVmomi::VIM::VirtualMachineConfigSpec(
    deviceChange: [{
      operation: :edit,
      device: ::RbVmomi::VIM::VirtualCdrom(
        backing: backing,
        key: cd_device.key,
        controllerKey: cd_device.controllerKey,
        connectable: ::RbVmomi::VIM::VirtualDeviceConnectInfo(
          startConnected: get_config(:on_boot) || false,
          connected: get_config(:attach) || false,
          allowGuestControl: true
        )
      ),
    }]
  )
end