diff --git a/src/cli/one_helper/onehost_helper.rb b/src/cli/one_helper/onehost_helper.rb index be8bfb3ed0..009928eee6 100644 --- a/src/cli/one_helper/onehost_helper.rb +++ b/src/cli/one_helper/onehost_helper.rb @@ -311,6 +311,41 @@ class OneHostHelper < OpenNebulaHelper::OneHelper end end + def get_wilds(host) + [host.to_hash['HOST']['TEMPLATE']['VM']].flatten + 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['DEPLOY_ID'] == 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) + end + private def print_update_info(current, total, host) @@ -360,14 +395,6 @@ class OneHostHelper < OpenNebulaHelper::OneHelper OpenNebula::HostPool.new(@client) end - def get_wilds(host) - [host.to_hash['HOST']['TEMPLATE']['VM']].flatten - end - - def get_importable_wilds(host) - get_wilds(host).select {|w| Hash === w && w['IMPORT_TEMPLATE'] } - end - def format_resource(host, options = {}) str = "%-22s: %-20s" str_h1 = "%-80s" diff --git a/src/cli/onehost b/src/cli/onehost index e8ffa51fc6..f92b22d0a6 100755 --- a/src/cli/onehost +++ b/src/cli/onehost @@ -239,4 +239,14 @@ 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| + helper.import_wild(o, args[1]) + end + end end