diff --git a/src/cli/one_helper/onehost_helper.rb b/src/cli/one_helper/onehost_helper.rb index 8038cc93a9..a9ed75ea51 100644 --- a/src/cli/one_helper/onehost_helper.rb +++ b/src/cli/one_helper/onehost_helper.rb @@ -406,8 +406,44 @@ class OneHostHelper < OpenNebulaHelper::OneHelper CLIHelper.print_header(str_h1 % "MONITORING INFORMATION", false) + wilds = host.wilds + + host.delete_element("TEMPLATE/VM") + host.delete_element("TEMPLATE_WILDS") + puts host.template_str + puts + CLIHelper.print_header("WILD VIRTUAL MACHINES", false) + puts + + format = "%30s %36s %4s %10s" + CLIHelper.print_header(format % ["NAME", "IMPORT_ID", "CPU", "MEMORY"], + true) + + wilds.each do |wild| + if wild['IMPORT_TEMPLATE'] + wild_tmplt = Base64::decode64(wild['IMPORT_TEMPLATE']).split("\n") + name = wild_tmplt.select { |line| + line[/^NAME/] + }[0].split("=")[1].gsub("\"", " ").strip + import = wild_tmplt.select { |line| + line[/^IMPORT_VM_ID/] + }[0].split("=")[1].gsub("\"", " ").strip + memory = wild_tmplt.select { |line| + line[/^MEMORY/] + }[0].split("=")[1].gsub("\"", " ").strip + cpu = wild_tmplt.select { |line| + line[/^CPU/] + }[0].split("=")[1].gsub("\"", " ").strip + else + name = wild['DEPLOY_ID'] + import = memory = cpu = "-" + end + + puts format % [name, import, cpu, memory] + end + puts CLIHelper.print_header("VIRTUAL MACHINES", false) puts diff --git a/src/cli/onehost b/src/cli/onehost index e8ffa51fc6..556c081549 100755 --- a/src/cli/onehost +++ b/src/cli/onehost @@ -239,4 +239,17 @@ cmd=CommandParser::CmdParser.new(ARGV) do o.rename(args[1]) end end + + importvm_desc = <<-EOT.unindent + Import VM to OpenNebula + EOT + + command :importvm, importvm_desc, :hostid, :name do + helper.perform_action(args[0], options, "imported") do |o| + rc = o.info + next rc if OpenNebula.is_error?(rc) + + o.import_wild(args[1]) + end + end end diff --git a/src/host/Host.cc b/src/host/Host.cc index 679f159e51..727a10843e 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -387,6 +387,8 @@ int Host::update_info(Template &tmpl, zombie << zname; } } + + delete *it; } else if (rc == 0) //not ours { @@ -405,9 +407,9 @@ int Host::update_info(Template &tmpl, } wild << wname; - } - delete *it; + obj_template->set(*it); + } } for(set_it = tmp_lost_vms.begin(); set_it != tmp_lost_vms.end(); set_it++) diff --git a/src/oca/ruby/opennebula/host.rb b/src/oca/ruby/opennebula/host.rb index 1a0980c3c0..2b75395b66 100644 --- a/src/oca/ruby/opennebula/host.rb +++ b/src/oca/ruby/opennebula/host.rb @@ -16,6 +16,7 @@ require 'opennebula/pool_element' +require 'base64' module OpenNebula class Host < PoolElement @@ -187,6 +188,37 @@ module OpenNebula return call(HOST_METHODS[:rename], @pe_id, name) end + # Imports a wild VM from the host and puts it in running state + # + # @param name [String] Name of the VM to import + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def import_wild(name) + vms = importable_wilds.select {|vm| vm['VM_NAME'] == name } + + if vms.length == 0 + return OpenNebula::Error.new("No importable wilds with name " << + "'#{name}' found.") + elsif vms.length > 1 + return OpenNebula::Error.new("More than one importable wild " << + "with name '#{name}' found.") + end + + wild = vms.first + + template = Base64.decode64(wild['IMPORT_TEMPLATE']) + + xml = OpenNebula::VirtualMachine.build_xml + vm = OpenNebula::VirtualMachine.new(xml, @client) + + rc = vm.allocate(template) + + return rc if OpenNebula.is_error?(rc) + + vm.deploy(id, false) + end + ####################################################################### # Helpers to get Host information ####################################################################### @@ -206,6 +238,22 @@ module OpenNebula SHORT_HOST_STATES[state_str] end + # Returns the