1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

feature 1112: Work integrating datastores and images

This commit is contained in:
Ruben S. Montero 2012-02-15 00:19:42 +01:00
parent 45382e76b1
commit d754c987e6
5 changed files with 119 additions and 103 deletions

View File

@ -146,17 +146,18 @@ public:
/**
* Adds a new image to the repository copying or creating it as needed
* @param iid id of image
* @param img pointer to the image
* @param ds_data data of the associated datastore in XML format
* @return 0 on success
*/
int register_image(int iid);
int register_image(int iid, const string& ds_data);
/**
* Deletes an image from the repository and the DB
* @param iid id of image
* @return 0 on success
*/
int delete_image(int iid);
int delete_image(int iid, const string& ds_data);
private:
/**

View File

@ -93,14 +93,11 @@ int Image::insert(SqlDB *db, string& error_str)
string path_attr;
string type_att;
string persistent_attr;
string datastore_attr;
string dev_prefix;
string source_attr;
string aname;
ostringstream oss;
istringstream iss;
string ds_id_str;
// ------------------------------------------------------------------------
// Check template for restricted attributes
@ -138,15 +135,6 @@ int Image::insert(SqlDB *db, string& error_str)
goto error_type;
}
// ------------ DATASTORE --------------------
erase_template_attribute("DATASTORE_ID", ds_id_str);
iss.str(ds_id_str);
iss >> ds_id;
erase_template_attribute("DATASTORE", ds_name);
// ------------ PERSISTENT --------------------
erase_template_attribute("PERSISTENT", persistent_attr);
@ -163,7 +151,6 @@ int Image::insert(SqlDB *db, string& error_str)
{
SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
ImagePool::default_dev_prefix());
obj_template->set(dev_att);
}

View File

@ -17,8 +17,6 @@
#include "ImageManager.h"
#include "NebulaLog.h"
#include "ImagePool.h"
#include "DatastorePool.h"
#include "Nebula.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -311,7 +309,7 @@ int ImageManager::enable_image(int iid, bool to_enable)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImageManager::delete_image(int iid)
int ImageManager::delete_image(int iid, const string& ds_data)
{
Image * img;
string source;
@ -372,75 +370,29 @@ int ImageManager::delete_image(int iid)
imd->rm(img->get_oid(),img->get_source());
}
int ds_id = img->get_ds_id();
img->unlock();
Datastore * ds;
DatastorePool * dspool;
Nebula& nd = Nebula::instance();
dspool = nd.get_dspool();
ds = dspool->get(ds_id, true);
// TODO check ds != 0
ds->del_image(iid);
dspool->update(ds);
ds->unlock();
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImageManager::register_image(int iid)
int ImageManager::register_image(int iid, const string& ds_data)
{
const ImageManagerDriver* imd = get();
ostringstream oss;
ostringstream oss;
Image * img;
string path;
Image* img;
if ( imd == 0 )
{
NebulaLog::log("ImM",Log::ERROR,
"Could not get driver to update repository");
NebulaLog::log("ImM",Log::ERROR, "Could not get datastore driver");
return -1;
}
img = ipool->get(iid,true);
if (img == 0)
{
return -1;
}
// Add the image to its datastore
int ds_id = img->get_ds_id();
img->unlock();
Datastore * ds;
DatastorePool * dspool;
Nebula& nd = Nebula::instance();
dspool = nd.get_dspool();
ds = dspool->get(ds_id, true);
// TODO check ds != 0
ds->add_image(iid);
dspool->update(ds);
ds->unlock();
img = ipool->get(iid,true);
if (img == 0)

View File

@ -68,19 +68,27 @@ int ImagePool::allocate (
int * oid,
string& error_str)
{
Image * img;
Image * img_aux = 0;
string name;
int ds_id;
ostringstream oss;
Datastore * ds;
DatastorePool * dspool;
Image * img;
Image * img_aux = 0;
Nebula& nd = Nebula::instance();
ostringstream oss;
istringstream iss;
string name, ds_id_str, ds_data;
int ds_id;
Datastore * ds = 0;
Nebula& nd = Nebula::instance();
DatastorePool * dspool = nd.get_dspool();
ImageManager * imagem = nd.get_imagem();
img = new Image(uid, gid, uname, gname, img_template);
// Check name
// -------------------------------------------------------------------------
// Check name & duplicates
// -------------------------------------------------------------------------
img->get_template_attribute("NAME", name);
if ( name.empty() )
@ -93,7 +101,6 @@ int ImagePool::allocate (
goto error_name_length;
}
// Check for duplicates
img_aux = get(name,uid,false);
if( img_aux != 0 )
@ -101,29 +108,39 @@ int ImagePool::allocate (
goto error_duplicated;
}
// Check datastore exists
// -------------------------------------------------------------------------
// Check that the datastore exists
// -------------------------------------------------------------------------
img->erase_template_attribute("DATASTORE", name);
img->erase_template_attribute("DATASTORE_ID", ds_id_str);
// TODO: get datastore by name, and replace datastore name from it
img->get_template_attribute("DATASTORE_ID", ds_id);
// TODO how to check if "DATASTORE_ID" exists?
// get_template_attribute returns 0 if the attribute does not exist...
// but it can exist and have value 0
if ( false )
if ( name.empty() )
{
goto error_common; // TODO error
iss.str(ds_id_str);
iss >> ds_id;
if (ds_id == 0)
{
goto error_ds_system;
}
ds = dspool->get(ds_id, true);
}
else
{
ds = dspool->get(name, true);
}
dspool = nd.get_dspool();
ds = dspool->get(ds_id, true);
if( ds == 0 )
if ( ds == 0 )
{
goto error_common; // TODO error
goto error_no_ds;
}
img->replace_template_attribute("DATASTORE", ds->get_name());
img->ds_name = ds->get_name();
img->ds_id = ds->get_oid();
ds_id = ds->get_oid();
ds->to_xml(ds_data);
ds->unlock();
@ -134,10 +151,20 @@ int ImagePool::allocate (
if ( *oid != -1 )
{
Nebula& nd = Nebula::instance();
ImageManager * imagem = nd.get_imagem();
ds = dspool->get(ds_id, true);
if ( ds == 0 )
{
goto error_ds_deleted;
}
if ( imagem->register_image(*oid) == -1 )
ds->add_image(*oid);
dspool->update(ds);
ds->unlock();
if ( imagem->register_image(*oid, ds_data) == -1 )
{
error_str = "Failed to copy image to repository. "
"Image left in ERROR state.";
@ -149,7 +176,6 @@ int ImagePool::allocate (
error_name:
oss << "NAME cannot be empty.";
goto error_common;
error_name_length:
@ -159,6 +185,19 @@ error_name_length:
error_duplicated:
oss << "NAME is already taken by IMAGE "
<< img_aux->get_oid() << ".";
goto error_common;
error_ds_system:
oss << "System or non datastore in image template.";
goto error_common;
error_ds_deleted:
oss << "Datastore was deleted while registering image."
<< "Image left in LOCKED state.";
return -1;
error_no_ds:
oss << "No datastore specify to register image.";
error_common:
delete img;

View File

@ -106,10 +106,47 @@ void RequestManagerDelete::request_execute(xmlrpc_c::paramList const& paramList,
int ImageDelete::drop(int oid, PoolObjectSQL * object, string& error_msg)
{
Nebula& nd = Nebula::instance();
ImageManager * imagem = nd.get_imagem();
object->unlock();
int rc = imagem->delete_image(oid);
ImageManager * imagem = nd.get_imagem();
DatastorePool * dspool = nd.get_dspool();
Datastore * ds;
Image * img;
int ds_id, rc;
string ds_data;
img = static_cast<Image *>(object);
ds_id = img->get_ds_id();
img->unlock();
ds = dspool->get(ds_id, true);
if ( ds == 0 )
{
error_msg = "Datastore no longer exists can not remove image";
return -1;
}
ds->to_xml(ds_data);
ds->unlock();
rc = imagem->delete_image(oid, ds_data);
if ( rc == 0 )
{
ds = dspool->get(ds_id, true);
if ( ds != 0 )
{
ds->del_image(oid);
dspool->update(ds);
ds->unlock();
}
}
return rc;
}