foreman/app/models/compute_resources/foreman/model/vmware.rb @ bb6aa56d
d4e8938a | Ohad Levy | require 'fog_extensions/vsphere/mini_servers'
|
|
617ee75f | Amos Benari | require 'foreman/exception'
|
|
d4e8938a | Ohad Levy | ||
4213d2be | Ohad Levy | module Foreman::Model
|
|
class Vmware < ComputeResource
|
|||
553a0beb | Joseph Magen | include ComputeResourceConsoleCommon
|
|
f2c78d4a | Joseph Magen | validates :user, :password, :server, :datacenter, :presence => true
|
|
3da587a3 | Ohad Levy | before_create :update_public_key
|
|
4213d2be | Ohad Levy | ||
def self.model_name
|
|||
ComputeResource.model_name
|
|||
end
|
|||
5b1f7529 | karmab | def user_data_supported?
|
|
true
|
|||
end
|
|||
dd42df0a | Ohad Levy | def capabilities
|
|
33040495 | Rufus Post | [:build, :image]
|
|
dd42df0a | Ohad Levy | end
|
|
d4e8938a | Ohad Levy | def vms(opts = {})
|
|
if opts[:eager_loading] == true
|
|||
super()
|
|||
else
|
|||
ba2a9e1b | Tomer Brisker | #VMware server loading is very slow
|
|
d4e8938a | Ohad Levy | #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
|
|||
1c9af75a | Ohad Levy | def provided_attributes
|
|
super.merge({ :mac => :mac })
|
|||
end
|
|||
4f7a4d0b | David Davis | def max_cpu_count(cluster = nil)
|
|
1c9af75a | Ohad Levy | return 8 unless cluster
|
|
cluster.num_cpu_cores
|
|||
4213d2be | Ohad Levy | end
|
|
def max_memory
|
|||
02cf7a53 | Tomer Brisker | 16*Foreman::SIZE[:giga]
|
|
4213d2be | Ohad Levy | end
|
|
def datacenters
|
|||
7a347c66 | Shlomi Zadok | name_sort(client.datacenters.all)
|
|
1c9af75a | Ohad Levy | end
|
|
fe1a2733 | Greg Petras | def cluster(cluster)
|
|
dc.clusters.get(cluster)
|
|||
end
|
|||
1c9af75a | Ohad Levy | def clusters
|
|
d76ca304 | Shlomi Zadok | if dc.clusters.nil?
|
|
Rails.logger.info "Datacenter #{dc.try(:name)} returned zero clusters"
|
|||
return []
|
|||
end
|
|||
bb6aa56d | slivik | dc.clusters.map(&:full_path).sort
|
|
1c9af75a | Ohad Levy | end
|
|
4f7a4d0b | David Davis | def datastores(opts = {})
|
|
a52580f5 | Greg Petras | if opts[:storage_domain]
|
|
7a347c66 | Shlomi Zadok | name_sort(dc.datastores.get(opts[:storage_domain]))
|
|
a52580f5 | Greg Petras | else
|
|
7a347c66 | Shlomi Zadok | name_sort(dc.datastores.all(:accessible => true))
|
|
a52580f5 | Greg Petras | end
|
|
end
|
|||
1c9af75a | Ohad Levy | def folders
|
|
7a347c66 | Shlomi Zadok | dc.vm_folders.sort_by{|f| [f.path, f.name]}
|
|
1c9af75a | Ohad Levy | end
|
|
4f7a4d0b | David Davis | def networks(opts = {})
|
|
7a347c66 | Shlomi Zadok | name_sort(dc.networks.all(:accessible => true))
|
|
1c9af75a | Ohad Levy | end
|
|
4f7a4d0b | David Davis | def resource_pools(opts = {})
|
|
fe1a2733 | Greg Petras | cluster = cluster(opts[:cluster_id])
|
|
7a347c66 | Shlomi Zadok | name_sort(cluster.resource_pools.all(:accessible => true))
|
|
fe1a2733 | Greg Petras | end
|
|
e4d88172 | Greg Petras | def available_clusters
|
|
clusters
|
|||
end
|
|||
fe1a2733 | Greg Petras | def available_folders
|
|
folders
|
|||
end
|
|||
4f7a4d0b | David Davis | def available_networks(cluster_id = nil)
|
|
e4d88172 | Greg Petras | networks
|
|
end
|
|||
4f7a4d0b | David Davis | def available_storage_domains(storage_domain = nil)
|
|
a52580f5 | Greg Petras | datastores({:storage_domain => storage_domain})
|
|
e4d88172 | Greg Petras | end
|
|
4f7a4d0b | David Davis | def available_resource_pools(opts = {})
|
|
fe1a2733 | Greg Petras | resource_pools({ :cluster_id => opts[:cluster_id] })
|
|
end
|
|||
c75be21b | Martin Matuska | def nictypes
|
|
{
|
|||
29769a4e | Martin Matuska | "VirtualE1000" => "E1000",
|
|
f2c78d4a | Joseph Magen | "VirtualVmxnet3" => "VMXNET 3"
|
|
c75be21b | Martin Matuska | }
|
|
f2c78d4a | Joseph Magen | end
|
|
c75be21b | Martin Matuska | ||
41ee835f | Martin Matuska | def scsi_controller_types
|
|
{
|
|||
7a347c66 | Shlomi Zadok | "VirtualBusLogicController" => "Bus Logic Parallel",
|
|
41ee835f | Martin Matuska | "VirtualLsiLogicController" => "LSI Logic Parallel",
|
|
"VirtualLsiLogicSASController" => "LSI Logic SAS",
|
|||
"ParaVirtualSCSIController" => "VMware Paravirtual"
|
|||
}
|
|||
end
|
|||
2fba2dfc | Michael Moll | # vSphere guest OS type descriptions
|
|
# list fetched from RbVmomi::VIM::VirtualMachineGuestOsIdentifier.values and
|
|||
# http://pubs.vmware.com/vsphere-55/topic/com.vmware.wssdk.apiref.doc/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
|
|||
def guest_types_descriptions
|
|||
507ebfd5 | Martin Matuska | {
|
|
"dosGuest" => "Microsoft MS-DOS",
|
|||
"win31Guest" => "Microsoft Windows 3.1",
|
|||
"win95Guest" => "Microsoft Windows 95",
|
|||
"win98Guest" => "Microsoft Windows 98",
|
|||
"winMeGuest" => "Microsoft Windows Millenium Edition",
|
|||
"winNTGuest" => "Microsoft Windows NT",
|
|||
"win2000ProGuest" => "Microsoft Windows 2000 Professional",
|
|||
"win2000ServGuest" => "Microsoft Windows 2000 Server",
|
|||
"win2000AdvServGuest" => "Microsoft Windows 2000 Advanced Server",
|
|||
"winXPHomeGuest" => "Microsoft Windows XP Home Edition",
|
|||
"winXPProGuest" => "Microsoft Windows XP Professional (32-bit)",
|
|||
"winXPPro64Guest" => "Microsoft Windows XP Professional (64-bit)",
|
|||
"winNetWebGuest" => "Microsoft Windows Server 2003 Web Edition (32-bit)",
|
|||
"winNetStandardGuest" => "Microsoft Windows Server 2003 Standard Edition (32-bit)",
|
|||
"winNetEnterpriseGuest" => "Microsoft Windows Server 2003 Enterprise Edition (32-bit)",
|
|||
"winNetDatacenterGuest" => "Microsoft Windows Server 2003 Datacenter Edition (32-bit)",
|
|||
"winNetBusinessGuest" => "Microsoft Windows Small Business Server 2003",
|
|||
"winNetStandard64Guest" => "Microsoft Windows Server 2003 Standard Edition (64-bit)",
|
|||
"winNetEnterprise64Guest" => "Microsoft Windows Server 2003 Enterprise Edition (64-bit)",
|
|||
"winLonghornGuest" => "Microsoft Windows Longhorn (32-bit)",
|
|||
"winLonghorn64Guest" => "Microsoft Windows Longhorn (64-bit)",
|
|||
"winNetDatacenter64Guest" => "Microsoft Windows Server 2003 Datacenter Edition (64-bit)",
|
|||
"winVistaGuest" => "Microsoft Windows Vista (32-bit)",
|
|||
"winVista64Guest" => "Microsoft Windows Vista (64-bit)",
|
|||
"windows7Guest" => "Microsoft Windows 7 (32-bit)",
|
|||
"windows7_64Guest" => "Microsoft Windows 7 (64-bit)",
|
|||
"windows7Server64Guest" => "Microsoft Windows Server 2008 R2 (64-bit)",
|
|||
"windows8Guest" => "Microsoft Windows 8 (32-bit)",
|
|||
"windows8_64Guest" => "Microsoft Windows 8 (64-bit)",
|
|||
"windows8Server64Guest" => "Microsoft Windows Server 2012 (64-bit)",
|
|||
2fba2dfc | Michael Moll | "windowsHyperVGuest" => "Microsoft Windows Hyper-V",
|
|
507ebfd5 | Martin Matuska | "freebsd64Guest" => "FreeBSD (64-bit)",
|
|
"freebsdGuest" => "FreeBSD (32-bit)",
|
|||
"redhatGuest" => "Red Hat Linux 2.1",
|
|||
"rhel2Guest" => "Red Hat Enterprise Linux 2.1",
|
|||
"rhel3Guest" => "Red Hat Enterprise Linux 3 (32-bit)",
|
|||
"rhel3_64Guest" => "Red Hat Enterprise Linux 3 (64-bit)",
|
|||
"rhel4Guest" => "Red Hat Enterprise Linux 4 (32-bit)",
|
|||
"rhel4_64Guest" => "Red Hat Enterprise Linux 4 (64-bit)",
|
|||
"rhel5Guest" => "Red Hat Enterprise Linux 5 (32-bit)",
|
|||
"rhel5_64Guest" => "Red Hat Enterprise Linux 5 (64-bit)",
|
|||
"rhel6Guest" => "Red Hat Enterprise Linux 6 (32-bit)",
|
|||
"rhel6_64Guest" => "Red Hat Enterprise Linux 6 (64-bit)",
|
|||
2fba2dfc | Michael Moll | "rhel7Guest" => "Red Hat Enterprise Linux 7 (32-bit)",
|
|
"rhel7_64Guest" => "Red Hat Enterprise Linux 7 (64-bit)",
|
|||
507ebfd5 | Martin Matuska | "centosGuest" => "CentOS 4/5/6 (32-bit)",
|
|
"centos64Guest" => "CentOS 4/5/6 (64-bit)",
|
|||
"oracleLinux64Guest" => "Oracle Linux 4/5/6 (64-bit)",
|
|||
"oracleLinuxGuest" => "Oracle Linux 4/5/6 (32-bit)",
|
|||
"suseGuest" => "Suse Linux (32-bit)",
|
|||
"suse64Guest" => "Suse Linux (64-bit)",
|
|||
"slesGuest" => "Novell SUSE Linux Enterprise 8/9 (32-bit)",
|
|||
"sles64Guest" => "Novell SUSE Linux Enterprise 8/9 (64-bit)",
|
|||
"sles10Guest" => "Novell SUSE Linux Enterprise 10 (32-bit)",
|
|||
"sles10_64Guest" => "Novell SUSE Linux Enterprise 10 (64-bit)",
|
|||
"sles11Guest" => "Novell SUSE Linux Enterprise 11 (32-bit)",
|
|||
"sles11_64Guest" => "Novell SUSE Linux Enterprise 11 (64-bit)",
|
|||
2fba2dfc | Michael Moll | "sles12Guest" => "Novell SUSE Linux Enterprise 12 (32-bit)",
|
|
"sles12_64Guest" => "Novell SUSE Linux Enterprise 12 (64-bit)",
|
|||
507ebfd5 | Martin Matuska | "nld9Guest" => "Novell Linux Desktop 9",
|
|
"oesGuest" => "Novell Open Enterprise Server",
|
|||
"sjdsGuest" => "Sun Java Desktop System",
|
|||
2fba2dfc | Michael Moll | "mandrakeGuest" => "Mandrake Linux",
|
|
507ebfd5 | Martin Matuska | "mandrivaGuest" => "Mandriva Linux (32-bit)",
|
|
"mandriva64Guest" => "Mandriva Linux (64-bit)",
|
|||
"turboLinuxGuest" => "Turbolinux (32-bit)",
|
|||
"turboLinux64Guest" => "Turbolinux (64-bit)",
|
|||
"ubuntu64Guest" => "Ubuntu Linux (64-bit)",
|
|||
"ubuntuGuest" => "Ubuntu Linux (32-bit)",
|
|||
"debian4Guest" => "Debian GNU/Linux 4 (32-bit)",
|
|||
"debian4_64Guest" => "Debian GNU/Linux 5 (64-bit)",
|
|||
"debian5Guest" => "Debian GNU/Linux 5 (32-bit)",
|
|||
"debian5_64Guest" => "Debian GNU/Linux 5 (64-bit)",
|
|||
"debian6Guest" => "Debian GNU/Linux 6 (32-bit)",
|
|||
"debian6_64Guest" => "Debian GNU/Linux 6 (64-bit)",
|
|||
2fba2dfc | Michael Moll | "debian7Guest" => "Debian GNU/Linux 7 (32-bit)",
|
|
"debian7_64Guest" => "Debian GNU/Linux 7 (64-bit)",
|
|||
507ebfd5 | Martin Matuska | "asianux3Guest" => "Asianux Server 3 (32-bit)",
|
|
"asianux3_64Guest" => "Asianux Server 3 (64-bit)",
|
|||
"asianux4Guest" => "Asianux Server 4 (32-bit)",
|
|||
"asianux4_64Guest" => "Asianux Server 4 (64-bit)",
|
|||
"opensuseGuest" => "OpenSUSE Linux (32-bit)",
|
|||
"opensuse64Guest" => "OpenSUSE Linux (64-bit)",
|
|||
2fba2dfc | Michael Moll | "fedoraGuest" => "Fedora Linux",
|
|
"fedora64Guest" => "Fedora Linux (64-bit)",
|
|||
507ebfd5 | Martin Matuska | "other24xLinuxGuest" => "Other 2.4.x Linux (32-bit)",
|
|
"other26xLinuxGuest" => "Other 2.6.x Linux (32-bit)",
|
|||
2fba2dfc | Michael Moll | "other3xLinuxGuest" => "Other Linux 3.x Guest",
|
|
507ebfd5 | Martin Matuska | "otherLinuxGuest" => "Other Linux (32-bit)",
|
|
2fba2dfc | Michael Moll | "genericLinuxGuest" => "Other Linux",
|
|
507ebfd5 | Martin Matuska | "other24xLinux64Guest" => "Other 2.4.x Linux (64-bit)",
|
|
"other26xLinux64Guest" => "Other 2.6.x Linux (64-bit)",
|
|||
2fba2dfc | Michael Moll | "other3xLinux64Guest" => "Other Linux 3.x Guest (64-bit)",
|
|
507ebfd5 | Martin Matuska | "otherLinux64Guest" => "Other Linux (64-bit)",
|
|
"solaris6Guest" => "Sun Microsystems Solaris 6",
|
|||
"solaris7Guest" => "Sun Microsystems Solaris 7",
|
|||
"solaris8Guest" => "Sun Microsystems Solaris 8",
|
|||
"solaris9Guest" => "Sun Microsystems Solaris 9",
|
|||
"solaris10Guest" => "Oracle Solaris 10 (32-bit)",
|
|||
"solaris10_64Guest" => "Oracle Solaris 10 (64-bit)",
|
|||
"solaris11_64Guest" => "Oracle Solaris 11 (64-bit)",
|
|||
"os2Guest" => "IBM OS/2",
|
|||
"eComStationGuest" => "Serenity Systems eComStation 1.x",
|
|||
"eComStation2Guest" => "Serenity Systems eComStation 2.0",
|
|||
"netware4Guest" => "Novell NetWare 4",
|
|||
"netware5Guest" => "Novell NetWare 5.1",
|
|||
"netware6Guest" => "Novell NetWare 6.x",
|
|||
"openServer5Guest" => "SCO OpenServer 5",
|
|||
"openServer6Guest" => "SCO OpenServer 6",
|
|||
"unixWare7Guest" => "SCO UnixWare 7",
|
|||
"darwinGuest" => "Apple Mac OS X 10.5 (32-bit)",
|
|||
"darwin64Guest" => "Apple Mac OS X 10.5 (64-bit)",
|
|||
"darwin10Guest" => "Apple Mac OS X 10.6 (32-bit)",
|
|||
"darwin10_64Guest" => "Apple Mac OS X 10.6 (64-bit)",
|
|||
"darwin11Guest" => "Apple Mac OS X 10.7 (32-bit)",
|
|||
"darwin11_64Guest" => "Apple Mac OS X 10.7 (64-bit)",
|
|||
2fba2dfc | Michael Moll | "darwin12_64Guest" => "Mac OS 10.8 (64-bit)",
|
|
"darwin13_64Guest" => "Mac OS 10.9 (64-bit)",
|
|||
ba2a9e1b | Tomer Brisker | "vmkernelGuest" => "VMware ESX 4.x",
|
|
"vmkernel5Guest" => "VMware ESXi 5.x",
|
|||
507ebfd5 | Martin Matuska | "otherGuest" => "Other (32-bit)",
|
|
"otherGuest64" => "Other (64-bit)"
|
|||
}
|
|||
end
|
|||
def guest_types
|
|||
types = { }
|
|||
RbVmomi::VIM::VirtualMachineGuestOsIdentifier.values.compact.each do |v|
|
|||
types[v] = guest_types_descriptions.has_key?(v) ? guest_types_descriptions[v] : v
|
|||
end
|
|||
types
|
|||
end
|
|||
41ee835f | Martin Matuska | def scsi_controller_default_type
|
|
"VirtualLsiLogicController"
|
|||
end
|
|||
26975bf5 | Michael Moll | def vm_hw_versions
|
|
{
|
|||
'Default' => _("Default"),
|
|||
'vmx-10' => '10 (ESXi 5.5)',
|
|||
'vmx-09' => '9 (ESXi 5.1)',
|
|||
'vmx-08' => '8 (ESXi 5.0)',
|
|||
'vmx-07' => '7 (ESX/ESXi 4.x)',
|
|||
'vmx-04' => '4 (ESX/ESXi 3.5)',
|
|||
}
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def test_connection(options = {})
|
|
4213d2be | Ohad Levy | super
|
|
ebac69ce | Shlomi Zadok | if errors[:server].empty? && errors[:user].empty? && errors[:password].empty?
|
|
617ee75f | Amos Benari | update_public_key options
|
|
95be0963 | Amos Benari | datacenters
|
|
end
|
|||
4213d2be | Ohad Levy | rescue => e
|
|
errors[:base] << e.message
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def parse_args(args)
|
|
3059cea1 | Tom Caspy | args = args.deep_symbolize_keys
|
|
1c9af75a | Ohad Levy | ||
80ad15ca | Dominic Cleal | # convert rails nested_attributes into a plain, symbolized hash
|
|
33040495 | Rufus Post | [:interfaces, :volumes].each do |collection|
|
|
nested_attrs = args.delete("#{collection}_attributes".to_sym)
|
|||
args[collection] = nested_attributes_for(collection, nested_attrs) if nested_attrs
|
|||
end
|
|||
7e27bdf3 | Dominic Cleal | if args[:scsi_controller_type].present?
|
|
args[:scsi_controller] = {:type => args.delete(:scsi_controller_type)}
|
|||
end
|
|||
26975bf5 | Michael Moll | args.except!(:hardware_version) if args[:hardware_version] == 'Default'
|
|
33040495 | Rufus Post | args.reject! { |k, v| v.nil? }
|
|
args
|
|||
end
|
|||
693391ba | Dominic Cleal | # Change network IDs for names only at the point of creation, as IDs are
|
|
# used in the UI for select boxes etc.
|
|||
5f029ed6 | Daniel Lobato | def parse_networks(args)
|
|
693391ba | Dominic Cleal | args = args.deep_dup
|
|
dc_networks = networks
|
|||
args["interfaces_attributes"].each do |key, interface|
|
|||
# Convert network id into name
|
|||
net = dc_networks.find { |n| [n.id, n.name].include?(interface["network"]) }
|
|||
raise "Unknown Network ID: #{interface["network"]}" if net.nil?
|
|||
interface["network"] = net.name
|
|||
end if args["interfaces_attributes"]
|
|||
args
|
|||
end
|
|||
33040495 | Rufus Post | ||
5f029ed6 | Daniel Lobato | def create_vm(args = { })
|
|
33040495 | Rufus Post | test_connection
|
|
return unless errors.empty?
|
|||
693391ba | Dominic Cleal | args = parse_networks(args)
|
|
140cfe26 | Chad Walstrom | args = args.with_indifferent_access
|
|
80ad15ca | Dominic Cleal | if args[:image_id].present?
|
|
33040495 | Rufus Post | clone_vm(args)
|
|
else
|
|||
vm = new_vm(args)
|
|||
vm.save
|
|||
end
|
|||
1c9af75a | Ohad Levy | rescue Fog::Errors::Error => e
|
|
01e78260 | Ivan Nečas | Foreman::Logging.exception("Unhandled VMware error", e)
|
|
c67f9c5e | Greg Sutcliffe | destroy_vm vm.id if vm
|
|
raise e
|
|||
1c9af75a | Ohad Levy | end
|
|
6d05514a | Tomas Strachota | def new_vm(args = {})
|
|
693391ba | Dominic Cleal | args = parse_args args
|
|
3059cea1 | Tom Caspy | opts = vm_instance_defaults.symbolize_keys.merge(args.symbolize_keys).deep_symbolize_keys
|
|
33040495 | Rufus Post | client.servers.new opts
|
|
end
|
|||
# === Power on
|
|||
#
|
|||
# Foreman will try and start this vm after clone in a seperate request.
|
|||
#
|
|||
# === Clusters
|
|||
#
|
|||
# Fog adaptor is incompatable with foreman because foreman does not have
|
|||
# concept of a resource pool.
|
|||
#
|
|||
# "resource_pool" => [args["cluster"], "Resources"]
|
|||
#
|
|||
# Fog calls +cluster.resourcePool.find("Resources")+ that actually calls
|
|||
# +searchIndex.FindChild("Resources")+ in RbVmomi that then returns nil
|
|||
# because it has no children.
|
|||
5f029ed6 | Daniel Lobato | def clone_vm(args)
|
|
693391ba | Dominic Cleal | args = parse_args args
|
|
e7d21bf0 | Timo Goebel | path_replace = %r{/[^/]+/#{datacenter}/vm(/|)}
|
|
33040495 | Rufus Post | ||
80ad15ca | Dominic Cleal | interfaces = client.list_vm_interfaces(args[:image_id])
|
|
e7d21bf0 | Timo Goebel | interface = interfaces.detect{|i| i[:name].end_with?('1') }
|
|
33040495 | Rufus Post | network_adapter_device_key = interface[:key]
|
|
opts = {
|
|||
"datacenter" => datacenter,
|
|||
80ad15ca | Dominic Cleal | "template_path" => args[:image_id],
|
|
"dest_folder" => args[:path].gsub(path_replace, ''),
|
|||
33040495 | Rufus Post | "power_on" => false,
|
|
80ad15ca | Dominic Cleal | "start" => args[:start],
|
|
"name" => args[:name],
|
|||
"numCPUs" => args[:cpus],
|
|||
"memoryMB" => args[:memory_mb],
|
|||
"datastore" => args[:volumes].first[:datastore],
|
|||
"network_label" => args[:interfaces].first[:network],
|
|||
f25e9797 | Francois Herbert | "nic_type" => args[:interfaces].first[:type],
|
|
33040495 | Rufus Post | "network_adapter_device_key" => network_adapter_device_key
|
|
}
|
|||
5d47a167 | karmab | opts["customization_spec"] = client.cloudinit_to_customspec(args[:user_data]) if args[:user_data]
|
|
33040495 | Rufus Post | client.servers.get(client.vm_clone(opts)['new_vm']['id'])
|
|
end
|
|||
4213d2be | Ohad Levy | def server
|
|
url
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def server=(value)
|
|
4213d2be | Ohad Levy | self.url = value
|
|
end
|
|||
1c9af75a | Ohad Levy | def datacenter
|
|
uuid
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def datacenter=(value)
|
|
1c9af75a | Ohad Levy | self.uuid = value
|
|
end
|
|||
5f029ed6 | Daniel Lobato | def console(uuid)
|
|
4213d2be | Ohad Levy | 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
|
|||
1c9af75a | Ohad Levy | values = { :port => unused_vnc_port(vm.hypervisor), :password => random_password, :enabled => true }
|
|
4213d2be | Ohad Levy | vm.config_vnc(values)
|
|
8ffd9aee | Ohad Levy | WsProxy.start(:host => vm.hypervisor, :host_port => values[:port], :password => values[:password]).merge(:type => 'vnc')
|
|
4213d2be | Ohad Levy | end
|
|
5f029ed6 | Daniel Lobato | def new_interface(attr = { })
|
|
1c9af75a | Ohad Levy | client.interfaces.new attr
|
|
end
|
|||
5f029ed6 | Daniel Lobato | def new_volume(attr = { })
|
|
1c9af75a | Ohad Levy | client.volumes.new attr.merge(:size_gb => 10)
|
|
end
|
|||
617ee75f | Amos Benari | def pubkey_hash
|
|
attrs[:pubkey_hash]
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def pubkey_hash=(key)
|
|
617ee75f | Amos Benari | attrs[:pubkey_hash] = key
|
|
end
|
|||
805358df | Jason Montleon | def associated_host(vm)
|
|
81a02cde | Tom Caspy | associate_by("mac", vm.mac)
|
|
805358df | Jason Montleon | end
|
|
ddce3dc1 | Amos Benari | def self.provider_friendly_name
|
|
ba2a9e1b | Tomer Brisker | "VMware"
|
|
2ebd2f22 | Joseph Magen | end
|
|
4213d2be | Ohad Levy | private
|
|
1c9af75a | Ohad Levy | def dc
|
|
client.datacenters.get(datacenter)
|
|||
end
|
|||
5f029ed6 | Daniel Lobato | def update_public_key(options = {})
|
|
617ee75f | Amos Benari | return unless pubkey_hash.blank? || options[:force]
|
|
3da587a3 | Ohad Levy | client
|
|
617ee75f | Amos Benari | rescue Foreman::FingerprintException => e
|
|
self.pubkey_hash = e.fingerprint
|
|||
3da587a3 | Ohad Levy | end
|
|
4213d2be | Ohad Levy | def client
|
|
@client ||= ::Fog::Compute.new(
|
|||
:provider => "vsphere",
|
|||
:vsphere_username => user,
|
|||
:vsphere_password => password,
|
|||
3da587a3 | Ohad Levy | :vsphere_server => server,
|
|
:vsphere_expected_pubkey_hash => pubkey_hash
|
|||
4213d2be | Ohad Levy | )
|
|
617ee75f | Amos Benari | rescue => e
|
|
if e.message =~ /The remote system presented a public key with hash (\w+) but we're expecting a hash of/
|
|||
raise Foreman::FingerprintException.new(
|
|||
N_("The remote system presented a public key with hash %s but we're expecting a different hash. If you are sure the remote system is authentic, go to the compute resource edit page, press the 'Test Connection' or 'Load Datacenters' button and submit"), $1)
|
|||
else
|
|||
raise e
|
|||
end
|
|||
4213d2be | Ohad Levy | end
|
|
5f029ed6 | Daniel Lobato | def unused_vnc_port(ip)
|
|
4213d2be | Ohad Levy | 10.times do
|
|
1c9af75a | Ohad Levy | port = 5901 + rand(64)
|
|
4213d2be | Ohad Levy | unused = (TCPSocket.connect(ip, port).close rescue true)
|
|
return port if unused
|
|||
end
|
|||
raise "no unused port found"
|
|||
end
|
|||
1c9af75a | Ohad Levy | def vm_instance_defaults
|
|
557b8543 | Joseph Mitchell Magen | super.merge(
|
|
1c9af75a | Ohad Levy | :memory_mb => 768,
|
|
:interfaces => [new_interface],
|
|||
:volumes => [new_volume],
|
|||
41ee835f | Martin Matuska | :scsi_controller => { :type => scsi_controller_default_type },
|
|
557b8543 | Joseph Mitchell Magen | :datacenter => datacenter
|
|
)
|
|||
1c9af75a | Ohad Levy | end
|
|
4213d2be | Ohad Levy | end
|
|
end
|