class Chef::Knife::Cloud::VcenterService

Extends the Service method, this is the bulk of the integration

Attributes

api_client[R]
connection_options[R]
ipaddress[R]
session_api[R]
session_id[R]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/chef/knife/cloud/vcenter_service.rb, line 44
def initialize(options = {})
  super(options)

  configuration = VSphereAutomation::Configuration.new.tap do |c|
    c.host = options[:host]
    c.username = options[:username]
    c.password = options[:password]
    c.scheme = "https"
    c.verify_ssl = options[:verify_ssl]
    c.verify_ssl_host = options[:verify_ssl]
  end

  Base.log.warn("SSL Verification is turned OFF") if options[:logs] && !options[:verify_ssl]

  @api_client = VSphereAutomation::ApiClient.new(configuration)
  api_client.default_headers["Authorization"] = configuration.basic_auth_token

  session_api = VSphereAutomation::CIS::SessionApi.new(api_client)
  session_id = session_api.create("").value

  api_client.default_headers["vmware-api-session-id"] = session_id

  # Set the class properties for the rbvmomi connections
  @connection_options = {
    user: options[:username],
    password: options[:password],
    insecure: options[:verify_ssl] ? false : true,
    host: options[:host],
  }
end

Public Instance Methods

create_server(options = {}) click to toggle source

Creates the server

@param [Object] options to override anything you need to do

# File lib/chef/knife/cloud/vcenter_service.rb, line 78
def create_server(options = {})
  # Create the vm object
  vm_api = VSphereAutomation::VCenter::VMApi.new(api_client)

  # Use the option to determine now a new machine is being created
  case options[:type]
  when "clone"

    datacenter_exists?(options[:datacenter])

    # Some of the options need to be the ID of the component in VMware
    # Update these using the REST API so that they can be passed to the support library
    options[:targethost] = get_host(options[:targethost]).host

    options[:resource_pool] = get_resource_pool(options[:resource_pool])

    # Configure the folder option as a has with the name an the id
    unless options[:folder].nil?
      options[:folder] = {
        name: options[:folder],
        id: get_folder(options[:folder]),
      }
    end

    # Clone the machine using the support library
    clone_obj = ::Support::CloneVm.new(connection_options, options)
    @ipaddress = clone_obj.clone

    # return an object from the restapi
    get_server(options[:name])

  when "create"

    # Create the placement object
    placement_spec = VSphereAutomation::VCenter::VcenterVMPlacementSpec.new( ###
      folder: get_folder(options[:folder]),
      host: get_host(options[:targethost]).host,
      datastore: get_datastore(options[:datastore]),
      resource_pool: get_resource_pool(options[:resource_pool])
    )

    # Create the CreateSpec object
    create_spec = VSphereAutomation::VCenter::VcenterVMCreateSpec.new(
      name: options[:name],
      guest_OS: VSphereAutomation::VCenter::VcenterVmGuestOS::OTHER,
      placement: placement_spec
    )

    # Create the new machine
    begin
      create_model = VSphereAutomation::VCenter::VcenterVMCreate.new(spec: create_spec)
      vm_api.create(create_model).value
    rescue StandardError => e
      puts e.message
    end
  end
end
datacenter_exists?(name) click to toggle source

Checks to see if the datacenter exists in the vCenter

@param [String] name is the name of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 166
def datacenter_exists?(name)
  dc_api = VSphereAutomation::VCenter::DatacenterApi.new(api_client)
  dcs = dc_api.list({ filter_names: name }).value

  raise format("Unable to find data center: %s", name) if dcs.empty?
end
delete_vm(name) click to toggle source

Deletes the VM

@param [String] name is the server to delete

# File lib/chef/knife/cloud/vcenter_service.rb, line 250
def delete_vm(name)
  vm = get_server(name)
  server_summary(vm)
  ui.msg("")

  ui.confirm("Do you really want to be delete this virtual machine")

  vm_api = VSphereAutomation::VCenter::VMApi.new(api_client)

  # check the power state of the machine, if it is powered on turn it off
  if vm.power_state == "POWERED_ON"
    power_api = VSphereAutomation::VCenter::VmPowerApi.new(api_client)
    ui.msg("Shutting down machine")
    power_api.stop(vm.vm)
  end

  vm_api.delete(vm.vm)
end
get_datastore(name) click to toggle source

Gets the datastore

@param [String] name is the datastore of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 206
def get_datastore(name)
  ds_api = VSphereAutomation::VCenter::DatastoreApi.new(api_client)
  ds = ds_api.list({ filter_names: name }).value

  raise format("Unable to find data store: %s", name) if ds.empty?

  ds.first.datastore
end
get_folder(name) click to toggle source

Gets the folder

