From dfcd8c449f16e403fe709763e4a82bead8d4cf02 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 8 May 2015 17:49:20 +0200 Subject: [PATCH] feature #3532: move import functionality to OCA --- src/cli/one_helper/onehost_helper.rb | 39 +------------------------- src/cli/onehost | 5 +++- src/oca/ruby/opennebula/host.rb | 41 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/cli/one_helper/onehost_helper.rb b/src/cli/one_helper/onehost_helper.rb index 8e3637a764..a9ed75ea51 100644 --- a/src/cli/one_helper/onehost_helper.rb +++ b/src/cli/one_helper/onehost_helper.rb @@ -311,43 +311,6 @@ class OneHostHelper < OpenNebulaHelper::OneHelper end end - def get_wilds(host) - [host.to_hash['HOST']['TEMPLATE']['VM']].flatten.compact - end - - def get_importable_wilds(host) - get_wilds(host).select {|w| Hash === w && w['IMPORT_TEMPLATE'] } - end - - def import_wild(host, name) - host.info - - wilds = get_importable_wilds(host) - - vms = 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(host.id, false) - end - private def print_update_info(current, total, host) @@ -443,7 +406,7 @@ class OneHostHelper < OpenNebulaHelper::OneHelper CLIHelper.print_header(str_h1 % "MONITORING INFORMATION", false) - wilds = get_wilds(host) + wilds = host.wilds host.delete_element("TEMPLATE/VM") host.delete_element("TEMPLATE_WILDS") diff --git a/src/cli/onehost b/src/cli/onehost index f92b22d0a6..556c081549 100755 --- a/src/cli/onehost +++ b/src/cli/onehost @@ -246,7 +246,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :importvm, importvm_desc, :hostid, :name do helper.perform_action(args[0], options, "imported") do |o| - helper.import_wild(o, args[1]) + rc = o.info + next rc if OpenNebula.is_error?(rc) + + o.import_wild(args[1]) end end end diff --git a/src/oca/ruby/opennebula/host.rb b/src/oca/ruby/opennebula/host.rb index 5ab9e0f993..2b75395b66 100644 --- a/src/oca/ruby/opennebula/host.rb +++ b/src/oca/ruby/opennebula/host.rb @@ -188,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 ####################################################################### @@ -213,6 +244,16 @@ module OpenNebula template_like_str('TEMPLATE', indent) end + # Get wild VMs in the host + def wilds + [self.to_hash['HOST']['TEMPLATE']['VM']].flatten.compact + end + + # Get importable wild VMs in the host + def importable_wilds + wilds.select {|w| Hash === w && w['IMPORT_TEMPLATE'] } + end + private def set_enabled(enabled) return Error.new('ID not defined') if !@pe_id