mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-16 17:53:08 +09:00
Now VM and Host can be specified using names in CLI
git-svn-id: http://svn.opennebula.org/trunk@87 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
1 parent
21c174845b
commit
04f78575b4
4 files changed
+148
-19
No files matched your search
@@ -218,3 +218,44 @@ def check_parameters(name, number)
|
||||
exit -1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def get_vm_id(vm, name)
|
||||
vm_id=vm.get_vm_id(name)
|
||||
|
||||
result=nil
|
||||
|
||||
if vm_id
|
||||
if vm_id.kind_of?(Array)
|
||||
puts "There are multiple VM's with name #{name}."
|
||||
exit -1
|
||||
else
|
||||
result=vm_id
|
||||
end
|
||||
else
|
||||
puts "VM named #{name} not found."
|
||||
exit -1
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def get_host_id(host, name)
|
||||
host_id=host.get_host_id(name)
|
||||
|
||||
result=nil
|
||||
|
||||
if host_id
|
||||
if host_id.kind_of?(Array)
|
||||
puts "There are multiple hosts with name #{name}."
|
||||
exit -1
|
||||
else
|
||||
result=host_id
|
||||
end
|
||||
else
|
||||
puts "Host named #{name} not found."
|
||||
exit -1
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
+65
-1
@@ -411,7 +411,52 @@ module ONE
|
||||
dtime=Time.at(etime-stime).getgm
|
||||
|
||||
"%02d %02d:%02d:%02d" % [dtime.yday-1, dtime.hour, dtime.min, dtime.sec]
|
||||
end
|
||||
end
|
||||
|
||||
def get_vm_id(name)
|
||||
vm_id=name.strip
|
||||
# Check if the name is not a number (is not an ID)
|
||||
vm_id=get_vm_from_name(vm_id) if !vm_id.match(/^[0123456789]+$/)
|
||||
return vm_id
|
||||
end
|
||||
|
||||
# Gets vm ids which name is "name"
|
||||
# Returns:
|
||||
# nil if not vm has that name
|
||||
# id if there is only one vm with that name
|
||||
# array of ids if there is more than one vm
|
||||
def get_vm_from_name(name)
|
||||
db=Database.new
|
||||
res_template=db.select_table_with_names(
|
||||
"vm_template",
|
||||
:where => "name=\"NAME\" AND value=\"#{name}\"")
|
||||
|
||||
return nil if !res_template[0] or res_template[1].length<1
|
||||
|
||||
selected_vms=res_template[1].collect {|sel_template|
|
||||
template_id=sel_template["id"]
|
||||
res_vm=get(:where => "template=#{template_id}")
|
||||
if !res_vm[0] or res_vm[1].length<1
|
||||
nil
|
||||
else
|
||||
res_vm[1].collect {|vm|
|
||||
vm["oid"]
|
||||
}
|
||||
end
|
||||
}
|
||||
|
||||
selected_vms.flatten!
|
||||
selected_vms.compact!
|
||||
|
||||
case selected_vms.length
|
||||
when 0
|
||||
nil
|
||||
when 1
|
||||
selected_vms[0]
|
||||
else
|
||||
selected_vms
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -498,6 +543,25 @@ module ONE
|
||||
|
||||
state_str
|
||||
end
|
||||
|
||||
def get_host_id(name)
|
||||
host_id=name.strip
|
||||
# Check if the name is not a number (is not an ID)
|
||||
host_id=get_host_from_name(host_id) if !host_id.match(/^[0123456789]+$/)
|
||||
return host_id
|
||||
end
|
||||
|
||||
def get_host_from_name(name)
|
||||
res=get(:where => "host_name=\"#{name}\"")
|
||||
|
||||
return nil if !res[0] or res[1].length<1
|
||||
|
||||
if res[1].length==1
|
||||
return res[1][0]["hid"]
|
||||
else
|
||||
return res[1].collect {|host| host["hid"] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -232,7 +232,8 @@ when "add"
|
||||
|
||||
when "show"
|
||||
check_parameters("show", 1)
|
||||
result=host.info(*ARGV)
|
||||
host_id=get_host_id(host, ARGV[0])
|
||||
result=host.info(host_id)
|
||||
if result[0]
|
||||
puts result[1]
|
||||
else
|
||||
@@ -242,7 +243,8 @@ when "show"
|
||||
|
||||
when "delete"
|
||||
check_parameters("delete", 1)
|
||||
result=host.delete(*ARGV)
|
||||
host_id=get_host_id(host, ARGV[0])
|
||||
result=host.delete(host_id)
|
||||
if result[0]
|
||||
puts "Host deleted"
|
||||
exit 0
|
||||
@@ -264,7 +266,8 @@ when "top"
|
||||
|
||||
when "enable"
|
||||
check_parameters("enable", 1)
|
||||
result=host.enable(*ARGV)
|
||||
host_id=get_host_id(host, ARGV[0])
|
||||
result=host.enable(host_id)
|
||||
if result[0]
|
||||
puts "Host enabled"
|
||||
exit 0
|
||||
@@ -272,7 +275,8 @@ when "enable"
|
||||
|
||||
when "disable"
|
||||
check_parameters("disable", 1)
|
||||
result=host.disable(*ARGV)
|
||||
host_id=get_host_id(host, ARGV[0])
|
||||
result=host.disable(host_id)
|
||||
if result[0]
|
||||
puts "Host disabled"
|
||||
exit 0
|
||||
|
||||
+34
-14
@@ -263,9 +263,10 @@ class VmShow
|
||||
def list_vm_history_array(ids, options=nil)
|
||||
res=[false, "No VMs found"]
|
||||
ids.each {|id|
|
||||
vm_id=get_vm_id(@vm, id)
|
||||
puts "History for VM #{id}"
|
||||
puts
|
||||
res=list_vm_history(id, options)
|
||||
res=list_vm_history(vm_id, options)
|
||||
puts
|
||||
}
|
||||
res
|
||||
@@ -359,6 +360,7 @@ end
|
||||
|
||||
server=ONE::Server.new
|
||||
vm=ONE::VM.new(server)
|
||||
host=ONE::Host.new(server)
|
||||
|
||||
onevm_opts=OnevmParse.new
|
||||
onevm_opts.parse(ARGV)
|
||||
@@ -378,7 +380,9 @@ when "submit"
|
||||
|
||||
when "deploy"
|
||||
check_parameters("deploy", 2)
|
||||
result=vm.deploy(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
host_id=get_host_id(host, ARGV[1])
|
||||
result=vm.deploy(vm_id, host_id)
|
||||
if result[0]
|
||||
puts "Deploying VM"
|
||||
exit 0
|
||||
@@ -386,7 +390,8 @@ when "deploy"
|
||||
|
||||
when "shutdown"
|
||||
check_parameters("shutdown", 1)
|
||||
result=vm.shutdown(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.shutdown(vm_id)
|
||||
if result[0]
|
||||
puts "Shutting down VM"
|
||||
exit 0
|
||||
@@ -394,7 +399,9 @@ when "shutdown"
|
||||
|
||||
when "livemigrate"
|
||||
check_parameters("livemigrate", 2)
|
||||
result=vm.livemigrate(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
host_id=get_host_id(host, ARGV[1])
|
||||
result=vm.livemigrate(vm_id, host_id)
|
||||
if result[0]
|
||||
puts "Migrating VM"
|
||||
exit 0
|
||||
@@ -402,7 +409,9 @@ when "livemigrate"
|
||||
|
||||
when "migrate"
|
||||
check_parameters("migrate", 2)
|
||||
result=vm.migrate(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
host_id=get_host_id(host, ARGV[1])
|
||||
result=vm.migrate(vm_id, host_id)
|
||||
if result[0]
|
||||
puts "Migrating VM"
|
||||
exit 0
|
||||
@@ -410,7 +419,8 @@ when "migrate"
|
||||
|
||||
when "hold"
|
||||
check_parameters("hold", 1)
|
||||
result=vm.hold(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.hold(vm_id)
|
||||
if result[0]
|
||||
puts "Setting VM to hold state"
|
||||
exit 0
|
||||
@@ -418,7 +428,8 @@ when "hold"
|
||||
|
||||
when "release"
|
||||
check_parameters("release", 1)
|
||||
result=vm.release(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.release(vm_id)
|
||||
if result[0]
|
||||
puts "Releasing VM"
|
||||
exit 0
|
||||
@@ -426,7 +437,8 @@ when "release"
|
||||
|
||||
when "stop"
|
||||
check_parameters("stop", 1)
|
||||
result=vm.stop(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.stop(vm_id)
|
||||
if result[0]
|
||||
puts "Stopping VM"
|
||||
exit 0
|
||||
@@ -434,7 +446,8 @@ when "stop"
|
||||
|
||||
when "suspend"
|
||||
check_parameters("suspend", 1)
|
||||
result=vm.suspend(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.suspend(vm_id)
|
||||
if result[0]
|
||||
puts "Suspending VM"
|
||||
exit 0
|
||||
@@ -442,7 +455,8 @@ when "suspend"
|
||||
|
||||
when "resume"
|
||||
check_parameters("resume", 1)
|
||||
result=vm.resume(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.resume(vm_id)
|
||||
if result[0]
|
||||
puts "Resuming VM"
|
||||
exit 0
|
||||
@@ -467,7 +481,9 @@ when "show_db"
|
||||
vmlist=VmShow.new(vm)
|
||||
|
||||
if ARGV[0]
|
||||
result=vmlist.list_long(ARGV[0])
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vmlist.list_long(vm_id)
|
||||
|
||||
vmlist.close
|
||||
else
|
||||
result=[false, "You must provide a VM id."]
|
||||
@@ -478,7 +494,9 @@ when "history"
|
||||
ops=onevm_opts.options
|
||||
ops[:columns]=ops[:list] if ops[:list]
|
||||
if ARGV[0]
|
||||
result=vmlist.list_vm_history_array(ARGV)
|
||||
ids=ARGV.collect {|arg| get_vm_id(vm, arg)}
|
||||
ids=ids.flatten.compact
|
||||
result=vmlist.list_vm_history_array(ids)
|
||||
else
|
||||
result=vmlist.list_vm_history_all(ops)
|
||||
end
|
||||
@@ -486,7 +504,8 @@ when "history"
|
||||
|
||||
when "delete"
|
||||
check_parameters("delete", 1)
|
||||
result=vm.delete(*ARGV)
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
result=vm.delete(vm_id)
|
||||
if result[0]
|
||||
puts "VM correctly deleted"
|
||||
exit 0
|
||||
@@ -494,7 +513,8 @@ when "delete"
|
||||
|
||||
when "show"
|
||||
check_parameters("get_info", 1)
|
||||
res = vm.get_info(ARGV[0])
|
||||
vm_id=get_vm_id(vm, ARGV[0])
|
||||
res = vm.get_info(vm_id)
|
||||
if res[0]
|
||||
res[1].gsub!(/^\n/, '')
|
||||
(normal, template)=res[1].split("Template")
|
||||
|
||||
Reference in new issue
Block a user