Class: Kitchen::Driver::Vcenter

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/vcenter.rb

Overview

Extends the Base class for vCenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client


31
32
33
# File 'lib/kitchen/driver/vcenter.rb', line 31

def api_client
  @api_client
end

#connection_optionsObject

Returns the value of attribute connection_options


31
32
33
# File 'lib/kitchen/driver/vcenter.rb', line 31

def connection_options
  @connection_options
end

#ipaddressObject

Returns the value of attribute ipaddress


31
32
33
# File 'lib/kitchen/driver/vcenter.rb', line 31

def ipaddress
  @ipaddress
end

Instance Method Details

#create(state) ⇒ Object

The main create method

Parameters:

  • state (Object)

    is the state of the vm


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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/kitchen/driver/vcenter.rb', line 84

def create(state)
  save_and_validate_parameters
  connect

  # Using the clone class, create a machine for TK
  # Find the identifier for the targethost to pass to rbvmomi
  config[:targethost] = get_host(config[:targethost])

  # Use the root resource pool of a specified cluster, if any
  if config[:cluster].nil?
    # Find the first resource pool on any cluster
    config[:resource_pool] = get_resource_pool(config[:resource_pool])
  else
    cluster = get_cluster(config[:cluster])
    root_pool = cluster.resource_pool

    if config[:resource_pool].nil?
      config[:resource_pool] = root_pool
    else
      rp_api = VSphereAutomation::VCenter::ResourcePoolApi.new(api_client)

      found_pool = nil
      pools = rp_api.get(root_pool).value.resource_pools
      pools.each do |pool|
        name = rp_api.get(pool).value.name
        found_pool = pool if name == config[:resource_pool]
      end

      raise format("Pool %s not found on cluster %s", config[:resource_pool], config[:cluster]) if found_pool.nil?
      config[:resource_pool] = found_pool
    end
  end

  # Check that the datacenter exists
  datacenter_exists?(config[:datacenter])

  # Check if network exists, if to be changed
  network_exists?(config[:network_name]) unless config[:network_name].nil?

  # Same thing needs to happen with the folder name if it has been set
  unless config[:folder].nil?
    config[:folder] = {
      name: config[:folder],
      id: get_folder(config[:folder]),
    }
  end

  # Allow different clone types
  config[:clone_type] = :linked if config[:clone_type] == "linked"
  config[:clone_type] = :instant if config[:clone_type] == "instant"

  # Create a hash of options that the clone requires
  options = {
    name: config[:vm_name],
    targethost: config[:targethost],
    poweron: config[:poweron],
    template: config[:template],
    datacenter: config[:datacenter],
    folder: config[:folder],
    resource_pool: config[:resource_pool],
    clone_type: config[:clone_type].to_sym,
    network_name: config[:network_name],
    interface: config[:interface],
    wait_timeout: config[:vm_wait_timeout],
    wait_interval: config[:vm_wait_interval],
    customize: config[:customize],
    active_discovery: config[:active_discovery],
    active_discovery_command: config[:active_discovery_command],
    vm_os: config[:vm_os],
    vm_username: config[:vm_username],
    vm_password: config[:vm_password],
    vm_win_network: config[:vm_win_network],
    benchmark: config[:benchmark],
    benchmark_file: config[:benchmark_file],
  }

  begin
    # Create an object from which the clone operation can be called
    new_vm = Support::CloneVm.new(connection_options, options)
    new_vm.clone

    state[:hostname] = new_vm.ip
    state[:vm_name] = new_vm.name

  rescue # Kitchen::ActionFailed => e
    if config[:vm_rollback] == true
      error format("Rolling back VM %s after critical error", config[:vm_name])

      # Inject name of failed VM for destroy to work
      state[:vm_name] = config[:vm_name]

      destroy(state)
    end

    raise
  end

  unless config[:tags].nil? || config[:tags].empty?
    tag_api = VSphereAutomation::CIS::TaggingTagApi.new(api_client)
    vm_tags = tag_api.list.value
    raise format("No configured tags found on VCenter, but %s specified", config[:tags].to_s) if vm_tags.empty?

    valid_tags = {}
    vm_tags.each do |uid|
      tag = tag_api.get(uid)

      valid_tags[tag.value.name] = tag.value.id if tag.is_a? VSphereAutomation::CIS::CisTaggingTagResult
    end

    # Error out on undefined tags
    invalid = config[:tags] - valid_tags.keys
    raise format("Specified tag(s) %s not valid", invalid.join(",")) unless invalid.empty?
    tag_service = VSphereAutomation::CIS::TaggingTagAssociationApi.new(api_client)
    tag_ids = config[:tags].map { |name| valid_tags[name] }

    request_body = {
      object_id: {
        id: get_vm(config[:vm_name]).vm,
        type: "VirtualMachine",
      },
      tag_ids: tag_ids,
    }
    tag_service.attach_multiple_tags_to_object(request_body)
  end
end

#destroy(state) ⇒ Object

The main destroy method

Parameters:

  • state (Object)

    is the state of the vm


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/kitchen/driver/vcenter.rb', line 213

def destroy(state)
  return if state[:vm_name].nil?

  # Reset resource pool, as it's not needed for the destroy action but might be a remnant of earlier calls to "connect"
  # Temporary fix until setting cluster + resource_pool at the same time is implemented (lines #64/#187)
  config[:resource_pool] = nil

  save_and_validate_parameters
  connect

  vm = get_vm(state[:vm_name])
  unless vm.nil?
    vm_api = VSphereAutomation::VCenter::VMApi.new(api_client)

    # shut the machine down if it is running
    if vm.power_state == "POWERED_ON"
      power = VSphereAutomation::VCenter::VmPowerApi.new(api_client)
      power.stop(vm.vm)
    end

    # delete the vm
    vm_api.delete(vm.vm)
  end
end