@param [String] name is the folder of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 176
def get_folder(name)
  folder_api = VSphereAutomation::VCenter::FolderApi.new(api_client)
  folders = folder_api.list({ filter_names: name }).value

  raise format("Unable to find folder: %s", name) if folders.empty?

  folders.first.folder
end
get_host(name) click to toggle source

Gets the host

@param [String] name is the host of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 188
def get_host(name)
  # create a host object to work with
  host_api = VSphereAutomation::VCenter::HostApi.new(api_client)

  if name.nil?
    hosts = host_api.list.value
  else
    hosts = host_api.list({ filter_names: name }).value
  end

  raise format("Unable to find target host: %s", name) if hosts.empty?

  hosts.first
end
get_resource_pool(name) click to toggle source

Gets the resource_pool

@param [String] name is the resource_pool of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 218
def get_resource_pool(name)
  rp_api = VSphereAutomation::VCenter::ResourcePoolApi.new(api_client)

  if name.nil?
    # Remove default pool for first pass (<= 1.2.1 behavior to pick first user-defined pool found)
    resource_pools = rp_api.list.value.delete_if { |pool| pool.name == "Resources" }
    puts "Search of all resource pools found: " + resource_pools.map(&:name).to_s

    # Revert to default pool, if no user-defined pool found (> 1.2.1 behavior)
    # (This one might not be found under some circumstances by the statement above)
    return get_resource_pool("Resources") if resource_pools.empty?
  else
    resource_pools = rp_api.list({ filter_names: name }).value
    puts "Search for resource pools found: " + resource_pools.map(&:name).to_s
  end

  raise format("Unable to find Resource Pool: %s", name) if resource_pools.empty?

  resource_pools.first.resource_pool
end
get_server(name) click to toggle source

Gets the server

@param [String] name is the server of the datacenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 242
def get_server(name)
  vm_api = VSphereAutomation::VCenter::VMApi.new(api_client)
  vm_api.list({ filter_names: name }).value.first
end
list_clusters() click to toggle source

Return a list of the clusters in the vCenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 159
def list_clusters
  VSphereAutomation::VCenter::ClusterApi.new(api_client).list.value
end
list_datacenters() click to toggle source

Return a list of the datacenters in the vCenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 153
def list_datacenters
  VSphereAutomation::VCenter::DatacenterApi.new(api_client).list.value
end
list_hosts() click to toggle source

Return a list of the hosts in the vCenter

# File lib/chef/knife/cloud/vcenter_service.rb, line 147
def list_hosts
  VSphereAutomation::VCenter::HostApi.new(api_client).list.value
end
list_servers() click to toggle source

Get a list of vms from the API

# File lib/chef/knife/cloud/vcenter_service.rb, line 138
def list_servers
  vms = VSphereAutomation::VCenter::VMApi.new(api_client).list.value

  # list_resource_command uses .send(:name) syntax, so convert to OpenStruct to keep it compatible
  vms.map { |vmsummary| OpenStruct.new(vmsummary.to_hash) }
end
server_summary(server, _coloumns_with_inf = nil) click to toggle source

Gets some server information

@param [Object] server is the server object

# File lib/chef/knife/cloud/vcenter_service.rb, line 272
def server_summary(server, _coloumns_with_inf = nil)
  msg_pair("ID", server.vm)
  msg_pair("Name", server.name)
  msg_pair("Power State", server.power_state)
end

Private Instance Methods

cleanup() click to toggle source

def bootstrap_common_params(bootstrap)

bootstrap.config[:run_list] = config[:run_list]
bootstrap.config[:environment] = get_config(:environment)
bootstrap.config[:first_boot_attributes] = get_config(:first_boot_attributes)
bootstrap.config[:chef_node_name] = get_config(:chef_node_name)
bootstrap.config[:node_ssl_verify_mode] = get_config(:node_ssl_verify_mode)
bootstrap

end

def bootstrap_for_node

Chef::Knife::Bootstrap.load_deps
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [config[:fqdn]]
bootstrap.config[:ssh_user] = get_config(:ssh_user)
bootstrap.config[:ssh_password] = get_config(:ssh_password)
bootstrap.config[:ssh_port] = get_config(:ssh_port)
bootstrap.config[:identity_file] = get_config(:identity_file)
bootstrap.config[:use_sudo] = true unless get_config(:ssh_user) == 'root'
bootstrap.config[:use_sudo_password] = true unless get_config(:ssh_user) == 'root'
bootstrap.config[:log_level] = get_config(:log_level)
bootstrap_common_params(bootstrap)

end

# File lib/chef/knife/cloud/vcenter_service.rb, line 303
def cleanup
  session_svc.delete unless session_id.nil?
end