mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-27 10:50:10 +03:00
Add import support in the CLI for running VMs in vCenter
This commit is contained in:
parent
406df133d8
commit
1babf402d4
74
src/cli/onevcenter
Executable file → Normal file
74
src/cli/onevcenter
Executable file → Normal file
@ -215,6 +215,80 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
exit 0
|
||||
end
|
||||
|
||||
vms_desc = <<-EOT.unindent
|
||||
Import vCenter running Virtual Machines into OpenNebula
|
||||
EOT
|
||||
|
||||
command :vms, vms_desc, :options=>[ VCENTER, USER, PASS ] do
|
||||
if options[:vuser].nil? ||
|
||||
options[:vpass].nil? ||
|
||||
options[:vcenter].nil?
|
||||
STDERR.puts "vCenter connection parameters are mandatory to import"\
|
||||
" VM templates:\n"\
|
||||
"\t --vcenter vCenter hostname\n"\
|
||||
"\t --vuser username to login in vcenter\n"\
|
||||
"\t --vpass password for the user"
|
||||
exit -1
|
||||
end
|
||||
|
||||
begin
|
||||
STDOUT.print "\nConnecting to vCenter: #{options[:vcenter]}..."
|
||||
|
||||
vc = VCenterDriver::VIClient.new_connection(
|
||||
:user => options[:vuser],
|
||||
:password => options[:vpass],
|
||||
:host => options[:vcenter])
|
||||
|
||||
STDOUT.print "done!\n\n"
|
||||
|
||||
STDOUT.print "Looking for running Virtual Machines..."
|
||||
|
||||
rs = vc.running_vms
|
||||
|
||||
STDOUT.print "done!\n"
|
||||
|
||||
rs.each {|dc, tmps|
|
||||
STDOUT.print "\nDo you want to process datacenter #{dc} [y/n]? "
|
||||
|
||||
next if STDIN.gets.strip.downcase != 'y'
|
||||
|
||||
if tmps.empty?
|
||||
STDOUT.print " No new running Virtual Machines found in"\
|
||||
" #{dc}...\n\n"
|
||||
next
|
||||
end
|
||||
|
||||
tmps.each{ |v|
|
||||
STDOUT.print "\n * Running Virtual Machine found:\n"\
|
||||
" - Name : #{v[:name]}\n"\
|
||||
" - UUID : #{v[:uuid]}\n"\
|
||||
" - Cluster: #{v[:host]}\n"\
|
||||
" Import this Virtual Machine [y/n]? "
|
||||
|
||||
next if STDIN.gets.strip.downcase != 'y'
|
||||
|
||||
one_v = ::OpenNebula::VirtualMachine.new(
|
||||
::OpenNebula::VirtualMachine.build_xml, vc.one)
|
||||
|
||||
rc = one_v.allocate(v[:one])
|
||||
|
||||
if ::OpenNebula.is_error?(rc)
|
||||
STDOUT.puts " Error creating Virtual Machine: "\
|
||||
"#{rc.message}\n"
|
||||
else
|
||||
STDOUT.puts " OpenNebula VM #{one_v.id} "\
|
||||
"created!\n"
|
||||
end
|
||||
}
|
||||
}
|
||||
rescue Exception => e
|
||||
STDOUT.puts "error: #{e.message}"
|
||||
exit -1
|
||||
end
|
||||
|
||||
exit 0
|
||||
end
|
||||
|
||||
network_desc = <<-EOT.unindent
|
||||
Import vCenter networks into OpenNebula
|
||||
EOT
|
||||
|
@ -246,6 +246,66 @@ class VIClient
|
||||
return vm_templates
|
||||
end
|
||||
|
||||
########################################################################
|
||||
# Builds a hash with the Datacenter / Virtual Machines for this VCenter
|
||||
# @param one_client [OpenNebula::Client] Use this client instead of @one
|
||||
# @return [Hash] in the form
|
||||
# { dc_name [String] => }
|
||||
########################################################################
|
||||
def running_vms(one_client=nil)
|
||||
running_vms = {}
|
||||
|
||||
vmpool = OpenNebula::VirtualMachinePool.new(
|
||||
(one_client||@one), OpenNebula::Pool::INFO_ALL)
|
||||
rc = vmpool.info
|
||||
|
||||
hostpool = OpenNebula::HostPool.new((one_client||@one))
|
||||
rc = hostpool.info
|
||||
# TODO check error
|
||||
|
||||
datacenters = get_entities(@root, 'Datacenter')
|
||||
|
||||
datacenters.each { |dc|
|
||||
vms = get_entities(dc.vmFolder, 'VirtualMachine')
|
||||
ccrs = get_entities(dc.hostFolder, 'ClusterComputeResource')
|
||||
|
||||
tmp = vms.select { |v|
|
||||
# Get rid of VM Templates and VMs not in running state
|
||||
v.config.template != true &&
|
||||
v.summary.runtime.powerState == "poweredOn"
|
||||
}
|
||||
|
||||
one_tmp = []
|
||||
|
||||
tmp.each { |v|
|
||||
vi_tmp = VCenterVm.new(self, v)
|
||||
|
||||
container_hostname = vi_tmp.vm.runtime.host.parent.name
|
||||
|
||||
cluster_name = ccrs.collect { |c|
|
||||
found_host=c.host.select {|h|
|
||||
h.parent.name == container_hostname}
|
||||
found_host.first.parent.name if found_host.size > 0
|
||||
}.first
|
||||
|
||||
if !vmpool["VM/USER_TEMPLATE/PUBLIC_CLOUD[\
|
||||
TYPE=\"vcenter\" \
|
||||
and VM_TEMPLATE=\"#{vi_tmp.vm.config.uuid}\"]"]
|
||||
one_tmp << {
|
||||
:name => vi_tmp.vm.name,
|
||||
:uuid => vi_tmp.vm.config.uuid,
|
||||
:host => container_hostname,
|
||||
:one => vi_tmp.vm_to_one
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
running_vms[dc.name] = one_tmp
|
||||
}
|
||||
|
||||
return running_vms
|
||||
end
|
||||
|
||||
########################################################################
|
||||
# Builds a hash with the Datacenter / CCR (Distributed)Networks
|
||||
# for this VCenter
|
||||
@ -948,6 +1008,39 @@ class VCenterVm
|
||||
return str
|
||||
end
|
||||
|
||||
########################################################################
|
||||
# Generates an OpenNebula VirtualMachine for this VCenterVm
|
||||
#
|
||||
#
|
||||
########################################################################
|
||||
def vm_to_one
|
||||
str = "NAME = \"#{@vm.name}\"\n"\
|
||||
"CPU = \"#{@vm.config.hardware.numCPU}\"\n"\
|
||||
"vCPU = \"#{@vm.config.hardware.numCPU}\"\n"\
|
||||
"MEMORY = \"#{@vm.config.hardware.memoryMB}\"\n"\
|
||||
"HYPERVISOR = \"vcenter\"\n"\
|
||||
"PUBLIC_CLOUD = [\n"\
|
||||
" TYPE =\"vcenter\",\n"\
|
||||
" VM_TEMPLATE =\"#{@vm.config.uuid}\"\n"\
|
||||
"]\n"\
|
||||
"GRAPHICS = [\n"\
|
||||
" TYPE =\"vnc\",\n"\
|
||||
" LISTEN =\"0.0.0.0\"\n"\
|
||||
"]\n"\
|
||||
"IMPORT_VM_ID = \"#{@vm.config.uuid}\"\n"\
|
||||
"SCHED_REQUIREMENTS=\"NAME=\\\"#{@vm.runtime.host.parent.name}\\\"\"\n"
|
||||
|
||||
if @vm.config.annotation.nil? || @vm.config.annotation.empty?
|
||||
str << "DESCRIPTION = \"vCenter Virtual Machine imported by OpenNebula"\
|
||||
" from Cluster #{@vm.runtime.host.parent.name}\"\n"
|
||||
else
|
||||
notes = @vm.config.annotation.gsub("\\", "\\\\").gsub("\"", "\\\"")
|
||||
str << "DESCRIPTION = \"#{notes}\"\n"
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
########################################################################
|
||||
|
Loading…
x
Reference in New Issue
Block a user