Class: Chef::Knife::Cloud::VcenterService

Inherits:
Service
  • Object
show all
Includes:
VcenterServiceHelpers
Defined in:
lib/chef/knife/cloud/vcenter_service.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VcenterServiceHelpers

#check_for_missing_config_values!, #create_service_instance, #validate!, #verify_ssl?

Constructor Details

#initialize(options = {}) ⇒ VcenterService

Returns a new instance of VcenterService.



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
# 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

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



41
42
43
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41

def api_client
  @api_client
end

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



42
43
44
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 42

def connection_options
  @connection_options
end

#ipaddressObject (readonly)

Returns the value of attribute ipaddress.



42
43
44
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 42

def ipaddress
  @ipaddress
end

#session_apiObject (readonly)

Returns the value of attribute session_api.



41
42
43
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41

def session_api
  @session_api
end

#session_idObject (readonly)

Returns the value of attribute session_id.



41
42
43
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 41

def session_id
  @session_id
end

Instance Method Details

#create_server(options = {}) ⇒ Object

Creates the server

Parameters:

  • options (Object) (defaults to: {})

    to override anything you need to do



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ Boolean

Checks to see if the datacenter exists in the vCenter

Parameters:

  • name (String)

    is the name of the datacenter

Returns:

  • (Boolean)


166
167
168
169
170
171
# 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) ⇒ Object

Deletes the VM

Parameters:

  • name (String)

    is the server to delete



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# 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) ⇒ Object

Gets the datastore

Parameters:

  • name (String)

    is the datastore of the datacenter



206
207
208
209
210
211
212
213
# 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) ⇒ Object

Gets the folder

Parameters:

  • name (String)

    is the folder of the datacenter



176
177
178
179
180
181
182
183
# 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) ⇒ Object

Gets the host

Parameters:

  • name (String)

    is the host of the datacenter



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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) ⇒ Object

Gets the resource_pool

Parameters:

  • name (String)

    is the resource_pool of the datacenter



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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 behaviour 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 behaviour)
    # (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) ⇒ Object

Gets the server

Parameters:

  • name (String)

    is the server of the datacenter



242
243
244
245
# 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_clustersObject

Return a list of the clusters in the vCenter



159
160
161
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 159

def list_clusters
  VSphereAutomation::VCenter::ClusterApi.new(api_client).list.value
end

#list_datacentersObject

Return a list of the datacenters in the vCenter



153
154
155
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 153

def list_datacenters
  VSphereAutomation::VCenter::DatacenterApi.new(api_client).list.value
end

#list_hostsObject

Return a list of the hosts in the vCenter



147
148
149
# File 'lib/chef/knife/cloud/vcenter_service.rb', line 147

def list_hosts
  VSphereAutomation::VCenter::HostApi.new(api_client).list.value
end

#list_serversObject

Get a list of vms from the API



138
139
140
141
142
143
# 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) ⇒ Object

Gets some server information

Parameters:

  • server (Object)

    is the server object



272
273
274
275
276
# 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