From b377e4db99b32d4f7e2e86d543617164198534f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 25 May 2012 12:56:51 +0200 Subject: [PATCH 01/15] Feature #1291: New one.image.clone method The core functionality is in place, but it is using the CP command. To make it work, the ds_map cp scripts need to ignore the check_restricted call. We will have to add another script to the ds_mad drivers, probably clone --- include/Image.h | 75 ++++++++++++++- include/ImagePool.h | 4 + include/ImageTemplate.h | 2 + include/PoolObjectSQL.h | 8 +- include/RequestManagerAllocate.h | 4 + include/RequestManagerImage.h | 26 ++++++ include/Template.h | 9 ++ src/image/Image.cc | 98 +++++++++++++++---- src/image/ImageManagerActions.cc | 44 ++++++++- src/image/ImageManagerDriver.cc | 37 ++++++++ src/image/ImagePool.cc | 6 ++ src/rm/RequestManager.cc | 2 + src/rm/RequestManagerAllocate.cc | 4 +- src/rm/RequestManagerImage.cc | 124 +++++++++++++++++++++++++ src/rm/RequestManagerVirtualMachine.cc | 1 + src/template/Template.cc | 14 +++ 16 files changed, 428 insertions(+), 30 deletions(-) diff --git a/include/Image.h b/include/Image.h index 3b4027b9bf..44052e7a0f 100644 --- a/include/Image.h +++ b/include/Image.h @@ -95,6 +95,25 @@ public: ERROR = 5 /** < Error state the operation FAILED*/ }; + /** + * Returns the string representation of an ImageState + * @param state The state + * @return the string representation + */ + static string state_to_str(ImageState state) + { + switch(state) + { + case INIT: return "INIT"; break; + case READY: return "READY"; break; + case USED: return "USED"; break; + case DISABLED: return "DISABLED"; break; + case LOCKED: return "LOCKED"; break; + case ERROR: return "ERROR"; break; + default: return ""; + } + }; + // ************************************************************************* // Image Public Methods // ************************************************************************* @@ -187,7 +206,7 @@ public: * Returns the image state * @return state of image */ - ImageState get_state() + ImageState get_state() const { return state; } @@ -225,6 +244,36 @@ public: return running_vms; } + int get_cloning() + { + return cloning_ops; + } + + int dec_cloning() + { + return --cloning_ops; + } + + int inc_cloning() + { + return ++cloning_ops; + } + + int get_source_img() + { + return source_img_id; + } + + void set_source_img(int id) + { + source_img_id = id; + } + + void unset_source_img() + { + source_img_id = -1; + } + /** * Sets the Image type. * @@ -365,7 +414,18 @@ public: { return ds_name; }; - + + /** + * Prepares a template to allocate a clone of this Image + * + * @param new_name Value for the NAME attribute + * @param tmpl Will contain the resulting template, if any + * @param error_str Returns the error reason, if any + * @return 0 in case of success, -1 otherwise + */ + int clone_template(string& new_name, + ImageTemplate * &tmpl, string& error_str) const; + private: // ------------------------------------------------------------------------- @@ -428,6 +488,17 @@ private: */ int running_vms; + /** + * Number of pending cloning operations + */ + int cloning_ops; + + /** + * Indicates if this Image is a clone of another one. + * Once the clone process is complete, it should be set to -1 + */ + int source_img_id; + /** * Datastore ID */ diff --git a/include/ImagePool.h b/include/ImagePool.h index 1bc30383cd..36713732a9 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -56,6 +56,9 @@ public: * @param ds_name the name of the datastore * @param ds_type disk type for the image * @param ds_data the datastore data + * @param source_img_id If the new Image is a clone, this must be the + * source Image ID. Otherwise, it must be set to -1 + * * @param oid the id assigned to the Image * @param error_str Returns the error reason, if any * @return the oid assigned to the object, @@ -72,6 +75,7 @@ public: const string& ds_name, Image::DiskType ds_type, const string& ds_data, + int source_img_id, int * oid, string& error_str); diff --git a/include/ImageTemplate.h b/include/ImageTemplate.h index 36c2727bb6..d6340432f0 100644 --- a/include/ImageTemplate.h +++ b/include/ImageTemplate.h @@ -29,6 +29,8 @@ class ImageTemplate : public Template public: ImageTemplate() : Template(true,'=',"TEMPLATE"){}; + ImageTemplate(ImageTemplate& tmpl):Template(tmpl){}; + ~ImageTemplate(){}; /** diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index bd4e72b449..7cba12669c 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -324,13 +324,7 @@ public: const string& name, const string& value) { - SingleAttribute * sattr = new SingleAttribute(name,value); - - obj_template->erase(sattr->name()); - - obj_template->set(sattr); - - return 0; + return obj_template->replace(name, value); } /** diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index e13bdbc42c..01161b6b3d 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -196,6 +196,7 @@ public: { Nebula& nd = Nebula::instance(); pool = nd.get_ipool(); + dspool = nd.get_dspool(); auth_object = PoolObjectSQL::IMAGE; }; @@ -209,6 +210,9 @@ public: bool allocate_authorization(Template * obj_template, RequestAttributes& att, PoolObjectAuth * cluster_perms); + +private: + DatastorePool * dspool; }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerImage.h b/include/RequestManagerImage.h index a6f6298bbd..a8e9bc4344 100644 --- a/include/RequestManagerImage.h +++ b/include/RequestManagerImage.h @@ -99,6 +99,32 @@ public: RequestAttributes& att); }; +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class ImageClone : public RequestManagerImage +{ +public: + ImageClone(): + RequestManagerImage("ImageClone", + "Clones an existing image", + "A:sis") + { + auth_op = AuthRequest::USE; + + Nebula& nd = Nebula::instance(); + dspool = nd.get_dspool(); + }; + + ~ImageClone(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + +private: + DatastorePool * dspool; +}; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/include/Template.h b/include/Template.h index ab4ab441fd..74203ad000 100644 --- a/include/Template.h +++ b/include/Template.h @@ -134,6 +134,15 @@ public: */ virtual void set(Attribute * attr); + /** + * Adds a new attribute to the template (replacing it if + * already defined) + * @param name of the new attribute + * @param value of the new attribute + * @return 0 on success + */ + int replace(const string& name, const string& value); + /** * Removes an attribute from the template. The attributes are returned. The * attributes MUST be freed by the calling funtion diff --git a/src/image/Image.cc b/src/image/Image.cc index b0eb1b53a9..7f647b2285 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -49,6 +49,8 @@ Image::Image(int _uid, size_mb(0), state(INIT), running_vms(0), + cloning_ops(0), + source_img_id(-1), ds_id(-1), ds_name("") { @@ -355,6 +357,8 @@ string& Image::to_xml(string& xml) const "" << size_mb << "" << "" << state << "" << "" << running_vms << "" << + "" << cloning_ops << "" << + "" << source_img_id << "" << "" << ds_id << ""<< "" << ds_name << "" << obj_template->to_xml(template_xml) << @@ -381,27 +385,29 @@ int Image::from_xml(const string& xml) update_from_str(xml); // Get class base attributes - rc += xpath(oid, "/IMAGE/ID", -1); - rc += xpath(uid, "/IMAGE/UID", -1); - rc += xpath(gid, "/IMAGE/GID", -1); + rc += xpath(oid, "/IMAGE/ID", -1); + rc += xpath(uid, "/IMAGE/UID", -1); + rc += xpath(gid, "/IMAGE/GID", -1); - rc += xpath(uname, "/IMAGE/UNAME", "not_found"); - rc += xpath(gname, "/IMAGE/GNAME", "not_found"); + rc += xpath(uname, "/IMAGE/UNAME", "not_found"); + rc += xpath(gname, "/IMAGE/GNAME", "not_found"); - rc += xpath(name, "/IMAGE/NAME", "not_found"); + rc += xpath(name, "/IMAGE/NAME", "not_found"); - rc += xpath(int_type, "/IMAGE/TYPE", 0); - rc += xpath(int_disk_type, "/IMAGE/DISK_TYPE", 0); - rc += xpath(persistent_img, "/IMAGE/PERSISTENT", 0); - rc += xpath(regtime, "/IMAGE/REGTIME", 0); + rc += xpath(int_type, "/IMAGE/TYPE", 0); + rc += xpath(int_disk_type, "/IMAGE/DISK_TYPE", 0); + rc += xpath(persistent_img, "/IMAGE/PERSISTENT",0); + rc += xpath(regtime, "/IMAGE/REGTIME", 0); - rc += xpath(source, "/IMAGE/SOURCE", "not_found"); - rc += xpath(size_mb, "/IMAGE/SIZE", 0); - rc += xpath(int_state, "/IMAGE/STATE", 0); - rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1); + rc += xpath(source, "/IMAGE/SOURCE", "not_found"); + rc += xpath(size_mb, "/IMAGE/SIZE", 0); + rc += xpath(int_state, "/IMAGE/STATE", 0); + rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1); + rc += xpath(cloning_ops, "/IMAGE/CLONING_OPS", -1); + rc += xpath(source_img_id, "/IMAGE/SOURCE_IMG", -1); - rc += xpath(ds_id, "/IMAGE/DATASTORE_ID", -1); - rc += xpath(ds_name,"/IMAGE/DATASTORE", "not_found"); + rc += xpath(ds_id, "/IMAGE/DATASTORE_ID", -1); + rc += xpath(ds_name, "/IMAGE/DATASTORE", "not_found"); // Permissions rc += perms_from_xml(); @@ -561,3 +567,63 @@ int Image::set_type(string& _type) /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ + +int Image::clone_template(string& new_name, + ImageTemplate * &tmpl, string& error_str) const +{ + if ( get_state() != READY ) + { + if ( isPersistent() ) + { + ostringstream oss; + + oss << "Image [" << oid << "] is in state " + << state_to_str( get_state() ) + << "; persistent Images can only be cloned in the " + << state_to_str( READY ) << " state."; + + error_str = oss.str(); + return -1; + } + else if ( get_state() != USED ) + { + ostringstream oss; + + oss << "Image [" << oid << "] is in state " + << state_to_str( get_state() ) + << "; non-persistent Images can only be cloned in the " + << state_to_str( READY ) << " or " + << state_to_str(USED) << " states."; + + error_str = oss.str(); + return -1; + } + } + + tmpl = new ImageTemplate( + *(static_cast(obj_template))); + + tmpl->replace("NAME", new_name); + tmpl->replace("TYPE", type_to_str(type)); + tmpl->replace("PATH", source); + tmpl->replace("FSTYPE", fs_type); + + ostringstream oss; + oss << size_mb; + + tmpl->replace("SIZE", oss.str()); + + if ( isPersistent() ) + { + tmpl->replace("PERSISTENT", "YES"); + } + else + { + tmpl->replace("PERSISTENT", "NO"); + } + + return 0; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ diff --git a/src/image/ImageManagerActions.cc b/src/image/ImageManagerActions.cc index 17f63c3f62..f370266cf4 100644 --- a/src/image/ImageManagerActions.cc +++ b/src/image/ImageManagerActions.cc @@ -159,7 +159,7 @@ void ImageManager::release_image(int iid, bool failed) img->set_state(Image::READY); } } - else if ( rvms == 0 ) + else if ( rvms == 0 && img->get_cloning() == 0 ) { img->set_state(Image::READY); } @@ -263,6 +263,7 @@ int ImageManager::delete_image(int iid, const string& ds_data) string source; string img_tmpl; string * drv_msg; + int source_id; img = ipool->get(iid,true); @@ -274,7 +275,7 @@ int ImageManager::delete_image(int iid, const string& ds_data) switch(img->get_state()) { case Image::READY: - if ( img->get_running() != 0 ) + if ( img->get_running() != 0 || img->get_cloning() > 0 ) { img->unlock(); return -1; //Cannot remove images in use @@ -293,6 +294,45 @@ int ImageManager::delete_image(int iid, const string& ds_data) break; } + source_id = img->get_source_img(); + + if ( source_id != -1 ) // An Image clone was in progress + { + Image * source_image; + + // Unlock to avoid death-locks with source_image + img->unlock(); + + source_image = ipool->get(source_id, true); + + if ( source_image != 0 ) + { + source_image->dec_cloning(); + + if ( source_image->get_state() == Image::USED && + source_image->get_running() == 0 && + source_image->get_cloning() == 0 ) + { + source_image->set_state(Image::READY); + } + + ipool->update(source_image); + source_image->unlock(); + } + + // Lock the image again + img = ipool->get(iid,true); + + if ( img == 0 ) + { + return -1; + } + + img->unset_source_img(); + + ipool->update(img); + } + const ImageManagerDriver* imd = get(); if ( imd == 0 ) diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index 43eae93c5a..7aaa002567 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -163,6 +163,43 @@ void ImageManagerDriver::protocol( // Driver Actions if ( action == "CP" ) { + int source_id = image->get_source_img(); + + if ( source_id != -1 ) // An Image clone operation finished + { + Image * source_image; + + // Unlock to avoid death-locks with source_image + image->unlock(); + + source_image = ipool->get(source_id, true); + + if ( source_image != 0 ) + { + source_image->dec_cloning(); + + if ( source_image->get_state() == Image::USED && + source_image->get_running() == 0 && + source_image->get_cloning() == 0 ) + { + source_image->set_state(Image::READY); + } + + ipool->update(source_image); + source_image->unlock(); + } + + // Lock the image again + image = ipool->get(id,true); + + if ( image == 0 ) + { + return; + } + + image->unset_source_img(); + } + if ( result == "SUCCESS" ) { image->set_source(source); diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 251c037a0e..560c786e5b 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -69,6 +69,7 @@ int ImagePool::allocate ( const string& ds_name, Image::DiskType disk_type, const string& ds_data, + int source_img_id, int * oid, string& error_str) { @@ -106,6 +107,11 @@ int ImagePool::allocate ( img->disk_type = disk_type; + if ( source_img_id != -1 ) + { + img->set_source_img(source_img_id); + } + // --------------------------------------------------------------------- // Insert the Object in the pool & Register the image in the repository // --------------------------------------------------------------------- diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index e2413c867c..f69edcc164 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -309,6 +309,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr image_persistent(new ImagePersistent()); xmlrpc_c::methodPtr image_enable(new ImageEnable()); xmlrpc_c::methodPtr image_chtype(new ImageChangeType()); + xmlrpc_c::methodPtr image_clone(new ImageClone()); // Chown Methods xmlrpc_c::methodPtr vm_chown(new VirtualMachineChown()); @@ -414,6 +415,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.image.chown", image_chown); RequestManagerRegistry.addMethod("one.image.chmod", image_chmod); RequestManagerRegistry.addMethod("one.image.chtype", image_chtype); + RequestManagerRegistry.addMethod("one.image.clone", image_clone); RequestManagerRegistry.addMethod("one.imagepool.info", imagepool_info); diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 8d7221c1cd..23faf9c0a7 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -306,9 +306,6 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params, string str_tmpl = xmlrpc_c::value_string(params.getString(1)); int ds_id = xmlrpc_c::value_int(params.getInt(2)); - Nebula& nd = Nebula::instance(); - - DatastorePool * dspool = nd.get_dspool(); ImagePool * ipool = static_cast(pool); ImageTemplate * tmpl = new ImageTemplate; @@ -392,6 +389,7 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params, ds_name, ds_disk_type, ds_data, + -1, &id, error_str); if ( rc < 0 ) diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index 206e0aec34..bf25072254 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -160,3 +160,127 @@ void ImageChangeType::request_execute(xmlrpc_c::paramList const& paramList, success_response(id, att); } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void ImageClone::request_execute( + xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int source_id = xmlrpc_c::value_int(paramList.getInt(1)); + string name = xmlrpc_c::value_string(paramList.getString(2)); + + int rc, new_id, ds_id; + string error_str, ds_name, ds_data; + + Image::DiskType disk_type; + PoolObjectAuth perms, ds_perms; + ImageTemplate * tmpl = 0; + Image * source_img; + Datastore * ds; + + ImagePool * ipool = static_cast(pool); + + // ------------------------- Get source Image info ------------------------- + + source_img = ipool->get(source_id, true); + + if ( source_img == 0 ) + { + failure_response(NO_EXISTS, + get_error(object_name(auth_object),source_id), + att); + + return; + } + + rc = source_img->clone_template(name, tmpl, error_str); + + if ( rc != 0 ) + { + source_img->unlock(); + + failure_response(INTERNAL, + request_error("Could not clone Image",error_str), + att); + + return; + } + + source_img->get_permissions(perms); + + ds_id = source_img->get_ds_id(); + ds_name = source_img->get_ds_name(); + + source_img->unlock(); + + // ------------------------- Get Datastore info ---------------------------- + + ds = dspool->get(ds_id, true); + if ( ds == 0 ) + { + failure_response(NO_EXISTS, + get_error(object_name(PoolObjectSQL::DATASTORE), ds_id), + att); + + delete tmpl; + return; + } + + ds->get_permissions(ds_perms); + disk_type = ds->get_disk_type(); + ds->to_xml(ds_data); + + ds->unlock(); + + // ------------- Set authorization request --------------------------------- + + if ( att.uid != 0 ) + { + AuthRequest ar(att.uid, att.gid); + string tmpl_str = ""; + + tmpl->to_xml(tmpl_str); + + ar.add_create_auth(auth_object, tmpl_str); // CREATE IMAGE + + ar.add_auth(AuthRequest::USE, ds_perms); // USE DATASTORE + + if (UserPool::authorize(ar) == -1) + { + failure_response(AUTHORIZATION, + authorization_error(ar.message, att), + att); + + delete tmpl; + return; + } + } + + rc = ipool->allocate(att.uid, att.gid, att.uname, att.gname, + tmpl, ds_id, ds_name, disk_type, + ds_data, source_id, &new_id, error_str); + + if ( rc < 0 ) + { + failure_response(INTERNAL, allocate_error(error_str), att); + return; + } + + source_img = ipool->get(source_id, true); + + if ( source_img != 0 ) + { + source_img->inc_cloning(); + source_img->set_state(Image::USED); + + ipool->update(source_img); + + source_img->unlock(); + } + + success_response(new_id, att); +} + + diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index fbfdcc102d..061ddb45c5 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -562,6 +562,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis ds_name, ds_disk_type, ds_data, + -1, &iid, error_str); if (rc < 0) diff --git a/src/template/Template.cc b/src/template/Template.cc index 18227d4af6..acc0487a39 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -200,6 +200,7 @@ void Template::marshall(string &str, const char delim) delete attr; } } + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -227,6 +228,19 @@ void Template::set(Attribute * attr) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int Template::replace(const string& name, const string& value) +{ + SingleAttribute * sattr = new SingleAttribute(name,value); + + erase(name); + set(sattr); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int Template::remove(const string& name, vector& values) { multimap::iterator i; From 226e5dff0fbff1572d04d2997295f84beccee75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 25 May 2012 13:01:17 +0200 Subject: [PATCH 02/15] Feature #1291: Ruby OCA and CLI methods for image clone --- src/cli/oneimage | 16 ++++++++++++++++ src/cli/onetemplate | 2 ++ src/oca/ruby/OpenNebula/Image.rb | 17 ++++++++++++++++- src/oca/ruby/OpenNebula/Template.rb | 6 +++--- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/cli/oneimage b/src/cli/oneimage index e461ae1371..3c23eb5b29 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -96,6 +96,22 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + clone_desc = <<-EOT.unindent + Creates a new Image from an existing one + EOT + + command :clone, clone_desc, :imageid, :name do + helper.perform_action(args[0],options,"cloned") do |image| + res = image.clone(args[1]) + + if !OpenNebula.is_error?(res) + puts "ID: #{res}" + else + puts res.message + end + end + end + delete_desc = <<-EOT.unindent Deletes the given Image EOT diff --git a/src/cli/onetemplate b/src/cli/onetemplate index 47a53b601d..6ece44f634 100755 --- a/src/cli/onetemplate +++ b/src/cli/onetemplate @@ -98,6 +98,8 @@ cmd=CommandParser::CmdParser.new(ARGV) do if !OpenNebula.is_error?(res) puts "ID: #{res}" + else + puts res.message end end end diff --git a/src/oca/ruby/OpenNebula/Image.rb b/src/oca/ruby/OpenNebula/Image.rb index 878115ef3d..5f80c89ee3 100644 --- a/src/oca/ruby/OpenNebula/Image.rb +++ b/src/oca/ruby/OpenNebula/Image.rb @@ -34,7 +34,8 @@ module OpenNebula :delete => "image.delete", :chown => "image.chown", :chmod => "image.chmod", - :chtype => "image.chtype" + :chtype => "image.chtype", + :clone => "image.clone" } IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR} @@ -183,6 +184,20 @@ module OpenNebula return rc end + # Clones this Image into a new one + # + # @param [String] name for the new Image. + # + # @return [Integer, OpenNebula::Error] The new Image ID in case + # of success, Error otherwise + def clone(name) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(IMAGE_METHODS[:clone], @pe_id, name) + + return rc + end + ####################################################################### # Helpers to get Image information ####################################################################### diff --git a/src/oca/ruby/OpenNebula/Template.rb b/src/oca/ruby/OpenNebula/Template.rb index 97b3e75d8e..f529e4ee1f 100644 --- a/src/oca/ruby/OpenNebula/Template.rb +++ b/src/oca/ruby/OpenNebula/Template.rb @@ -144,12 +144,12 @@ module OpenNebula group_m, group_a, other_u, other_m, other_a) end - # Clones this template into a new one + # Clones this Template into a new one # - # @param name [String] Name for the new Template. + # @param [String] name for the new Template. # # @return [Integer, OpenNebula::Error] The new Template ID in case - # of success, Error otherwise + # of success, Error otherwise def clone(name) return Error.new('ID not defined') if !@pe_id From 45cdb8da9e7c6d2c6922065bd44365a55719d055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 25 May 2012 13:05:52 +0200 Subject: [PATCH 03/15] Feature #1291: Add new Image attributes for image cloning to onedb migrator --- src/onedb/3.4.1_to_3.5.80.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/onedb/3.4.1_to_3.5.80.rb b/src/onedb/3.4.1_to_3.5.80.rb index 42ebf9e3e6..ad2786549c 100644 --- a/src/onedb/3.4.1_to_3.5.80.rb +++ b/src/onedb/3.4.1_to_3.5.80.rb @@ -62,6 +62,8 @@ module Migrator } doc.root.add_element("DISK_TYPE").text = "0" + doc.root.add_element("SOURCE_IMG").text = "-1" + doc.root.add_element("CLONING_OPS").text = "0" @db[:image_pool].insert( :oid => row[:oid], From 21025051c8388137384ab4c9abd1e423cc763290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 25 May 2012 14:49:32 +0200 Subject: [PATCH 04/15] Feature #1291: Allow multiple clone operations for persistent images --- src/image/Image.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/image/Image.cc b/src/image/Image.cc index 7f647b2285..f138cc53da 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -358,7 +358,7 @@ string& Image::to_xml(string& xml) const "" << state << "" << "" << running_vms << "" << "" << cloning_ops << "" << - "" << source_img_id << "" << + "" << source_img_id << "" << "" << ds_id << ""<< "" << ds_name << "" << obj_template->to_xml(template_xml) << @@ -573,7 +573,9 @@ int Image::clone_template(string& new_name, { if ( get_state() != READY ) { - if ( isPersistent() ) + // Persistent Images can have several simultaneous clone operations, + // but not if there is a VM using it. + if ( isPersistent() && (get_state() != USED || running_vms > 0) ) { ostringstream oss; From 4ecb7598d819ecf9f98a24795b28b13e079aa685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 31 May 2012 17:03:19 +0200 Subject: [PATCH 05/15] Feature #1291: Update the Java OCA to the latest changes. Fixes Bug #1294 --- .../client/cluster/ClusterPool.java | 2 + .../client/datastore/Datastore.java | 34 ++++-- .../src/org/opennebula/client/host/Host.java | 24 +--- .../org/opennebula/client/image/Image.java | 109 +++++++++++++----- .../opennebula/client/vm/VirtualMachine.java | 4 +- 5 files changed, 117 insertions(+), 56 deletions(-) diff --git a/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java b/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java index 49e25f419b..403f7d86ba 100644 --- a/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java +++ b/src/oca/java/src/org/opennebula/client/cluster/ClusterPool.java @@ -31,6 +31,8 @@ import org.w3c.dom.Node; */ public class ClusterPool extends Pool implements Iterable{ + public static final int NONE_CLUSTER_ID = -1; + private static final String ELEMENT_NAME = "CLUSTER"; private static final String INFO_METHOD = "clusterpool.info"; diff --git a/src/oca/java/src/org/opennebula/client/datastore/Datastore.java b/src/oca/java/src/org/opennebula/client/datastore/Datastore.java index 4604c5d960..7665af9853 100644 --- a/src/oca/java/src/org/opennebula/client/datastore/Datastore.java +++ b/src/oca/java/src/org/opennebula/client/datastore/Datastore.java @@ -18,6 +18,7 @@ package org.opennebula.client.datastore; import org.opennebula.client.Client; import org.opennebula.client.OneResponse; import org.opennebula.client.PoolElement; +import org.opennebula.client.cluster.ClusterPool; import org.w3c.dom.Node; /** @@ -57,6 +58,21 @@ public class Datastore extends PoolElement // Static XML-RPC methods // ================================= + /** + * Allocates a new Datastore in OpenNebula. + * + * @param client XML-RPC Client. + * @param description A string containing the template of the datastore. + * @param clusterId Id of the cluster + * @return If successful the message contains the associated + * id generated for this Datastore. + */ + public static OneResponse allocate(Client client, + String description, int clusterId) + { + return client.call(ALLOCATE, description, clusterId); + } + /** * Allocates a new Datastore in OpenNebula. * @@ -67,7 +83,7 @@ public class Datastore extends PoolElement */ public static OneResponse allocate(Client client, String description) { - return client.call(ALLOCATE, description); + return allocate(client, description, ClusterPool.NONE_CLUSTER_ID); } /** @@ -125,7 +141,7 @@ public class Datastore extends PoolElement /** * Changes the owner/group - * + * * @param client XML-RPC Client. * @param id The id of the target datastore we want to modify. * @param uid The new owner user ID. Set it to -1 to leave the current one. @@ -139,7 +155,7 @@ public class Datastore extends PoolElement /** * Changes the datastore permissions - * + * * @param client XML-RPC Client. * @param id The id of the target datastore. * @param owner_u 1 to allow, 0 deny, -1 do not change @@ -166,7 +182,7 @@ public class Datastore extends PoolElement /** * Changes the permissions - * + * * @param client XML-RPC Client. * @param id The id of the target object. * @param octet Permissions octet, e.g. 640 @@ -179,7 +195,7 @@ public class Datastore extends PoolElement /** * Changes the permissions - * + * * @param client XML-RPC Client. * @param id The id of the target object. * @param octet Permissions octed , e.g. 640 @@ -261,7 +277,7 @@ public class Datastore extends PoolElement /** * Changes the owner/group - * + * * @param uid The new owner user ID. Set it to -1 to leave the current one. * @param gid The new group ID. Set it to -1 to leave the current one. * @return If an error occurs the error message contains the reason. @@ -273,7 +289,7 @@ public class Datastore extends PoolElement /** * Changes the owner - * + * * @param uid The new owner user ID. * @return If an error occurs the error message contains the reason. */ @@ -284,7 +300,7 @@ public class Datastore extends PoolElement /** * Changes the group - * + * * @param gid The new group ID. * @return If an error occurs the error message contains the reason. */ @@ -295,7 +311,7 @@ public class Datastore extends PoolElement /** * Changes the datastore permissions - * + * * @param owner_u 1 to allow, 0 deny, -1 do not change * @param owner_m 1 to allow, 0 deny, -1 do not change * @param owner_a 1 to allow, 0 deny, -1 do not change diff --git a/src/oca/java/src/org/opennebula/client/host/Host.java b/src/oca/java/src/org/opennebula/client/host/Host.java index facfcc57c7..5b3c890b3f 100644 --- a/src/oca/java/src/org/opennebula/client/host/Host.java +++ b/src/oca/java/src/org/opennebula/client/host/Host.java @@ -35,9 +35,11 @@ public class Host extends PoolElement{ private static final String UPDATE = METHOD_PREFIX + "update"; private static final String[] HOST_STATES = - {"INIT", "MONITORING_MONITORED", "MONITORED", "ERROR", "DISABLED", + {"INIT", "MONITORING_MONITORED", "MONITORED", "ERROR", "DISABLED", "MONITORING_ERROR"}; + private static final String[] SHORT_HOST_STATES = + {"init", "update", "on", "err", "off", "retry"}; /** * Creates a new Host representation. @@ -265,24 +267,8 @@ public class Host extends PoolElement{ */ public String shortStateStr() { - String st = stateStr(); - - if(st == null) - return null; - else if(st.equals("ERROR")) - return "err"; - else if (st.equals("DISABLED")) - return "off"; - else if (st.equals("INIT")) - return "init"; - else if (st.equals("MONITORING_MONITORED")) - return "update"; - else if (st.equals("MONITORED")) - return "on"; - else if (st.equals("MONITORING_ERROR")) - return "retry"; - - return ""; + int state = state(); + return state != -1 ? SHORT_HOST_STATES[state()] : null; } /** 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 281af821b3..8ddca901ec 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -28,20 +28,22 @@ public class Image extends PoolElement { private static final String METHOD_PREFIX = "image."; - private static final String ALLOCATE = METHOD_PREFIX + "allocate"; - private static final String INFO = METHOD_PREFIX + "info"; - private static final String DELETE = METHOD_PREFIX + "delete"; - private static final String UPDATE = METHOD_PREFIX + "update"; - private static final String ENABLE = METHOD_PREFIX + "enable"; - private static final String CHOWN = METHOD_PREFIX + "chown"; - private static final String CHMOD = METHOD_PREFIX + "chmod"; - private static final String CHTYPE = METHOD_PREFIX + "chtype"; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String INFO = METHOD_PREFIX + "info"; + private static final String DELETE = METHOD_PREFIX + "delete"; + private static final String UPDATE = METHOD_PREFIX + "update"; + private static final String ENABLE = METHOD_PREFIX + "enable"; + private static final String PERSISTENT = METHOD_PREFIX + "persistent"; + private static final String CHOWN = METHOD_PREFIX + "chown"; + private static final String CHMOD = METHOD_PREFIX + "chmod"; + private static final String CHTYPE = METHOD_PREFIX + "chtype"; + private static final String CLONE = METHOD_PREFIX + "clone"; private static final String[] IMAGE_STATES = - {"INIT", "READY", "USED", "DISABLED"}; + {"INIT", "READY", "USED", "DISABLED", "LOCKED", "ERROR"}; private static final String[] SHORT_IMAGE_STATES = - {"init", "rdy", "used", "disa"}; + {"init", "rdy", "used", "disa", "lock", "err"}; private static final String[] IMAGE_TYPES = {"OS", "CDROM", "DATABLOCK"}; @@ -76,7 +78,7 @@ public class Image extends PoolElement * * @param client XML-RPC Client. * @param description A string containing the template of the image. - * @param clusterId The cluster ID. If it is -1, this image + * @param datastoreId The cluster ID. If it is -1, this image * won't be added to any cluster. * * @return If successful the message contains the associated @@ -85,22 +87,9 @@ public class Image extends PoolElement public static OneResponse allocate( Client client, String description, - int clusterId) + int datastoreId) { - return client.call(ALLOCATE, description, clusterId); - } - - /** - * Allocates a new Image in OpenNebula. - * - * @param client XML-RPC Client. - * @param description A string containing the template of the image. - * @return If successful the message contains the associated - * id generated for this Image. - */ - public static OneResponse allocate(Client client, String description) - { - return allocate(client, description, -1); + return client.call(ALLOCATE, description, datastoreId); } /** @@ -154,6 +143,19 @@ public class Image extends PoolElement return client.call(ENABLE, id, enable); } + /** + * Sets the Image as persistent or not persistent. + * + * @param client XML-RPC Client. + * @param id The image id of the target image we want to modify. + * @param persistent True to make it persistent, false non-persistent + * @return If successful the message contains the image id. + */ + public static OneResponse persistent(Client client, int id, boolean persistent) + { + return client.call(PERSISTENT, id, persistent); + } + /** * Publishes or unpublishes an image. * @@ -249,6 +251,19 @@ public class Image extends PoolElement return client.call(CHTYPE, id, type); } + /** + * Clones this Image into a new one + * + * @param client XML-RPC Client. + * @param id The Image id of the target Image. + * @param name Name for the new Image. + * @return If successful the message contains the new Image ID. + */ + public static OneResponse clone(Client client, int id, String name) + { + return client.call(CLONE, id, name); + } + // ================================= // Instanced object XML-RPC methods // ================================= @@ -318,6 +333,37 @@ public class Image extends PoolElement return enable(false); } + /** + * Sets the Image as persistent or not persistent. + * + * @param persistent True for enabling, false for disabling. + * @return If successful the message contains the image id. + */ + public OneResponse persistent(boolean persistent) + { + return persistent(client, id, persistent); + } + + /** + * Sets the Image as persistent + * + * @return If successful the message contains the image id. + */ + public OneResponse persistent() + { + return persistent(true); + } + + /** + * Sets the Image as persistent or not persistent. + * + * @return If successful the message contains the image id. + */ + public OneResponse nonpersistent() + { + return persistent(false); + } + /** * Publishes or unpublishes the image. * @@ -440,6 +486,17 @@ public class Image extends PoolElement return chtype(client, id, type); } + /** + * Clones this Image into a new one + * + * @param name Name for the new Image. + * @return If successful the message contains the new Image ID. + */ + public OneResponse clone(String name) + { + return clone(client, id, name); + } + // ================================= // Helpers // ================================= 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 bec5a7bafa..cf8f4cc4f9 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -96,7 +96,7 @@ public class VirtualMachine extends PoolElement{ "shut", "shut", "fail", - "dele", + "clea", "unkn" }; /** @@ -498,7 +498,7 @@ public class VirtualMachine extends PoolElement{ */ public OneResponse restart() { - return action("shutdown"); + return action("restart"); } /** From 1e638efccc2d97866abf46426c6119b966304ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 1 Jun 2012 12:30:52 +0200 Subject: [PATCH 06/15] Feature #1291: Update Java OCA Image test --- src/oca/java/test/ImageTest.java | 113 ++++++++++++++++++++++--------- src/oca/java/test/oned.conf | 13 ++-- 2 files changed, 88 insertions(+), 38 deletions(-) diff --git a/src/oca/java/test/ImageTest.java b/src/oca/java/test/ImageTest.java index 0e56d01529..2980c32ed3 100644 --- a/src/oca/java/test/ImageTest.java +++ b/src/oca/java/test/ImageTest.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,6 +22,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.opennebula.client.Client; import org.opennebula.client.OneResponse; +import org.opennebula.client.datastore.Datastore; import org.opennebula.client.image.*; @@ -47,6 +48,33 @@ public class ImageTest "ATT2 = \"VAL2\""; } + /** + * Wait until the Image changes to the specified state. + * There is a time-out of 10 seconds. + */ + void waitAssert(Image img, String state) + { + int n_steps = 10; + int step = 1000; + + int i = 0; + + img.info(); + + while( !( img.stateString().equals(state) || i > n_steps ) + && !img.stateString().equals("ERROR") ) + { + try{ Thread.sleep(step); } catch (Exception e){} + + img.info(); + i++; + } + + assertTrue( + "Image timeout, wanted state: " + state + "; it is in state " + + img.stateString(), img.stateString().equals(state) ); + } + /** * @throws java.lang.Exception */ @@ -71,10 +99,12 @@ public class ImageTest @Before public void setUp() throws Exception { - res = Image.allocate(client, template()); + res = Image.allocate(client, template(), 1); - int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); image = new Image(imgid, client); + + waitAssert(image, "READY"); } /** @@ -91,15 +121,15 @@ public class ImageTest { image.delete(); - res = Image.allocate(client, template()); - assertTrue( !res.isError() ); + res = Image.allocate(client, template(), 1); + assertTrue( res.getErrorMessage(), !res.isError() ); - int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); image = new Image(imgid, client); res = imagePool.info(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); boolean found = false; for(Image img : imagePool) @@ -110,12 +140,37 @@ public class ImageTest assertTrue( found ); } + @Test + public void clone_method() + { + res = image.clone("cloned_image"); + assertTrue(res.getErrorMessage(), !res.isError()); + + int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + Image cloned_image = new Image(imgid, client); + + res = imagePool.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + boolean found = false; + for(Image img : imagePool) + { + found = found || img.getName().equals("cloned_image"); + } + + assertTrue( found ); + + res = cloned_image.info(); + assertTrue( res.getErrorMessage(), !res.isError() ); + assertTrue( cloned_image.getName().equals("cloned_image") ); + } + @Test public void info() { res = image.info(); - assertTrue( !res.isError() ); - + assertTrue( res.getErrorMessage(), !res.isError() ); + // assertTrue( image.getId().equals("0") ); // assertTrue( image.id() == 0 ); assertTrue( image.getName().equals("test_img_"+cont) ); @@ -125,7 +180,7 @@ public class ImageTest public void update() { res = image.info(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); assertTrue( image.xpath("TEMPLATE/ATT1").equals( "VAL1" ) ); assertTrue( image.xpath("TEMPLATE/ATT2").equals( "VAL2" ) ); @@ -134,65 +189,61 @@ public class ImageTest "ATT3 = VAL3"; res = image.update(new_template); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); res = image.info(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); assertTrue( image.xpath("TEMPLATE/ATT1").equals( "" ) ); assertTrue( image.xpath("TEMPLATE/ATT2").equals( "NEW_VAL" ) ); assertTrue( image.xpath("TEMPLATE/ATT3").equals( "VAL3" ) ); } - -// TODO -// @Test + @Test public void enable() { res = image.enable(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); image.info(); assertTrue( image.isEnabled() ); } -// TODO -// @Test + @Test public void disable() { res = image.disable(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); image.info(); - assertTrue( !image.isEnabled() ); + assertTrue( res.getErrorMessage(), !image.isEnabled() ); } -// TODO -/* + @Test public void publish() { res = image.publish(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); image.info(); - assertTrue( image.isPublic() ); + assertTrue( image.xpath("PERMISSIONS/GROUP_U").equals( "1" ) ); } @Test public void unpublish() { res = image.unpublish(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); image.info(); - assertTrue( !image.isPublic() ); + assertTrue( image.xpath("PERMISSIONS/GROUP_U").equals( "0" ) ); } -*/ + @Test public void attributes() { res = image.info(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); // assertTrue( image.xpath("ID").equals("0") ); assertTrue( image.xpath("NAME").equals("test_img_"+cont) ); @@ -202,7 +253,7 @@ public class ImageTest public void delete() { res = image.delete(); - assertTrue( !res.isError() ); + assertTrue( res.getErrorMessage(), !res.isError() ); // res = image.info(); // assertTrue( res.isError() ); diff --git a/src/oca/java/test/oned.conf b/src/oca/java/test/oned.conf index 82fbf924f0..a2cc5ac529 100644 --- a/src/oca/java/test/oned.conf +++ b/src/oca/java/test/oned.conf @@ -298,7 +298,7 @@ TM_MAD = [ DATASTORE_MAD = [ executable = "one_datastore", - arguments = "-t 15 -d fs,vmware,iscsi,dummy" + arguments = "-t 15 -d fs,vmware,iscsi,lvm" ] #******************************************************************************* @@ -406,10 +406,9 @@ HM_MAD = [ # absolute path or relative to $ONE_LOCATION/lib/mads (or # /usr/lib/one/mads/ if OpenNebula was installed in /) # -# arguments : -# --authn: list of authentication modules separated by commas, if not -# defined all the modules available will be enabled -# --authz: authorization module +# authn : list of authentication modules separated by commas, if not +# defined all the modules available will be enabled +# authz : list of authentication modules separated by commas # # SESSION_EXPIRATION_TIME: Time in seconds to keep an authenticated token as # valid. During this time, the driver is not used. Use 0 to disable session @@ -422,8 +421,8 @@ HM_MAD = [ AUTH_MAD = [ executable = "one_auth_mad", - arguments = "--authn ssh,x509,ldap,server_cipher,server_x509" -# arguments = "--authz quota --authn ssh,x509,ldap,server_cipher,server_x509" + authn = "ssh,x509,ldap,server_cipher,server_x509" +# , authz = "quota" ] SESSION_EXPIRATION_TIME = 900 From 29366ca7f2b4d4638d756bafb5508a895b61e95a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 13 Jun 2012 00:32:01 +0200 Subject: [PATCH 07/15] feature #1291: Clone operation in ImageDriver. Clone logic moved to ImageManager. Added missing add_image for saveas and clone operations --- include/ImageManager.h | 13 ++++ include/ImageManagerDriver.h | 12 +-- include/ImagePool.h | 5 +- src/image/Image.cc | 2 +- src/image/ImageManagerActions.cc | 103 ++++++++++++++++++++++++- src/image/ImageManagerDriver.cc | 11 +++ src/image/ImagePool.cc | 24 ++++-- src/rm/RequestManagerImage.cc | 91 +++++++++++++--------- src/rm/RequestManagerVirtualMachine.cc | 11 +++ 9 files changed, 218 insertions(+), 54 deletions(-) diff --git a/include/ImageManager.h b/include/ImageManager.h index 7556d03880..c4921bfbee 100644 --- a/include/ImageManager.h +++ b/include/ImageManager.h @@ -125,6 +125,19 @@ public: */ int register_image(int iid, const string& ds_data); + /** + * Clone an existing image to the repository + * @param new_id of the new image + * @param cloning_id of the image to be cloned + * @param ds_data data of the associated datastore in XML format + * @param error describing the error + * @return 0 on success + */ + int clone_image(int new_id, + int cloning_id, + const string& ds_data, + string& error); + /** * Deletes an image from the repository and the DB * @param iid id of image diff --git a/include/ImageManagerDriver.h b/include/ImageManagerDriver.h index 8ac43a2878..1fcbe56641 100644 --- a/include/ImageManagerDriver.h +++ b/include/ImageManagerDriver.h @@ -65,11 +65,6 @@ private: */ ImagePool * ipool; - /** - * Configuration file for the driver - */ - //Template driver_conf; - /** * Sends a copy request to the MAD. * @param oid the image id. @@ -77,6 +72,13 @@ private: */ void cp(int oid, const string& drv_msg) const; + /** + * Sends a clone request to the MAD. + * @param oid the image id. + * @param drv_msg xml data for the mad operation. + */ + void clone(int oid, const string& drv_msg) const; + /** * Sends a stat request to the MAD. * @param oid the id of the stat request diff --git a/include/ImagePool.h b/include/ImagePool.h index 36713732a9..6dfad77182 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -57,8 +57,7 @@ public: * @param ds_type disk type for the image * @param ds_data the datastore data * @param source_img_id If the new Image is a clone, this must be the - * source Image ID. Otherwise, it must be set to -1 - * + * source Image ID. Otherwise, it must be set to -1 * @param oid the id assigned to the Image * @param error_str Returns the error reason, if any * @return the oid assigned to the object, @@ -75,7 +74,7 @@ public: const string& ds_name, Image::DiskType ds_type, const string& ds_data, - int source_img_id, + int source_img_id, int * oid, string& error_str); diff --git a/src/image/Image.cc b/src/image/Image.cc index 45a57e7177..813e74a639 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -568,7 +568,7 @@ ImageTemplate * Image::clone_template(const string& new_name) const tmpl->replace("PERSISTENT", "NO"); } - return 0; + return tmpl; } /* ------------------------------------------------------------------------ */ diff --git a/src/image/ImageManagerActions.cc b/src/image/ImageManagerActions.cc index 84909e1775..29326f90b9 100644 --- a/src/image/ImageManagerActions.cc +++ b/src/image/ImageManagerActions.cc @@ -457,7 +457,7 @@ int ImageManager::delete_image(int iid, const string& ds_data) /* --------------- Update cloning image if needed ----------------------- */ - if ( cloning_id != -1 ) // An Image clone was in progress + if ( cloning_id != -1 ) { release_cloning_image(cloning_id); } @@ -467,6 +467,107 @@ int ImageManager::delete_image(int iid, const string& ds_data) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int ImageManager::clone_image(int new_id, + int cloning_id, + const string& ds_data, + string& error) +{ + const ImageManagerDriver* imd = get(); + + ostringstream oss; + Image * img; + + string path; + string img_tmpl; + string* drv_msg; + + if ( imd == 0 ) + { + error = "Could not get datastore driver"; + + NebulaLog::log("ImM", Log::ERROR, error); + return -1; + } + + img = ipool->get(cloning_id, true); + + if (img == 0) + { + error = "Cannot clone image, it does not exist"; + return -1; + } + + switch(img->get_state()) + { + case Image::READY: + img->inc_cloning(); + + if (img->isPersistent()) + { + img->set_state(Image::CLONE); + } + else + { + img->set_state(Image::USED); + } + + ipool->update(img); + + img->unlock(); + break; + + case Image::USED_PERS: + img->inc_cloning(); + + ipool->update(img); + + img->unlock(); + break; + + case Image::USED: + case Image::CLONE: + case Image::INIT: + case Image::DISABLED: + case Image::ERROR: + case Image::DELETE: + case Image::LOCKED: + ostringstream oss; + oss << "Cannot clone image in state: " + << Image::state_to_str(img->get_state()); + + error = oss.str(); + img->unlock(); + return -1; + break; + } + + img = ipool->get(new_id,true); + + if (img == 0) //TODO: Rollback clonning counter + { + error = "Target image deleted during clonning operation"; + return -1; + } + + drv_msg = format_message(img->to_xml(img_tmpl), ds_data); + + imd->clone(img->get_oid(), *drv_msg); + + oss << "Cloning image " << img->get_path() + <<" to repository as image "<get_oid(); + + NebulaLog::log("ImM", Log::INFO, oss); + + img->unlock(); + + delete drv_msg; + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int ImageManager::register_image(int iid, const string& ds_data) { const ImageManagerDriver* imd = get(); diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index be1a53298b..814c668ae7 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -38,6 +38,17 @@ void ImageManagerDriver::cp(int oid, /* -------------------------------------------------------------------------- */ +void ImageManagerDriver::clone(int oid, + const string& drv_msg) const +{ + ostringstream os; + + os << "CLONE " << oid << " " << drv_msg << endl; + + write(os); +} + +/* -------------------------------------------------------------------------- */ void ImageManagerDriver::stat(int oid, const string& drv_msg) const { diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 7759308705..3370cdcb8a 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -69,7 +69,7 @@ int ImagePool::allocate ( const string& ds_name, Image::DiskType disk_type, const string& ds_data, - int source_img_id, + int cloning_id, int * oid, string& error_str) { @@ -107,9 +107,9 @@ int ImagePool::allocate ( img->disk_type = disk_type; - if ( source_img_id != -1 ) + if ( cloning_id != -1 ) { - img->set_cloning_id(source_img_id); + img->set_cloning_id(cloning_id); } // --------------------------------------------------------------------- @@ -122,11 +122,21 @@ int ImagePool::allocate ( Nebula& nd = Nebula::instance(); ImageManager * imagem = nd.get_imagem(); - if ( imagem->register_image(*oid, ds_data) == -1 ) + if (cloning_id != -1) { - error_str = "Failed to copy image to repository. " - "Image left in ERROR state."; - return -1; + if ( imagem->register_image(*oid, ds_data) == -1 ) + { + error_str = "Failed to copy image to repository. " + "Image left in ERROR state."; + return -1; + } + } + else + { + if (imagem->clone_image(*oid, cloning_id, ds_data, error_str) == -1) + { + return -1; + } } } diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index ce62e173d1..e22c13a36c 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -168,59 +168,52 @@ void ImageClone::request_execute( xmlrpc_c::paramList const& paramList, RequestAttributes& att) { - int source_id = xmlrpc_c::value_int(paramList.getInt(1)); - string name = xmlrpc_c::value_string(paramList.getString(2)); + int clone_id = xmlrpc_c::value_int(paramList.getInt(1)); + string name = xmlrpc_c::value_string(paramList.getString(2)); - int rc, new_id, ds_id; + int rc, new_id, ds_id, size; string error_str, ds_name, ds_data; Image::DiskType disk_type; PoolObjectAuth perms, ds_perms; - ImageTemplate * tmpl = 0; - Image * source_img; + + ImageTemplate * tmpl; + Template img_usage; + Image * img; Datastore * ds; - Nebula& nd = Nebula::instance(); + Nebula& nd = Nebula::instance(); DatastorePool * dspool = nd.get_dspool(); ImagePool * ipool = static_cast(pool); // ------------------------- Get source Image info ------------------------- - source_img = ipool->get(source_id, true); + img = ipool->get(clone_id, true); - if ( source_img == 0 ) + if ( img == 0 ) { failure_response(NO_EXISTS, - get_error(object_name(auth_object),source_id), + get_error(object_name(auth_object), clone_id), att); return; } - //TODO: UPDATE THIS rc = source_img->clone_template(name, tmpl, error_str); - //TODO: ADD QUOTA AUTH - if ( rc != 0 ) - { - source_img->unlock(); + tmpl = img->clone_template(name); + + img->get_permissions(perms); - failure_response(INTERNAL, - request_error("Could not clone Image",error_str), - att); + ds_id = img->get_ds_id(); + ds_name = img->get_ds_name(); + size = img->get_size(); - return; - } - - source_img->get_permissions(perms); - - ds_id = source_img->get_ds_id(); - ds_name = source_img->get_ds_name(); - - source_img->unlock(); + img->unlock(); // ------------------------- Get Datastore info ---------------------------- ds = dspool->get(ds_id, true); + if ( ds == 0 ) { failure_response(NO_EXISTS, @@ -232,17 +225,24 @@ void ImageClone::request_execute( } ds->get_permissions(ds_perms); + disk_type = ds->get_disk_type(); + ds->to_xml(ds_data); ds->unlock(); // ------------- Set authorization request --------------------------------- + + img_usage.add("DATASTORE", ds_id); + img_usage.add("SIZE", size); if ( att.uid != 0 ) { AuthRequest ar(att.uid, att.gid); - string tmpl_str = ""; + string tmpl_str; + + // ------------------ Check permissions and ACLs ---------------------- tmpl->to_xml(tmpl_str); @@ -259,28 +259,45 @@ void ImageClone::request_execute( delete tmpl; return; } + + // -------------------------- Check Quotas ---------------------------- + + if ( quota_authorization(&img_usage, att) == false ) + { + delete tmpl; + return; + } } - rc = ipool->allocate(att.uid, att.gid, att.uname, att.gname, - tmpl, ds_id, ds_name, disk_type, - ds_data, source_id, &new_id, error_str); - + rc = ipool->allocate(att.uid, + att.gid, + att.uname, + att.gname, + tmpl, + ds_id, + ds_name, + disk_type, + ds_data, + clone_id, + &new_id, + error_str); if ( rc < 0 ) { + quota_rollback(&img_usage, att); + failure_response(INTERNAL, allocate_error(error_str), att); return; } - source_img = ipool->get(source_id, true); + ds = dspool->get(ds_id, true); - if ( source_img != 0 ) + if ( ds != 0 ) // TODO: error otherwise or leave image in ERROR? { - source_img->inc_cloning(); - source_img->set_state(Image::USED); + ds->add_image(new_id); - ipool->update(source_img); + dspool->update(ds); - source_img->unlock(); + ds->unlock(); } success_response(new_id, att); diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index e12a476493..4802e44286 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -583,6 +583,17 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis return; } + ds = dspool->get(ds_id, true); + + if ( ds != 0 ) // TODO: error otherwise or leave image in ERROR? + { + ds->add_image(iid); + + dspool->update(ds); + + ds->unlock(); + } + // Return the new allocated Image ID success_response(iid, att); } From 44f01658ec8f5a823fe4bbf6ce410c92a5949b04 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 13 Jun 2012 00:56:27 +0200 Subject: [PATCH 08/15] feature #1291: Clone actions for core and drivers. Driver plugins for FS and summy datastores --- install.sh | 2 + src/datastore_mad/one_datastore.rb | 22 ++++--- src/datastore_mad/remotes/dummy/clone | 19 ++++++ src/datastore_mad/remotes/fs/clone | 67 ++++++++++++++++++++ src/image/ImageManagerDriver.cc | 88 ++++++++++++++++++++++++++- src/image/ImagePool.cc | 2 +- 6 files changed, 191 insertions(+), 9 deletions(-) create mode 100755 src/datastore_mad/remotes/dummy/clone create mode 100755 src/datastore_mad/remotes/fs/clone diff --git a/install.sh b/install.sh index 121f6bc754..4e8b27954a 100755 --- a/install.sh +++ b/install.sh @@ -864,11 +864,13 @@ DATASTORE_DRIVER_COMMON_SCRIPTS="src/datastore_mad/remotes/xpath.rb \ DATASTORE_DRIVER_DUMMY_SCRIPTS="src/datastore_mad/remotes/dummy/cp \ src/datastore_mad/remotes/dummy/mkfs \ src/datastore_mad/remotes/dummy/stat \ + src/datastore_mad/remotes/dummy/clone \ src/datastore_mad/remotes/dummy/rm" DATASTORE_DRIVER_FS_SCRIPTS="src/datastore_mad/remotes/fs/cp \ src/datastore_mad/remotes/fs/mkfs \ src/datastore_mad/remotes/fs/stat \ + src/datastore_mad/remotes/fs/clone \ src/datastore_mad/remotes/fs/rm" DATASTORE_DRIVER_VMWARE_SCRIPTS="src/datastore_mad/remotes/vmware/cp \ diff --git a/src/datastore_mad/one_datastore.rb b/src/datastore_mad/one_datastore.rb index c77d937225..641b7aac7b 100755 --- a/src/datastore_mad/one_datastore.rb +++ b/src/datastore_mad/one_datastore.rb @@ -44,11 +44,12 @@ class DatastoreDriver < OpenNebulaDriver # Image Driver Protocol constants ACTION = { - :cp => "CP", - :rm => "RM", - :mkfs => "MKFS", - :log => "LOG", - :stat => "STAT" + :cp => "CP", + :rm => "RM", + :mkfs => "MKFS", + :log => "LOG", + :stat => "STAT", + :clone => "CLONE" } # Register default actions for the protocol @@ -61,7 +62,8 @@ class DatastoreDriver < OpenNebulaDriver ACTION[:stat] => nil, ACTION[:cp] => nil, ACTION[:rm] => nil, - ACTION[:mkfs] => nil + ACTION[:mkfs] => nil, + ACTION[:clone]=> nil } }.merge!(options) @@ -80,7 +82,8 @@ class DatastoreDriver < OpenNebulaDriver register_action(ACTION[:cp].to_sym, method("cp")) register_action(ACTION[:rm].to_sym, method("rm")) register_action(ACTION[:mkfs].to_sym, method("mkfs")) - register_action(ACTION[:stat].to_sym, method("stat")) + register_action(ACTION[:stat].to_sym, method("stat")) + register_action(ACTION[:clone].to_sym,method("clone")) end ############################################################################ @@ -107,6 +110,11 @@ class DatastoreDriver < OpenNebulaDriver do_image_action(id, ds, :stat, "#{drv_message} #{id}") end + def clone(id, drv_message) + ds = get_ds_type(drv_message) + do_image_action(id, ds, :clone, "#{drv_message} #{id}") + end + private def is_available?(ds, id, action) diff --git a/src/datastore_mad/remotes/dummy/clone b/src/datastore_mad/remotes/dummy/clone new file mode 100755 index 0000000000..9cf7239279 --- /dev/null +++ b/src/datastore_mad/remotes/dummy/clone @@ -0,0 +1,19 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +echo "dummy_path" diff --git a/src/datastore_mad/remotes/fs/clone b/src/datastore_mad/remotes/fs/clone new file mode 100755 index 0000000000..2c987de961 --- /dev/null +++ b/src/datastore_mad/remotes/fs/clone @@ -0,0 +1,67 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to copy a VM image (SRC) to the image repository as DST +# Several SRC types are supported +############################################################################### + +# -------- Set up the environment to source common tools & conf ------------ + +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one +else + LIB_LOCATION=$ONE_LOCATION/lib +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh + +# -------- Get cp and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ + /DS_DRIVER_ACTION_DATA/IMAGE/PATH) + +BASE_PATH="${XPATH_ELEMENTS[0]}" +UMASK="${XPATH_ELEMENTS[1]}" +SRC="${XPATH_ELEMENTS[2]}" + +mkdir -p "$BASE_PATH" +set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" + +DST=`generate_image_path` + +# ------------ Copy the image to the repository ------------- + +log "Copying local image $SRC to the image repository" + +exec_and_log "cp -f $SRC $DST" "Error copying $SRC to $DST" + +echo "$DST" diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index 814c668ae7..c1e0923959 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -181,7 +181,7 @@ static void cp_action(istringstream& is, return; error: - oss << "Error copying image in the repository"; + oss << "Error copying image in the datastore"; getline(is, info); @@ -204,6 +204,88 @@ error: /* -------------------------------------------------------------------------- */ +static void clone_action(istringstream& is, + ImagePool* ipool, + int id, + const string& result) +{ + int cloning_id; + string source; + string info; + + Image * image; + + ostringstream oss; + + Nebula& nd = Nebula::instance(); + ImageManager * im = nd.get_imagem(); + + image = ipool->get(id, true); + + if ( image == 0 ) + { + return; + } + + cloning_id = image->get_cloning_id(); + + if ( result == "FAILURE" ) + { + goto error; + } + + if ( is.good() ) + { + is >> source >> ws; + } + + if ( is.fail() ) + { + goto error; + } + + image->set_source(source); + + image->set_state(Image::READY); + + ipool->update(image); + + image->clear_cloning_id(); + + image->unlock(); + + NebulaLog::log("ImM", Log::INFO, "Image cloned and ready to use."); + + im ->release_cloning_image(cloning_id); + + return; + +error: + oss << "Error cloning image "; + + getline(is, info); + + if (!info.empty() && (info[0] != '-')) + { + oss << ": " << info; + } + + NebulaLog::log("ImM", Log::ERROR, oss); + + image->set_template_error_message(oss.str()); + image->set_state(Image::ERROR); + + ipool->update(image); + + image->unlock(); + + im ->release_cloning_image(cloning_id); + + return; +} + +/* -------------------------------------------------------------------------- */ + static void mkfs_action(istringstream& is, ImagePool* ipool, int id, @@ -463,6 +545,10 @@ void ImageManagerDriver::protocol( { cp_action(is, ipool, id, result); } + else if ( action == "CLONE" ) + { + clone_action(is, ipool, id, result); + } else if ( action == "MKFS" ) { mkfs_action(is, ipool, id, result); diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 3370cdcb8a..57e5560cc5 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -122,7 +122,7 @@ int ImagePool::allocate ( Nebula& nd = Nebula::instance(); ImageManager * imagem = nd.get_imagem(); - if (cloning_id != -1) + if (cloning_id == -1) { if ( imagem->register_image(*oid, ds_data) == -1 ) { From ec2040694e50a2cda887f10b7876135f359049c0 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 13 Jun 2012 11:22:51 +0200 Subject: [PATCH 09/15] feature #1291: Image state name for OCA ruby-bindings --- src/oca/ruby/OpenNebula/Image.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/oca/ruby/OpenNebula/Image.rb b/src/oca/ruby/OpenNebula/Image.rb index 5f80c89ee3..ccac60026d 100644 --- a/src/oca/ruby/OpenNebula/Image.rb +++ b/src/oca/ruby/OpenNebula/Image.rb @@ -38,7 +38,7 @@ module OpenNebula :clone => "image.clone" } - IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR} + IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR CLONE DELETE USED_PERS} SHORT_IMAGE_STATES={ "INIT" => "init", @@ -46,7 +46,10 @@ module OpenNebula "USED" => "used", "DISABLED" => "disa", "LOCKED" => "lock", - "ERROR" => "err" + "ERROR" => "err", + "CLONE" => "clon", + "DELETE" => "dele", + "USED_PERS" => "used" } IMAGE_TYPES=%w{OS CDROM DATABLOCK} From 1c754efcab2ec05060300b42d2f9ca0ff944b94d Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 13 Jun 2012 16:53:48 +0200 Subject: [PATCH 10/15] feature #1291: Update ec2 imageState --- src/cloud/ec2/lib/EC2QueryServer.rb | 16 ++++++++ src/cloud/ec2/lib/views/describe_images.erb | 42 +++++++++------------ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/cloud/ec2/lib/EC2QueryServer.rb b/src/cloud/ec2/lib/EC2QueryServer.rb index dc9234d5c8..9e0a7bfde6 100644 --- a/src/cloud/ec2/lib/EC2QueryServer.rb +++ b/src/cloud/ec2/lib/EC2QueryServer.rb @@ -59,6 +59,18 @@ class EC2QueryServer < CloudServer 'unkn' => :terminated } + EC2_IMAGE_STATES={ + "INIT" => "pending", + "READY" => "available", + "USED" => "available", + "DISABLED" => nil, + "LOCKED" => "pending", + "ERROR" => "failed", + "CLONE" => "available", + "DELETE" => nil, + "USED_PERS" => "available" + } + ########################################################################### def initialize(client, oneadmin_client, config, logger) @@ -251,6 +263,10 @@ class EC2QueryServer < CloudServer #{ec2_state[:name]}" end + def render_image_state(image) + EC2_IMAGE_STATES[image.state_str] + end + def render_launch_time(vm) return "#{Time.at(vm["STIME"].to_i).xmlschema}" end diff --git a/src/cloud/ec2/lib/views/describe_images.erb b/src/cloud/ec2/lib/views/describe_images.erb index fff015f68a..a38c99861d 100644 --- a/src/cloud/ec2/lib/views/describe_images.erb +++ b/src/cloud/ec2/lib/views/describe_images.erb @@ -1,26 +1,20 @@ - - - <% impool.each do |im| %> - - ami-<%= sprintf('%08i', im.id) %> - <%= im['SOURCE'].split('/').last %> - <% if im['STATE'] == '3' || im['STATE'] == '5' %> - failed - <% elsif im['STATE'] == '1' || im['STATE'] == '2' %> - available - <% elsif im['STATE'] == '4'%> - pending - <% end %> - <%= im['UNAME'] %> - <% if im['PUBLIC'] == '0' %> - false - <% elsif im['PUBLIC'] == '1' %> - true - <% end %> - i386 - machine - - <% end %> - + + + <% impool.each do |im| %> + <% if state_image = render_image_state(im) %> + + ami-<%= sprintf('%08i', im.id) %> + <%= im['SOURCE'].split('/').last %> + <%= state_image %> + <%= im['UNAME'] %> + <%= im.public? ? "true" : "false" %> + i386 + machine + + <% else + next + end + end %> + From 8ccbf3e397a44390c91bab969ebe5ad8127b749d Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Thu, 14 Jun 2012 18:18:04 +0200 Subject: [PATCH 11/15] feature #1291: Add DS_MAD clone scripts for LVM and iSCSI --- install.sh | 2 + src/datastore_mad/remotes/iscsi/clone | 94 +++++++++++++++++++++++++++ src/datastore_mad/remotes/lvm/clone | 84 ++++++++++++++++++++++++ src/mad/sh/scripts_common.sh | 4 +- 4 files changed, 182 insertions(+), 2 deletions(-) create mode 100755 src/datastore_mad/remotes/iscsi/clone create mode 100755 src/datastore_mad/remotes/lvm/clone diff --git a/install.sh b/install.sh index 4e8b27954a..baaed3f620 100755 --- a/install.sh +++ b/install.sh @@ -882,12 +882,14 @@ DATASTORE_DRIVER_ISCSI_SCRIPTS="src/datastore_mad/remotes/iscsi/cp \ src/datastore_mad/remotes/iscsi/mkfs \ src/datastore_mad/remotes/iscsi/stat \ src/datastore_mad/remotes/iscsi/rm \ + src/datastore_mad/remotes/iscsi/clone \ src/datastore_mad/remotes/iscsi/iscsi.conf" DATASTORE_DRIVER_LVM_SCRIPTS="src/datastore_mad/remotes/lvm/cp \ src/datastore_mad/remotes/lvm/mkfs \ src/datastore_mad/remotes/lvm/stat \ src/datastore_mad/remotes/lvm/rm \ + src/datastore_mad/remotes/lvm/clone \ src/datastore_mad/remotes/lvm/lvm.conf" #------------------------------------------------------------------------------- diff --git a/src/datastore_mad/remotes/iscsi/clone b/src/datastore_mad/remotes/iscsi/clone new file mode 100755 index 0000000000..8b0fe39205 --- /dev/null +++ b/src/datastore_mad/remotes/iscsi/clone @@ -0,0 +1,94 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to copy a VM image (SRC) to the image repository as DST +# Several SRC types are supported +############################################################################### +# -------- Set up the environment to source common tools & conf ------------ +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one +else + LIB_LOCATION=$ONE_LOCATION/lib +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh +source ${DRIVER_PATH}/iscsi.conf + +# -------- Get cp and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RESTRICTED_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/SAFE_DIRS \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/HOST \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VG_NAME \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BASE_IQN \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BASE_TID \ + /DS_DRIVER_ACTION_DATA/IMAGE/PATH \ + /DS_DRIVER_ACTION_DATA/IMAGE/SIZE) + +BASE_PATH="${XPATH_ELEMENTS[0]}" +RESTRICTED_DIRS="${XPATH_ELEMENTS[1]}" +SAFE_DIRS="${XPATH_ELEMENTS[2]}" +UMASK="${XPATH_ELEMENTS[3]}" +DST_HOST="${XPATH_ELEMENTS[4]:-$HOST}" +VG_NAME="${XPATH_ELEMENTS[5]:-$VG_NAME}" +BASE_IQN="${XPATH_ELEMENTS[6]:-$BASE_IQN}" +BASE_TID="${XPATH_ELEMENTS[7]:-$BASE_TID}" +SRC="${XPATH_ELEMENTS[8]}" +SIZE="${XPATH_ELEMENTS[9]}" + +set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" + +LV_NAME="lv-one-${ID}" +IQN="$BASE_IQN:$DST_HOST.$VG_NAME.$LV_NAME" +DEV="/dev/$VG_NAME/$LV_NAME" + +let TID=ID+BASE_TID + +LV_SRC=$(echo $SRC|awk -F. '{print $NF}') +DEV_SRC="/dev/$VG_NAME/$LV_SRC" + +CLONE_CMD=$(cat < Date: Thu, 14 Jun 2012 19:19:33 +0200 Subject: [PATCH 12/15] feature #1291: Add DS_MAD common clone script for vmware and fs --- src/datastore_mad/remotes/common/clone | 78 ++++++++++++++++++++++++++ src/datastore_mad/remotes/fs/clone | 68 +--------------------- src/datastore_mad/remotes/vmware/clone | 1 + 3 files changed, 80 insertions(+), 67 deletions(-) create mode 100755 src/datastore_mad/remotes/common/clone mode change 100755 => 120000 src/datastore_mad/remotes/fs/clone create mode 120000 src/datastore_mad/remotes/vmware/clone diff --git a/src/datastore_mad/remotes/common/clone b/src/datastore_mad/remotes/common/clone new file mode 100755 index 0000000000..d7c7603f18 --- /dev/null +++ b/src/datastore_mad/remotes/common/clone @@ -0,0 +1,78 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +############################################################################### +# This script is used to copy a VM image (SRC) to the image repository as DST +# Several SRC types are supported +############################################################################### + +# -------- Set up the environment to source common tools & conf ------------ + +if [ -z "${ONE_LOCATION}" ]; then + LIB_LOCATION=/usr/lib/one +else + LIB_LOCATION=$ONE_LOCATION/lib +fi + +. $LIB_LOCATION/sh/scripts_common.sh + +DRIVER_PATH=$(dirname $0) +source ${DRIVER_PATH}/../libfs.sh + +# -------- Get cp and datastore arguments from OpenNebula core ------------ + +DRV_ACTION=$1 +ID=$2 + +XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" + +unset i XPATH_ELEMENTS + +while IFS= read -r -d '' element; do + XPATH_ELEMENTS[i++]="$element" +done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ + /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/DS_MAD \ + /DS_DRIVER_ACTION_DATA/IMAGE/PATH) + +BASE_PATH="${XPATH_ELEMENTS[0]}" +UMASK="${XPATH_ELEMENTS[1]}" +DS_MAD="${XPATH_ELEMENTS[2]}" +SRC="${XPATH_ELEMENTS[3]}" + +mkdir -p "$BASE_PATH" +set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" + +DST=`generate_image_path` + +case "$DS_MAD" in +vmware) + CP_FLAGS="-r" + ;; +*) + CP_FLAGS="-f" + ;; +esac + +# ------------ Copy the image to the repository ------------- + +log "Copying local image $SRC to the image repository" + +exec_and_log "cp $CP_FLAGS $SRC $DST" "Error copying $SRC to $DST" + +echo "$DST" diff --git a/src/datastore_mad/remotes/fs/clone b/src/datastore_mad/remotes/fs/clone deleted file mode 100755 index 2c987de961..0000000000 --- a/src/datastore_mad/remotes/fs/clone +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -# -------------------------------------------------------------------------- # -# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # -# # -# Licensed under the Apache License, Version 2.0 (the "License"); you may # -# not use this file except in compliance with the License. You may obtain # -# a copy of the License at # -# # -# http://www.apache.org/licenses/LICENSE-2.0 # -# # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # -#--------------------------------------------------------------------------- # - -############################################################################### -# This script is used to copy a VM image (SRC) to the image repository as DST -# Several SRC types are supported -############################################################################### - -# -------- Set up the environment to source common tools & conf ------------ - -if [ -z "${ONE_LOCATION}" ]; then - LIB_LOCATION=/usr/lib/one -else - LIB_LOCATION=$ONE_LOCATION/lib -fi - -. $LIB_LOCATION/sh/scripts_common.sh - -DRIVER_PATH=$(dirname $0) -source ${DRIVER_PATH}/../libfs.sh - -# -------- Get cp and datastore arguments from OpenNebula core ------------ - -DRV_ACTION=$1 -ID=$2 - -XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION" - -unset i XPATH_ELEMENTS - -while IFS= read -r -d '' element; do - XPATH_ELEMENTS[i++]="$element" -done < <($XPATH /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \ - /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/UMASK \ - /DS_DRIVER_ACTION_DATA/IMAGE/PATH) - -BASE_PATH="${XPATH_ELEMENTS[0]}" -UMASK="${XPATH_ELEMENTS[1]}" -SRC="${XPATH_ELEMENTS[2]}" - -mkdir -p "$BASE_PATH" -set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS" "$UMASK" - -DST=`generate_image_path` - -# ------------ Copy the image to the repository ------------- - -log "Copying local image $SRC to the image repository" - -exec_and_log "cp -f $SRC $DST" "Error copying $SRC to $DST" - -echo "$DST" diff --git a/src/datastore_mad/remotes/fs/clone b/src/datastore_mad/remotes/fs/clone new file mode 120000 index 0000000000..305cd254d4 --- /dev/null +++ b/src/datastore_mad/remotes/fs/clone @@ -0,0 +1 @@ +../common/clone \ No newline at end of file diff --git a/src/datastore_mad/remotes/vmware/clone b/src/datastore_mad/remotes/vmware/clone new file mode 120000 index 0000000000..305cd254d4 --- /dev/null +++ b/src/datastore_mad/remotes/vmware/clone @@ -0,0 +1 @@ +../common/clone \ No newline at end of file From b136938841384108d7864fcf441e8b258f679892 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 15 Jun 2012 17:17:11 +0200 Subject: [PATCH 13/15] feautre #1291: Do not update images if they have been removed --- src/image/ImageManagerDriver.cc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index c1e0923959..9f4e337efb 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -477,13 +477,6 @@ error: NebulaLog::log("ImM", Log::ERROR, oss); - image->set_template_error_message(oss.str()); - image->set_state(Image::ERROR); - - ipool->update(image); - - image->unlock(); - return; } From 9eb7df267f57966991d93084d49febcafc22f01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 18 Jun 2012 16:52:34 +0200 Subject: [PATCH 14/15] Feature #1291: Add new Image states to Java OCA --- src/oca/java/src/org/opennebula/client/image/Image.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 8ddca901ec..245da2a152 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -40,10 +40,11 @@ public class Image extends PoolElement private static final String CLONE = METHOD_PREFIX + "clone"; private static final String[] IMAGE_STATES = - {"INIT", "READY", "USED", "DISABLED", "LOCKED", "ERROR"}; + {"INIT", "READY", "USED", "DISABLED", "LOCKED", + "ERROR", "CLONE", "DELETE", "USED_PERS"}; private static final String[] SHORT_IMAGE_STATES = - {"init", "rdy", "used", "disa", "lock", "err"}; + {"init", "rdy", "used", "disa", "lock", "err", "clon", "dele", "used"}; private static final String[] IMAGE_TYPES = {"OS", "CDROM", "DATABLOCK"}; From 9e2a15f820b8767836fb61e8159b4fa7622b383e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 18 Jun 2012 16:53:14 +0200 Subject: [PATCH 15/15] Feature #1291: Update image.xsd doc file --- share/doc/xsd/image.xsd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/share/doc/xsd/image.xsd b/share/doc/xsd/image.xsd index 8f664824bc..51867915b6 100644 --- a/share/doc/xsd/image.xsd +++ b/share/doc/xsd/image.xsd @@ -34,6 +34,8 @@ + +