1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-22 17:57:46 +03:00

feature #1288: Integrate stat driver function with image allocation process

This commit is contained in:
Ruben S. Montero 2012-06-01 19:18:49 +02:00
parent ea898cdf7d
commit 7b934d673c
8 changed files with 138 additions and 45 deletions

View File

@ -55,6 +55,13 @@ public:
}
};
/**
* Return the string representation of an ImageType
* @param ob the type
* @return the string
*/
static ImageType str_to_type(string& str_type);
/**
* Type of Disks (used by the VMM_MAD). Values: BLOCK, CDROM or
* FILE

View File

@ -26,6 +26,7 @@ using namespace std;
extern "C" void * image_action_loop(void *arg);
class Image;
class Template;
class ImageManager : public MadManager, public ActionListener
{
@ -135,7 +136,7 @@ public:
* occurred describing the error.
* @result 0 on success
*/
int stat_image(const string& img_tmpl, const string& ds_tmpl, string& res);
int stat_image(Template* img_tmpl, const string& ds_tmpl, string& res);
private:
/**

View File

@ -88,7 +88,7 @@ EOF
# @param $1 - Path to the image
# @return size of the image in Mb
#-------------------------------------------------------------------------------
function fs_size{
function fs_size {
case $1 in
http://*)
@ -103,6 +103,7 @@ function fs_size{
SIZE=`stat -c %s "$1"`
error=$?
fi
;;
esac
if [ $error -ne 0 ]; then

View File

@ -51,9 +51,9 @@ SRC="${XPATH_ELEMENTS[0]}"
SIZE=`fs_size $SRC`
if [ SIZE = "0"]; then
if [ "$SIZE" = "0" ]; then
log_error "Cannot determine size for $SRC"
exit -1
fi
echo "SIZE"
echo "$SIZE"

View File

@ -97,7 +97,9 @@ int Image::insert(SqlDB *db, string& error_str)
string dev_prefix;
string source_attr;
string saved_id;
string size_attr;
istringstream iss;
ostringstream oss;
// ---------------------------------------------------------------------
@ -141,6 +143,13 @@ int Image::insert(SqlDB *db, string& error_str)
obj_template->set(dev_att);
}
// ------------ SIZE --------------------
erase_template_attribute("SIZE", size_attr);
iss.str(size_attr);
iss >> size_mb;
// ------------ PATH & SOURCE --------------------
erase_template_attribute("PATH", path);
@ -150,26 +159,13 @@ int Image::insert(SqlDB *db, string& error_str)
{
if ( source.empty() && path.empty() )
{
string size_attr;
istringstream iss;
erase_template_attribute("SIZE", size_attr);
erase_template_attribute("FSTYPE", fs_type);
// DATABLOCK image needs SIZE and FSTYPE
if (type != DATABLOCK || size_attr.empty() || fs_type.empty())
// DATABLOCK image needs FSTYPE
if (type != DATABLOCK || fs_type.empty())
{
goto error_no_path;
}
iss.str(size_attr);
iss >> size_mb;
if (iss.fail() == true)
{
goto error_size_format;
}
}
else if ( !source.empty() && !path.empty() )
{
@ -178,20 +174,7 @@ int Image::insert(SqlDB *db, string& error_str)
}
else
{
string size_attr;
istringstream iss;
fs_type = "save_as";
erase_template_attribute("SIZE", size_attr);
iss.str(size_attr);
iss >> size_mb;
if (iss.fail() == true)
{
goto error_size_format;
}
}
state = LOCKED; //LOCKED till the ImageManager copies it to the Repository
@ -220,10 +203,6 @@ error_no_path:
}
goto error_common;
error_size_format:
error_str = "Wrong number in SIZE.";
goto error_common;
error_path_and_source:
error_str = "Template malformed, PATH and SOURCE are mutually exclusive.";
goto error_common;
@ -561,3 +540,30 @@ int Image::set_type(string& _type)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
Image::ImageType Image::str_to_type(string& str_type)
{
Image::ImageType it = OS;
if (str_type.empty())
{
str_type = ImagePool::default_type();
}
TO_UPPER(str_type);
if ( str_type == "OS" )
{
it = OS;
}
else if ( str_type == "CDROM" )
{
it = CDROM;
}
else if ( str_type == "DATABLOCK" )
{
it = DATABLOCK;
}
return it;
}

View File

@ -19,6 +19,7 @@
#include "ImagePool.h"
#include "SSLTools.h"
#include "SyncRequest.h"
#include "Template.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -399,20 +400,61 @@ int ImageManager::register_image(int iid, const string& ds_data)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImageManager::stat_image(const string& img_tmpl,
const string& ds_tmpl,
string& res)
int ImageManager::stat_image(Template* img_tmpl,
const string& ds_data,
string& res)
{
const ImageManagerDriver* imd = get();
string * drv_msg;
string* drv_msg;
string type_att;
ostringstream img_data;
SyncRequest sr;
int rc;
int rc = 0;
img_tmpl->get("TYPE", type_att);
switch (Image::str_to_type(type_att))
{
case Image::OS:
case Image::CDROM:
img_tmpl->get("SOURCE", res);
if (!res.empty())
{
res = "0";
return 0;
}
img_tmpl->get("PATH", res);
if (res.empty())
{
res = "Either PATH or SOURCE are required for " + type_att;
return -1;
}
img_data << "<IMAGE><PATH>" << res << "</PATH></IMAGE>";
break;
case Image::DATABLOCK:
img_tmpl->get("SIZE", res);
if (res.empty())
{
res = "SIZE attribute is mandatory for DATABLOCK.";
return -1;
}
return 0;
}
add_request(&sr);
drv_msg = format_message(img_tmpl, ds_tmpl);
drv_msg = format_message(img_data.str(), ds_data);
imd->stat(sr.id, *drv_msg);

View File

@ -341,6 +341,8 @@ static void rm_action(istringstream& is,
rc = ipool->drop(image, tmp_error);
image->unlock();
if ( rc < 0 )
{
goto error_drop;

View File

@ -296,8 +296,14 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
RequestAttributes& att)
{
string error_str;
string size_str;
int size_mb;
istringstream iss;
string ds_name;
string ds_data;
int rc, id;
PoolObjectAuth ds_perms;
@ -309,6 +315,7 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
DatastorePool * dspool = nd.get_dspool();
ImagePool * ipool = static_cast<ImagePool *>(pool);
ImageManager * imagem = nd.get_imagem();
ImageTemplate * tmpl = new ImageTemplate;
@ -359,6 +366,34 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
ds->unlock();
// --------------- Get the SIZE for the Image, (DS driver) -----------------
rc = imagem->stat_image(tmpl, ds_data, size_str);
if ( rc == -1 )
{
failure_response(INTERNAL,
request_error("Cannot determine Image SIZE", size_str),
att);
delete tmpl;
return;
}
iss.str(size_str);
iss >> size_mb;
if ( iss.fail() )
{
failure_response(INTERNAL,
request_error("Cannot parse SIZE", size_str),
att);
delete tmpl;
return;
}
tmpl->erase("SIZE");
tmpl->add("SIZE", size_str);
// ------------- Set authorization request for non-oneadmin's --------------
if ( att.uid != 0 )
@ -368,7 +403,6 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
// ------------------ Check permissions and ACLs ----------------------
tmpl->add("DATASTORE", ds_name);
tmpl->add("SIZE", "0");
tmpl->to_xml(tmpl_str);
@ -413,7 +447,7 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params,
Template img_usage;
img_usage.add("DATASTORE", ds_name);
img_usage.add("SIZE", "0");
img_usage.add("SIZE", size_str);
quota_rollback(&img_usage, att);