Class: Foreman::Model::Vmware

Inherits:
ComputeResource show all
Defined in:
lib/foreman/model/vmware.rb

Constant Summary collapse

NETWORK_INTERFACE_TYPES =
%w(VirtualE1000)

Constants inherited from ComputeResource

ComputeResource::PROVIDERS, ComputeResource::STI_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComputeResource

#as_json, #available_images, #destroy_vm, #find_vm_by_uuid, #hardware_profile, #hardware_profiles, new_provider, #ping, #provider, #provider=, #provider_friendly_name, #save_vm, #start_vm, #stop_vm, #supports_update?, #to_label, #to_param, #update_required?

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Methods included from Taxonomix

included

Class Method Details

.model_nameObject


11
12
13
# File 'lib/foreman/model/vmware.rb', line 11

def self.model_name
  ComputeResource.model_name
end

Instance Method Details

#capabilitiesObject


15
16
17
# File 'lib/foreman/model/vmware.rb', line 15

def capabilities
  [:build]
end

#clustersObject


47
48
49
# File 'lib/foreman/model/vmware.rb', line 47

def clusters
  dc.clusters
end

#console(uuid) ⇒ Object


115
116
117
118
119
120
121
122
123
# File 'lib/foreman/model/vmware.rb', line 115

def console uuid
  vm = find_vm_by_uuid(uuid)
  raise "VM is not running!" unless vm.ready?
  #TOOD port, password
  #NOTE this requires the following port to be open on your ESXi FW
  values = { :port => unused_vnc_port(vm.hypervisor), :password => random_password, :enabled => true }
  vm.config_vnc(values)
  WsProxy.start(:host => vm.hypervisor, :host_port => values[:port], :password => values[:password]).merge(:type => 'vnc')
end

#create_vm(args = { }) ⇒ Object


89
90
91
92
93
94
95
96
97
# File 'lib/foreman/model/vmware.rb', line 89

def create_vm args = { }
  vm = new_vm(args)

  vm.save
rescue Fog::Errors::Error => e
  logger.debug e.backtrace
  errors.add(:base, e.to_s)
  false
end

#datacenterObject


107
108
109
# File 'lib/foreman/model/vmware.rb', line 107

def datacenter
  uuid
end

#datacenter=(value) ⇒ Object


111
112
113
# File 'lib/foreman/model/vmware.rb', line 111

def datacenter= value
  self.uuid = value
end

#datacentersObject


43
44
45
# File 'lib/foreman/model/vmware.rb', line 43

def datacenters
  client.datacenters.all
end

#datastoresObject


59
60
61
# File 'lib/foreman/model/vmware.rb', line 59

def datastores
  dc.datastores.all(:accessible => true)
end

#foldersObject


51
52
53
# File 'lib/foreman/model/vmware.rb', line 51

def folders
  dc.vm_folders.sort_by{|f| f.path}
end

#max_cpu_count(cluster = nil) ⇒ Object


34
35
36
37
# File 'lib/foreman/model/vmware.rb', line 34

def max_cpu_count (cluster = nil)
  return 8 unless cluster
  cluster.num_cpu_cores
end

#max_memoryObject


39
40
41
# File 'lib/foreman/model/vmware.rb', line 39

def max_memory
  16*1024*1024*1024
end

#networksObject


55
56
57
# File 'lib/foreman/model/vmware.rb', line 55

def networks
  dc.networks.all(:accessible => true)
end

#new_interface(attr = { }) ⇒ Object


125
126
127
# File 'lib/foreman/model/vmware.rb', line 125

def new_interface attr = { }
  client.interfaces.new attr
end

#new_vm(attr = { }) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/foreman/model/vmware.rb', line 73

def new_vm attr={ }
  test_connection
  return unless errors.empty?
  opts = vm_instance_defaults.merge(attr.to_hash).symbolize_keys

  # convert rails nested_attributes into a plain hash
  [:interfaces, :volumes].each do |collection|
    nested_attrs = opts.delete("#{collection}_attributes".to_sym)
    opts[collection] = nested_attributes_for(collection, nested_attrs) if nested_attrs
  end

  opts.reject! { |k, v| v.nil? }

  client.servers.new opts
end

#new_volume(attr = { }) ⇒ Object


129
130
131
# File 'lib/foreman/model/vmware.rb', line 129

def new_volume attr = { }
  client.volumes.new attr.merge(:size_gb => 10)
end

#provided_attributesObject


30
31
32
# File 'lib/foreman/model/vmware.rb', line 30

def provided_attributes
  super.merge({ :mac => :mac })
end

#pubkey_hashObject


133
134
135
# File 'lib/foreman/model/vmware.rb', line 133

def pubkey_hash
  attrs[:pubkey_hash]
end

#pubkey_hash=(key) ⇒ Object


137
138
139
# File 'lib/foreman/model/vmware.rb', line 137

def pubkey_hash= key
  attrs[:pubkey_hash] = key
end

#serverObject


99
100
101
# File 'lib/foreman/model/vmware.rb', line 99

def server
  url
end

#server=(value) ⇒ Object


103
104
105
# File 'lib/foreman/model/vmware.rb', line 103

def server= value
  self.url = value
end

#test_connection(options = {}) ⇒ Object


63
64
65
66
67
68
69
70
71
# File 'lib/foreman/model/vmware.rb', line 63

def test_connection options = {}
  super
  if errors[:server].empty? and errors[:user].empty? and errors[:password].empty?
    update_public_key options
    datacenters
  end
rescue => e
  errors[:base] << e.message
end

#vms(opts = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
# File 'lib/foreman/model/vmware.rb', line 19

def vms(opts = {})
  if opts[:eager_loading] == true
    super()
  else
    #VMWare server loading is very slow
    #not using FOG models directly to save the time
    #and minimize the amount of time required (as we don't require all attributes by default when listing)
    FogExtensions::Vsphere::MiniServers.new(client, datacenter)
  end
end