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

feature #523: Integration of Image Drivers with the Image Manager

This commit is contained in:
Ruben S. Montero 2011-03-24 18:27:19 +01:00
parent 7f67ceff0a
commit 8d3a1e3557
5 changed files with 144 additions and 28 deletions

View File

@ -103,6 +103,14 @@ public:
return source;
}
/**
* Returns the type of the image
* @return type
*/
ImageType get_type()
{
return type;
}
/**
* Returns the image state
* @return state of image

View File

@ -90,8 +90,7 @@ private:
void mkfs(int oid,
const string& destination,
const string& fs,
int size_mb) const;
const string& size_mb) const;
/**
* Sends a delete request to the MAD: "DELETE IMAGE_ID PATH"
* @param oid the image id.

View File

@ -147,14 +147,12 @@ int Image::insert(SqlDB *db, string& error_str)
{
SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
ImagePool::default_dev_prefix());
image_template->set(dev_att);
}
// ------------ PATH --------------------
get_template_attribute("PATH", path_attr);
// ------------ PATH & SOURCE --------------------
// ------------ SOURCE (path to store the image) --------------------
get_template_attribute("PATH", path_attr);
get_template_attribute("SOURCE", source);
// The template should contain PATH or SOURCE
@ -163,14 +161,26 @@ int Image::insert(SqlDB *db, string& error_str)
string size_attr;
string fstype_attr;
istringstream iss;
int size_mb;
get_template_attribute("SIZE", size_attr);
get_template_attribute("FSTYPE", fstype_attr);
// It could be an empty DATABLOCK image, if it declares SIZE and FSTYPE
if ( type_att != "DATABLOCK" || size_attr.empty() || fstype_attr.empty() )
// DATABLOCK image needs SIZE and FSTYPE
if (type != DATABLOCK || size_attr.empty() || fstype_attr.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_attr.empty() )
{
@ -190,7 +200,7 @@ int Image::insert(SqlDB *db, string& error_str)
source = tmp_sourcestream.str();
}
state = DISABLED;
state = LOCKED; //LOCKED till the ImageManager copies it to the Repository
//--------------------------------------------------------------------------
// Insert the Image
@ -214,7 +224,7 @@ error_public_and_persistent:
goto error_common;
error_no_path:
if ( type_att == "DATABLOCK" )
if ( type == DATABLOCK )
{
error_str = "A DATABLOCK type IMAGE has to declare a PATH, or both "
"SIZE and FSTYPE.";
@ -225,6 +235,10 @@ 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 mutuallly exclusive.";
goto error_common;

View File

@ -105,6 +105,7 @@ int ImageManager::acquire_image(Image *img)
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -267,7 +268,53 @@ int ImageManager::enable_image(Image *img, bool to_enable)
int ImageManager::register_image(Image *img)
{
/* TEmplate src.... */
/* CALL DRIVER */
const ImageManagerDriver* imd = get();
ostringstream oss;
string path;
if ( imd == 0 )
{
NebulaLog::log("ImM",Log::ERROR,
"Could not get driver to update repository");
return -1;
}
img->get_template_attribute("PATH",path);
switch (img->get_type())
{
case Image::DATABLOCK:
if (path.empty() == true)
{
string fs;
string size;
img->get_template_attribute("SIZE", size);
img->get_template_attribute("FSTYPE", fs);
imd->mkfs(img->get_oid(), img->get_source(), fs, size);
oss << "Creating disk at " << img->get_source()
<< " of " << size << "Mb with format " << fs;
}
else
{
imd->cp(img->get_oid(),path, img->get_source());
oss << "Copying file " << path << " to " << img->get_source();
}
break;
case Image::CDROM:
case Image::OS:
imd->cp(img->get_oid(),path, img->get_source());
oss << "Copying file " << path << " to " << img->get_source();
break;
default:
break;
}
NebulaLog::log("ImM",Log::INFO,oss);
return 0;
}

View File

@ -55,7 +55,7 @@ void ImageManagerDriver::mv(int oid,
void ImageManagerDriver::mkfs(int oid,
const string& destination,
const string& fs,
int size_mb) const
const string& size_mb) const
{
ostringstream os;
@ -94,6 +94,8 @@ void ImageManagerDriver::protocol(
int id;
Image * image;
string info;
os << "Message received: " << message;
NebulaLog::log("ImG", Log::DEBUG, os);
@ -141,61 +143,106 @@ void ImageManagerDriver::protocol(
// Driver Actions
if ( action == "CP" )
{
// Nebula &ne = Nebula::instance();
// ImageManager* imgm= ne.get_imgm();
if ( result == "SUCCESS" )
{
NebulaLog::log("ImG", Log::INFO, "CP SUCCESS");
image->set_state(Image::READY);
ipool->update(image);
image->unlock();
NebulaLog::log("ImM", Log::INFO, "Image copied and ready to use.");
}
else
{
NebulaLog::log("ImG", Log::INFO, "CP FAILURE");
goto error_cp;
}
}
else if ( action == "MV" )
{
if ( result == "SUCCESS" )
{
NebulaLog::log("ImG", Log::INFO, "MV SUCCESS");
image->set_state(Image::READY);
ipool->update(image);
image->unlock();
NebulaLog::log("ImM", Log::INFO, "Image saved and ready to use.");
}
else
{
NebulaLog::log("ImG", Log::INFO, "MV FAILURE");
goto error_mv;
}
}
else if ( action == "MKFS" )
{
if ( result == "SUCCESS" )
{
NebulaLog::log("ImG", Log::INFO, "MKFS SUCCESS");
image->set_state(Image::READY);
ipool->update(image);
image->unlock();
NebulaLog::log("ImM", Log::INFO, "Image created and ready to use");
}
else
{
NebulaLog::log("ImG", Log::INFO, "MKFS FAILURE");
goto error_mkfs;
}
}
else if ( action == "RM" )
{
goto error_rm;
/* TODO
if ( result == "SUCCESS" )
{
NebulaLog::log("ImG", Log::INFO, "RM SUCCESS");
}
else
NebulaLog::log("ImG", Log::INFO, "RM FAILURE");
{
}
*/
}
else if (action == "LOG")
{
string info;
getline(is,info);
NebulaLog::log("ImG", Log::INFO, info.c_str());
NebulaLog::log("ImM", Log::INFO, info.c_str());
}
image->unlock();
error_cp:
os.str("");
os << "Error copying image in the repository";
goto error_common;
error_mv:
os.str("");
os << "Error saving image to the repository";
goto error_common;
error_mkfs:
os.str("");
os << "Error creating datablock";
goto error_common;
error_rm:
os.str("");
os << "Error removing image. Removed it manually and delete image";
goto error_common;
error_common:
getline(is,info);
if (!info.empty() && (info[0] != '-'))
{
os << ": " << info;
}
NebulaLog::log("ImM", Log::ERROR, os);
image->set_state(Image::ERROR);
ipool->update(image);
image->unlock();
return;
}
@ -206,3 +253,4 @@ void ImageManagerDriver::recover()
{
NebulaLog::log("ImG",Log::INFO,"Recovering Image Repository drivers");
}