module Chef::Knife::VcVmCommon

Public Class Methods

included(includer) click to toggle source
# File lib/chef/knife/common/vc_vm_common.rb, line 23
def self.included(includer)
  includer.class_eval do
    option :vcloud_vdc,
           :long => "--vdc VDC_NAME",
           :description => "VDC to whom VM's vApp belongs",
           :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_vdc] = key }

    option :vcloud_vapp,
           :long => "--vapp VAPP_NAME",
           :description => "vApp to whom VM belongs",
           :proc => Proc.new { |key| Chef::Config[:knife][:vcloud_vapp] = key }
  end
end

Public Instance Methods

get_vm(vm_arg) click to toggle source
# File lib/chef/knife/common/vc_vm_common.rb, line 37
def get_vm(vm_arg)
  vm = nil
  vapp_name = locate_config_value(:vcloud_vapp)
  org_name = locate_org_option
  vdc_name = locate_config_value(:vcloud_vdc)

  unless vdc_name && vapp_name
    notice_msg("--vapp and --vdc not specified, assuming VM is an ID")
    vm = connection.get_vm vm_arg
  else
    org = connection.get_organization_by_name org_name
    vm = connection.get_vm_by_name org, vdc_name, vapp_name, vm_arg
  end
  raise ArgumentError, "VM #{vm_arg} not found" unless vm
  vm
end
sanitize_guest_name(name) click to toggle source

Accept only characters and hyphens

Underscores are converted to hyphens

# File lib/chef/knife/common/vc_vm_common.rb, line 57
def sanitize_guest_name(name)
  name.gsub(/_/, '-').gsub(/[^[0-9]|^[a-z]|^[A-Z]|^-]/, '')
end
stop_if_running(connection, vm) click to toggle source

Verify a VM and stop it if it's running

Return :nothing if nothing was made

:errored for errors
:stopped if was stopped
# File lib/chef/knife/common/vc_vm_common.rb, line 66
def stop_if_running(connection, vm)
  if vm[:status] == 'running'
    if ui.confirm("Guest customizations must be applied to a stopped VM, " \
                  "but it's running. Can I #{ui.color('STOP', :red)} it")
      ui.msg "Stopping VM..."
      task_id, response = connection.poweroff_vm vm[:id]
      return :errored unless wait_task(connection, task_id)
    end
    return :stopped
  end
  return :nothing
end