mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-16 22:50:10 +03:00
M #-: Add cleartags functionality to onevcenter
(cherry picked from commit 600e6cd95d541f781bec7a2576e089de0a554738)
This commit is contained in:
parent
081bdbdaed
commit
2ab65e1254
@ -425,4 +425,59 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper
|
||||
|
||||
return opts
|
||||
end
|
||||
|
||||
def clear_tags(vmid)
|
||||
client = Client.new
|
||||
|
||||
vm_pool = VirtualMachinePool.new(client, -1)
|
||||
host_pool = HostPool.new(client)
|
||||
deploy_id = -1
|
||||
host_id = -1
|
||||
hostname = ''
|
||||
|
||||
rc = vm_pool.info
|
||||
raise rc.message if OpenNebula.is_error?(rc)
|
||||
|
||||
rc = host_pool.info
|
||||
raise rc.message if OpenNebula.is_error?(rc)
|
||||
|
||||
vm_pool.each do |vm|
|
||||
next if vm.id.to_s != vmid
|
||||
|
||||
deploy_id = vm.deploy_id
|
||||
vm_history = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY']
|
||||
hostname = vm_history['HOSTNAME']
|
||||
break
|
||||
end
|
||||
|
||||
host_pool.each do |host|
|
||||
if host.name == hostname
|
||||
host_id = host.id
|
||||
end
|
||||
end
|
||||
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
vm = VCenterDriver::VirtualMachine
|
||||
.new(vi_client, deploy_id, vmid)
|
||||
|
||||
keys_to_remove = []
|
||||
vm['config.extraConfig'].each do |extraconfig|
|
||||
if extraconfig.key.include?('opennebula.disk') ||
|
||||
extraconfig.key.include?('opennebula.vm')
|
||||
keys_to_remove << extraconfig.key
|
||||
end
|
||||
end
|
||||
|
||||
return vm, keys_to_remove
|
||||
end
|
||||
|
||||
def remove_keys(vm, keys_to_remove)
|
||||
spec_hash = keys_to_remove.map {|key| { :key => key, :value => '' } }
|
||||
|
||||
spec = RbVmomi::VIM.VirtualMachineConfigSpec(
|
||||
:extraConfig => spec_hash
|
||||
)
|
||||
vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -286,4 +286,49 @@ CommandParser::CmdParser.new(ARGV) do
|
||||
|
||||
exit 0
|
||||
end
|
||||
|
||||
############################################################################
|
||||
# Clear VM tags
|
||||
############################################################################
|
||||
cleartags_desc = <<-EOT.unindent
|
||||
Clear extraconfig tags from a vCenter VM, useful when a VM has been
|
||||
launched by OpenNebula and needs to be reimported
|
||||
|
||||
Example:
|
||||
- Clean VM 15:
|
||||
|
||||
onevcenter cleargs 15
|
||||
EOT
|
||||
command :cleartags, cleartags_desc, :vmid do
|
||||
vmid = args[0]
|
||||
remove_str = "\n onevm recover --delete-db #{vmid}" \
|
||||
"\n\nAfter a monitoring cycle, the VM will appear "\
|
||||
'as a Wild VM for reimport.'
|
||||
|
||||
print 'Extracting information from VM ' + vmid
|
||||
|
||||
begin
|
||||
print '.'
|
||||
vm, keys = helper.clear_tags(vmid)
|
||||
print '.'
|
||||
|
||||
if keys.empty?
|
||||
puts "\n\nNo OpenNebula keys present, is safe to remove the VM."
|
||||
puts remove_str
|
||||
exit 0
|
||||
end
|
||||
puts '.'
|
||||
|
||||
puts 'The following keys will be removed:'
|
||||
keys.each {|key| puts "\t- #{key}" }
|
||||
|
||||
helper.remove_keys(vm, keys)
|
||||
rescue StandardError => e
|
||||
STDERR.puts "Couldn't clear VM tags. Reason: #{e.message}"
|
||||
exit 1
|
||||
end
|
||||
puts "\nKeys removed from VM. Is safe to remove it"
|
||||
puts remove_str
|
||||
exit 0
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user