diff --git a/include/ImageManager.h b/include/ImageManager.h index 266ceea332..95c6b7887b 100644 --- a/include/ImageManager.h +++ b/include/ImageManager.h @@ -220,6 +220,17 @@ private: * @param source path of the disk file */ void move_image(Image *img, const string& source); + + /** + * Formats an XML message for the MAD + * + * @param img_data Image XML representation + * @param ds_data Datastore XML representation + * @return the XML message + */ + string * format_message( + const string& img_data, + const string& ds_data); }; #endif /*IMAGE_MANAGER_H*/ diff --git a/include/ImageManagerDriver.h b/include/ImageManagerDriver.h index 081c7bd31a..7840364844 100644 --- a/include/ImageManagerDriver.h +++ b/include/ImageManagerDriver.h @@ -70,7 +70,12 @@ private: */ //Template driver_conf; - void cp(int oid, const string& source) const; + /** + * Sends a copy request to the MAD + * @param oid the image id. + * @param drv_msg xml data for the mad operation. + */ + void cp(int oid, const string& drv_msg) const; /** * Sends a move request to the MAD: "MV IMAGE_ID SRC_PATH DST_PATH" @@ -93,9 +98,9 @@ private: /** * Sends a delete request to the MAD: "DELETE IMAGE_ID PATH" * @param oid the image id. - * @param destination is the path to the image to be removed + * @param drv_msg xml data for the mad operation. */ - void rm(int oid, const string& destination) const; + void rm(int oid, const string& drv_msg) const; }; /* -------------------------------------------------------------------------- */ diff --git a/src/image/ImageManagerActions.cc b/src/image/ImageManagerActions.cc index 6f4eab9833..f9a075bffc 100644 --- a/src/image/ImageManagerActions.cc +++ b/src/image/ImageManagerActions.cc @@ -17,6 +17,7 @@ #include "ImageManager.h" #include "NebulaLog.h" #include "ImagePool.h" +#include "SSLTools.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -311,8 +312,10 @@ int ImageManager::enable_image(int iid, bool to_enable) int ImageManager::delete_image(int iid, const string& ds_data) { - Image * img; - string source; + Image * img; + string source; + string img_tmpl; + string * drv_msg; img = ipool->get(iid,true); @@ -350,6 +353,8 @@ int ImageManager::delete_image(int iid, const string& ds_data) return -1; } + drv_msg = format_message(img->to_xml(img_tmpl), ds_data); + source = img->get_source(); if (source.empty()) @@ -367,11 +372,13 @@ int ImageManager::delete_image(int iid, const string& ds_data) } else { - imd->rm(img->get_oid(),img->get_source()); + imd->rm(img->get_oid(), *drv_msg); } img->unlock(); + delete drv_msg; + return 0; } @@ -384,8 +391,11 @@ int ImageManager::register_image(int iid, const string& ds_data) ostringstream oss; Image * img; - - string path; + + string path; + string img_tmpl; + string * drv_msg; + if ( imd == 0 ) { @@ -400,6 +410,8 @@ int ImageManager::register_image(int iid, const string& ds_data) return -1; } + drv_msg = format_message(img->to_xml(img_tmpl), ds_data); + path = img->get_path(); if ( path.empty() == true ) //NO PATH -> USE SOURCE OR MKFS FOR DATABLOCK @@ -430,7 +442,7 @@ int ImageManager::register_image(int iid, const string& ds_data) } else //PATH -> COPY TO REPOSITORY AS SOURCE { - imd->cp(img->get_oid(), path); + imd->cp(img->get_oid(), *drv_msg); oss << "Copying " << path <<" to repository for image "<get_oid(); } @@ -438,9 +450,28 @@ int ImageManager::register_image(int iid, const string& ds_data) img->unlock(); + delete drv_msg; + return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +string * ImageManager::format_message( + const string& img_data, + const string& ds_data) +{ + ostringstream oss; + + oss << "" + << img_data + << ds_data + << ""; + + return SSLTools::base64_encode(oss.str()); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc index 7c1862fb65..5715636606 100644 --- a/src/image/ImageManagerDriver.cc +++ b/src/image/ImageManagerDriver.cc @@ -27,11 +27,11 @@ /* ************************************************************************** */ void ImageManagerDriver::cp(int oid, - const string& source) const + const string& drv_msg) const { ostringstream os; - os << "CP " << oid << " " << source << endl; + os << "CP " << oid << " " << drv_msg << endl; write(os); } @@ -63,11 +63,11 @@ void ImageManagerDriver::mkfs(int oid, /* -------------------------------------------------------------------------- */ -void ImageManagerDriver::rm(int oid, const string& destination) const +void ImageManagerDriver::rm(int oid, const string& drv_msg) const { ostringstream os; - os << "RM " << oid << " " << destination << endl; + os << "RM " << oid << " " << drv_msg << endl; write(os); } diff --git a/src/image_mad/one_image.rb b/src/image_mad/one_image.rb index b45a93df00..c481727894 100755 --- a/src/image_mad/one_image.rb +++ b/src/image_mad/one_image.rb @@ -33,6 +33,8 @@ $: << RUBY_LIB_LOCATION require "OpenNebulaDriver" require 'getoptlong' +require 'base64' +require 'rexml/document' # This class provides basic messaging and logging functionality # to implement Image Repository Drivers. A image repository driver @@ -86,11 +88,23 @@ class ImageDriver < OpenNebulaDriver do_image_action(id, ds, :mv, "'#{src}' '#{dst}' '#{id}'") end - def cp(id, ds, src) + def cp(id, drv_message) + data = decode(drv_message) +# TODO +# ds = data.elements['DATASTORE/DRIVER'].text + ds = "fs" + src = data.elements['IMAGE/PATH'].text + do_image_action(id, ds, :cp, "'#{src}' '#{id}'") end - def rm(id, ds, dst) + def rm(id, drv_message) + data = decode(drv_message) +# TODO +# ds = data.elements['DATASTORE/DRIVER'].text + ds = "fs" + dst = data.elements['IMAGE/SOURCE'].text + do_image_action(id, ds, :rm, "'#{dst}' '#{id}'") end @@ -124,6 +138,20 @@ class ImageDriver < OpenNebulaDriver send_message(ACTION[action], result, id, info) end + + # TODO: Move decode to OpenNebulaDriver ? + + # Decodes the encoded XML driver message received from the core + # + # @param [String] drv_message the driver message + # @return [REXML::Element] the root element of the decoded XML message + def decode(drv_message) + message = Base64.decode64(drv_message) + + xml_doc = REXML::Document.new(message) + + xml_doc.root + end end