Class: VmShepherd::VsphereManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vm_shepherd/vsphere_manager.rb

Constant Summary collapse

TEMPLATE_PREFIX =
'tpl'.freeze
VALID_FOLDER_REGEX =
/\A([\w-]{1,80}\/)*[\w-]{1,80}\/?\z/
VALID_DISK_FOLDER_REGEX =
/\A[\w-]{1,80}\z/

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, datacenter_name, logger) ⇒ VsphereManager

Returns a new instance of VsphereManager.



10
11
12
13
14
15
16
# File 'lib/vm_shepherd/vsphere_manager.rb', line 10

def initialize(host, username, password, datacenter_name, logger)
  @host            = host
  @username        = username
  @password        = password
  @datacenter_name = datacenter_name
  @logger          = logger
end

Instance Method Details

#clean_environment(datacenter_folders_to_clean:, datastores:, datastore_folders_to_clean:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vm_shepherd/vsphere_manager.rb', line 30

def clean_environment(datacenter_folders_to_clean:, datastores:, datastore_folders_to_clean:)
  return if datacenter_folders_to_clean.nil? || datastores.nil? || datacenter_folders_to_clean.nil?

  datacenter_folders_to_clean.each do |folder_name|
    validate_folder_name!(folder_name)
    delete_folder_and_vms(folder_name)
  end

  datastore_folders_to_clean.each do |folder_name|
    datastores.each do |datastore|
      validate_disk_folder_name!(folder_name)
      begin
        logger.info("BEGIN datastore_folder.destroy_task folder=#{folder_name}")

        file_manager.DeleteDatastoreFile_Task(
          datacenter: datacenter,
          name:       "[#{datastore}] #{folder_name}"
        ).wait_for_completion

        logger.info("END   datastore_folder.destroy_task folder=#{folder_name}")
      rescue RbVmomi::Fault => e
        logger.info("ERROR datastore_folder.destroy_task folder=#{folder_name} #{e.inspect}")
      end
    end
  end
end

#deploy(ova_path, vm_config, vsphere_config) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vm_shepherd/vsphere_manager.rb', line 18

def deploy(ova_path, vm_config, vsphere_config)
  validate_folder_name!(vsphere_config[:folder])

  ensure_no_running_vm(vm_config)

  ovf_file_path = extract_ovf_from(ova_path)

  boot_vm(ovf_file_path, vm_config, vsphere_config)
ensure
  FileUtils.remove_entry_secure(ovf_file_path, force: true) unless ovf_file_path.nil?
end

#destroy(ip_address, resource_pool_name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/vm_shepherd/vsphere_manager.rb', line 60

def destroy(ip_address, resource_pool_name)
  vms = connection.serviceContent.searchIndex.FindAllByIp(ip: ip_address, vmSearch: true)
  vms = vms.select { |vm| resource_pool_name == vm.resourcePool.name } if resource_pool_name
  vms.each do |vm|
    power_off_vm(vm)
    destroy_vm(vm)
  end
end

#destroy_vm(vm) ⇒ Object



69
70
71
72
73
74
# File 'lib/vm_shepherd/vsphere_manager.rb', line 69

def destroy_vm(vm)
  vm_name = vm.name
  logger.info("BEGIN vm.destroy_task vm=#{vm_name}")
  vm.Destroy_Task.wait_for_completion
  logger.info("END   vm.destroy_task vm=#{vm_name}")
end

#prepare_environmentObject



57
58
# File 'lib/vm_shepherd/vsphere_manager.rb', line 57

def prepare_environment
end