From 04f78575b49305adf9aa0809e366229691237c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Font=C3=A1n=20Mui=C3=B1os?= Date: Fri, 5 Sep 2008 14:33:51 +0000 Subject: [PATCH] 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 --- src/client/ruby/client_utilities.rb | 41 ++++++++++++++++++ src/client/ruby/one.rb | 66 ++++++++++++++++++++++++++++- src/client/ruby/onehost | 12 ++++-- src/client/ruby/onevm | 48 +++++++++++++++------ 4 files changed, 148 insertions(+), 19 deletions(-) diff --git a/src/client/ruby/client_utilities.rb b/src/client/ruby/client_utilities.rb index 7ffabc654f..8a437c7968 100644 --- a/src/client/ruby/client_utilities.rb +++ b/src/client/ruby/client_utilities.rb @@ -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 diff --git a/src/client/ruby/one.rb b/src/client/ruby/one.rb index 26ce199294..e7ba6b6486 100644 --- a/src/client/ruby/one.rb +++ b/src/client/ruby/one.rb @@ -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 diff --git a/src/client/ruby/onehost b/src/client/ruby/onehost index fbd286b7c8..042e4d833f 100755 --- a/src/client/ruby/onehost +++ b/src/client/ruby/onehost @@ -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 diff --git a/src/client/ruby/onevm b/src/client/ruby/onevm index 84a456a755..cf5493bcf6 100755 --- a/src/client/ruby/onevm +++ b/src/client/ruby/onevm @@ -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")