Class: Vmesh::VSphere

Inherits:
Object
  • Object
show all
Defined in:
lib/vmesh/vsphere.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_options) ⇒ VSphere

Returns a new instance of VSphere.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vmesh/vsphere.rb', line 8

def initialize(connection_options)
  Vmesh::logger.debug "Opening connection to #{connection_options[:host]} as #{connection_options[:user]}, dc #{connection_options[:datacenter]}, resource pool #{connection_options[:resource_pool]}, insecure #{connection_options[:insecure]}"
  opts = {
    :host => connection_options[:host],
    :user => connection_options[:user],
    :password => connection_options[:password],
    :datacenter => connection_options[:datacenter],
    :host => connection_options[:host],
    :resource_pool => connection_options[:resource_pool],
    :insecure => connection_options[:insecure],
  }
  @vim = RbVmomi::VIM.connect opts
  @options = opts
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/vmesh/vsphere.rb', line 7

def options
  @options
end

#vimObject

Returns the value of attribute vim.



7
8
9
# File 'lib/vmesh/vsphere.rb', line 7

def vim
  @vim
end

Instance Method Details

#clone_machine(vm_type, vm_target, default_vm_folder, machine_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vmesh/vsphere.rb', line 29

def clone_machine(vm_type, vm_target, default_vm_folder, machine_options)
  Vmesh::template.has_key? vm_type.to_sym or raise "unknown machine type #{vm_type}, known types are #{Vmesh::template.keys.to_s}"
  config = {}
  template = Vmesh::template[vm_type.to_sym]
  vm_dest = VSphere.parse_vm_target vm_target, default_vm_folder
  vm_template = get_machine(template[:name], @options[:datacenter])
  # Refactor & pass in options with the get or alternatively add a get and set method
  spec = get_custom_spec(template[:spec])
  spec.destination_ip_address = machine_options[:ip_address] if machine_options[:ip_address]
  config[:numCPUs] = machine_options[:numCPUs] if machine_options[:numCPUs].to_s != ''
  config[:memoryMB] = machine_options[:memoryMB] if machine_options[:memoryMB].to_s != ''
  pool = get_resource_pool(@options[:resource_pool], @options[:datacenter])
  datacenter = get_datacenter(@options[:datacenter])
  datastore = get_datastore(machine_options[:datastore], datacenter)
  raise "No datastore found matching #{machine_options[:datastore]}. Exiting." if datastore.nil?
  Vmesh::logger.debug "Got datastore named #{datastore.name} with free space #{datastore.free_space}."
  Vmesh::logger.debug "Creating vm in folder #{vm_dest[:folder]} with name #{vm_dest[:name]}."
  folder = get_folder(vm_dest[:folder], @options[:datacenter])
  Vmesh::logger.debug "Using folder #{folder.to_s}."
  vm_template.clone_to(vm_dest[:name], folder, datastore, spec, pool, config)
end

#get_custom_spec(name) ⇒ Object



51
52
53
54
# File 'lib/vmesh/vsphere.rb', line 51

def get_custom_spec(name)
  spec_mgr = @vim.serviceContent.customizationSpecManager
  CustomSpec::get(spec_mgr, name)
end

#get_datacenter(name) ⇒ Object



67
68
69
# File 'lib/vmesh/vsphere.rb', line 67

def get_datacenter(name)
  Vmesh::Datacenter::get(@vim, name)
end

#get_datastore(name, datacenter) ⇒ Object



62
63
64
65
# File 'lib/vmesh/vsphere.rb', line 62

def get_datastore(name, datacenter)
  Vmesh::logger.debug "Getting datastore #{name}"
  Datastore::get(@vim, name, datacenter)
end

#get_folder(path, datacenter_name = ) ⇒ Object



79
80
81
82
# File 'lib/vmesh/vsphere.rb', line 79

def get_folder(path, datacenter_name = @options[:datacenter])
  Vmesh::logger.debug "Looking for folder #{path}."
  path.to_s == '/' ? vm_root_folder : vm_root_folder.traverse(path)
end

#get_machine(machine_name, datacenter) ⇒ Object

Can we search through the datacenters for a machine? Should we get the machine from the datacenters?



25
26
27
# File 'lib/vmesh/vsphere.rb', line 25

def get_machine(machine_name, datacenter)
  Machine::get(self, datacenter, machine_name)
end

#get_resource_pool(name, datacenter_name) ⇒ Object



56
57
58
59
60
# File 'lib/vmesh/vsphere.rb', line 56

def get_resource_pool(name, datacenter_name)
  # KRUT this should be a ResourcePool wrapper object instead?
  Vmesh::logger.debug "Chasing resource pool #{name}"
  Datacenter::find_pool(@vim, name, datacenter_name)
end

#root_folderObject



71
72
73
# File 'lib/vmesh/vsphere.rb', line 71

def root_folder
  @vim.serviceInstance.content.rootFolder
end

#vm_root_folderObject



75
76
77
# File 'lib/vmesh/vsphere.rb', line 75

def vm_root_folder
  @vim.serviceInstance.content.rootFolder.traverse(@options[:datacenter]).vmFolder
end