From 395d98a2740e3e425545e0838927c453e83bf353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 21 Oct 2011 18:00:52 +0200 Subject: [PATCH 1/3] Bug #868: Add option to set the image type in one.vm.savedisk; done in core, ruby & java oca, CLI --- src/cli/onevm | 15 +++++++++++++-- .../opennebula/client/vm/VirtualMachine.java | 17 ++++++++++++++++- src/oca/ruby/OpenNebula/VirtualMachine.rb | 10 ++++++++-- src/rm/RequestManagerVirtualMachine.cc | 6 ++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/cli/onevm b/src/cli/onevm index 0c5716f38c..3cd90e80ec 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -36,6 +36,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper = OneVMHelper.new + + TYPE={ + :name => "type", + :short => "-t type", + :large => "--type type", + :format => String, + :description => "Type of the new Image" + } + ######################################################################## # Global Options ######################################################################## @@ -142,14 +151,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do States: ANY EOT - command :saveas, saveas_desc, :vmid, :diskid, :img_name do + command :saveas, saveas_desc, :vmid, :diskid, :img_name, :options=>[TYPE] do disk_id = args[1].to_i image_name = args[2] + image_type = options[:type] || "" + verbose = "disk #{disk_id} prepared to be saved in " << "the image #{image_name}" helper.perform_action(args[0],options,verbose) do |vm| - res = vm.save_as(disk_id, image_name) + res = vm.save_as(disk_id, image_name, image_type) if !OpenNebula.is_error?(res) puts "Image ID: #{res}" diff --git a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java index b15db02bdb..ccb89f254d 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -292,7 +292,22 @@ public class VirtualMachine extends PoolElement{ */ public OneResponse savedisk(int diskId, String imageName) { - return client.call(SAVEDISK, id ,diskId, imageName); + return savedisk(diskId, imageName, ""); + } + + /** + * Sets the specified vm's disk to be saved in a new image when the + * VirtualMachine shutdowns. + * + * @param diskId ID of the disk to be saved. + * @param imageName Name of the new Image that will be created. + * @param imageType Type of the new image. Set to empty string to use + * the default type + * @return If an error occurs the error message contains the reason. + */ + public OneResponse savedisk(int diskId, String imageName, String imageType) + { + return client.call(SAVEDISK, id ,diskId, imageName, imageType); } /** diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index 7360be8ef0..ee98880a9e 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -217,13 +217,19 @@ module OpenNebula # @param disk_id [Integer] ID of the disk to be saved # @param image_name [String] Name for the new image where the # disk will be saved + # @param image_type [String] Type of the new image. Set to empty string + # to use the default type # # @return [Integer, OpenNebula::Error] the new Image ID in case of # success, error otherwise - def save_as(disk_id, image_name) + def save_as(disk_id, image_name, image_type="") return Error.new('ID not defined') if !@pe_id - rc = @client.call(VM_METHODS[:savedisk], @pe_id, disk_id, image_name) + rc = @client.call(VM_METHODS[:savedisk], + @pe_id, + disk_id, + image_name, + image_type) return rc end diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index cdbef2c83e..17dd537084 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -394,6 +394,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis int id = xmlrpc_c::value_int(paramList.getInt(1)); int disk_id = xmlrpc_c::value_int(paramList.getInt(2)); string img_name = xmlrpc_c::value_string(paramList.getString(3)); + string img_type = xmlrpc_c::value_string(paramList.getString(4)); VirtualMachine * vm; string vm_owner; @@ -412,6 +413,11 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis oss << "PUBLIC = NO " << endl; oss << "SOURCE = - " << endl; + if ( img_type != "" ) + { + oss << "TYPE = " << img_type << endl; + } + itemplate = new ImageTemplate; itemplate->parse(oss.str(), &error_char); From f0b1a28d892c931a34cb43f4a695fc7548f3fb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 21 Oct 2011 18:50:04 +0200 Subject: [PATCH 2/3] Bug #868: Add new one.image.chtype method, in core, ruby & java oca, and CLI. Needs testing --- include/Image.h | 30 ++---------- include/RequestManagerImage.h | 28 +++++++++-- src/cli/oneimage | 10 ++++ src/image/Image.cc | 31 +++++++++++- .../org/opennebula/client/image/Image.java | 25 ++++++++++ src/oca/ruby/OpenNebula/Image.rb | 16 ++++++- src/rm/RequestManager.cc | 2 + src/rm/RequestManagerImage.cc | 48 +++++++++++++++++++ 8 files changed, 158 insertions(+), 32 deletions(-) diff --git a/include/Image.h b/include/Image.h index d3328dadc1..fe103947e0 100644 --- a/include/Image.h +++ b/include/Image.h @@ -156,32 +156,12 @@ public: } /** - * Set enum type - * @return 0 on success, -1 otherwise + * Sets the Image type. + * + * @param _type the new type. It will be transformed to upper case + * @return 0 on success, -1 otherwise */ - int set_type(const string& _type) - { - int rc = 0; - - if ( _type == "OS" ) - { - type = OS; - } - else if ( _type == "CDROM" ) - { - type = CDROM; - } - else if ( _type == "DATABLOCK" ) - { - type = DATABLOCK; - } - else - { - rc = -1; - } - - return rc; - } + int set_type(string& _type); /** * Publish or unpublish an image diff --git a/include/RequestManagerImage.h b/include/RequestManagerImage.h index c7cda823f0..32c3d3c62b 100644 --- a/include/RequestManagerImage.h +++ b/include/RequestManagerImage.h @@ -30,8 +30,9 @@ class RequestManagerImage: public Request { protected: RequestManagerImage(const string& method_name, - const string& help) - :Request(method_name,"A:sib",help) + const string& help, + const string& params) + :Request(method_name,params,help) { Nebula& nd = Nebula::instance(); pool = nd.get_ipool(); @@ -55,7 +56,8 @@ class ImageEnable : public RequestManagerImage { public: ImageEnable(): - RequestManagerImage("ImageEnable", "Enables or disables an image"){}; + RequestManagerImage("ImageEnable", "Enables or disables an image", + "A:sib"){}; ~ImageEnable(){}; @@ -71,7 +73,8 @@ class ImagePersistent : public RequestManagerImage public: ImagePersistent(): RequestManagerImage("ImagePersistent", - "Makes an image persistent or non-persistent"){}; + "Makes an image persistent or non-persistent", + "A:sib"){}; ~ImagePersistent(){}; @@ -79,6 +82,23 @@ public: RequestAttributes& att); }; +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class ImageChangeType : public RequestManagerImage +{ +public: + ImageChangeType(): + RequestManagerImage("ImageChangeType", + "Changes the type of an image", + "A:sis"){}; + + ~ImageChangeType(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/cli/oneimage b/src/cli/oneimage index 8bc019d045..a401b393cf 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -159,6 +159,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + chtype_desc = <<-EOT.unindent + Changes the Image group + EOT + + command :chtype, chtype_desc,[:range, :imageid_list], :type do + helper.perform_actions(args[0],options,"Type changed") do |image| + image.chtype(args[1]) + end + end + disable_desc = <<-EOT.unindent Disables the given Image EOT diff --git a/src/image/Image.cc b/src/image/Image.cc index 647860d6f9..09c0a9c387 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -103,8 +103,6 @@ int Image::insert(SqlDB *db, string& error_str) get_template_attribute("TYPE", type_att); - TO_UPPER(type_att); - if ( type_att.empty() == true ) { type_att = ImagePool::default_type(); @@ -524,3 +522,32 @@ int Image::disk_attribute( VectorAttribute * disk, /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ + +int Image::set_type(string& _type) +{ + int rc = 0; + + TO_UPPER(_type); + + if ( _type == "OS" ) + { + type = OS; + } + else if ( _type == "CDROM" ) + { + type = CDROM; + } + else if ( _type == "DATABLOCK" ) + { + type = DATABLOCK; + } + else + { + rc = -1; + } + + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ diff --git a/src/oca/java/src/org/opennebula/client/image/Image.java b/src/oca/java/src/org/opennebula/client/image/Image.java index 00d72212bf..0038ab4fcb 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -35,6 +35,7 @@ public class Image extends PoolElement private static final String ENABLE = METHOD_PREFIX + "enable"; private static final String PUBLISH = METHOD_PREFIX + "publish"; private static final String CHOWN = METHOD_PREFIX + "chown"; + private static final String CHTYPE = METHOD_PREFIX + "chtype"; private static final String[] IMAGE_STATES = {"INIT", "READY", "USED", "DISABLED"}; @@ -162,6 +163,19 @@ public class Image extends PoolElement return client.call(CHOWN, id, uid, gid); } + /** + * Changes the Image type + * + * @param client XML-RPC Client. + * @param id The image id of the target image we want to modify. + * @param type The new Image type + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse chtype(Client client, int id, String type) + { + return client.call(CHTYPE, id, type); + } + // ================================= // Instanced object XML-RPC methods // ================================= @@ -296,6 +310,17 @@ public class Image extends PoolElement return chown(-1, gid); } + /** + * Changes the Image type + * + * @param type The new Image type + * @return If an error occurs the error message contains the reason. + */ + public OneResponse chtype(String type) + { + return chtype(client, id, type); + } + // ================================= // Helpers // ================================= diff --git a/src/oca/ruby/OpenNebula/Image.rb b/src/oca/ruby/OpenNebula/Image.rb index 668add9c0b..9aa496fc17 100644 --- a/src/oca/ruby/OpenNebula/Image.rb +++ b/src/oca/ruby/OpenNebula/Image.rb @@ -33,7 +33,8 @@ module OpenNebula :publish => "image.publish", :persistent => "image.persistent", :delete => "image.delete", - :chown => "image.chown" + :chown => "image.chown", + :chtype => "image.chtype" } IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR} @@ -145,6 +146,19 @@ module OpenNebula super(IMAGE_METHODS[:chown], uid, gid) end + # Changes the Image type + # @param type [String] new Image type + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def chtype(type) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(IMAGE_METHODS[:chtype], @pe_id, type) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + ####################################################################### # Helpers to get Image information ####################################################################### diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index ba6aba6086..8b89ffc192 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -264,6 +264,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr template_publish(new TemplatePublish()); xmlrpc_c::methodPtr vn_publish(new VirtualNetworkPublish()); xmlrpc_c::methodPtr image_publish(new ImagePublish()); + xmlrpc_c::methodPtr image_chtype(new ImageChangeType()); // Delete Methods xmlrpc_c::methodPtr host_delete(new HostDelete()); @@ -383,6 +384,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.image.delete", image_delete); RequestManagerRegistry.addMethod("one.image.info", image_info); RequestManagerRegistry.addMethod("one.image.chown", image_chown); + RequestManagerRegistry.addMethod("one.image.chtype", image_chtype); RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info); diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index 0348ffa3a7..b3fe5d7479 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -112,3 +112,51 @@ void ImagePersistent::request_execute(xmlrpc_c::paramList const& paramList, success_response(id, att); } + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void ImageChangeType::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int id = xmlrpc_c::value_int(paramList.getInt(1)); + string type = xmlrpc_c::value_string(paramList.getString(2)); + int rc; + + Image * image; + string err_msg; + + if ( basic_authorization(id, att) == false ) + { + return; + } + + image = static_cast(pool->get(id,true)); + + if ( image == 0 ) + { + failure_response(NO_EXISTS, + get_error(object_name(auth_object),id), + att); + + return; + } + + rc = image->set_type(type); + + if ( rc != 0 ) + { + err_msg = "Unknown type " + type; + + failure_response(INTERNAL,request_error(err_msg,""), att); + + image->unlock(); + return; + } + + pool->update(image); + + image->unlock(); + + success_response(id, att); +} From ff0a469cb1a5bc80c2443f3e76147cb7aaffa283 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Wed, 26 Oct 2011 19:04:31 +0200 Subject: [PATCH 3/3] Task #933: Re-enable image type when doing save_as of a VM --- .../models/OpenNebulaJSON/VirtualMachineJSON.rb | 2 +- src/sunstone/public/js/plugins/vms-tab.js | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb index 40488754d6..d00c0efc32 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb @@ -88,7 +88,7 @@ module OpenNebulaJSON end def save_as(params=Hash.new) - super(params['disk_id'].to_i, params['image_name']) + super(params['disk_id'].to_i, params['image_name'], params['type']) end def chown(params=Hash.new) diff --git a/src/sunstone/public/js/plugins/vms-tab.js b/src/sunstone/public/js/plugins/vms-tab.js index e7b099b7ce..c3959d6d22 100644 --- a/src/sunstone/public/js/plugins/vms-tab.js +++ b/src/sunstone/public/js/plugins/vms-tab.js @@ -812,7 +812,7 @@ function setupSaveasDialog(){ var id = $('#vm_id',this).text(); var disk_id = $('#vm_disk_id',this).val(); var image_name = $('#image_name',this).val(); - //var type = $('#image_type',this).val(); + var type = $('#image_type',this).val(); if (!id.length || !disk_id.length || !image_name.length) { notifyError("Skipping VM "+id+ @@ -821,8 +821,8 @@ function setupSaveasDialog(){ else { var obj = { disk_id : disk_id, - image_name : image_name - //type: type + image_name : image_name, + type: type }; args.push(id); Sunstone.runAction("VM.saveas",id,obj); @@ -865,20 +865,15 @@ function popUpSaveasDialog(elems){ \ \ \ - \ \ '; $('#saveas_tabs',dialog).append(tab);