1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-25 02:50:08 +03:00

F #4936: Refactor Driver and Message classes in OpenNebula daemon

co-authored-by: Pavel Czerny <pczerny@opennebula.systems>
This commit is contained in:
Ruben S. Montero 2020-06-29 12:14:00 +02:00
parent ebfd63ee9d
commit a3d8d4a65e
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
160 changed files with 5875 additions and 7058 deletions

View File

@ -82,7 +82,6 @@ main_env.Append(LIBPATH=[
cwd+'/src/cluster',
cwd+'/src/datastore',
cwd+'/src/group',
cwd+'/src/mad',
cwd+'/src/nebula',
cwd+'/src/pool',
cwd+'/src/template',
@ -281,7 +280,6 @@ build_scripts = [
'src/cluster/SConstruct',
'src/datastore/SConstruct',
'src/group/SConstruct',
'src/mad/SConstruct',
'src/nebula/SConstruct',
'src/pool/SConstruct',
'src/vm/SConstruct',

View File

@ -374,15 +374,6 @@ private:
*/
SqlDB * db;
/**
* Tablename for the ACL rules
*/
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
/**
* Callback function to unmarshall the ACL rules
* @param num the number of columns read from the DB

View File

@ -19,17 +19,13 @@
#include <time.h>
#include "MadManager.h"
#include "NebulaLog.h"
#include "ActionManager.h"
#include "AuthManagerDriver.h"
#include "PoolObjectSQL.h"
using namespace std;
#include "ProtocolMessages.h"
#include "DriverManager.h"
//Forward definitions
class AuthRequest;
class PoolObjectAuth;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -44,10 +40,10 @@ public:
};
AMAction(Actions a, AuthRequest *r):ActionRequest(ActionRequest::USER),
_action(a), _request(r){};
_action(a), _request(r) {}
AMAction(const AMAction& o):ActionRequest(o._type), _action(o._action),
_request(o._request){};
_request(o._request) {}
Actions action() const
{
@ -75,19 +71,22 @@ private:
extern "C" void * authm_action_loop(void *arg);
class AuthManager : public MadManager, public ActionListener
class AuthManager :
public DriverManager<Driver<auth_msg_t>>,
public ActionListener
{
public:
AuthManager(
time_t timer,
vector<const VectorAttribute*>& _mads):
MadManager(_mads), timer_period(timer)
const std::string& mads_location):
DriverManager(mads_location),
timer_period(timer)
{
am.addListener(this);
};
}
~AuthManager(){};
~AuthManager() {}
/**
* Triggers specific actions to the Auth Manager. This function
@ -116,15 +115,13 @@ public:
void finalize()
{
am.finalize();
};
}
/**
* Loads Virtual Machine Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Gets the thread identification.
@ -133,7 +130,7 @@ public:
pthread_t get_thread_id() const
{
return authm_thread;
};
}
/**
* Returns true if there is an authorization driver enabled
@ -143,7 +140,7 @@ public:
bool is_authz_enabled()
{
return authz_enabled;
};
}
private:
/**
@ -164,12 +161,12 @@ private:
/**
* Generic name for the Auth driver
*/
static const char * auth_driver_name;
static const char * auth_driver_name;
/**
* True if there is an authorization driver enabled
*/
bool authz_enabled;
bool authz_enabled;
/**
* Returns a pointer to a Auth Manager driver.
@ -178,13 +175,10 @@ private:
* @return the Auth driver with attribute name equal to value
* or 0 in not found
*/
const AuthManagerDriver * get(
const string& name,
const string& value)
const Driver<auth_msg_t> * get(const string& name)
{
return static_cast<const AuthManagerDriver *>
(MadManager::get(0,name,value));
};
return DriverManager::get_driver(name);
}
/**
* Returns a pointer to a Auth Manager driver. The driver is
@ -193,13 +187,10 @@ private:
* @return the TM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const AuthManagerDriver * get()
const Driver<auth_msg_t> * get()
{
string name("NAME");
return static_cast<const AuthManagerDriver *>
(MadManager::get(0,name,auth_driver_name));
};
return DriverManager::get_driver(auth_driver_name);
}
/**
* This function authenticates a user
@ -217,20 +208,45 @@ private:
*/
friend void * authm_action_loop(void *arg);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
/**
*
*/
static void _undefined(unique_ptr<auth_msg_t> msg);
/**
*
*/
void _authorize(unique_ptr<auth_msg_t> msg);
/**
*
*/
void _authenticate(unique_ptr<auth_msg_t> msg);
/**
*
*/
static void _log(unique_ptr<auth_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
void timer_action(const ActionRequest& ar)
{
check_time_outs_action();
};
}
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("AuM",Log::INFO,"Stopping Authorization Manager...");
MadManager::stop();
};
DriverManager::stop(drivers_timeout);
}
void user_action(const ActionRequest& ar);
};

View File

@ -1,101 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef AUTH_MANAGER_DRIVER_H_
#define AUTH_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "Mad.h"
using namespace std;
//Forward definition of the AuthManager Class
class AuthManager;
/**
* AuthManagerDriver provides a base class to implement TM
* Drivers. This class implements the protocol and recover functions
* from the Mad interface.
*/
class AuthManagerDriver : public Mad
{
public:
AuthManagerDriver(
int userid,
const map<string,string>& attrs,
bool sudo,
AuthManager * _authm):
Mad(userid,attrs,sudo), authm(_authm){};
virtual ~AuthManagerDriver(){};
/**
* Implements the VM Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* Re-starts the driver
*/
void recover();
private:
friend class AuthManager;
/**
* The AuthManager to notify results.
*/
AuthManager * authm;
/**
* Sends an authorization request to the MAD:
* "AUTHORIZE OPERATION_ID USER_ID REQUEST1 REQUEST2..."
* @param oid an id to identify the request.
* @param uid the user id.
* @param requests space separated list of requests in the form OP:OB:ID
* @param acl is the authorization result using the ACL engine for
* this request
*/
void authorize(int oid, int uid, const string& requests, bool acl) const;
/**
* Sends an authorization request to the MAD:
* "AUTHENTICATE REQUEST_ID USER_ID USER_NAME PASSWORD XMLRPC_TOKEN"
* @param oid an id to identify the request.
* @param uid the user id.
* @param auth_driver
* @param username
* @param password
* @param session token from the xml-rpc parameter
*/
void authenticate(int oid,
int uid,
const string& auth_driver,
const string& username,
const string& password,
const string& session) const;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*AUTH_MANAGER_DRIVER_H_*/

View File

@ -17,7 +17,6 @@
#ifndef AUTH_REQUEST_H_
#define AUTH_REQUEST_H_
#include <time.h>
#include <set>
#include "ActionManager.h"
@ -27,8 +26,6 @@
#include "SyncRequest.h"
using namespace std;
/**
* The AuthRequest class is used to pass an Authorization or Authentication
* request to the AuthManager. The result of the request will be stored
@ -37,7 +34,11 @@ using namespace std;
class AuthRequest : public SyncRequest
{
public:
AuthRequest(int _uid, set<int> _gids): uid(_uid),gids(_gids),self_authorize(true){};
AuthRequest(int _uid, const std::set<int>& _gids)
: uid(_uid)
, gids(_gids)
, self_authorize(true)
{}
~AuthRequest(){};
@ -57,7 +58,7 @@ public:
NONE = 0x0LL
};
static string operation_to_str(Operation op)
static std::string operation_to_str(Operation op)
{
switch (op)
{
@ -75,7 +76,7 @@ public:
return "";
};
static Operation str_to_operation(string str)
static Operation str_to_operation(std::string str)
{
if (str == "USE") return USE;
else if (str == "MANAGE") return MANAGE;
@ -88,10 +89,10 @@ public:
* Sets the challenge to authenticate an user
* @param challenge a driver specific authentication challenge
*/
void add_authenticate(const string &_driver,
const string &_username,
const string &_password,
const string &_session)
void add_authenticate(const std::string &_driver,
const std::string &_username,
const std::string &_password,
const std::string &_session)
{
username = _username;
password = _password;
@ -110,7 +111,8 @@ public:
* @param type of the object to be created
* @param txml template of the new object
*/
void add_create_auth(int uid, int gid, PoolObjectSQL::ObjectType type, const string& txml)
void add_create_auth(int uid, int gid, PoolObjectSQL::ObjectType type,
const std::string& txml)
{
PoolObjectAuth perms; //oid & gid set to -1
@ -140,14 +142,14 @@ public:
* @return a space separated list of auth requests, or an empty string if
* no auth requests were added
*/
string get_auths()
std::string get_auths()
{
ostringstream oss;
std::ostringstream oss;
unsigned int i;
if ( auths.empty() )
{
return string();
return std::string();
}
for (i=0; i<auths.size()-1; i++)
@ -167,8 +169,8 @@ public:
bool core_authenticate()
{
string sha1_session = one_util::sha1_digest(session);
string sha256_session = one_util::sha256_digest(session);
std::string sha1_session = one_util::sha1_digest(session);
std::string sha256_session = one_util::sha256_digest(session);
return (password == sha1_session) || (password == sha256_session);
}
@ -185,32 +187,32 @@ private:
/**
* The user groups ID set
*/
set<int> gids;
std::set<int> gids;
/**
* Username to authenticate the user
*/
string username;
std::string username;
/**
* User password to authenticate the user
*/
string password;
std::string password;
/**
* Authentication token as sent in the XML-RPC call (user:session)
*/
string session;
std::string session;
/**
* Authentication driver to be used with this request
*/
string driver;
std::string driver;
/**
* A list of authorization requests
*/
vector<string> auths;
std::vector<std::string> auths;
/**
* Plain authorization for the request
@ -230,7 +232,7 @@ private:
*/
void add_auth(Operation op,
const PoolObjectAuth& ob_perms,
string ob_template);
const std::string& ob_template);
};
#endif

View File

@ -18,7 +18,6 @@
#define CLUSTER_H_
#include "PoolObjectSQL.h"
#include "ObjectCollection.h"
#include "DatastorePool.h"
#include "ClusterTemplate.h"
#include "BitMap.h"
@ -161,19 +160,7 @@ private:
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
static const char * datastore_table;
static const char * datastore_db_names;
static const char * datastore_db_bootstrap;
static const char * network_table;
static const char * network_db_names;
static const char * network_db_bootstrap;
static const char * bitmap_table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -187,22 +174,7 @@ private:
* Bootstraps the database table(s) associated to the Cluster
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
int rc;
ostringstream oss;
oss.str(Cluster::db_bootstrap);
rc = db->exec_local_wr(oss);
oss.str(Cluster::datastore_db_bootstrap);
rc += db->exec_local_wr(oss);
oss.str(Cluster::network_db_bootstrap);
rc += db->exec_local_wr(oss);
return rc;
};
static int bootstrap(SqlDB * db);
/**
* Writes the Cluster in the database.

View File

@ -19,6 +19,7 @@
#include "Cluster.h"
#include "PoolSQL.h"
#include "OneDB.h"
using namespace std;
@ -190,7 +191,7 @@ public:
rc = Cluster::bootstrap(_db);
rc += _db->exec_local_wr(
BitMap<0>::bootstrap(Cluster::bitmap_table, oss_bitmap));
BitMap<0>::bootstrap(one_db::cluster_bitmap_table, oss_bitmap));
return rc;
};
@ -209,7 +210,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "CLUSTER_POOL", "body", Cluster::table, where,
return PoolSQL::dump(oss, "CLUSTER_POOL", "body",
one_db::cluster_table, where,
sid, eid, desc);
};

View File

@ -359,12 +359,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -378,12 +372,7 @@ private:
* Bootstraps the database table(s) associated to the Datastore
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(Datastore::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the Datastore in the database.

View File

@ -19,6 +19,7 @@
#include "Datastore.h"
#include "PoolSQL.h"
#include "OneDB.h"
using namespace std;
@ -152,7 +153,7 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "DATASTORE_POOL", "body", Datastore::table,
return PoolSQL::dump(oss, "DATASTORE_POOL", "body", one_db::ds_table,
where, sid, eid, desc);
};
@ -164,7 +165,7 @@ public:
*/
int list(vector<int>& oids)
{
return PoolSQL::list(oids, Datastore::table);
return PoolSQL::list(oids, one_db::ds_table);
}
/**

View File

@ -103,12 +103,7 @@ private:
* Bootstraps the database table(s) associated to the Document
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(Document::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Rebuilds the object from an xml formatted string
@ -138,12 +133,6 @@ protected:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the Document in the database.
* @param db pointer to the db

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "Document.h"
#include "OneDB.h"
/**
* The Document Pool class.
@ -27,7 +28,7 @@ class DocumentPool : public PoolSQL
{
public:
DocumentPool(SqlDB * db) : PoolSQL(db, Document::table){};
DocumentPool(SqlDB * db) : PoolSQL(db, one_db::doc_table) {};
~DocumentPool(){};
@ -103,8 +104,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "DOCUMENT_POOL", "body", Document::table, where,
sid, eid, desc);
return PoolSQL::dump(oss, "DOCUMENT_POOL", "body", one_db::doc_table,
where, sid, eid, desc);
};
/**

View File

@ -1,5 +1,5 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
/* */
/* 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 */
@ -21,6 +21,7 @@
#include <algorithm>
#include <assert.h>
#include <typeinfo>
#include <sstream>
/**
* Converts string to enum and back
@ -42,8 +43,10 @@ public:
if (check_sanity && (int)T::ENUM_MAX != enum_map.size())
{
std::ostringstream oss;
oss << " EString: Not all strings defined for enum "
<< typeid(T).name();
throw std::runtime_error(oss.str());
}
}

View File

@ -177,17 +177,7 @@ private:
// Constructor
// *************************************************************************
Group(int id, const string& name):
PoolObjectSQL(id,GROUP,name,-1,-1,"","",table),
quota(),
users("USERS"),
admins("ADMINS")
{
// Allow users in this group to see it
group_u = 1;
obj_template = new GroupTemplate;
}
Group(int id, const string& name);
virtual ~Group() = default;
@ -217,12 +207,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -236,12 +220,7 @@ private:
* Bootstraps the database table(s) associated to the Group
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_group(Group::db_bootstrap);
return db->exec_local_wr(oss_group);
}
static int bootstrap(SqlDB * db);
/**
* Reads the Group (identified with its OID) from the database.

View File

@ -70,15 +70,6 @@ private:
friend class VirtualMachine;
friend class VirtualMachinePool;
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
void non_persistent_data();
// ----------------------------------------

View File

@ -93,18 +93,7 @@ private:
// Constructor/Destructor
// *************************************************************************
Hook(Template * tmpl): PoolObjectSQL(-1, HOOK, "", -1, -1, "", "", table),
type(HookType::UNDEFINED), cmd(""), remote(false), _hook(0)
{
if (tmpl != 0)
{
obj_template = tmpl;
}
else
{
obj_template = new Template();
}
};
Hook(Template * tmpl);
~Hook();
@ -164,12 +153,6 @@ private:
// Database implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Construct the XML representation of the hook
* @param xml the resulting XML string
@ -183,12 +166,7 @@ private:
* Bootstraps the database table(s) associated to the Host
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
std::ostringstream oss_hook(Hook::db_bootstrap);
return db->exec_local_wr(oss_hook);
};
static int bootstrap(SqlDB * db);
/**
* Writes/updates the Hosts data fields in the database.

View File

@ -106,11 +106,6 @@ private:
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
/**
* Pointer to the database.

View File

@ -17,9 +17,9 @@
#ifndef HOOK_MANAGER_H_
#define HOOK_MANAGER_H_
#include "MadManager.h"
#include "ProtocolMessages.h"
#include "DriverManager.h"
#include "ActionManager.h"
#include "HookManagerDriver.h"
#include <vector>
@ -67,14 +67,16 @@ private:
extern "C" void * hm_action_loop(void *arg);
class HookManager : public MadManager, public ActionListener
class HookManager :
public DriverManager<Driver<hook_msg_t>>,
public ActionListener
{
public:
HookManager(std::vector<const VectorAttribute*>& _mads):MadManager(_mads)
HookManager(const std::string& mad_location): DriverManager(mad_location)
{
am.addListener(this);
};
}
virtual ~HookManager() = default;
@ -97,11 +99,9 @@ public:
/**
* Loads Hook Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid=0);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Triggers specific actions to the Hook Manager.
@ -121,7 +121,7 @@ public:
void finalize()
{
am.finalize();
};
}
/**
* Returns a pointer to a Information Manager MAD. The driver is
@ -130,13 +130,10 @@ public:
* @return the Hook driver owned by uid 0, with attribute "NAME" equal to
* name or 0 in not found
*/
const HookManagerDriver * get()
const Driver<hook_msg_t> * get()
{
std::string name("NAME");
return static_cast<const HookManagerDriver *>
(MadManager::get(0, name,hook_driver_name));
};
return DriverManager::get_driver(hook_driver_name);
}
static std::string * format_message(const string& args, const string&remote_host,
int hook_id);
@ -151,7 +148,7 @@ private:
/**
* Generic name for the Hook driver
*/
static const char * hook_driver_name;
static const char * hook_driver_name;
/**
* Thread id for the HookManager
@ -175,14 +172,39 @@ private:
*/
void retry_action(const std::string& message);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
/**
*
*/
static void _undefined(unique_ptr<hook_msg_t> msg);
/**
*
*/
void _execute(unique_ptr<hook_msg_t> msg);
/**
*
*/
void _retry(unique_ptr<hook_msg_t> msg);
/**
*
*/
static void _log(unique_ptr<hook_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("HKM",Log::INFO,"Stopping Hook Manager...");
MadManager::stop();
DriverManager::stop(drivers_timeout);
};
void user_action(const ActionRequest& ar);

View File

@ -1,101 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef HOOK_MANAGER_DRIVER_H_
#define HOOK_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "Mad.h"
#include "VirtualMachinePool.h"
#include "HookLog.h"
using namespace std;
/**
* HookManagerDriver provides a base class to implement Hook (Execution)
* Drivers. This class implements the protocol and recover functions
* from the Mad interface. This class may be used to further specialize
* the Execution driver.
*/
class HookManagerDriver : public Mad
{
public:
HookManagerDriver(
int userid,
const map<string,string>& attrs,
bool sudo)
: Mad(userid,attrs,sudo) {};
virtual ~HookManagerDriver(){};
/**
* Implements the Hook driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* TODO: What do we need here? just poll the Hosts to recover..
*/
void recover();
/**<id> <hook_name> <LOCAL|host> <script> <args|->
* Sends an execution request to the MAD: "EXECUTE id name local cmd args"
* @param oid the virtual machine id.
* @param hook_name the name of the hook
* @param command to be executed
* @param arguments for the command
*/
void execute(
int oid,
const string& hook_name,
const string& command,
const string& arguments ) const;
/**
* Sends an execution request to the MAD: "EXECUTE id name host cmd args"
* @param oid the virtual machine id.
* @param hook_name the name of the hook
* @param host_name the name of the host to execute the hook
* @param command to be executed
* @param arguments for the command
*/
void execute(
int oid,
const string& hook_name,
const string& host_name,
const string& command,
const string& arguments ) const;
void execute(
const string& message ) const;
void retry(
const string& message ) const;
private:
friend class HookManager;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*HOOK_MANAGER_DRIVER_H_*/

View File

@ -20,6 +20,7 @@
#include "PoolSQL.h"
#include "Hook.h"
#include "HookAPI.h"
#include "OneDB.h"
using namespace std;
@ -27,7 +28,7 @@ class HookPool : public PoolSQL
{
public:
HookPool(SqlDB * db) : PoolSQL(db, Hook::table){};
HookPool(SqlDB * db) : PoolSQL(db, one_db::hook_table){};
~HookPool(){};
@ -83,8 +84,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "HOOK_POOL", "body", Hook::table, where,
sid, eid, desc);
return PoolSQL::dump(oss, "HOOK_POOL", "body", one_db::hook_table,
where, sid, eid, desc);
};
/**

View File

@ -19,14 +19,14 @@
#include <time.h>
#include "MadManager.h"
#include "ProtocolMessages.h"
#include "DriverManager.h"
#include "ActionManager.h"
#include "IPAMManagerDriver.h"
#include "Attribute.h"
#include "NebulaLog.h"
//Forward definitions
class IPAMRequest;
class VectorAttribute;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -75,12 +75,14 @@ private:
extern "C" void * ipamm_action_loop(void *arg);
class IPAMManager : public MadManager, public ActionListener
class IPAMManager :
public DriverManager<Driver<ipam_msg_t>>,
public ActionListener
{
public:
IPAMManager(time_t timer, std::vector<const VectorAttribute*>& _mads):
MadManager(_mads), timer_period(timer)
IPAMManager(time_t timer, const std::string mad_location):
DriverManager(mad_location), timer_period(timer)
{
am.addListener(this);
};
@ -110,11 +112,9 @@ public:
/**
* Loads IPAM Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Gets the thread identification.
@ -154,20 +154,6 @@ private:
*/
static const char * ipam_driver_name;
/**
* Returns a pointer to a IPAM Manager driver.
* @param name of an attribute of the driver (e.g. its type)
* @param value of the attribute
* @return the IPAM driver with attribute name equal to value
* or 0 in not found
*/
const IPAMManagerDriver * get(const std::string& name,
const std::string& value)
{
return static_cast<const IPAMManagerDriver *>
(MadManager::get(0, name, value));
};
/**
* Returns a pointer to a IPAM Manager driver. The driver is
* searched by its name.
@ -175,12 +161,9 @@ private:
* @return the IPAM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const IPAMManagerDriver * get()
const Driver<ipam_msg_t> * get()
{
std::string name("NAME");
return static_cast<const IPAMManagerDriver *>
(MadManager::get(0, name, ipam_driver_name));
return DriverManager::get_driver(ipam_driver_name);
};
/**
@ -213,7 +196,7 @@ private:
* @param ir the IPAM request
* @return pointer to the IPAM driver to use, 0 on failure
*/
const IPAMManagerDriver * setup_request(IPAMRequest * ir);
void send_request(IPAMManagerMessages type, IPAMRequest * ir);
/**
* Function to execute the Manager action loop method within a new pthread
@ -221,6 +204,24 @@ private:
*/
friend void * ipamm_action_loop(void *arg);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
/**
*
*/
static void _undefined(unique_ptr<ipam_msg_t> msg);
/**
*
*/
void _notify_request(unique_ptr<ipam_msg_t> msg);
/**
*
*/
static void _log(unique_ptr<ipam_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
@ -229,11 +230,13 @@ private:
check_time_outs_action();
};
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("IPM",Log::INFO,"Stopping IPAM Manager...");
MadManager::stop();
DriverManager::stop(drivers_timeout);
};
void user_action(const ActionRequest& ar);

View File

@ -1,119 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef IPAM_MANAGER_DRIVER_H_
#define IPAM_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "Mad.h"
//Forward definition of the IPAMManager Class
class IPAMManager;
/**
* IPAMManagerDriver provides a base class to implement IPAM
* Drivers. This class implements the protocol and recover functions
* from the Mad interface.
*/
class IPAMManagerDriver : public Mad
{
public:
IPAMManagerDriver(int uid, const std::map<string,string>& attrs, bool sudo,
IPAMManager * _ipamm):Mad(uid,attrs,sudo), ipamm(_ipamm){};
virtual ~IPAMManagerDriver(){};
/**
* Implements the IPAM Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* Re-starts the driver
*/
void recover();
private:
friend class IPAMManager;
/**
* The IPAMManager to notify results.
*/
IPAMManager * ipamm;
/**
* Register or requests a new AddressRange (network) into the IPAM
*/
void register_address_range(int id, const std::string& arg) const
{
send_message("REGISTER_ADDRESS_RANGE", id, arg);
}
/**
* Unregister an AddressRange from the IPAM
*/
void unregister_address_range(int id, const std::string& arg) const
{
send_message("UNREGISTER_ADDRESS_RANGE", id, arg);
}
/**
* Get a free address (or range)
*/
void get_address(int id, const std::string& arg) const
{
send_message("GET_ADDRESS", id, arg);
}
/**
* Sets an address (or range) as used
*/
void allocate_address(int id, const std::string& arg) const
{
send_message("ALLOCATE_ADDRESS", id, arg);
}
/**
* Free a previously requested or allocated address
*/
void free_address(int id, const std::string& arg) const
{
send_message("FREE_ADDRESS", id, arg);
}
/**
* Sends a generic message to the IPAM driver
*/
void send_message(const char * name, int id, const std::string& arg) const
{
std::ostringstream os;
os << name << " " << id << " " << arg << endl;
write(os);
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*IPAM_MANAGER_DRIVER_H_*/

View File

@ -699,12 +699,7 @@ private:
* Bootstraps the database table(s) associated to the Image
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_image(Image::db_bootstrap);
return db->exec_local_wr(oss_image);
};
static int bootstrap(SqlDB * db);
/**
* "Encrypts" the password with SHA256 digest
@ -732,12 +727,6 @@ protected:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the Image in the database.
* @param db pointer to the db

View File

@ -17,20 +17,23 @@
#ifndef IMAGE_MANAGER_H_
#define IMAGE_MANAGER_H_
#include "MadManager.h"
#include "DriverManager.h"
#include "ProtocolMessages.h"
#include "ActionManager.h"
#include "ImageManagerDriver.h"
#include "NebulaLog.h"
using namespace std;
extern "C" void * image_action_loop(void *arg);
class DatastorePool;
class Image;
class ImagePool;
class Snapshots;
class Template;
class ImageManager : public MadManager, public ActionListener
class ImageManager :
public DriverManager<Driver<image_msg_t>>,
public ActionListener
{
public:
@ -38,9 +41,9 @@ public:
time_t _monitor_period,
ImagePool * _ipool,
DatastorePool * _dspool,
vector<const VectorAttribute*>& _mads,
const std::string& _mads_location,
int _monitor_vm_disk):
MadManager(_mads),
DriverManager(_mads_location),
timer_period(_timer_period),
monitor_period(_monitor_period),
monitor_vm_disk(_monitor_vm_disk),
@ -62,11 +65,9 @@ public:
/**
* Loads the Image Driver defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid=0);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Gets the thread identification.
@ -99,7 +100,7 @@ public:
* @param attach true if attaching the image to a VM
* @return pointer to the image or 0 if could not be acquired
*/
Image * acquire_image(int vm_id, int image_id, bool attach, string& error);
Image * acquire_image(int vm_id, int image_id, bool attach, std::string& error);
/**
* Try to acquire an image from the repository for a VM.
@ -109,7 +110,8 @@ public:
* @param attach true if attaching the image to a VM
* @return pointer to the image or 0 if could not be acquired
*/
Image * acquire_image(int vm_id, const string& name, int uid, bool attach, string& error);
Image * acquire_image(int vm_id, const std::string& name,
int uid, bool attach, std::string& error);
/**
* Releases an image and triggers any needed operations in the repo
@ -153,7 +155,7 @@ public:
* @param error_str Error reason, if any
* @return 0 on success
*/
int enable_image(int iid, bool to_enable, string& error_str);
int enable_image(int iid, bool to_enable, std::string& error_str);
/**
* Adds a new image to the repository copying or creating it as needed
@ -165,9 +167,9 @@ public:
* @return 0 on success
*/
int register_image(int iid,
const string& ds_data,
const string& extra_data,
string& error);
const std::string& ds_data,
const std::string& extra_data,
std::string& error);
/**
* Checks if an image is ready to be cloned
@ -177,7 +179,7 @@ public:
*
* @return 0 if the image can be cloned, -1 otherwise
*/
int can_clone_image(int cloning_id, ostringstream& oss_error);
int can_clone_image(int cloning_id, std::ostringstream& oss_error);
/**
* Sets the state to CLONE for the given image
@ -225,9 +227,9 @@ public:
*/
int clone_image(int new_id,
int cloning_id,
const string& ds_data,
const string& extra_data,
string& error);
const std::string& ds_data,
const std::string& extra_data,
std::string& error);
/**
* Deletes an image from the repository and the DB. The Datastore image list
* is also updated
@ -235,7 +237,7 @@ public:
* @param error_str Error reason, if any
* @return 0 on success
*/
int delete_image(int iid, string& error_str);
int delete_image(int iid, std::string& error_str);
/**
* Gets the size of an image by calling the STAT action of the associated
@ -247,7 +249,9 @@ public:
* occurred describing the error.
* @result 0 on success
*/
int stat_image(Template* img_tmpl, const string& ds_tmpl, string& res);
int stat_image(Template* img_tmpl,
const std::string& ds_tmpl,
std::string& res);
/**
* Trigger a monitor action for the datastore.
@ -284,7 +288,7 @@ public:
* @param error_str Error reason, if any
* @return 0 on success
*/
int delete_snapshot(int iid, int sid, string& error);
int delete_snapshot(int iid, int sid, std::string& error);
/**
* Reverts image state to a previous snapshot
@ -293,7 +297,7 @@ public:
* @param error_str Error reason, if any
* @return 0 on success
*/
int revert_snapshot(int iid, int sid, string& error);
int revert_snapshot(int iid, int sid, std::string& error);
/**
* Flattens ths snapshot by commiting changes to base image.
@ -302,13 +306,13 @@ public:
* @param error_str Error reason, if any
* @return 0 on success
*/
int flatten_snapshot(int iid, int sid, string& error);
int flatten_snapshot(int iid, int sid, std::string& error);
private:
/**
* Generic name for the Image driver
*/
static const char * image_driver_name;
static const char * image_driver_name;
/**
* Thread id for the Image Manager
@ -350,12 +354,9 @@ private:
* Returns a pointer to the Image Manager Driver used for the Repository
* @return the Image Manager driver or 0 in not found
*/
const ImageManagerDriver * get()
const Driver<image_msg_t> * get()
{
string name("NAME");
return static_cast<const ImageManagerDriver *>
(MadManager::get(0,name,image_driver_name));
return DriverManager::get_driver(image_driver_name);
};
/**
@ -369,7 +370,7 @@ private:
* @param action the name of the action
* @param arg arguments for the action function
*/
void do_action(const string& action, void * arg);
void do_action(const std::string& action, void * arg);
/**
* Acquires an image updating its state.
@ -377,14 +378,14 @@ private:
* @param attach true if attaching the image to a VM
* @return 0 on success
*/
int acquire_image(int vm_id, Image *img, bool attach, string& error);
int acquire_image(int vm_id, Image *img, bool attach, std::string& error);
/**
* Moves a file to an image in the repository
* @param image to be updated (it's source attribute)
* @param source path of the disk file
*/
void move_image(Image *img, const string& source);
void move_image(Image *img, const std::string& source);
/**
* Formats an XML message for the MAD
@ -394,9 +395,67 @@ private:
* @param extra_data additional XML formatted data for the driver
* @return the XML message
*/
static string * format_message(const string& img_data,
const string& ds_data,
const string& extra_data);
static std::string* format_message(const std::string& img_data,
const std::string& ds_data,
const std::string& extra_data);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
/**
*
*/
static void _undefined(unique_ptr<image_msg_t> msg);
/**
*
*/
void _stat(unique_ptr<image_msg_t> msg);
/**
*
*/
void _cp(unique_ptr<image_msg_t> msg);
/**
*
*/
void _clone(unique_ptr<image_msg_t> msg);
/**
*
*/
void _mkfs(unique_ptr<image_msg_t> msg);
/**
*
*/
void _rm(unique_ptr<image_msg_t> msg);
/**
*
*/
void _monitor(unique_ptr<image_msg_t> msg);
/**
*
*/
void _snap_delete(unique_ptr<image_msg_t> msg);
/**
*
*/
void _snap_revert(unique_ptr<image_msg_t> msg);
/**
*
*/
void _snap_flatten(unique_ptr<image_msg_t> msg);
/**
*
*/
static void _log(unique_ptr<image_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
@ -406,10 +465,12 @@ private:
*/
void timer_action(const ActionRequest& ar);
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("ImM",Log::INFO,"Stopping Image Manager...");
MadManager::stop();
stop(drivers_timeout);
};
};

View File

@ -1,144 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef IMAGE_MANAGER_DRIVER_H_
#define IMAGE_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "Mad.h"
using namespace std;
//Forward definition of related classes
class ImagePool;
class ImageManager;
class DatastorePool;
/**
* ImageManagerDriver represents the basic abstraction for Image Repository
* drivers. It implements the protocol and recover functions from the Mad
* interface.
*/
class ImageManagerDriver : public Mad
{
public:
ImageManagerDriver(int userid,
const map<string,string>& attrs,
bool sudo,
ImagePool* _ipool,
DatastorePool * _dspool):
Mad(userid,attrs,sudo),ipool(_ipool),dspool(_dspool){};
virtual ~ImageManagerDriver(){};
/**
* Implements the Image Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* TODO: What do we need here? Check on-going xfr?
*/
void recover();
private:
friend class ImageManager;
/**
* Reference to the ImagePool
*/
ImagePool * ipool;
/**
* Reference to the DatastorePool
*/
DatastorePool * dspool;
/**
* 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 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
* @param drv_msg xml data for the mad operation.
*/
void stat(int oid, const string& drv_msg) const;
/**
* Sends a make filesystem request to the MAD.
* @param oid the image id.
* @param drv_msg xml data for the mad operation.
*/
void mkfs(int oid, const string& drv_msg) const;
/**
* Sends a delete request to the MAD: "DELETE IMAGE_ID PATH"
* @param oid the image id.
* @param drv_msg xml data for the mad operation.
*/
void rm(int oid, const string& drv_msg) const;
/**
* Sends a monitor request to the MAD: "MONITOR DS_ID DS_XML"
* @param oid the datastore id.
* @param drv_msg xml data for the mad operation.
*/
void monitor(int oid, const string& drv_msg) const;
/**
* Sends a delete snapshot command: "SNAP_DELETE DS_ID DS_XML"
* @param oid the datastore id.
* @param drv_msg xml data for the mad operation.
*/
void snapshot_delete(int oid, const string& drv_msg) const;
/**
* Sends a revert snapshot command: "SNAP_REVERT DS_ID DS_XML"
* @param oid the datastore id.
* @param drv_msg xml data for the mad operation.
*/
void snapshot_revert(int oid, const string& drv_msg) const;
/**
* Sends a flatten snapshot command: "SNAP_FLATTEN DS_ID DS_XML"
* @param oid the datastore id.
* @param drv_msg xml data for the mad operation.
*/
void snapshot_flatten(int oid, const string& drv_msg) const;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*IMAGE_MANAGER_DRIVER_H_*/

View File

@ -21,6 +21,7 @@
#include "Image.h"
#include "NebulaLog.h"
#include "Datastore.h"
#include "OneDB.h"
#include <time.h>
#include <sstream>
@ -164,8 +165,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "IMAGE_POOL", "body", Image::table, where, sid,
eid, desc);
return PoolSQL::dump(oss, "IMAGE_POOL", "body", one_db::image_table,
where, sid, eid, desc);
}
/**

View File

@ -19,7 +19,7 @@
#include "DriverManager.h"
#include "ActionManager.h"
#include "OpenNebulaMessages.h"
#include "ProtocolMessages.h"
#include "RaftManager.h"
class HostPool;
@ -27,11 +27,10 @@ class Host;
class VirtualMachinePool;
class InformationManager :
public DriverManager<OpenNebulaMessages, Driver<OpenNebulaMessages>>,
public DriverManager<Driver<im_msg_t>>,
public ActionListener
{
public:
InformationManager(
HostPool * _hpool,
VirtualMachinePool * _vmpool,
@ -104,22 +103,22 @@ protected:
/**
* Received undefined message -> print error
*/
static void _undefined(unique_ptr<Message<OpenNebulaMessages>> msg);
static void _undefined(unique_ptr<im_msg_t> msg);
/**
* Message HOST_STATE update from monitor
*/
void _host_state(unique_ptr<Message<OpenNebulaMessages>> msg);
void _host_state(unique_ptr<im_msg_t> msg);
/**
* Message HOST_SYSTEM update from monitor
*/
void _host_system(unique_ptr<Message<OpenNebulaMessages>> msg);
void _host_system(unique_ptr<im_msg_t> msg);
/**
* Message VM_STATE from monitor
*/
void _vm_state(unique_ptr<Message<OpenNebulaMessages>> msg);
void _vm_state(unique_ptr<im_msg_t> msg);
private:
/**

View File

@ -19,22 +19,22 @@
#include <map>
#include <string>
#include <sstream>
#include "VirtualMachineManagerDriver.h"
class LibVirtDriver : public VirtualMachineManagerDriver
{
public:
LibVirtDriver(int userid, const map<string,string> &attrs, bool sudo,
VirtualMachinePool * pool, const string& emu)
:VirtualMachineManagerDriver(userid, attrs,sudo,pool), emulator(emu)
{};
LibVirtDriver(const std::string& mad_location,
const std::map<std::string, std::string> &attrs,
const std::string& emu)
: VirtualMachineManagerDriver(mad_location, attrs), emulator(emu)
{}
~LibVirtDriver(){};
~LibVirtDriver() = default;
int validate_raw(const string& raw_section, string& error) const override;
int validate_raw(const std::string& raw_section,
std::string& error) const override;
private:
static const float CGROUP_BASE_CPU_SHARES;
@ -47,7 +47,8 @@ private:
static const char * XML_DOMAIN_RNG_PATH;
int deployment_description(const VirtualMachine * vm, const string& fn) const override
int deployment_description(const VirtualMachine * vm,
const std::string& fn) const override
{
int rc = -1;
@ -59,9 +60,10 @@ private:
return rc;
}
int deployment_description_kvm(const VirtualMachine * v, const string& f) const;
int deployment_description_kvm(const VirtualMachine * v,
const std::string& f) const;
const string emulator;
const std::string emulator;
};
#endif /*LIBVIRT_DRIVER_H_*/

View File

@ -341,11 +341,6 @@ private:
// -------------------------------------------------------------------------
// DataBase implementation
// -------------------------------------------------------------------------
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
/**
* Replicates writes in the followers and apply changes to DB state once

View File

@ -1,186 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef MAD_H_
#define MAD_H_
#include <pthread.h>
#include <sys/types.h>
#include <map>
#include <string>
#include <sstream>
#include <unistd.h>
#include "Log.h"
using namespace std;
/**
* Base class to build specific middleware access drivers (MAD).
* This class provides generic MAD functionality.
*/
class Mad
{
protected:
/**
* The constructor initialize the class members but DOES NOT start the
* driver. A subsequent call to the start() method is needed.
* @param userid user running this MAD
* @param attrs configuration attributes for the driver
* @param sudo the driver is started through sudo if true
*/
Mad(
int userid,
const map<string,string> &attrs,
bool sudo):
uid(userid),
attributes(attrs),
sudo_execution(sudo),
pid(-1)
{};
/**
* The destructor of the class finalizes the driver process, and all its
* associated resources (i.e. pipes)
*/
virtual ~Mad();
/**
* Send a command to the driver
* @param os an output string stream with the message, it must be
* terminated with the end of line character.
*/
void write(
ostringstream& os) const
{
string str;
const char * cstr;
str = os.str();
cstr = str.c_str();
::write(nebula_mad_pipe, cstr, str.size());
};
/**
* Send a DRIVER_CANCEL command to the driver
* @param oid identifies the action (that associated with oid)
*/
void driver_cancel (const int oid) const
{
ostringstream os;
os << "DRIVER_CANCEL " << oid << endl;
write(os);
};
/**
* Sets the log message type as specify by the driver.
* @param first character of the type string
* @return the message type
*/
Log::MessageType log_type(const char r) const
{
Log::MessageType lt;
switch (r)
{
case 'E':
lt = Log::ERROR;
break;
case 'I':
lt = Log::INFO;
break;
case 'D':
lt = Log::DEBUG;
break;
default:
lt = Log::INFO;
}
return lt;
}
friend class MadManager;
/**
* Communication pipe file descriptor. Represents the MAD to nebula
* communication stream (nebula<-mad)
*/
int mad_nebula_pipe;
/**
* Communication pipe file descriptor. Represents the nebula to MAD
* communication stream (nebula->mad)
*/
int nebula_mad_pipe;
private:
/**
* User running this MAD as defined in the upool DB
*/
int uid;
/**
* Mad configuration attributes (e.g. executable, attributes...). Attribute
* names MUST be lowecase.
*/
map<string,string> attributes;
/**
* True if the mad is to be executed through sudo, with the identity of the
* Mad owner (uid).
*/
bool sudo_execution;
/**
* Process ID of the running MAD.
*/
pid_t pid;
/**
* Starts the MAD. This function creates a new process, sets up the
* communication pipes and sends the initialization command to the driver.
* @return 0 on success
*/
int start();
/**
* Reloads the driver: sends the finalize command, "waits" for the
* driver process and closes the communication pipes. Then the driver is
* started again by calling the start() function
* @return 0 on success
*/
int reload();
/**
* Implements the driver specific protocol, this function should trigger
* actions on the associated manager.
* @param message the string read from the driver
*/
virtual void protocol(const string& message) const = 0;
/**
* This function is called whenever the driver crashes. This function
* should perform the actions needed to recover the VMs.
*/
virtual void recover() = 0;
};
#endif /*MAD_H_*/

View File

@ -1,198 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef MAD_MANAGER_H_
#define MAD_MANAGER_H_
#include <pthread.h>
#include <sys/types.h>
#include <string>
#include <vector>
#include <sstream>
#include <vector>
#include "Mad.h"
#include "Attribute.h"
using namespace std;
class SyncRequest;
extern "C" void * mad_manager_listener(void * _mm);
/**
* Provides general functionality for driver management. The MadManager serves
* Nebula managers as base clase.
*/
class MadManager
{
public:
/**
* Function to initialize the MAD management system. This function
* MUST be called once before using the MadManager class. This function
* blocks the SIG_PIPE (broken pipe) signal that may occur when a driver
* crashes
*/
static void mad_manager_system_init();
/**
* Loads Virtual Machine Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
*/
virtual int load_mads(int uid) = 0;
/**
* Notify the result of an auth request
*/
void notify_request(int id, bool result, const string& message);
protected:
MadManager(vector<const VectorAttribute *>& _mads);
virtual ~MadManager();
/**
* Vector containing Mad configuration for this Manager, as described in
* the nebula location
*/
vector<const VectorAttribute *> mad_conf;
/**
* This function initialize the communication pipes to register new MADs
* in the manager, and starts a listener to wait for driver messages.
*/
virtual int start();
/**
* This function closes the communication pipes, stops the listener thread,
* and finalize the associated drivers.
*/
virtual void stop();
/**
* Get a mad
*/
virtual const Mad * get(int uid, const string& name, const string& value);
/**
* Register a new mad in the manager. The Mad is previously started, and
* then the listener thread is notified through the pipe_w stream. In case
* of failure the calling function MUST free the Mad.
* @param mad pointer to the mad to be added to the manager.
* @return 0 on success.
*/
int add(Mad *mad);
/**
* This function can be periodically executed to check time_outs on
* request. It will fail requests with an expired timeout and will notify
* the clients.
*/
void check_time_outs_action();
/**
* Add a new request to the Request map
* @param ar pointer to the request
* @return the id for the request
*/
void add_request(SyncRequest *ar);
/**
* Gets request from the Request map
* @param id for the request
* @return pointer to the Request
*/
SyncRequest * get_request(int id);
private:
/**
* Function to lock the Manager
*/
void lock()
{
pthread_mutex_lock(&mutex);
};
/**
* Function to unlock the Manager
*/
void unlock()
{
pthread_mutex_unlock(&mutex);
};
/**
* Function to execute the listener method within a new pthread (requires
* C linkage)
*/
friend void * mad_manager_listener(void * _mm);
/**
* Synchronization mutex (listener & manager threads)
*/
pthread_mutex_t mutex;
/**
* Thread id for the listener process
*/
pthread_t listener_thread;
/**
* Communication pipe (read end) to link the Manager and the listener
* thread
*/
int pipe_r;
/**
* Communication pipe (write end) to link the Manager and the listener
* thread
*/
int pipe_w;
/**
* This vector contains the file descriptors of the driver pipes (to read
* Mads responses)
*/
vector<int> fds;
/**
* The sets of Mads managed by the MadManager
*/
vector<Mad *> mads;
/**
* Read buffer for the listener. This variable is in the class so it
* can be free upon listener thread cancellation.
*/
ostringstream buffer;
/**
* List of pending requests
*/
map<int, SyncRequest *> sync_requests;
/**
* Listener thread implementation.
*/
void listener();
};
#endif /*MAD_MANAGER_H_*/

View File

@ -190,12 +190,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Builds the marketplace from the template. This function MUST be called
* with the template initialized
@ -217,12 +211,7 @@ private:
* Bootstraps the database table(s) associated to the MarketPlace
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the MarketPlace in the database.

View File

@ -361,12 +361,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Builds the market app from the template. This function MUST be called
* with apptemplate initialized
@ -388,12 +382,7 @@ private:
* Bootstraps the database table(s) associated to the MarketPlace
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the MarketPlace in the database.

View File

@ -18,13 +18,14 @@
#define MARKETPLACEAPP_POOL_H_
#include "MarketPlaceApp.h"
#include "OneDB.h"
class SqlDB;
class MarketPlaceAppPool : public PoolSQL
{
public:
MarketPlaceAppPool(SqlDB * db):PoolSQL(db, MarketPlaceApp::table){};
MarketPlaceAppPool(SqlDB * db):PoolSQL(db, one_db::mp_app_table) {};
~MarketPlaceAppPool(){};
@ -154,7 +155,7 @@ public:
bool desc)
{
return PoolSQL::dump(oss, "MARKETPLACEAPP_POOL", "body",
MarketPlaceApp::table, where, sid, eid, desc);
one_db::mp_app_table, where, sid, eid, desc);
};
/** Update a particular MarketPlaceApp

View File

@ -17,9 +17,9 @@
#ifndef MARKETPLACE_MANAGER_H_
#define MARKETPLACE_MANAGER_H_
#include "MadManager.h"
#include "ProtocolMessages.h"
#include "DriverManager.h"
#include "ActionManager.h"
#include "MarketPlaceManagerDriver.h"
#include "NebulaLog.h"
extern "C" void * marketplace_action_loop(void *arg);
@ -32,7 +32,9 @@ class DatastorePool;
class ImageManager;
class RaftManager;
class MarketPlaceManager : public MadManager, public ActionListener
class MarketPlaceManager :
public DriverManager<Driver<market_msg_t>>,
public ActionListener
{
public:
@ -42,7 +44,7 @@ public:
* @param m, monitor_period to monitor marketplaces
* @param mad, list of drivers for the manager
*/
MarketPlaceManager(time_t t, time_t m, std::vector<const VectorAttribute*>& mad);
MarketPlaceManager(time_t t, time_t m, const std::string& _mad_location);
~MarketPlaceManager(){};
@ -62,11 +64,9 @@ public:
/**
* Loads the MarketPlace Driver defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid=0);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Gets the thread identification.
@ -190,12 +190,9 @@ private:
* Returns a pointer to the marketplace driver.
* @return the marketplace manager driver or 0 in not found
*/
const MarketPlaceManagerDriver * get()
const Driver<market_msg_t> * get()
{
std::string name("NAME");
return static_cast<const MarketPlaceManagerDriver *>
(MadManager::get(0, name, market_driver_name));
return DriverManager::get_driver(market_driver_name);
};
/**
@ -218,6 +215,15 @@ private:
const std::string& market_data,
const std::string& extra_data);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
static void _undefined(unique_ptr<market_msg_t> msg);
void _import(unique_ptr<market_msg_t> msg);
void _delete(unique_ptr<market_msg_t> msg);
void _monitor(unique_ptr<market_msg_t> msg);
static void _log(unique_ptr<market_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
@ -226,10 +232,12 @@ private:
*/
void timer_action(const ActionRequest& ar);
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("MKP", Log::INFO, "Stopping Marketplace Manager...");
MadManager::stop();
DriverManager::stop(drivers_timeout);
};
};

View File

@ -1,97 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef MARKETPLACE_MANAGER_DRIVER_H_
#define MARKETPLACE_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include "Mad.h"
class MarketPlacePool;
class MarketPlaceAppPool;
class MarketPlaceManager;
/**
* ImageManagerDriver represents the basic abstraction for Image Repository
* drivers. It implements the protocol and recover functions from the Mad
* interface.
*/
class MarketPlaceManagerDriver : public Mad
{
public:
MarketPlaceManagerDriver(int userid,
const std::map<string,string>& attrs,
bool sudo,
MarketPlacePool * _marketpool,
MarketPlaceAppPool * _apppool,
MarketPlaceManager * _marketm
):Mad(userid,attrs,sudo), marketpool(_marketpool), apppool(_apppool),
marketm(_marketm){};
virtual ~MarketPlaceManagerDriver(){};
/**
* Implements the Image Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const std::string& message) const;
/**
* TODO: What do we need here? Check on-going xfr?
*/
void recover();
private:
friend class MarketPlaceManager;
/**
* Reference to Marketplace related pools
*/
MarketPlacePool * marketpool;
MarketPlaceAppPool * apppool;
MarketPlaceManager * marketm;
/**
* Imports a new object into the marketplace
* @param oid of the app
* @param drv_msg xml data for the mad operation.
*/
void importapp(int oid, const std::string& drv_msg) const;
/**
* Deletes an app from the marketplace
* @param oid of the app
* @param drv_msg xml data for the mad operation.
*/
void deleteapp(int oid, const std::string& drv_msg) const;
/**
* Monitors the marketplace
* @param oid of the operation
* @param drv_msg xml data for the mad operation.
*/
void monitor(int oid, const std::string& drv_msg) const;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*MARKETPLACE_MANAGER_DRIVER_H_*/

View File

@ -19,6 +19,7 @@
#include "MarketPlace.h"
#include "PoolSQL.h"
#include "OneDB.h"
class MarketPlaceApp;
@ -120,7 +121,7 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "MARKETPLACE_POOL", "body", MarketPlace::table,
return PoolSQL::dump(oss, "MARKETPLACE_POOL", "body", one_db::mp_table,
where, sid, eid, desc);
};
@ -132,7 +133,7 @@ public:
*/
int list(std::vector<int>& oids)
{
return PoolSQL::list(oids, MarketPlace::table);
return PoolSQL::list(oids, one_db::mp_table);
}
/**

View File

@ -14,8 +14,8 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#ifndef MONITOR_MESSAGE_H
#define MONITOR_MESSAGE_H
#ifndef DRIVER_MESSAGE_H
#define DRIVER_MESSAGE_H
#include <unistd.h>
@ -24,25 +24,7 @@
#include <sstream>
#include "EnumString.h"
void base64_decode(const std::string& in, std::string& out);
int base64_encode(const std::string& in, std::string &out);
int zlib_decompress(const std::string& in, std::string& out);
int zlib_compress(const std::string& in, std::string& out);
/**
* Set path to public and private rsa keys
*/
void init_rsa_keys(const std::string& pub_key, const std::string& pri_key);
bool is_rsa_set();
int rsa_public_encrypt(const std::string& in, std::string& out);
int rsa_private_decrypt(const std::string& in, std::string& out);
#include "SSLUtil.h"
/**
* This class represents a generic message used by the Monitoring Protocol.
@ -62,30 +44,40 @@ int rsa_private_decrypt(const std::string& in, std::string& out);
* '\n' End of message delimiter
*
*/
template<typename E>
template<typename E, //Enum class for the message types
bool encode = false, //Payload is base64 encoded
bool compress = false, //Payload is compressed
bool encrypt = false, //Payload is encrypted
bool has_timestamp = false> //Message includes timestamp
class Message
{
public:
using msg_enum = E;
Message() = default;
Message(E type, std::string &&status, int oid, const std::string& msg_payload);
/**
* Parse the Message from an input string
* @param input string with the message
*/
int parse_from(const std::string& input, bool decrypt);
int parse_from(const std::string& input);
/**
* Writes this object to the given string
*/
int write_to(std::string& out, bool encrypt) const;
int write_to(std::string& out) const;
/**
* Writes this object to the given file descriptor
*/
int write_to(int fd, bool encrypt) const;
int write_to(int fd) const;
/**
* Writes this object to the given output stream
*/
int write_to(std::ostream& oss, bool encrypt) const;
int write_to(std::ostream& oss) const;
/**
*
@ -160,11 +152,13 @@ public:
*/
time_t timestamp() const
{
static_assert(has_timestamp == true, "Timestamp disabled");
return _timestamp;
}
void timestamp(time_t ts)
{
static_assert(has_timestamp == true, "Timestamp disabled");
_timestamp = ts;
}
@ -187,15 +181,26 @@ private:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* Message Template Implementation */
/* Message Template Implementation */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
template<typename E>
int Message<E>::parse_from(const std::string& input, bool decrypt)
template<typename E, bool compress, bool encode, bool encrypt, bool has_timestamp>
Message<E, compress, encode, encrypt, has_timestamp>
::Message(E type, std::string &&status, int oid, const std::string& payload)
: _type(type)
, _status(std::move(status))
, _oid(oid)
, _payload(payload)
{
}
template<typename E, bool compress, bool encode, bool encrypt, bool has_timestamp>
int Message<E, compress, encode, encrypt, has_timestamp>
::parse_from(const std::string& input)
{
std::istringstream is(input);
std::string buffer, payloaz;
std::string buffer;
if (!is.good())
{
@ -215,11 +220,14 @@ int Message<E>::parse_from(const std::string& input, bool decrypt)
is >> _status;
is >> _oid;
is >> _oid >> std::ws;
is >> _timestamp;
if (has_timestamp)
{
is >> _timestamp >> std::ws;
}
is >> buffer;
getline(is, buffer);
if (buffer.empty())
{
@ -227,19 +235,26 @@ int Message<E>::parse_from(const std::string& input, bool decrypt)
return 0;
}
base64_decode(buffer, payloaz);
if ( zlib_decompress(payloaz, _payload) == -1 )
if (encode)
{
goto error;
}
ssl_util::base64_decode(buffer, _payload);
if ( decrypt && is_rsa_set() )
{
if ( rsa_private_decrypt(_payload, _payload) == -1 )
if ( compress && ssl_util::zlib_decompress(_payload, _payload) == -1 )
{
goto error;
}
if ( encrypt && ssl_util::is_rsa_set() )
{
if ( ssl_util::rsa_private_decrypt(_payload, _payload) == -1 )
{
// Accept also not encrypted messages
}
}
}
else
{
_payload = buffer;
}
return 0;
@ -253,49 +268,51 @@ error:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
template<typename E>
int Message<E>::write_to(std::string& out, bool encrypt) const
template<typename E, bool compress, bool encode, bool encrypt, bool has_timestamp>
int Message<E, compress, encode, encrypt, has_timestamp>
::write_to(std::string& out) const
{
out.clear();
std::string secret;
std::string payloaz;
std::string payloaz64;
if (!_payload.empty())
{
if (encrypt)
{
if (rsa_public_encrypt(_payload, secret) != 0)
{
return -1;
}
}
else
{
secret = _payload;
}
if (zlib_compress(secret, payloaz) == -1)
{
return -1;
}
if ( base64_encode(payloaz, payloaz64) == -1)
{
return -1;
}
}
out = _type_str._to_str(_type);
out += ' ';
out += _status.empty() ? "-" : _status;
out += _status;
out += ' ';
out += std::to_string(_oid);
out += ' ';
out += std::to_string(_timestamp);
out += ' ';
out += payloaz64;
if (has_timestamp)
{
out += std::to_string(_timestamp);
out += ' ';
}
if (!_payload.empty())
{
if (encode)
{
std::string msg;
msg = _payload;
if (compress && ssl_util::zlib_compress(msg, msg) == -1)
{
return -1;
}
if ( ssl_util::base64_encode(msg, msg) == -1)
{
return -1;
}
out += msg;
}
else
{
out += _payload;
}
}
out += '\n';
return 0;
@ -304,12 +321,13 @@ int Message<E>::write_to(std::string& out, bool encrypt) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
template<typename E>
int Message<E>::write_to(int fd, bool encrypt) const
template<typename E, bool compress, bool encode, bool encrypt, bool has_timestamp>
int Message<E, compress, encode, encrypt, has_timestamp>
::write_to(int fd) const
{
std::string out;
if ( write_to(out, encrypt) == -1)
if ( write_to(out) == -1)
{
return -1;
}
@ -322,12 +340,13 @@ int Message<E>::write_to(int fd, bool encrypt) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
template<typename E>
int Message<E>::write_to(std::ostream& oss, bool encrypt) const
template<typename E, bool compress, bool encode, bool encrypt, bool has_timestamp>
int Message<E, compress, encode, encrypt, has_timestamp>
::write_to(std::ostream& oss) const
{
std::string out;
if ( write_to(out, encrypt) == -1)
if ( write_to(out) == -1)
{
return -1;
}
@ -337,4 +356,4 @@ int Message<E>::write_to(std::ostream& oss, bool encrypt) const
return 0;
}
#endif /*MONITOR_MESSAGE_H_*/
#endif /*DRIVER_MESSAGE_H_*/

View File

@ -38,7 +38,7 @@ namespace one_db
extern const char * host_monitor_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* VM TABLES */
/* VM TABLES */
/* ---------------------------------------------------------------------- */
extern const char * vm_table;
@ -58,6 +58,18 @@ namespace one_db
extern const char * vm_showback_db_bootstrap;
extern const char * vm_group_db_names;
extern const char * vm_group_db_bootstrap;
extern const char * vm_group_table;
extern const char * vm_template_db_names;
extern const char * vm_template_db_bootstrap;
extern const char * vm_template_table;
// -------------------------------------------------------------------------
// Virtual Machine ID - Deploy ID index for imported VMs
// The index is managed by the VirtualMachinePool
@ -67,6 +79,190 @@ namespace one_db
extern const char * vm_import_db_names;
extern const char * vm_import_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* Cluster tables */
/* ---------------------------------------------------------------------- */
extern const char * cluster_db_names;
extern const char * cluster_db_bootstrap;
extern const char * cluster_table;
extern const char * cluster_datastore_table;
extern const char * cluster_datastore_db_names;
extern const char * cluster_datastore_db_bootstrap;
extern const char * cluster_network_table;
extern const char * cluster_network_db_names;
extern const char * cluster_network_db_bootstrap;
extern const char * cluster_bitmap_table;
/* ---------------------------------------------------------------------- */
/* ACL tables */
/* ---------------------------------------------------------------------- */
extern const char * acl_table;
extern const char * acl_db_names;
extern const char * acl_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* Datastore tables */
/* ---------------------------------------------------------------------- */
extern const char * ds_db_names;
extern const char * ds_db_bootstrap;
extern const char * ds_table;
/* ---------------------------------------------------------------------- */
/* Document tables */
/* ---------------------------------------------------------------------- */
extern const char * doc_db_names;
extern const char * doc_db_bootstrap;
extern const char * doc_table;
/* ---------------------------------------------------------------------- */
/* Group tables */
/* ---------------------------------------------------------------------- */
extern const char * group_db_names;
extern const char * group_db_bootstrap;
extern const char * group_table;
/* ---------------------------------------------------------------------- */
/* History tables */
/* ---------------------------------------------------------------------- */
extern const char * history_table;
extern const char * history_db_names;
extern const char * history_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* Hook tables */
/* ---------------------------------------------------------------------- */
extern const char * hook_db_names;
extern const char * hook_db_bootstrap;
extern const char * hook_table;
extern const char * hook_log_table;
extern const char * hook_log_db_names;
extern const char * hook_log_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* Image tables */
/* ---------------------------------------------------------------------- */
extern const char * image_db_names;
extern const char * image_db_bootstrap;
extern const char * image_table;
/* ---------------------------------------------------------------------- */
/* Log tables */
/* ---------------------------------------------------------------------- */
extern const char * log_table;
extern const char * log_db_names;
extern const char * log_db_bootstrap;
/* ---------------------------------------------------------------------- */
/* Marketplace tables */
/* ---------------------------------------------------------------------- */
extern const char * mp_db_names;
extern const char * mp_db_bootstrap;
extern const char * mp_table;
extern const char * mp_app_db_names;
extern const char * mp_app_db_bootstrap;
extern const char * mp_app_table;
/* ---------------------------------------------------------------------- */
/* Quotas tables */
/* ---------------------------------------------------------------------- */
extern const char * group_quotas_db_names;
extern const char * group_quotas_db_bootstrap;
extern const char * group_quotas_db_table;
extern const char * group_quotas_db_oid_column;
extern const char * user_quotas_db_names;
extern const char * user_quotas_db_bootstrap;
extern const char * user_quotas_db_table;
extern const char * user_quotas_db_oid_column;
/* ---------------------------------------------------------------------- */
/* Security Group tables */
/* ---------------------------------------------------------------------- */
extern const char * sg_db_names;
extern const char * sg_db_bootstrap;
extern const char * sg_table;
/* ---------------------------------------------------------------------- */
/* User tables */
/* ---------------------------------------------------------------------- */
extern const char * user_db_names;
extern const char * user_db_bootstrap;
extern const char * user_table;
/* ---------------------------------------------------------------------- */
/* VDC tables */
/* ---------------------------------------------------------------------- */
extern const char * vdc_db_names;
extern const char * vdc_db_bootstrap;
extern const char * vdc_table;
/* ---------------------------------------------------------------------- */
/* Virtual Network tables */
/* ---------------------------------------------------------------------- */
extern const char * vn_table;
extern const char * vn_db_names;
extern const char * vn_db_bootstrap;
extern const char * vn_template_db_names;
extern const char * vn_template_db_bootstrap;
extern const char * vn_template_table;
/* ---------------------------------------------------------------------- */
/* Virtual Router tables */
/* ---------------------------------------------------------------------- */
extern const char * vr_db_names;
extern const char * vr_db_bootstrap;
extern const char * vr_table;
/* ---------------------------------------------------------------------- */
/* Zone tables */
/* ---------------------------------------------------------------------- */
extern const char * zone_db_names;
extern const char * zone_db_bootstrap;
extern const char * zone_table;
}
#endif /*ONE_DB_H_*/

View File

@ -19,6 +19,7 @@
#include "Quotas.h"
#include "ObjectSQL.h"
#include "OneDB.h"
class QuotasSQL : public Quotas, ObjectSQL
{
@ -165,7 +166,7 @@ public:
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_quota(GroupQuotas::db_bootstrap);
ostringstream oss_quota(one_db::group_quotas_db_bootstrap);
return db->exec_local_wr(oss_quota);
};
@ -174,30 +175,23 @@ protected:
const char * table() const
{
return db_table;
return one_db::group_quotas_db_table;
};
const char * table_names() const
{
return db_names;
return one_db::group_quotas_db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
return one_db::group_quotas_db_oid_column;
};
private:
friend class GroupPool;
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
};
/* -------------------------------------------------------------------------- */
@ -218,7 +212,7 @@ public:
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_quota(UserQuotas::db_bootstrap);
ostringstream oss_quota(one_db::user_quotas_db_bootstrap);
return db->exec_local_wr(oss_quota);
};
@ -227,30 +221,22 @@ protected:
const char * table() const
{
return db_table;
return one_db::user_quotas_db_table;
};
const char * table_names() const
{
return db_names;
return one_db::user_quotas_db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
return one_db::user_quotas_db_oid_column;
};
private:
friend class UserPool;
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
};
#endif /*QUOTAS_SQL_H_*/

View File

@ -1,5 +1,5 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
/* */
/* 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 */
@ -14,31 +14,31 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#ifndef OPENNEBULA_MESSAGES_H
#define OPENNEBULA_MESSAGES_H
#ifndef SSL_UTIL_H
#define SSL_UTIL_H
#include "StreamManager.h"
#include <string>
/**
* Messages between the Monitor daemon and OpenNebula daemon
*/
enum class OpenNebulaMessages : unsigned short int
namespace ssl_util
{
UNDEFINED = 0,
INIT,
FINALIZE,
HOST_LIST,
UPDATE_HOST,
DEL_HOST,
START_MONITOR,
STOP_MONITOR,
HOST_STATE,
VM_STATE,
HOST_SYSTEM,
RAFT_STATUS,
ENUM_MAX
};
void base64_decode(const std::string& in, std::string& out);
typedef StreamManager<OpenNebulaMessages> one_stream_t;
int base64_encode(const std::string& in, std::string &out);
#endif /*OPENNEBULA_MESSAGES_H*/
int zlib_decompress(const std::string& in, std::string& out);
int zlib_compress(const std::string& in, std::string& out);
/**
* Set path to public and private rsa keys
*/
void init_rsa_keys(const std::string& pub_key, const std::string& pri_key);
bool is_rsa_set();
int rsa_public_encrypt(const std::string& in, std::string& out);
int rsa_private_decrypt(const std::string& in, std::string& out);
} // namespace ssl_util
#endif /*SSL_UTIL_H_*/

View File

@ -194,12 +194,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -213,12 +207,7 @@ private:
* Bootstraps the database table(s) associated to the SecurityGroup
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(SecurityGroup::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the SecurityGroup in the database.

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "SecurityGroup.h"
#include "OneDB.h"
using namespace std;
@ -115,7 +116,7 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "SECURITY_GROUP_POOL", "body", SecurityGroup::table,
return PoolSQL::dump(oss, "SECURITY_GROUP_POOL", "body", one_db::sg_table,
where, sid, eid, desc);
};

View File

@ -86,15 +86,13 @@ public:
am.loop(t);
};
protected:
friend class MadManager;
/**
* Time in seconds when this request will expire
*/
time_t time_out;
protected:
/**
* The ActionManager that will be notify when the request is ready.
*/

View File

@ -17,17 +17,16 @@
#ifndef TRANSFER_MANAGER_H_
#define TRANSFER_MANAGER_H_
#include "MadManager.h"
#include "ProtocolMessages.h"
#include "DriverManager.h"
#include "ActionManager.h"
#include "VirtualMachinePool.h"
#include "LifeCycleManager.h"
#include "TransferManagerDriver.h"
using namespace std;
extern "C" void * tm_action_loop(void *arg);
class HostPool;
class VirtualMachine;
class VirtualMachineDisk;
class VirtualMachinePool;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -85,15 +84,17 @@ private:
int _vm_id;
};
class TransferManager : public MadManager, public ActionListener
class TransferManager :
public DriverManager<Driver<transfer_msg_t>>,
public ActionListener
{
public:
TransferManager(
VirtualMachinePool * _vmpool,
HostPool * _hpool,
vector<const VectorAttribute*>& _mads):
MadManager(_mads),
const std::string& _mad_location):
DriverManager(_mad_location),
vmpool(_vmpool),
hpool(_hpool)
{
@ -130,12 +131,10 @@ public:
int start();
/**
* Loads Virtual Machine Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* Loads Transfer Manager Drivers in configuration file
* @param _mads configuration of drivers
*/
int load_mads(int uid);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Gets the thread identification.
@ -162,10 +161,10 @@ public:
int prolog_transfer_command(
VirtualMachine * vm,
const VirtualMachineDisk* disk,
string& system_tm_mad,
string& opennebula_hostname,
ostream& xfr,
ostringstream& error);
std::string& system_tm_mad,
std::string& opennebula_hostname,
std::ostream& xfr,
std::ostringstream& error);
/**
* Inserts a context command in the xfs stream
@ -180,10 +179,10 @@ public:
*/
int prolog_context_command(
VirtualMachine * vm,
const string& token_password,
string& system_tm_mad,
const std::string& token_password,
std::string& system_tm_mad,
int& disk_id,
ostream& xfr);
std::ostream& xfr);
/**
* Inserts a transfer command in the xfs stream
@ -196,9 +195,9 @@ public:
*/
void epilog_transfer_command(
VirtualMachine * vm,
const string& host,
const std::string& host,
const VirtualMachineDisk * disk,
ostream& xfr);
std::ostream& xfr);
/**
* Inserts a transfer command in the xfs stream, for live migration
*
@ -207,7 +206,7 @@ public:
*/
void migrate_transfer_command(
VirtualMachine * vm,
ostream& xfr);
std::ostream& xfr);
/**
* This function generates the epilog_delete sequence for current,
@ -220,7 +219,7 @@ public:
* @return 0 on success
*/
int epilog_delete_commands(VirtualMachine *vm,
ostream& xfr,
std::ostream& xfr,
bool local,
bool previous);
/**
@ -233,7 +232,7 @@ public:
*/
int snapshot_transfer_command(VirtualMachine * vm,
const char * snap_action,
ostream& xfr);
std::ostream& xfr);
/**
* Inserts a resize command in the xfr stream
@ -244,7 +243,7 @@ public:
void resize_command(
VirtualMachine * vm,
const VirtualMachineDisk * disk,
ostream& xfr);
std::ostream& xfr);
private:
/**
* Thread id for the Transfer Manager
@ -271,21 +270,6 @@ private:
*/
static const char * transfer_driver_name;
/**
* Returns a pointer to a Transfer Manager driver.
* @param name of an attribute of the driver (e.g. its type)
* @param value of the attribute
* @return the TM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const TransferManagerDriver * get(
const string& name,
const string& value)
{
return static_cast<const TransferManagerDriver *>
(MadManager::get(0,name,value));
};
/**
* Returns a pointer to a Transfer Manager driver. The driver is
* searched by its name.
@ -293,12 +277,9 @@ private:
* @return the TM driver owned by uid with attribute name equal to value
* or 0 in not found
*/
const TransferManagerDriver * get(
const string& name)
const Driver<transfer_msg_t> * get(const std::string& name)
{
string _name("NAME");
return static_cast<const TransferManagerDriver *>
(MadManager::get(0,_name,name));
return DriverManager::get_driver(name);
};
/**
@ -306,11 +287,9 @@ private:
* searched by its name.
* @return the TM driver for the Transfer Manager
*/
const TransferManagerDriver * get()
const Driver<transfer_msg_t> * get()
{
string _name("NAME");
return static_cast<const TransferManagerDriver *>
(MadManager::get(0,_name,transfer_driver_name));
return DriverManager::get_driver(transfer_driver_name);
};
/**
@ -319,14 +298,23 @@ private:
*/
friend void * tm_action_loop(void *arg);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
static void _undefined(unique_ptr<transfer_msg_t> msg);
void _transfer(unique_ptr<transfer_msg_t> msg);
static void _log(unique_ptr<transfer_msg_t> msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("TM",Log::INFO,"Stopping Transfer Manager...");
MadManager::stop();
DriverManager::stop(drivers_timeout);
};
void user_action(const ActionRequest& ar);

View File

@ -1,78 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef TRANSFER_MANAGER_DRIVER_H_
#define TRANSFER_MANAGER_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "Mad.h"
#include "VirtualMachinePool.h"
using namespace std;
/**
* TransferManagerDriver provides a base class to implement TM
* Drivers. This class implements the protocol and recover functions
* from the Mad interface.
*/
class TransferManagerDriver : public Mad
{
public:
TransferManagerDriver(
int userid,
const map<string,string>& attrs,
bool sudo,
VirtualMachinePool * pool):
Mad(userid,attrs,sudo), vmpool(pool){};
virtual ~TransferManagerDriver(){};
/**
* Implements the VM Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* TODO: What do we need here? Check on-going xfr?
*/
void recover();
private:
friend class TransferManager;
/**
* Pointer to the Virtual Machine Pool, to access VMs
*/
VirtualMachinePool * vmpool;
/**
* Sends a transfer request to the MAD: "TRANSFER ID XFR_FILE"
* @param oid the virtual machine id.
* @param xfr_file is the path to the transfer script
*/
void transfer (const int oid, const string& xfr_file) const;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#endif /*TRANSFER_MANAGER_DRIVER_H_*/

View File

@ -323,7 +323,7 @@ private:
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_user(User::db_bootstrap);
ostringstream oss_user(one_db::user_db_bootstrap);
return db->exec_local_wr(oss_user);
}
@ -382,7 +382,7 @@ protected:
const string& _password,
const string& _auth_driver,
bool _enabled):
PoolObjectSQL(id,USER,_uname,-1,_gid,"",_gname,table),
PoolObjectSQL(id,USER,_uname,-1,_gid,"",_gname,one_db::user_table),
quota(),
password(_password),
auth_driver(_auth_driver),
@ -399,12 +399,6 @@ protected:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the User in the database.
* @param db pointer to the db

View File

@ -80,7 +80,7 @@ public:
POFF_HARD_MIGRATE_ACTION = 49 // "one.vm.migrate"
};
static string action_to_str(Action action);
static std::string action_to_str(Action action);
static int action_from_str(const string& st, Action& action);
@ -116,7 +116,7 @@ public:
/**
* Sets the auth operations based on the provided template
*/
int set_auth_ops(const Template& tmpl, string& error);
int set_auth_ops(const Template& tmpl, std::string& error);
private:
/**

View File

@ -143,11 +143,6 @@ private:
// -------------------------------------------------------------------------
// DataBase implementation
// -------------------------------------------------------------------------
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
@ -162,12 +157,7 @@ private:
* Bootstraps the database table(s) associated to the VMGroup
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(VMGroup::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the VMGroup in the database.

View File

@ -19,13 +19,14 @@
#include "PoolSQL.h"
#include "VMGroup.h"
#include "OneDB.h"
class AuthRequest;
class VMGroupPool : public PoolSQL
{
public:
VMGroupPool(SqlDB * db):PoolSQL(db, VMGroup::table){};
VMGroupPool(SqlDB * db):PoolSQL(db, one_db::vm_group_table){};
~VMGroupPool(){};
@ -108,8 +109,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VM_GROUP_POOL", "body", VMGroup::table, where,
sid, eid, desc);
return PoolSQL::dump(oss, "VM_GROUP_POOL", "body",
one_db::vm_group_table, where, sid, eid, desc);
};
/**

View File

@ -152,12 +152,7 @@ private:
* Bootstraps the database table(s) associated to the VMTemplate
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(VMTemplate::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Rebuilds the object from an xml formatted string
@ -191,12 +186,6 @@ protected:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the VMTemplate in the database.
* @param db pointer to the db

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "VMTemplate.h"
#include "OneDB.h"
/**
* The VMTemplate Pool class.
@ -27,7 +28,7 @@ class VMTemplatePool : public PoolSQL
{
public:
VMTemplatePool(SqlDB * db) : PoolSQL(db, VMTemplate::table){};
VMTemplatePool(SqlDB * db) : PoolSQL(db, one_db::vm_template_table){};
~VMTemplatePool(){};
@ -93,8 +94,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VMTEMPLATE_POOL", "body", VMTemplate::table,
where, sid, eid, desc);
return PoolSQL::dump(oss, "VMTEMPLATE_POOL", "body",
one_db::vm_template_table, where, sid, eid, desc);
};
/**

View File

@ -92,12 +92,7 @@ private:
* Bootstraps the database table(s) associated to the VNTemplate
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(VNTemplate::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Rebuilds the object from an xml formatted string
@ -126,12 +121,6 @@ protected:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the VNTemplate in the database.
* @param db pointer to the db

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "VNTemplate.h"
#include "OneDB.h"
/**
* The VNetTemplate Pool class.
@ -27,7 +28,7 @@ class VNTemplatePool : public PoolSQL
{
public:
VNTemplatePool(SqlDB * db) : PoolSQL(db, VNTemplate::table){};
VNTemplatePool(SqlDB * db) : PoolSQL(db, one_db::vn_template_table){};
~VNTemplatePool(){};
@ -93,8 +94,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VNTEMPLATE_POOL", "body", VNTemplate::table,
where, sid, eid, desc);
return PoolSQL::dump(oss, "VNTEMPLATE_POOL", "body",
one_db::vn_template_table, where, sid, eid, desc);
};
/**

View File

@ -341,12 +341,6 @@ private:
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -360,12 +354,7 @@ private:
* Bootstraps the database table(s) associated to the Vdc
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(Vdc::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the Vdc in the database.

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "Vdc.h"
#include "OneDB.h"
using namespace std;
@ -99,8 +100,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VDC_POOL", "body", Vdc::table, where, sid,
eid, desc);
return PoolSQL::dump(oss, "VDC_POOL", "body", one_db::vdc_table,
where, sid, eid, desc);
};
/**
@ -111,7 +112,7 @@ public:
*/
int list(std::vector<int>& oids)
{
return PoolSQL::list(oids, Vdc::table);
return PoolSQL::list(oids, one_db::vdc_table);
}
/**

View File

@ -17,15 +17,13 @@
#ifndef VIRTUAL_MACHINE_MANAGER_H_
#define VIRTUAL_MACHINE_MANAGER_H_
#include "MadManager.h"
#include "ActionManager.h"
#include "VirtualMachineManagerDriver.h"
#include "VirtualMachinePool.h"
#include "HostPool.h"
#include "DatastorePool.h"
#include "NebulaTemplate.h"
#include "DriverManager.h"
#include "ActionManager.h"
using namespace std;
class DatastorePool;
class HostPool;
class VirtualMachinePool;
extern "C" void * vmm_action_loop(void *arg);
@ -89,16 +87,18 @@ private:
int _vm_id;
};
class VirtualMachineManager : public MadManager, public ActionListener
class VirtualMachineManager :
public DriverManager<VirtualMachineManagerDriver>,
public ActionListener
{
public:
VirtualMachineManager(
time_t _timer_period,
int _vm_limit,
vector<const VectorAttribute*>& _mads);
const std::string& _mads);
~VirtualMachineManager(){};
~VirtualMachineManager() = default;
/**
* Triggers specific actions to the Virtual Machine Manager. This function
@ -138,11 +138,9 @@ public:
/**
* Loads Virtual Machine Manager Mads defined in configuration file
* @param uid of the user executing the driver. When uid is 0 the nebula
* identity will be used. Otherwise the Mad will be loaded through the
* sudo application.
* @param _mads configuration of drivers
*/
int load_mads(int uid);
int load_drivers(const std::vector<const VectorAttribute*>& _mads);
/**
* Check if action is supported for imported VMs
@ -154,7 +152,7 @@ public:
{
const VirtualMachineManagerDriver * vmd = get(mad);
if ( vmd == 0 )
if ( vmd == nullptr )
{
return false;
}
@ -212,9 +210,7 @@ public:
const VirtualMachineManagerDriver * get(
const string& name)
{
string _name("NAME");
return static_cast<const VirtualMachineManagerDriver *>
(MadManager::get(0,_name,name));
return DriverManager::get_driver(name);
};
/**
@ -269,31 +265,160 @@ private:
*/
friend void * vmm_action_loop(void *arg);
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
static void _undefined(unique_ptr<vm_msg_t> msg);
/**
* Returns a pointer to a Virtual Machine Manager driver.
* @param uid of the owner of the driver
* @param name of an attribute of the driver (e.g. its type)
* @param value of the attribute
* @return the VM driver owned by uid with attribute name equal to value
* or 0 in not found
*
*/
const VirtualMachineManagerDriver * get(
const string& name,
const string& value)
{
return static_cast<const VirtualMachineManagerDriver *>
(MadManager::get(0,name,value));
};
void _deploy(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _shutdown(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _reset(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _reboot(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _cancel(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _cleanup(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _checkpoint(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _save(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _restore(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _migrate(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _attachdisk(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _detachdisk(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _attachnic(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _detachnic(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _snapshotcreate(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _snapshotrevert(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _snapshotdelete(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _disksnapshotcreate(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _disksnapshotrevert(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _resizedisk(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _updateconf(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _updatesg(unique_ptr<vm_msg_t> msg);
/**
*
*/
void _driver_cancel(unique_ptr<vm_msg_t> msg);
/**
*
*/
static void _log(unique_ptr<vm_msg_t> msg);
/**
*
*/
void log_error(VirtualMachine* vm_id,
const std::string& payload,
const char* msg);
/**
*
*/
void log_error(int vm_id, const std::string& payload, const char* msg);
/**
*
*/
bool check_vm_state(int vm_id, vm_msg_t* msg);
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
static const int drivers_timeout = 10;
void finalize_action(const ActionRequest& ar)
{
NebulaLog::log("VMM",Log::INFO,"Stopping Virtual Machine Manager...");
MadManager::stop();
DriverManager::stop(drivers_timeout);
};
void user_action(const ActionRequest& ar);

View File

@ -21,14 +21,13 @@
#include <string>
#include <sstream>
#include "Mad.h"
#include "ProtocolMessages.h"
#include "Driver.h"
#include "ActionSet.h"
#include "VirtualMachinePool.h"
#include "VMActions.h"
#include "Host.h"
#include "Cluster.h"
using namespace std;
#include "VirtualMachine.h"
/**
* VirtualMachineManagerDriver provides a base class to implement VM Manager
@ -37,30 +36,15 @@ using namespace std;
* must implement the deployment function to generate specific VM
* deployment information for the unerlying MAD.
*/
class VirtualMachineManagerDriver : public Mad
class VirtualMachineManagerDriver : public Driver<vm_msg_t>
{
public:
VirtualMachineManagerDriver(
int userid,
const map<string,string>& attrs,
bool sudo,
VirtualMachinePool * pool);
VirtualMachineManagerDriver(const std::string& mad_location,
const std::map<std::string,std::string>& attrs);
virtual ~VirtualMachineManagerDriver() = default;
/**
* Implements the VM Manager driver protocol.
* @param message the string read from the driver
*/
void protocol(const string& message) const;
/**
* TODO: What do we need here? just poll the active VMs to recover
* connections? Or an specific recover action from the MAD?
*/
void recover();
/**
* Generates a driver-specific deployment file:
* @param vm pointer to a virtual machine
@ -69,7 +53,7 @@ public:
*/
virtual int deployment_description(
const VirtualMachine * vm,
const string& file_name) const = 0;
const std::string& file_name) const = 0;
/**
* Validates de VM raws section
@ -77,7 +61,7 @@ public:
* @param error description on error
* @return 0 on success
*/
virtual int validate_raw(const string& raw, string& error) const
virtual int validate_raw(const std::string& raw, std::string& error) const
{
return 0;
}
@ -123,7 +107,7 @@ protected:
* @param value of the attribute
*/
template<typename T>
void get_default(const string& name, T& value) const
void get_default(const std::string& name, T& value) const
{
driver_conf.get(name, value);
}
@ -162,7 +146,7 @@ protected:
bool get_attribute(const VirtualMachine * vm,
const Host * host,
const Cluster * cluster,
const string& name,
const std::string& name,
T& value) const
{
// Get value from VM
@ -201,8 +185,8 @@ protected:
bool get_attribute(const VirtualMachine * vm,
const Host * host,
const Cluster* cluster,
const string& name,
const string& vname,
const std::string& name,
const std::string& vname,
T& value) const
{
const VectorAttribute * vattr;
@ -249,8 +233,8 @@ protected:
private:
friend class VirtualMachineManager;
static const string imported_actions_default;
static const string imported_actions_default_public;
static const std::string imported_actions_default;
static const std::string imported_actions_default_public;
/**
* Configuration file for the driver
@ -279,21 +263,16 @@ private:
*/
bool cold_nic_attach;
/**
* Pointer to the Virtual Machine Pool, to access VMs
*/
VirtualMachinePool * vmpool;
/**
* Sends a deploy request to the MAD: "DEPLOY ID XML_DRV_MSG"
* @param oid the virtual machine id.
* @param drv_msg xml data for the mad operation
*/
void deploy(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("DEPLOY", oid, drv_msg);
write_drv(VMManagerMessages::DEPLOY, oid, drv_msg);
}
/**
@ -302,10 +281,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void shutdown(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("SHUTDOWN", oid, drv_msg);
write_drv(VMManagerMessages::SHUTDOWN, oid, drv_msg);
}
/**
@ -314,10 +293,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void reset(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("RESET", oid, drv_msg);
write_drv(VMManagerMessages::RESET, oid, drv_msg);
}
/**
@ -326,10 +305,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void reboot(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("REBOOT", oid, drv_msg);
write_drv(VMManagerMessages::REBOOT, oid, drv_msg);
}
/**
@ -338,10 +317,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void cancel(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("CANCEL", oid, drv_msg);
write_drv(VMManagerMessages::CANCEL, oid, drv_msg);
}
/**
@ -350,10 +329,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void cleanup(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("CLEANUP", oid, drv_msg);
write_drv(VMManagerMessages::CLEANUP, oid, drv_msg);
}
/**
@ -362,10 +341,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void checkpoint(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("CHECKPOINT", oid, drv_msg);
write_drv(VMManagerMessages::CHECKPOINT, oid, drv_msg);
}
/**
@ -374,12 +353,20 @@ private:
* @param drv_msg xml data for the mad operation
*/
void save(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("SAVE", oid, drv_msg);
write_drv(VMManagerMessages::SAVE, oid, drv_msg);
}
/**
* Sends a save request to the MAD: "SAVE ID XML_DRV_MSG"
* @param oid the virtual machine id.
*/
void driver_cancel(const int oid) const
{
write_drv(VMManagerMessages::DRIVER_CANCEL, oid, "");
}
/**
* Sends a save request to the MAD: "RESTORE ID XML_DRV_MSG"
@ -387,10 +374,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void restore(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("RESTORE", oid, drv_msg);
write_drv(VMManagerMessages::RESTORE, oid, drv_msg);
}
@ -400,22 +387,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void migrate(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("MIGRATE", oid, drv_msg);
}
/**
* Sends a poll request to the MAD: "POLL ID XML_DRV_MSG"
* @param oid the virtual machine id.
* @param drv_msg xml data for the mad operation
*/
void poll(
const int oid,
const string& drv_msg) const
{
write_drv("POLL", oid, drv_msg);
write_drv(VMManagerMessages::MIGRATE, oid, drv_msg);
}
/**
@ -424,10 +399,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void attach(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("ATTACHDISK", oid, drv_msg);
write_drv(VMManagerMessages::ATTACHDISK, oid, drv_msg);
}
/**
@ -436,10 +411,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void detach(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("DETACHDISK", oid, drv_msg);
write_drv(VMManagerMessages::DETACHDISK, oid, drv_msg);
}
/**
@ -448,10 +423,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void attach_nic(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("ATTACHNIC", oid, drv_msg);
write_drv(VMManagerMessages::ATTACHNIC, oid, drv_msg);
}
/**
@ -460,10 +435,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void detach_nic(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("DETACHNIC", oid, drv_msg);
write_drv(VMManagerMessages::DETACHNIC, oid, drv_msg);
}
/**
@ -473,10 +448,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void snapshot_create(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("SNAPSHOTCREATE", oid, drv_msg);
write_drv(VMManagerMessages::SNAPSHOTCREATE, oid, drv_msg);
}
/**
@ -486,10 +461,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void snapshot_revert(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("SNAPSHOTREVERT", oid, drv_msg);
write_drv(VMManagerMessages::SNAPSHOTREVERT, oid, drv_msg);
}
/**
@ -499,10 +474,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void snapshot_delete(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("SNAPSHOTDELETE", oid, drv_msg);
write_drv(VMManagerMessages::SNAPSHOTDELETE, oid, drv_msg);
}
/**
@ -512,10 +487,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void disk_snapshot_create(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("DISKSNAPSHOTCREATE", oid, drv_msg);
write_drv(VMManagerMessages::DISKSNAPSHOTCREATE, oid, drv_msg);
}
/**
@ -525,10 +500,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void disk_resize(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("RESIZEDISK", oid, drv_msg);
write_drv(VMManagerMessages::RESIZEDISK, oid, drv_msg);
}
/**
@ -537,10 +512,10 @@ private:
* @param drv_msg xml data for the mad operation
*/
void update_conf(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("UPDATECONF", oid, drv_msg);
write_drv(VMManagerMessages::UPDATECONF, oid, drv_msg);
}
/**
@ -550,22 +525,21 @@ private:
* @param drv_msg xml data for the mad operation
*/
void updatesg(
const int oid,
const string& drv_msg) const
const int oid,
const std::string& drv_msg) const
{
write_drv("UPDATESG", oid, drv_msg);
write_drv(VMManagerMessages::UPDATESG, oid, drv_msg);
}
/**
*
*/
void write_drv(const char * aname, const int oid, const string& msg) const
void write_drv(VMManagerMessages type,
const int oid,
const std::string& msg) const
{
ostringstream os;
os << aname << " " << oid << " " << msg << endl;
write(os);
vm_msg_t drv_msg(type, "", oid, msg);
write(drv_msg);
}
};

View File

@ -753,12 +753,7 @@ private:
* Bootstraps the database table(s) associated to the Virtual Network
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss_vnet(VirtualNetwork::db_bootstrap);
return db->exec_local_wr(oss_vnet);
};
static int bootstrap(SqlDB * db);
/**
* Function to print the VirtualNetwork object into a string in
@ -805,12 +800,6 @@ private:
// DataBase implementation
// *************************************************************************
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
/**
* Writes the Virtual Network and its associated template and leases in the database.
* @param db pointer to the db

View File

@ -20,6 +20,7 @@
#include "PoolSQL.h"
#include "VirtualNetwork.h"
#include "BitMap.h"
#include "OneDB.h"
#include <time.h>
@ -166,7 +167,7 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VNET_POOL", "body", VirtualNetwork::table,
return PoolSQL::dump(oss, "VNET_POOL", "body", one_db::vn_table,
where, sid, eid, desc);
}
@ -196,7 +197,7 @@ public:
*/
int search(vector<int>& oids, const string& where)
{
return PoolSQL::search(oids, VirtualNetwork::table, where);
return PoolSQL::search(oids, one_db::vn_table, where);
};
//--------------------------------------------------------------------------

View File

@ -202,12 +202,7 @@ private:
* Bootstraps the database table(s) associated to the VirtualRouter
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(VirtualRouter::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Rebuilds the object from an xml formatted string
@ -234,12 +229,6 @@ private:
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Writes the VirtualRouter in the database.
* @param db pointer to the db

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "VirtualRouter.h"
#include "OneDB.h"
/**
* The VirtualRouter Pool class.
@ -27,7 +28,7 @@ class VirtualRouterPool : public PoolSQL
{
public:
VirtualRouterPool(SqlDB * db) : PoolSQL(db, VirtualRouter::table){};
VirtualRouterPool(SqlDB * db) : PoolSQL(db, one_db::vn_table){};
~VirtualRouterPool(){};
@ -69,7 +70,7 @@ public:
*
* @return a pointer to the object, 0 in case of failure
*/
VirtualRouter * get(int oid)
VirtualRouter * get(int oid)
{
return static_cast<VirtualRouter *>(PoolSQL::get(oid));
};
@ -101,7 +102,7 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "VROUTER_POOL", "body", VirtualRouter::table,
return PoolSQL::dump(oss, "VROUTER_POOL", "body", one_db::vr_table,
where, sid, eid, desc);
};
@ -122,7 +123,7 @@ public:
*/
int search(vector<int>& oids, const string& where)
{
return PoolSQL::search(oids, VirtualRouter::table, where);
return PoolSQL::search(oids, one_db::vr_table, where);
};
private:

View File

@ -19,11 +19,8 @@
#include <map>
#include <string>
#include <sstream>
#include "VirtualMachineManagerDriver.h"
using namespace std;
/**
* XML Driver class implements a Generic VM Manager Driver that uses a neutral
* XML representation for the VM templates.
@ -32,15 +29,12 @@ class XMLDriver : public VirtualMachineManagerDriver
{
public:
XMLDriver(
int userid,
const map<string,string> &attrs,
bool sudo,
VirtualMachinePool * pool):
VirtualMachineManagerDriver(userid, attrs,sudo,pool)
{};
XMLDriver(const std::string& mad_location,
const std::map<std::string, std::string> &attrs):
VirtualMachineManagerDriver(mad_location, attrs)
{}
~XMLDriver(){};
~XMLDriver() = default;
private:
/**
@ -51,7 +45,7 @@ private:
*/
int deployment_description(
const VirtualMachine * vm,
const string& file_name) const override;
const std::string& file_name) const override;
};
#endif /*XML_DRIVER_H_*/

View File

@ -19,11 +19,8 @@
#include <map>
#include <string>
#include <sstream>
#include "VirtualMachineManagerDriver.h"
using namespace std;
/**
* Xen Driver class implements a VM Manager Driver to interface with the Xen
* hypervisor.
@ -32,15 +29,12 @@ class XenDriver : public VirtualMachineManagerDriver
{
public:
XenDriver(
int userid,
const map<string,string> &attrs,
bool sudo,
VirtualMachinePool * pool):
VirtualMachineManagerDriver(userid, attrs,sudo,pool)
{};
XenDriver(const std::string& mad_location,
const std::map<std::string, std::string> &attrs):
VirtualMachineManagerDriver(mad_location, attrs)
{}
~XenDriver(){};
~XenDriver() = default;
private:
/**
@ -51,7 +45,7 @@ private:
*/
int deployment_description(
const VirtualMachine * vm,
const string& file_name) const override;
const std::string& file_name) const override;
};
#endif /*XEN_DRIVER_H_*/

View File

@ -108,11 +108,6 @@ private:
// -------------------------------------------------------------------------
// DataBase implementation (Private)
// -------------------------------------------------------------------------
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
@ -127,12 +122,7 @@ private:
* Bootstraps the database table(s) associated to the Zone
* @return 0 on success
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(Zone::db_bootstrap);
return db->exec_local_wr(oss);
};
static int bootstrap(SqlDB * db);
/**
* Writes the Zone in the database.

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "Zone.h"
#include "OneDB.h"
using namespace std;
@ -100,8 +101,8 @@ public:
int dump(std::string& oss, const std::string& where, int sid, int eid,
bool desc)
{
return PoolSQL::dump(oss, "ZONE_POOL", "body", Zone::table, where,
sid, eid, desc);
return PoolSQL::dump(oss, "ZONE_POOL", "body", one_db::zone_table,
where, sid, eid, desc);
};
/**
@ -119,7 +120,7 @@ public:
*/
int list_zones(vector<int>& zone_ids)
{
return list( zone_ids, Zone::table);
return list( zone_ids, one_db::zone_table);
}
/**

View File

@ -19,17 +19,7 @@
#include "AclManager.h"
#include "PoolObjectAuth.h"
#include "SqlDB.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * AclManager::table = "acl";
const char * AclManager::db_names = "oid, userset, resource, rights, zone";
const char * AclManager::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"acl (oid INT PRIMARY KEY, userset BIGINT, resource BIGINT, "
"rights BIGINT, zone BIGINT, UNIQUE(userset, resource, rights, zone))";
#include "OneDB.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -1135,7 +1125,7 @@ void AclManager::reverse_search(int uid,
int AclManager::bootstrap(SqlDB * _db)
{
ostringstream oss(db_bootstrap);
ostringstream oss(one_db::acl_db_bootstrap);
return _db->exec_local_wr(oss);
}
@ -1206,7 +1196,7 @@ int AclManager::select()
ostringstream oss;
int rc;
oss << "SELECT " << db_names << " FROM " << table;
oss << "SELECT " << one_db::acl_db_names << " FROM " << one_db::acl_table;
set_callback(static_cast<Callbackable::Callback>(&AclManager::select_cb));
@ -1239,7 +1229,8 @@ int AclManager::insert(AclRule * rule, SqlDB * db)
// Construct the SQL statement to Insert
oss << "INSERT INTO " << table <<" ("<< db_names <<") VALUES ("
oss << "INSERT INTO " << one_db::acl_table
<< " (" << one_db::acl_db_names << ") VALUES ("
<< rule->oid << ","
<< rule->user << ","
<< rule->resource << ","
@ -1260,7 +1251,7 @@ int AclManager::drop(int oid)
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE "
oss << "DELETE FROM " << one_db::acl_table << " WHERE "
<< "oid=" << oid;
rc = db->exec_wr(oss);

View File

@ -21,6 +21,8 @@
#include "PoolObjectAuth.h"
#include "Nebula.h"
using namespace std;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -31,7 +33,7 @@ const char * AuthManager::auth_driver_name = "auth_exe";
void AuthRequest::add_auth(Operation op,
const PoolObjectAuth& ob_perms,
string ob_template)
const string& ob_template)
{
ostringstream oss;
bool auth;
@ -43,7 +45,7 @@ void AuthRequest::add_auth(Operation op,
{
string * encoded_id = one_util::base64_encode(ob_template);
if (encoded_id != 0)
if (encoded_id != nullptr)
{
oss << *encoded_id << ":";
delete encoded_id;
@ -123,7 +125,7 @@ extern "C" void * authm_action_loop(void *arg)
{
AuthManager * authm;
if ( arg == 0 )
if ( arg == nullptr )
{
return 0;
}
@ -143,22 +145,37 @@ extern "C" void * authm_action_loop(void *arg)
int AuthManager::start()
{
int rc;
pthread_attr_t pattr;
rc = MadManager::start();
using namespace std::placeholders; // for _1
if ( rc != 0 )
register_action(AuthManagerMessages::UNDEFINED,
&AuthManager::_undefined);
register_action(AuthManagerMessages::AUTHORIZE,
bind(&AuthManager::_authorize, this, _1));
register_action(AuthManagerMessages::AUTHENTICATE,
bind(&AuthManager::_authenticate, this, _1));
register_action(AuthManagerMessages::LOG,
&AuthManager::_log);
string error;
if (DriverManager::start(error) != 0)
{
NebulaLog::error("AuM", error);
return -1;
}
NebulaLog::log("AuM",Log::INFO,"Starting Auth Manager...");
pthread_attr_init (&pattr);
pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_init(&pattr);
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
rc = pthread_create(&authm_thread,&pattr,authm_action_loop,(void *) this);
int rc = pthread_create(&authm_thread,&pattr,authm_action_loop,(void *) this);
return rc;
}
@ -171,12 +188,12 @@ void AuthManager::user_action(const ActionRequest& ar)
const AMAction& auth_ar = static_cast<const AMAction& >(ar);
AuthRequest * request = auth_ar.request();
if ( request == 0 )
if ( request == nullptr )
{
return;
}
switch(auth_ar.action())
switch (auth_ar.action())
{
case AMAction::AUTHENTICATE:
authenticate_action(request);
@ -193,17 +210,19 @@ void AuthManager::user_action(const ActionRequest& ar)
void AuthManager::authenticate_action(AuthRequest * ar)
{
const AuthManagerDriver * authm_md;
// ------------------------------------------------------------------------
// Get the driver
// ------------------------------------------------------------------------
authm_md = get();
auto authm_md = get();
if (authm_md == 0)
if (authm_md == nullptr)
{
goto error_driver;
ar->result = false;
ar->message = "Could not find Authorization driver";
ar->notify();
return;
}
// ------------------------------------------------------------------------
@ -216,18 +235,17 @@ void AuthManager::authenticate_action(AuthRequest * ar)
// Make the request to the driver
// ---- --------------------------------------------------------------------
authm_md->authenticate(ar->id,
ar->uid,
ar->driver,
ar->username,
ar->password,
ar->session);
return;
ostringstream oss;
error_driver:
ar->result = false;
ar->message = "Could not find Authorization driver";
ar->notify();
oss << ar->uid << " "
<< ar->driver << " "
<< ar->username << " "
<< ar->password << " "
<< ar->session << " " << endl;
auth_msg_t msg(AuthManagerMessages::AUTHENTICATE, "", ar->id, oss.str());
authm_md->write(msg);
}
/* -------------------------------------------------------------------------- */
@ -235,19 +253,30 @@ error_driver:
void AuthManager::authorize_action(AuthRequest * ar)
{
const AuthManagerDriver * authm_md;
string auths;
// ------------------------------------------------------------------------
// Get the driver
// ------------------------------------------------------------------------
authm_md = get();
auto authm_md = get();
if (authm_md == 0)
if (authm_md == nullptr)
{
ar->message = "Could not find Authorization driver";
goto error;
ar->result = false;
ar->notify();
return;
}
auto auths = ar->get_auths();
if (auths.empty())
{
ar->message = "Empty authorization string";
ar->result = false;
ar->notify();
return;
}
// ------------------------------------------------------------------------
@ -260,47 +289,34 @@ void AuthManager::authorize_action(AuthRequest * ar)
// Make the request to the driver
// ------------------------------------------------------------------------
auths = ar->get_auths();
ostringstream oss;
if ( auths.empty() )
{
ar->message = "Empty authorization string";
goto error;
}
oss << ar->uid << " "
<< auths << " "
<< ar->self_authorize << endl;
authm_md->authorize(ar->id, ar->uid, auths, ar->self_authorize);
auth_msg_t msg(AuthManagerMessages::AUTHORIZE, "", ar->id, oss.str());
return;
error:
ar->result = false;
ar->notify();
return;
authm_md->write(msg);
}
/* ************************************************************************** */
/* MAD Loading */
/* ************************************************************************** */
int AuthManager::load_mads(int uid)
int AuthManager::load_drivers(const std::vector<const VectorAttribute*>& _mads)
{
ostringstream oss;
const VectorAttribute * vattr = 0;
int rc;
string name;
AuthManagerDriver * authm_driver = 0;
const VectorAttribute * vattr = nullptr;
oss << "Loading Auth. Manager driver.";
NebulaLog::log("AuM", Log::INFO, "Loading Auth. Manager driver.");
NebulaLog::log("AuM",Log::INFO,oss);
if ( mad_conf.size() > 0 )
if ( _mads.size() > 0 )
{
vattr = static_cast<const VectorAttribute *>(mad_conf[0]);
vattr = _mads[0];
}
if ( vattr == 0 )
if ( vattr == nullptr )
{
NebulaLog::log("AuM",Log::ERROR,"Failed to load Auth. Manager driver.");
return -1;
@ -333,17 +349,13 @@ int AuthManager::load_mads(int uid)
auth_conf.replace("ARGUMENTS", oss.str());
authm_driver = new AuthManagerDriver(uid,auth_conf.value(),(uid!=0),this);
rc = add(authm_driver);
if ( rc == 0 )
if (load_driver(&auth_conf) != 0)
{
oss.str("");
oss << "\tAuth Manager loaded";
NebulaLog::log("AuM",Log::INFO,oss);
NebulaLog::error("ImM", "Unable to load Auth Manager driver");
return -1;
}
return rc;
NebulaLog::log("AuM", Log::INFO, "\tAuth Manager loaded");
return 0;
}

View File

@ -1,131 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "AuthManagerDriver.h"
#include "AuthManager.h"
#include "NebulaLog.h"
#include "Nebula.h"
#include <sstream>
/* ************************************************************************** */
/* Driver ASCII Protocol Implementation */
/* ************************************************************************** */
void AuthManagerDriver::authorize(int oid,
int uid,
const string& reqs,
bool acl) const
{
ostringstream os;
os << "AUTHORIZE " << oid << " " << uid << " " << reqs << " " << acl <<endl;
write(os);
}
void AuthManagerDriver::authenticate(int oid,
int uid,
const string& driver,
const string& username,
const string& password,
const string& session) const
{
ostringstream os;
os << "AUTHENTICATE " << oid << " "
<< uid << " "
<< driver << " "
<< username << " "
<< password << " "
<< session << endl;
write(os);
}
/* ************************************************************************** */
/* MAD Interface */
/* ************************************************************************** */
void AuthManagerDriver::protocol(const string& message) const
{
istringstream is(message);
ostringstream os;
string action;
string result;
string info="";
int id;
os << "Message received: " << message;
NebulaLog::log("AuM", Log::DEBUG, os);
// Parse the driver message
if ( is.good() )
is >> action >> ws;
else
return;
if ( is.good() )
is >> result >> ws;
else
return;
if ( is.good() )
{
is >> id >> ws;
if ( is.fail() )
{
if ( action == "LOG" )
{
is.clear();
getline(is,info);
NebulaLog::log("AuM", log_type(result[0]), info.c_str());
}
return;
}
}
else
return;
getline(is,info);
if (action == "LOG")
{
NebulaLog::log("AuM",Log::INFO,info.c_str());
}
else if (result == "SUCCESS")
{
authm->notify_request(id,true,info);
}
else
{
authm->notify_request(id,false,info);
}
return;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void AuthManagerDriver::recover()
{
NebulaLog::log("AuM",Log::INFO,"Recovering Authorization drivers");
}

View File

@ -0,0 +1,66 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "AuthManager.h"
#include "NebulaLog.h"
/* ************************************************************************** */
/* Driver Protocol Implementation */
/* ************************************************************************** */
void AuthManager::_undefined(unique_ptr<auth_msg_t> msg)
{
NebulaLog::warn("AuM", "Received UNDEFINED msg: " + msg->payload());
}
/* -------------------------------------------------------------------------- */
void AuthManager::_authorize(unique_ptr<auth_msg_t> msg)
{
NebulaLog::debug("AuM", "_authorize: " + msg->payload());
if (msg->status() == "SUCCESS")
{
notify_request(msg->oid(), true, msg->payload());
}
else
{
notify_request(msg->oid(), false, msg->payload());
}
}
/* -------------------------------------------------------------------------- */
void AuthManager::_authenticate(unique_ptr<auth_msg_t> msg)
{
NebulaLog::debug("AuM", "_authenticate: " + msg->payload());
if (msg->status() == "SUCCESS")
{
notify_request(msg->oid(), true, msg->payload());
}
else
{
notify_request(msg->oid(), false, msg->payload());
}
}
/* -------------------------------------------------------------------------- */
void AuthManager::_log(unique_ptr<auth_msg_t> msg)
{
NebulaLog::log("AuM", log_type(msg->status()[0]), msg->payload());
}

View File

@ -23,7 +23,7 @@ lib_name='nebula_authm'
# Sources to generate the library
source_files=[
'AuthManager.cc',
'AuthManagerDriver.cc'
'AuthManagerProtocol.cc'
]
# Build library

View File

@ -17,33 +17,10 @@
#include "Cluster.h"
#include "GroupPool.h"
#include "Nebula.h"
#include "OneDB.h"
#include <sstream>
const char * Cluster::table = "cluster_pool";
const char * Cluster::db_names =
"oid, name, body, uid, gid, owner_u, group_u, other_u";
const char * Cluster::db_bootstrap = "CREATE TABLE IF NOT EXISTS cluster_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, "
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
"UNIQUE(name))";
const char * Cluster::datastore_table = "cluster_datastore_relation";
const char * Cluster::datastore_db_names = "cid, oid";
const char * Cluster::datastore_db_bootstrap =
"CREATE TABLE IF NOT EXISTS cluster_datastore_relation ("
"cid INTEGER, oid INTEGER, PRIMARY KEY(cid, oid))";
const char * Cluster::network_table = "cluster_network_relation";
const char * Cluster::network_db_names = "cid, oid";
const char * Cluster::network_db_bootstrap =
"CREATE TABLE IF NOT EXISTS cluster_network_relation ("
"cid INTEGER, oid INTEGER, PRIMARY KEY(cid, oid))";
const char * Cluster::bitmap_table = "cluster_vnc_bitmap";
/* ************************************************************************** */
/* Cluster :: Constructor/Destructor */
/* ************************************************************************** */
@ -53,11 +30,11 @@ Cluster::Cluster(
const string& name,
ClusterTemplate* cl_template,
const VectorAttribute& vnc_conf):
PoolObjectSQL(id,CLUSTER,name,-1,-1,"","",table),
PoolObjectSQL(id,CLUSTER,name,-1,-1,"","",one_db::cluster_table),
hosts("HOSTS"),
datastores("DATASTORES"),
vnets("VNETS"),
vnc_bitmap(vnc_conf, id, bitmap_table)
vnc_bitmap(vnc_conf, id, one_db::cluster_bitmap_table)
{
if (cl_template != 0)
{
@ -184,7 +161,7 @@ int Cluster::insert_replace(SqlDB *db, bool replace, string& error_str)
if ( replace )
{
oss << "UPDATE " << table << " SET "
oss << "UPDATE " << one_db::cluster_table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
@ -197,7 +174,8 @@ int Cluster::insert_replace(SqlDB *db, bool replace, string& error_str)
}
else
{
oss << "INSERT INTO " << table << " (" << db_names << ") VALUES ("
oss << "INSERT INTO " << one_db::cluster_table
<< " (" << one_db::cluster_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
@ -239,6 +217,26 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Cluster::bootstrap(SqlDB * db)
{
int rc;
ostringstream oss;
oss.str(one_db::cluster_db_bootstrap);
rc = db->exec_local_wr(oss);
oss.str(one_db::cluster_datastore_db_bootstrap);
rc += db->exec_local_wr(oss);
oss.str(one_db::cluster_network_db_bootstrap);
rc += db->exec_local_wr(oss);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Cluster::to_xml(string& xml) const
{
ostringstream oss;

View File

@ -39,7 +39,7 @@ const int ClusterPool::DEFAULT_CLUSTER_ID = 0;
ClusterPool::ClusterPool(SqlDB * db,
const VectorAttribute * _vnc_conf,
vector<const SingleAttribute *>& encrypted_attrs):
PoolSQL(db, Cluster::table), vnc_conf(_vnc_conf)
PoolSQL(db, one_db::cluster_table), vnc_conf(_vnc_conf)
{
ostringstream oss;
string error_str;
@ -192,13 +192,13 @@ void ClusterPool::cluster_acl_filter(ostringstream& filter,
break;
case PoolObjectSQL::DATASTORE:
filter << " OR oid IN ( SELECT oid from " << Cluster::datastore_table
<< " WHERE ";
filter << " OR oid IN ( SELECT oid from "
<< one_db::cluster_datastore_table << " WHERE ";
fc = ")";
break;
case PoolObjectSQL::NET:
filter << " OR oid IN ( SELECT oid from " << Cluster::network_table
filter << " OR oid IN ( SELECT oid from " << one_db::cluster_network_table
<< " WHERE ";
fc = ")";
break;
@ -230,8 +230,8 @@ int ClusterPool::query_datastore_clusters(int oid, set<int> &cluster_ids)
cb.set_callback(&cluster_ids);
oss << "SELECT cid FROM " << Cluster::datastore_table << " WHERE oid = "
<< oid;
oss << "SELECT cid FROM " << one_db::cluster_datastore_table
<< " WHERE oid = " << oid;
int rc = db->exec_rd(oss, &cb);
@ -255,7 +255,7 @@ int ClusterPool::query_vnet_clusters(int oid, set<int> &cluster_ids)
cb.set_callback(&cluster_ids);
oss << "SELECT cid FROM " << Cluster::network_table << " WHERE oid = "<<oid;
oss << "SELECT cid FROM " << one_db::cluster_network_table << " WHERE oid = "<<oid;
int rc = db->exec_rd(oss, &cb);
@ -280,12 +280,12 @@ int ClusterPool::add_to_cluster(PoolObjectSQL::ObjectType type, Cluster* cluster
switch (type)
{
case PoolObjectSQL::DATASTORE:
table = cluster->datastore_table;
names = cluster->datastore_db_names;
table = one_db::cluster_datastore_table;
names = one_db::cluster_datastore_db_names;
break;
case PoolObjectSQL::NET:
table = cluster->network_table;
names = cluster->network_db_names;
table = one_db::cluster_network_table;
names = one_db::cluster_network_db_names;
break;
case PoolObjectSQL::HOST:
break;
@ -337,10 +337,10 @@ int ClusterPool::del_from_cluster(PoolObjectSQL::ObjectType type, Cluster* clust
switch (type)
{
case PoolObjectSQL::DATASTORE:
table = cluster->datastore_table;
table = one_db::cluster_datastore_table;
break;
case PoolObjectSQL::NET:
table = cluster->network_table;
table = one_db::cluster_network_table;
break;
case PoolObjectSQL::HOST:
break;

View File

@ -26,7 +26,8 @@ source_files=[
'Attribute.cc',
'ExtendedAttribute.cc',
'NebulaService.cc',
'NebulaUtil.cc'
'NebulaUtil.cc',
'SSLUtil.cc'
]
# Build library

327
src/common/SSLUtil.cc Normal file
View File

@ -0,0 +1,327 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "SSLUtil.h"
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <zlib.h>
namespace ssl_util
{
void base64_decode(const std::string& in, std::string& out)
{
BIO * bio_64 = BIO_new(BIO_f_base64());
BIO * bio_mem_in = BIO_new(BIO_s_mem());
BIO * bio_mem_out = BIO_new(BIO_s_mem());
bio_64 = BIO_push(bio_64, bio_mem_in);
BIO_set_flags(bio_64, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio_mem_in, in.c_str(), in.length());
char inbuf[512];
int inlen;
while ((inlen = BIO_read(bio_64, inbuf, 512)) > 0)
{
BIO_write(bio_mem_out, inbuf, inlen);
}
char * decoded_c;
long int size = BIO_get_mem_data(bio_mem_out, &decoded_c);
out.assign(decoded_c, size);
BIO_free_all(bio_64);
BIO_free_all(bio_mem_out);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int base64_encode(const std::string& in, std::string &out)
{
BIO * bio_64 = BIO_new(BIO_f_base64());
BIO * bio_mem = BIO_new(BIO_s_mem());
BIO_push(bio_64, bio_mem);
BIO_set_flags(bio_64, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio_64, in.c_str(), in.length());
if (BIO_flush(bio_64) != 1)
{
return -1;
}
char * encoded_c;
long int size = BIO_get_mem_data(bio_mem, &encoded_c);
out.assign(encoded_c, size);
BIO_free_all(bio_64);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* Buffer length for zlib inflate/deflate
*/
#define ZBUFFER 16384
int zlib_decompress(const std::string& in, std::string& out)
{
if ( in.empty() )
{
return -1;
}
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
zs.avail_in = 0;
zs.next_in = Z_NULL;
if ( inflateInit(&zs) != Z_OK)
{
return -1;
}
zs.avail_in = in.size();
zs.next_in = (unsigned char *) const_cast<char *>(in.c_str());
if ( zs.avail_in <= 2 ) //At least 2 byte header
{
inflateEnd(&zs);
return -1;
}
unsigned char zbuf[ZBUFFER];
std::string result;
int rc;
do
{
zs.avail_out = ZBUFFER;
zs.next_out = zbuf;
rc = inflate(&zs, Z_FINISH);
if ( rc != Z_STREAM_END && rc != Z_OK && rc != Z_BUF_ERROR )
{
inflateEnd(&zs);
return -1;
}
result.append((const char *) zbuf, (size_t) (ZBUFFER - zs.avail_out));
} while (rc != Z_STREAM_END);
inflateEnd(&zs);
out = result;
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int zlib_compress(const std::string& in, std::string& out)
{
if ( in.empty() )
{
return -1;
}
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
if ( deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK )
{
return -1;
}
zs.avail_in = in.size();
zs.next_in = (unsigned char *) const_cast<char *>(in.c_str());
unsigned char zbuf[ZBUFFER];
std::string result;
do
{
zs.avail_out = ZBUFFER;
zs.next_out = zbuf;
if ( deflate(&zs, Z_FINISH) == Z_STREAM_ERROR )
{
deflateEnd(&zs);
return -1;
}
result.append((const char *) zbuf, (size_t) (ZBUFFER - zs.avail_out));
} while (zs.avail_out == 0);
deflateEnd(&zs);
out = result;
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static std::string pubk_path;
static std::string prik_path;
void init_rsa_keys(const std::string& pub_key, const std::string& pri_key)
{
pubk_path = pub_key;
prik_path = pri_key;
}
bool is_rsa_set()
{
return !prik_path.empty();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int rsa_public_encrypt(const std::string& in, std::string& out)
{
static RSA * rsa = nullptr;
if ( rsa == nullptr) //initialize RSA structure
{
FILE * fp = fopen(pubk_path.c_str(), "r");
if ( fp == nullptr )
{
return -1;
}
rsa = PEM_read_RSAPublicKey(fp, &rsa, nullptr, nullptr);
if ( rsa == nullptr )
{
return -1;
}
fclose(fp);
}
char * out_c = (char *) malloc(sizeof(char) * RSA_size(rsa));
int rc = RSA_public_encrypt(in.length(), (const unsigned char *) in.c_str(),
(unsigned char *) out_c, rsa, RSA_PKCS1_PADDING);
if ( rc != -1 )
{
out.assign(out_c, rc);
rc = 0;
}
free(out_c);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int rsa_private_decrypt(const std::string& in, std::string& out)
{
static RSA * rsa = nullptr;
if ( rsa == nullptr) //initialize RSA structure
{
FILE * fp = fopen(prik_path.c_str(), "r");
if ( fp == nullptr )
{
return -1;
}
rsa = PEM_read_RSAPrivateKey(fp, &rsa, nullptr, nullptr);
if ( rsa == nullptr )
{
return -1;
}
fclose(fp);
}
std::string result;
int key_size = RSA_size(rsa);
int in_size = in.length();
char * out_c = (char *) malloc(sizeof(char) * RSA_size(rsa));
const char * in_c = in.c_str();
for (int index = 0; index < in_size; index += key_size)
{
int block_size = key_size;
if ( index + key_size > in_size )
{
block_size = in_size - index;
}
int rc = RSA_private_decrypt(block_size, (const unsigned char *)
in_c + index, (unsigned char *) out_c, rsa, RSA_PKCS1_PADDING);
if ( rc != -1 )
{
result.append(out_c, rc);
rc = 0;
}
else
{
free(out_c);
return -1;
}
}
free(out_c);
out = result;
return 0;
}
} // namespase ssl_util

View File

@ -20,16 +20,7 @@
#include "NebulaLog.h"
#include "Nebula.h"
#include "VirtualMachineDisk.h"
const char * Datastore::table = "datastore_pool";
const char * Datastore::db_names =
"oid, name, body, uid, gid, owner_u, group_u, other_u";
const char * Datastore::db_bootstrap =
"CREATE TABLE IF NOT EXISTS datastore_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, "
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER)";
#include "OneDB.h"
/* ************************************************************************ */
/* Datastore :: Constructor/Destructor */
@ -43,7 +34,7 @@ Datastore::Datastore(
int umask,
DatastoreTemplate* ds_template,
const set<int> &cluster_ids):
PoolObjectSQL(-1,DATASTORE,"",uid,gid,uname,gname,table),
PoolObjectSQL(-1,DATASTORE,"",uid,gid,uname,gname,one_db::ds_table),
Clusterable(cluster_ids),
ds_mad(""),
tm_mad(""),
@ -704,7 +695,7 @@ int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str)
if ( replace )
{
oss << "UPDATE " << table << " SET "
oss << "UPDATE " << one_db::ds_table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
@ -716,7 +707,8 @@ int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str)
}
else
{
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
oss << "INSERT INTO " << one_db::ds_table
<< " ("<< one_db::ds_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
@ -758,6 +750,16 @@ error_common:
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::bootstrap(SqlDB * db)
{
ostringstream oss(one_db::ds_db_bootstrap);
return db->exec_local_wr(oss);
};
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Datastore::to_xml(string& xml) const
{
ostringstream oss;

View File

@ -45,7 +45,7 @@ DatastorePool::DatastorePool(
SqlDB * db,
const vector<const SingleAttribute *>& _inherit_attrs,
vector<const SingleAttribute *>& encrypted_attrs) :
PoolSQL(db, Datastore::table)
PoolSQL(db, one_db::ds_table)
{
ostringstream oss;

View File

@ -20,6 +20,7 @@
#include "VirtualMachineManager.h"
#include "TransferManager.h"
#include "ImageManager.h"
#include "LifeCycleManager.h"
#include "Quotas.h"
#include "Request.h"
#include "Nebula.h"
@ -1311,7 +1312,7 @@ int DispatchManager::delete_vm_db(VirtualMachine * vm,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static void close_cp_history(VirtualMachinePool *vmpool, VirtualMachine *vm,
static void close_cp_history(VirtualMachinePool *vmpool, VirtualMachine *vm,
VMActions::Action action, const RequestAttributes& ra)
{
time_t the_time = time(0);

View File

@ -15,6 +15,7 @@
/* ------------------------------------------------------------------------ */
#include "Document.h"
#include "OneDB.h"
/* ************************************************************************ */
/* Document :: Constructor/Destructor */
@ -27,8 +28,9 @@ Document::Document( int id,
const string& _gname,
int _umask,
int _type,
Template * _template_contents):
PoolObjectSQL(id,DOCUMENT,"",_uid,_gid,_uname,_gname,table), type(_type)
Template * _template_contents)
: PoolObjectSQL(id,DOCUMENT,"",_uid,_gid,_uname,_gname,one_db::doc_table)
, type(_type)
{
if (_template_contents != 0)
{
@ -46,19 +48,6 @@ Document::Document( int id,
/* Document :: Database Access Functions */
/* ************************************************************************ */
const char * Document::table = "document_pool";
const char * Document::db_names =
"oid, name, body, type, uid, gid, owner_u, group_u, other_u";
const char * Document::db_bootstrap =
"CREATE TABLE IF NOT EXISTS document_pool (oid INTEGER PRIMARY KEY, "
"name VARCHAR(128), body MEDIUMTEXT, type INTEGER, uid INTEGER, gid INTEGER, "
"owner_u INTEGER, group_u INTEGER, other_u INTEGER)";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Document::insert(SqlDB *db, string& error_str)
{
int rc;
@ -126,7 +115,7 @@ int Document::insert_replace(SqlDB *db, bool replace, string& error_str)
if(replace)
{
oss << "UPDATE " << table << " SET "
oss << "UPDATE " << one_db::doc_table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "type = " << type << ", "
@ -139,7 +128,8 @@ int Document::insert_replace(SqlDB *db, bool replace, string& error_str)
}
else
{
oss << "INSERT INTO " << table << " (" << db_names << ") VALUES ("
oss << "INSERT INTO " << one_db::doc_table
<< " (" << one_db::doc_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
@ -179,6 +169,16 @@ error_common:
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Document::bootstrap(SqlDB * db)
{
ostringstream oss(one_db::doc_db_bootstrap);
return db->exec_local_wr(oss);
}
/* ************************************************************************ */
/* Document :: Misc */
/* ************************************************************************ */

View File

@ -17,18 +17,21 @@
#include "Group.h"
#include "Nebula.h"
#include "AclManager.h"
#include "OneDB.h"
#include <sstream>
const char * Group::table = "group_pool";
Group::Group(int id, const string& name):
PoolObjectSQL(id,GROUP,name,-1,-1,"","",one_db::group_table),
quota(),
users("USERS"),
admins("ADMINS")
{
// Allow users in this group to see it
group_u = 1;
const char * Group::db_names =
"oid, name, body, uid, gid, owner_u, group_u, other_u";
const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, "
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
"UNIQUE(name))";
obj_template = new GroupTemplate;
}
/* ************************************************************************ */
/* Group :: Database Access Functions */
@ -138,7 +141,7 @@ int Group::insert_replace(SqlDB *db, bool replace, string& error_str)
if ( replace )
{
oss << "UPDATE " << table << " SET "
oss << "UPDATE " << one_db::group_table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
@ -150,15 +153,16 @@ int Group::insert_replace(SqlDB *db, bool replace, string& error_str)
}
else
{
oss << "INSERT INTO " << table << " (" << db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";
oss << "INSERT INTO " << one_db::group_table
<< " (" << one_db::group_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";
}
rc = db->exec_wr(oss);
@ -242,6 +246,16 @@ string& Group::to_xml_extended(string& xml, bool extended) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Group::bootstrap(SqlDB * db)
{
ostringstream oss_group(one_db::group_db_bootstrap);
return db->exec_local_wr(oss_group);
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Group::from_xml(const string& xml)
{
int rc = 0;

View File

@ -17,6 +17,7 @@
#include "GroupPool.h"
#include "Nebula.h"
#include "NebulaLog.h"
#include "OneDB.h"
#include <stdexcept>
@ -38,7 +39,8 @@ const int GroupPool::USERS_ID = 1;
/* -------------------------------------------------------------------------- */
GroupPool::GroupPool(SqlDB * db, bool is_slave,
vector<const SingleAttribute *>& restricted_attrs):PoolSQL(db, Group::table)
vector<const SingleAttribute *>& restricted_attrs)
: PoolSQL(db, one_db::group_table)
{
ostringstream oss;
string error_str;
@ -225,10 +227,10 @@ int GroupPool::dump(string& oss, const string& where, int sid, int eid, bool des
ostringstream cmd;
cmd << "SELECT " << Group::table << ".body, "
<< GroupQuotas::db_table << ".body" << " FROM " << Group::table
<< " LEFT JOIN " << GroupQuotas::db_table << " ON "
<< Group::table << ".oid=" << GroupQuotas::db_table << ".group_oid";
cmd << "SELECT " << one_db::group_table << ".body, "
<< one_db::group_quotas_db_table << ".body" << " FROM " << one_db::group_table
<< " LEFT JOIN " << one_db::group_quotas_db_table << " ON "
<< one_db::group_table << ".oid=" << one_db::group_quotas_db_table << ".group_oid";
if ( !where.empty() )
{

View File

@ -21,21 +21,26 @@
#include "HookStateHost.h"
#include "HookStateVM.h"
#include "HookLog.h"
#include "OneDB.h"
#include <string>
/* ************************************************************************ */
/* Hook :: Database Access Functions */
/* ************************************************************************ */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * Hook::table = "hook_pool";
const char * Hook::db_names =
"oid, name, body, uid, gid, owner_u, group_u, other_u, type";
const char * Hook::db_bootstrap = "CREATE TABLE IF NOT EXISTS hook_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER,"
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, type INTEGER)";
Hook::Hook(Template * tmpl):
PoolObjectSQL(-1, HOOK, "", -1, -1, "", "", one_db::hook_table),
type(HookType::UNDEFINED), cmd(""), remote(false), _hook(0)
{
if (tmpl != 0)
{
obj_template = tmpl;
}
else
{
obj_template = new Template();
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -175,6 +180,16 @@ int Hook::post_update_template(string& error)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Hook::bootstrap(SqlDB * db)
{
std::ostringstream oss_hook(one_db::hook_db_bootstrap);
return db->exec_local_wr(oss_hook);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Hook::insert(SqlDB *db, std::string& error_str)
{
std::string type_str;
@ -286,7 +301,8 @@ int Hook::insert_replace(SqlDB *db, bool replace, std::string& error_str)
}
// Construct the SQL statement to Insert or Replace
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
oss <<" INTO "<<one_db::hook_table
<< " (" << one_db::hook_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
@ -391,7 +407,7 @@ int Hook::drop(SqlDB *db)
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
oss << "DELETE FROM " << one_db::hook_table << " WHERE oid=" << oid;
rc = db->exec_wr(oss);

View File

@ -27,20 +27,9 @@
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * HookLog::table = "hook_log";
const char * HookLog::db_names = "hkid, exeid, timestamp, rc, body";
const char * HookLog::db_bootstrap = "CREATE TABLE IF NOT EXISTS hook_log"
" (hkid INTEGER, exeid INTEGER, timestamp INTEGER, rc INTEGER,"
" body MEDIUMTEXT,PRIMARY KEY(hkid, exeid))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HookLog::bootstrap(SqlDB * db)
{
std::ostringstream oss_hook(HookLog::db_bootstrap);
std::ostringstream oss_hook(one_db::hook_log_db_bootstrap);
return db->exec_local_wr(oss_hook);
}
@ -64,7 +53,7 @@ int HookLog::_dump_log(int hkid, int exec_id, std::string &xml_log)
string_cb cb(1);
cmd << "SELECT body FROM "<< table;
cmd << "SELECT body FROM "<< one_db::hook_log_table;
if ( hkid == -1 )
{
@ -99,7 +88,7 @@ int HookLog::dump_log(const std::string &where_clause, std::string &xml_log)
string_cb cb(1);
cmd << "SELECT body FROM "<< table;
cmd << "SELECT body FROM "<< one_db::hook_log_table;
if (!where_clause.empty())
{
@ -141,8 +130,9 @@ int HookLog::dump_log(std::string &xml_log)
int HookLog::drop(SqlDB *db, const int hook_id)
{
ostringstream oss;
oss << "DELETE FROM " << table << " WHERE hkid =" << hook_id;
oss << "DELETE FROM " << one_db::hook_log_table
<< " WHERE hkid =" << hook_id;
return db->exec_wr(oss);
}
@ -204,7 +194,8 @@ int HookLog::add(int hkid, int hkrc, std::string &xml_result)
oss.str("");
oss <<"INSERT INTO "<< table <<" ("<< db_names <<") VALUES ("
oss << "INSERT INTO " << one_db::hook_log_table
<< " (" << one_db::hook_log_db_names << ") VALUES ("
<< hkid << ","
<< last_exeid << ","
<< the_time << ","
@ -217,7 +208,8 @@ int HookLog::add(int hkid, int hkrc, std::string &xml_result)
{
oss.str("");
oss << "DELETE FROM " << table << " WHERE hkid = " << hkid
oss << "DELETE FROM " << one_db::hook_log_table
<< " WHERE hkid = " << hkid
<< " AND exeid <= " << last_exeid - log_retention;
rc = db->exec_wr(oss);

View File

@ -26,7 +26,7 @@ extern "C" void * hm_action_loop(void *arg)
{
HookManager * hm;
if ( arg == 0 )
if ( arg == nullptr )
{
return 0;
}
@ -47,22 +47,35 @@ extern "C" void * hm_action_loop(void *arg)
int HookManager::start()
{
int rc;
pthread_attr_t pattr;
rc = MadManager::start();
using namespace std::placeholders; // for _1
if ( rc != 0 )
register_action(HookManagerMessages::UNDEFINED,
&HookManager::_undefined);
register_action(HookManagerMessages::EXECUTE,
bind(&HookManager::_execute, this, _1));
register_action(HookManagerMessages::RETRY,
bind(&HookManager::_retry, this, _1));
register_action(HookManagerMessages::LOG,
&HookManager::_log);
string error;
if ( DriverManager::start(error) != 0 )
{
NebulaLog::error("HKM", error);
return -1;
}
NebulaLog::log("HKM",Log::INFO,"Starting Hook Manager...");
pthread_attr_init (&pattr);
pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_init(&pattr);
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
rc = pthread_create(&hm_thread,&pattr,hm_action_loop,(void *) this);
int rc = pthread_create(&hm_thread,&pattr,hm_action_loop,(void *) this);
return rc;
}
@ -70,21 +83,18 @@ int HookManager::start()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HookManager::load_mads(int uid)
int HookManager::load_drivers(const std::vector<const VectorAttribute*>& _mads)
{
HookManagerDriver * hm_mad;
ostringstream oss;
const VectorAttribute * vattr = 0;
int rc;
const VectorAttribute * vattr = nullptr;
NebulaLog::log("HKM",Log::INFO,"Loading Hook Manager driver.");
if ( mad_conf.size() > 0 )
if ( _mads.size() > 0 )
{
vattr = static_cast<const VectorAttribute *>(mad_conf[0]);
vattr = static_cast<const VectorAttribute *>(_mads[0]);
}
if ( vattr == 0 )
if ( vattr == nullptr )
{
NebulaLog::log("HKM",Log::INFO,"Failed to load Hook Manager driver.");
return -1;
@ -94,19 +104,15 @@ int HookManager::load_mads(int uid)
hook_conf.replace("NAME",hook_driver_name);
hm_mad = new HookManagerDriver(0,hook_conf.value(),false);
rc = add(hm_mad);
if ( rc == 0 )
if ( load_driver(&hook_conf) != 0 )
{
oss.str("");
oss << "\tHook Manager loaded";
NebulaLog::log("HKM",Log::INFO,oss);
NebulaLog::error("HKM", "Unable to load Hook Manager driver");
return -1;
}
return rc;
NebulaLog::log("HKM",Log::INFO,"\tHook Manager loaded");
return 0;
}
/* -------------------------------------------------------------------------- */
@ -133,14 +139,15 @@ void HookManager::user_action(const ActionRequest& ar)
void HookManager::send_event_action(const std::string& message)
{
const HookManagerDriver* hmd = get();
auto hmd = get();
if ( hmd == nullptr )
{
return;
}
hmd->execute(message);
hook_msg_t msg(HookManagerMessages::EXECUTE, "", -1, message);
hmd->write(msg);
}
/* -------------------------------------------------------------------------- */
@ -148,14 +155,15 @@ void HookManager::send_event_action(const std::string& message)
void HookManager::retry_action(const std::string& message)
{
const HookManagerDriver* hmd = get();
auto hmd = get();
if ( hmd == nullptr )
{
return;
}
hmd->retry(message);
hook_msg_t msg(HookManagerMessages::RETRY, "", -1, message);
hmd->write(msg);
}
/* -------------------------------------------------------------------------- */

View File

@ -1,159 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "HookManagerDriver.h"
#include "Nebula.h"
#include "NebulaLog.h"
#include <sstream>
/* ************************************************************************** */
/* Driver ASCII Protocol Implementation */
/* ************************************************************************** */
void HookManagerDriver::execute(
const string& message ) const
{
ostringstream oss;
oss << "EXECUTE " << message << endl;
write(oss);
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
void HookManagerDriver::retry(
const string& message ) const
{
ostringstream oss;
oss << "RETRY " << message << endl;
write(oss);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void HookManagerDriver::protocol(const string& message) const
{
std::string error_str;
std::istringstream is(message);
//stores the action name
std::string action;
std::string result;
NebulaLog::log("HKM", Log::DEBUG, "Message received: " + message);
Nebula& nd = Nebula::instance();
HookLog* hl = nd.get_hl();
// Parse the driver message
if ( is.good() )
{
is >> action >> ws;
}
else
{
error_str = "Error reading driver action.";
goto error_common;
}
if ( is.good() )
{
is >> result >> ws;
}
else
{
error_str = "Error reading action result.";
goto error_common;
}
// -------------------------------------------------------------------------
// Protocol implementation
// -------------------------------------------------------------------------
if ( action == "EXECUTE" )
{
std::ostringstream oss;
std::string info_b64;
std::string *info;
int hook_id;
int hook_rc;
// Parse the hook info
if ( is.good() )
{
is >> hook_id >> ws;
}
else
{
error_str = "Error reading hook id.";
goto error_common;
}
if ( is.good() )
{
is >> hook_rc >> ws;
}
else
{
error_str = "Error reading hook execution return code.";
goto error_common;
}
getline(is, info_b64);
if (result == "SUCCESS")
{
oss << "Success executing Hook " << hook_id;
NebulaLog::log("HKM",Log::INFO,oss);
}
else
{
oss << "Error executing Hook " << hook_id;
NebulaLog::log("HKM",Log::ERROR,oss);
}
info = one_util::base64_decode(info_b64);
if ( info != 0 )
{
hl->add(hook_id, hook_rc, *info);
delete info;
}
}
return;
error_common:
NebulaLog::log("HKM", Log::ERROR, error_str);
return;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void HookManagerDriver::recover()
{
NebulaLog::log("HKM", Log::ERROR, "Hook driver crashed, recovering...");
}

View File

@ -0,0 +1,99 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "HookManager.h"
#include "HookLog.h"
#include "Nebula.h"
#include "NebulaLog.h"
#include <sstream>
static void log_msg(hook_msg_t *msg)
{
ostringstream oss;
oss << "Message received: ";
msg->write_to(oss);
NebulaLog::log("HKM", Log::DEBUG, oss);
}
/* ************************************************************************** */
/* Driver Protocol Implementation */
/* ************************************************************************** */
void HookManager::_undefined(unique_ptr<hook_msg_t> msg)
{
NebulaLog::warn("HKM", "Received UNDEFINED msg: " + msg->payload());
}
/* -------------------------------------------------------------------------- */
void HookManager::_execute(unique_ptr<hook_msg_t> msg)
{
log_msg(msg.get());
int hook_id = msg->oid();
int hook_rc;
istringstream is(msg->payload());
is >> hook_rc >> ws;
if (is.bad())
{
NebulaLog::error("HKM", "Error reading hook execution return code.");
return;
}
ostringstream oss;
if (msg->status() == "SUCCESS")
{
oss << "Success executing Hook " << hook_id;
NebulaLog::log("HKM", Log::INFO, oss);
}
else
{
oss << "Error executing Hook " << hook_id;
NebulaLog::log("HKM", Log::ERROR, oss);
}
std::string info_b64;
getline(is, info_b64);
string* info = one_util::base64_decode(info_b64);
if (info != nullptr)
{
HookLog* hl = Nebula::instance().get_hl();
hl->add(hook_id, hook_rc, *info);
delete info;
}
}
/* -------------------------------------------------------------------------- */
void HookManager::_retry(unique_ptr<hook_msg_t> msg)
{
log_msg(msg.get());
}
/* -------------------------------------------------------------------------- */
void HookManager::_log(unique_ptr<hook_msg_t> msg)
{
NebulaLog::log("HKM", log_type(msg->status()[0]), msg->payload());
}

View File

@ -28,7 +28,7 @@ source_files=[
'HookStateVM.cc',
'HookStateHost.cc',
'HookManager.cc',
'HookManagerDriver.cc',
'HookManagerProtocol.cc',
'ExecuteHook.cc',
'HookLog.cc'
]

View File

@ -83,7 +83,7 @@ class HookManagerDriver < OpenNebulaDriver
end
end
def action_execute(*arguments)
def action_execute(_not_used, *arguments)
arg_xml = Nokogiri::XML(Base64.decode64(arguments.flatten[0]))
type = arg_xml.xpath('//HOOK_TYPE').text
@ -97,7 +97,7 @@ class HookManagerDriver < OpenNebulaDriver
end
end
def action_retry(*arguments)
def action_retry(_not_used, *arguments)
arguments.flatten!
command = arguments[0]

View File

@ -16,7 +16,6 @@
#include "InformationManager.h"
#include "HostPool.h"
#include "OpenNebulaMessages.h"
#include "VirtualMachinePool.h"
#include "Nebula.h"
#include "LifeCycleManager.h"
@ -30,16 +29,16 @@ int InformationManager::start()
using namespace std::placeholders; // for _1
register_action(OpenNebulaMessages::UNDEFINED,
register_action(InformationManagerMessages::UNDEFINED,
&InformationManager::_undefined);
register_action(OpenNebulaMessages::HOST_STATE,
register_action(InformationManagerMessages::HOST_STATE,
bind(&InformationManager::_host_state, this, _1));
register_action(OpenNebulaMessages::HOST_SYSTEM,
register_action(InformationManagerMessages::HOST_SYSTEM,
bind(&InformationManager::_host_system, this, _1));
register_action(OpenNebulaMessages::VM_STATE,
register_action(InformationManagerMessages::VM_STATE,
bind(&InformationManager::_vm_state, this, _1));
int rc = DriverManager::start(error);
@ -85,9 +84,9 @@ void InformationManager::stop_monitor(int hid, const string& name, const string&
data.add("IM_MAD", im_mad);
string tmp;
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::STOP_MONITOR);
msg.type(InformationManagerMessages::STOP_MONITOR);
msg.oid(hid);
msg.payload(data.to_xml(tmp));
@ -112,9 +111,9 @@ int InformationManager::start_monitor(Host * host, bool update_remotes)
return -1;
}
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::START_MONITOR);
msg.type(InformationManagerMessages::START_MONITOR);
msg.oid(host->get_oid());
msg.payload(to_string(update_remotes));
@ -136,9 +135,9 @@ void InformationManager::update_host(Host *host)
}
string tmp;
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::UPDATE_HOST);
msg.type(InformationManagerMessages::UPDATE_HOST);
msg.oid(host->get_oid());
msg.payload(host->to_xml(tmp));
@ -157,9 +156,9 @@ void InformationManager::delete_host(int hid)
return;
}
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::DEL_HOST);
msg.type(InformationManagerMessages::DEL_HOST);
msg.oid(hid);
imd->write(msg);
@ -186,17 +185,17 @@ void InformationManager::raft_status(RaftManager::State state)
hpool->dump(xml_hosts, "", 0, -1, false);
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::HOST_LIST);
msg.type(InformationManagerMessages::HOST_LIST);
msg.payload(xml_hosts);
imd->write(msg);
}
Message<OpenNebulaMessages> msg;
im_msg_t msg;
msg.type(OpenNebulaMessages::RAFT_STATUS);
msg.type(InformationManagerMessages::RAFT_STATUS);
msg.payload(RaftManager::state_to_str(state));
imd->write(msg);
@ -205,7 +204,7 @@ void InformationManager::raft_status(RaftManager::State state)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void InformationManager::_undefined(unique_ptr<Message<OpenNebulaMessages>> msg)
void InformationManager::_undefined(unique_ptr<im_msg_t> msg)
{
NebulaLog::warn("InM", "Received undefined message: " + msg->payload() +
"from host: " + to_string(msg->oid()));
@ -215,7 +214,7 @@ void InformationManager::_undefined(unique_ptr<Message<OpenNebulaMessages>> msg)
/* -------------------------------------------------------------------------- */
/* HOST_STATE - <state_str> <optional_desciption> */
void InformationManager::_host_state(unique_ptr<Message<OpenNebulaMessages>> msg)
void InformationManager::_host_state(unique_ptr<im_msg_t> msg)
{
NebulaLog::ddebug("InM", "HOST_STATE update from host: " +
to_string(msg->oid()) + ". Host information: " + msg->payload());
@ -281,7 +280,7 @@ void InformationManager::_host_state(unique_ptr<Message<OpenNebulaMessages>> msg
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void InformationManager::_host_system(unique_ptr<Message<OpenNebulaMessages>> msg)
void InformationManager::_host_system(unique_ptr<im_msg_t> msg)
{
NebulaLog::ddebug("InM", "HOST_SYSTEM update from host: " +
to_string(msg->oid()) + ". Host information: " + msg->payload());
@ -397,7 +396,7 @@ static LCMAction::Actions test_and_trigger(const string& state_str,
}
void InformationManager::_vm_state(unique_ptr<Message<OpenNebulaMessages>> msg)
void InformationManager::_vm_state(unique_ptr<im_msg_t> msg)
{
LifeCycleManager* lcm = Nebula::instance().get_lcm();

View File

@ -213,7 +213,7 @@ class InformationManagerDriver < OpenNebulaDriver
zline = Zlib::Deflate.deflate(line.strip, Zlib::BEST_COMPRESSION)
zline64 = Base64.strict_encode64(zline)
send_message('LOG', severity, id, zline64)
send_message('LOG', severity, id, "#{Time.now.to_i} #{zline64}")
end
end

View File

@ -40,7 +40,7 @@ Image::Image(int _uid,
const string& _gname,
int _umask,
ImageTemplate * _image_template):
PoolObjectSQL(-1,IMAGE,"",_uid,_gid,_uname,_gname,table),
PoolObjectSQL(-1,IMAGE,"",_uid,_gid,_uname,_gname,one_db::image_table),
type(OS),
disk_type(FILE),
regtime(time(0)),
@ -76,19 +76,6 @@ Image::Image(int _uid,
/* Image :: Database Access Functions */
/* ************************************************************************ */
const char * Image::table = "image_pool";
const char * Image::db_names =
"oid, name, body, uid, gid, owner_u, group_u, other_u";
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, "
"gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
"UNIQUE(name,uid) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::insert(SqlDB *db, string& error_str)
{
int rc;
@ -286,7 +273,7 @@ int Image::insert_replace(SqlDB *db, bool replace, string& error_str)
if (replace)
{
oss << "UPDATE " << table << " SET "
oss << "UPDATE " << one_db::image_table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
@ -298,7 +285,8 @@ int Image::insert_replace(SqlDB *db, bool replace, string& error_str)
}
else
{
oss << "INSERT INTO " << table << " (" << db_names << ") VALUES ("
oss << "INSERT INTO " << one_db::image_table
<< " (" << one_db::image_db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
@ -337,8 +325,18 @@ error_common:
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::bootstrap(SqlDB * db)
{
ostringstream oss_image(one_db::image_db_bootstrap);
return db->exec_local_wr(oss_image);
};
/* ************************************************************************ */
/* Image :: Misc */
/* Image :: Misc */
/* ************************************************************************ */
string& Image::to_xml(string& xml) const

View File

@ -48,21 +48,18 @@ extern "C" void * image_action_loop(void *arg)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImageManager::load_mads(int uid)
int ImageManager::load_drivers(const std::vector<const VectorAttribute*>& _mads)
{
ImageManagerDriver * imagem_mad;
ostringstream oss;
const VectorAttribute * vattr = 0;
int rc;
NebulaLog::log("ImM",Log::INFO,"Loading Image Manager driver.");
if ( mad_conf.size() > 0 )
if (_mads.size() > 0)
{
vattr = static_cast<const VectorAttribute *>(mad_conf[0]);
vattr = _mads[0];
}
if ( vattr == 0 )
if (vattr == nullptr)
{
NebulaLog::log("ImM",Log::INFO,"Failed to load Image Manager driver.");
return -1;
@ -70,21 +67,17 @@ int ImageManager::load_mads(int uid)
VectorAttribute image_conf("IMAGE_MAD",vattr->value());
image_conf.replace("NAME",image_driver_name);
image_conf.replace("NAME", image_driver_name);
imagem_mad= new ImageManagerDriver(0,image_conf.value(),false,ipool,dspool);
rc = add(imagem_mad);
if ( rc == 0 )
if (load_driver(&image_conf) != 0)
{
oss.str("");
oss << "\tImage Manager loaded";
NebulaLog::log("ImM",Log::INFO,oss);
NebulaLog::error("ImM", "Unable to load Image Manager driver");
return -1;
}
return rc;
NebulaLog::info("ImM", "\tImage Manager loaded");
return 0;
}
/* -------------------------------------------------------------------------- */
@ -95,17 +88,53 @@ int ImageManager::start()
int rc;
pthread_attr_t pattr;
rc = MadManager::start();
using namespace std::placeholders; // for _1
if ( rc != 0 )
register_action(ImageManagerMessages::UNDEFINED,
&ImageManager::_undefined);
register_action(ImageManagerMessages::STAT,
bind(&ImageManager::_stat, this, _1));
register_action(ImageManagerMessages::CP,
bind(&ImageManager::_cp, this, _1));
register_action(ImageManagerMessages::CLONE,
bind(&ImageManager::_clone, this, _1));
register_action(ImageManagerMessages::MKFS,
bind(&ImageManager::_mkfs, this, _1));
register_action(ImageManagerMessages::RM,
bind(&ImageManager::_rm, this, _1));
register_action(ImageManagerMessages::MONITOR,
bind(&ImageManager::_monitor, this, _1));
register_action(ImageManagerMessages::SNAP_DELETE,
bind(&ImageManager::_snap_delete, this, _1));
register_action(ImageManagerMessages::SNAP_REVERT,
bind(&ImageManager::_snap_revert, this, _1));
register_action(ImageManagerMessages::SNAP_FLATTEN,
bind(&ImageManager::_snap_flatten, this, _1));
register_action(ImageManagerMessages::LOG,
&ImageManager::_log);
NebulaLog::info("ImM", "Starting Image Manager...");
string error;
if (DriverManager::start(error) != 0)
{
NebulaLog::error("ImM", "Unable to start Image Manager driver: "
+ error);
return -1;
}
NebulaLog::log("ImM",Log::INFO,"Starting Image Manager...");
pthread_attr_init (&pattr);
pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_init(&pattr);
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
rc = pthread_create(&imagem_thread,&pattr,image_action_loop,(void *) this);
@ -139,7 +168,6 @@ void ImageManager::timer_action(const ActionRequest& ar)
int rc;
vector<int> datastores;
vector<int>::iterator it;
Nebula& nd = Nebula::instance();
DatastorePool * dspool = nd.get_dspool();
@ -157,9 +185,9 @@ void ImageManager::timer_action(const ActionRequest& ar)
return;
}
for(it = datastores.begin() ; it != datastores.end(); it++)
for (auto ds : datastores)
{
monitor_datastore(*it);
monitor_datastore(ds);
}
return;
@ -173,7 +201,6 @@ void ImageManager::monitor_datastore(int ds_id)
{
static map<int,int> monitor_vm_disk_counter;
string ds_data, ds_location, ds_name;
string* drv_msg;
bool shared;
@ -184,7 +211,7 @@ void ImageManager::monitor_datastore(int ds_id)
ostringstream oss;
const ImageManagerDriver* imd = get();
const auto* imd = get();
if ( imd == 0 )
{
@ -245,14 +272,13 @@ void ImageManager::monitor_datastore(int ds_id)
break;
}
drv_msg = ImageManager::format_message("", ds_data, ds_location);
unique_ptr<string> drv_msg(ImageManager::format_message("", ds_data, ds_location));
oss.str("");
oss << "Monitoring datastore " << ds_name << " (" << ds_id << ")";
NebulaLog::log("InM", Log::DEBUG, oss);
imd->monitor(ds_id, *drv_msg);
delete drv_msg;
image_msg_t msg(ImageManagerMessages::MONITOR, "", ds_id, *drv_msg);
imd->write(msg);
}

View File

@ -34,7 +34,7 @@ Image * ImageManager::acquire_image(int vm_id, int image_id, bool attach,
img = ipool->get(image_id);
if ( img == 0 )
if ( img == nullptr )
{
ostringstream oss;
oss << "Image with ID: " << image_id << " does not exist";
@ -48,7 +48,7 @@ Image * ImageManager::acquire_image(int vm_id, int image_id, bool attach,
if ( rc != 0 )
{
img->unlock();
img = 0;
img = nullptr;
}
return img;
@ -64,7 +64,7 @@ Image * ImageManager::acquire_image(int vm_id, const string& name, int uid,
img = ipool->get(name,uid);
if ( img == 0 )
if ( img == nullptr )
{
ostringstream oss;
oss << "User " << uid << " does not own an image with name: " << name
@ -79,7 +79,7 @@ Image * ImageManager::acquire_image(int vm_id, const string& name, int uid,
if ( rc != 0 )
{
img->unlock();
img = 0;
img = nullptr;
}
return img;
@ -209,7 +209,7 @@ void ImageManager::release_image(int vm_id, int iid, bool failed)
Image * img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
return;
}
@ -321,7 +321,7 @@ void ImageManager::release_cloning_resource(
{
Image * img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
return;
}
@ -383,7 +383,7 @@ int ImageManager::enable_image(int iid, bool to_enable, string& error_str)
img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
return -1;
}
@ -447,7 +447,6 @@ int ImageManager::delete_image(int iid, string& error_str)
string source;
string img_tmpl;
string * drv_msg;
string ds_data;
long long size;
@ -462,7 +461,7 @@ int ImageManager::delete_image(int iid, string& error_str)
img = ipool->get_ro(iid);
if ( img == 0 )
if ( img == nullptr )
{
error_str = "Image does not exist";
return -1;
@ -474,7 +473,7 @@ int ImageManager::delete_image(int iid, string& error_str)
ds = dspool->get_ro(ds_id);
if ( ds == 0 )
if ( ds == nullptr )
{
error_str = "Datastore no longer exists cannot remove image";
return -1;
@ -486,7 +485,7 @@ int ImageManager::delete_image(int iid, string& error_str)
img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
error_str = "Image does not exist";
return -1;
@ -543,9 +542,9 @@ int ImageManager::delete_image(int iid, string& error_str)
/* ------------------- Send RM operation request to the DS -------------- */
const ImageManagerDriver* imd = get();
const auto* imd = get();
if ( imd == 0 )
if ( imd == nullptr )
{
error_str = "Error getting ImageManagerDriver";
@ -553,7 +552,6 @@ int ImageManager::delete_image(int iid, string& error_str)
return -1;
}
drv_msg = format_message(img->to_xml(img_tmpl), ds_data, "");
source = img->get_source();
size = img->get_size();
ds_id = img->get_ds_id();
@ -575,7 +573,12 @@ int ImageManager::delete_image(int iid, string& error_str)
}
else
{
imd->rm(img->get_oid(), *drv_msg);
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, ""));
image_msg_t msg(ImageManagerMessages::RM, "", iid, *drv_msg);
imd->write(msg);
img->set_state(Image::DELETE);
img->clear_cloning_id();
@ -587,8 +590,6 @@ int ImageManager::delete_image(int iid, string& error_str)
img->unlock();
delete drv_msg;
/* -------------------- Update Group & User quota counters -------------- */
Template img_usage;
@ -607,7 +608,7 @@ int ImageManager::delete_image(int iid, string& error_str)
ds = dspool->get(ds_id);
if ( ds != 0 )
if ( ds != nullptr )
{
ds->del_image(iid);
dspool->update(ds);
@ -649,7 +650,7 @@ int ImageManager::can_clone_image(int cloning_id, ostringstream& oss_error)
img = ipool->get_ro(cloning_id);
if (img == 0)
if (img == nullptr)
{
oss_error << "Cannot clone image, it does not exist";
return -1;
@ -693,7 +694,7 @@ int ImageManager::set_clone_state(
int rc = 0;
Image * img = ipool->get(cloning_id);
if (img == 0)
if (img == nullptr)
{
error = "Cannot clone image, it does not exist";
return -1;
@ -749,16 +750,15 @@ int ImageManager::clone_image(int new_id,
const string& extra_data,
string& error)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
ostringstream oss;
Image * img;
string path;
string img_tmpl;
string* drv_msg;
if ( imd == 0 )
if ( imd == nullptr )
{
error = "Could not get datastore driver";
@ -773,7 +773,7 @@ int ImageManager::clone_image(int new_id,
img = ipool->get_ro(new_id);
if (img == 0)
if (img == nullptr)
{
release_cloning_image(cloning_id, new_id);
@ -781,9 +781,11 @@ int ImageManager::clone_image(int new_id,
return -1;
}
drv_msg = format_message(img->to_xml(img_tmpl), ds_data, extra_data);
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, extra_data));
imd->clone(img->get_oid(), *drv_msg);
image_msg_t msg(ImageManagerMessages::CLONE, "", img->get_oid(), *drv_msg);
imd->write(msg);
oss << "Cloning image " << img->get_path()
<<" to repository as image "<<img->get_oid();
@ -792,8 +794,6 @@ int ImageManager::clone_image(int new_id,
img->unlock();
delete drv_msg;
return 0;
}
@ -805,17 +805,16 @@ int ImageManager::register_image(int iid,
const string& extra_data,
string& error)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
ostringstream oss;
Image * img;
string path;
string img_tmpl;
string * drv_msg;
if ( imd == 0 )
if ( imd == nullptr )
{
error = "Could not get datastore driver";
NebulaLog::log("ImM",Log::ERROR, error);
@ -824,13 +823,13 @@ int ImageManager::register_image(int iid,
img = ipool->get(iid);
if (img == 0)
if (img == nullptr)
{
error = "Image deleted during copy operation";
return -1;
}
drv_msg = format_message(img->to_xml(img_tmpl), ds_data, extra_data);
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, extra_data));
path = img->get_path();
if ( path.empty() == true ) //NO PATH
@ -848,7 +847,8 @@ int ImageManager::register_image(int iid,
else if ( img->is_saving() || img->get_type() == Image::DATABLOCK
|| img->get_type() == Image::OS)
{
imd->mkfs(img->get_oid(), *drv_msg);
image_msg_t msg(ImageManagerMessages::MKFS, "", img->get_oid(), *drv_msg);
imd->write(msg);
oss << "Creating disk at " << source << " of "<< img->get_size()
<< "Mb (type: " << img->get_fstype() << ")";
@ -856,7 +856,9 @@ int ImageManager::register_image(int iid,
}
else //PATH -> COPY TO REPOSITORY AS SOURCE
{
imd->cp(img->get_oid(), *drv_msg);
image_msg_t msg(ImageManagerMessages::CP, "", img->get_oid(), *drv_msg);
imd->write(msg);
oss << "Copying " << path <<" to repository for image "<<img->get_oid();
}
@ -864,8 +866,6 @@ int ImageManager::register_image(int iid,
img->unlock();
delete drv_msg;
return 0;
}
/* -------------------------------------------------------------------------- */
@ -875,18 +875,15 @@ int ImageManager::stat_image(Template* img_tmpl,
const string& ds_data,
string& res)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
string* drv_msg;
string type_att;
ostringstream img_data;
SyncRequest sr;
int rc = 0;
if ( imd == 0 )
if ( imd == nullptr )
{
res = "Could not get datastore driver";
NebulaLog::log("ImM",Log::ERROR, res);
@ -975,16 +972,17 @@ int ImageManager::stat_image(Template* img_tmpl,
break;
}
SyncRequest sr;
add_request(&sr);
drv_msg = format_message(img_data.str(), ds_data, "");
unique_ptr<string> drv_msg(format_message(img_data.str(), ds_data, ""));
imd->stat(sr.id, *drv_msg);
image_msg_t msg(ImageManagerMessages::STAT, "", sr.id, *drv_msg);
imd->write(msg);
sr.wait();
delete drv_msg;
res = sr.message;
if ( sr.result != true )
@ -998,7 +996,7 @@ int ImageManager::stat_image(Template* img_tmpl,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string * ImageManager::format_message(
string* ImageManager::format_message(
const string& img_data,
const string& ds_data,
const string& extra_data)
@ -1021,7 +1019,7 @@ void ImageManager::set_image_snapshots(int iid, const Snapshots& s)
{
Image * img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
return;
}
@ -1077,7 +1075,7 @@ void ImageManager::set_image_size(int iid, long long size)
{
Image * img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
return;
}
@ -1114,9 +1112,9 @@ void ImageManager::set_image_size(int iid, long long size)
int ImageManager::delete_snapshot(int iid, int sid, string& error)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
if ( imd == 0 )
if ( imd == nullptr )
{
error = "Could not get datastore driver";
NebulaLog::log("ImM",Log::ERROR, error);
@ -1126,7 +1124,7 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
Image * img = ipool->get_ro(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1143,7 +1141,7 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
Datastore * ds = dspool->get_ro(ds_id);
if ( ds == 0 )
if ( ds == nullptr )
{
error = "Datastore no longer exists";
return -1;
@ -1160,7 +1158,7 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
/* ---------------------------------------------------------------------- */
img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1187,9 +1185,11 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
img->set_target_snapshot(sid);
string img_tmpl;
string * drv_msg = format_message(img->to_xml(img_tmpl), ds_data, "");
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, ""));
imd->snapshot_delete(iid, *drv_msg);
image_msg_t msg(ImageManagerMessages::SNAP_DELETE, "", iid, *drv_msg);
imd->write(msg);
img->set_state(Image::LOCKED);
@ -1197,8 +1197,6 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
img->unlock();
delete drv_msg;
return 0;
}
@ -1207,9 +1205,9 @@ int ImageManager::delete_snapshot(int iid, int sid, string& error)
int ImageManager::revert_snapshot(int iid, int sid, string& error)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
if ( imd == 0 )
if ( imd == nullptr )
{
error = "Could not get datastore driver";
NebulaLog::log("ImM",Log::ERROR, error);
@ -1219,7 +1217,7 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
Image * img = ipool->get_ro(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1236,7 +1234,7 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
Datastore * ds = dspool->get_ro(ds_id);
if ( ds == 0 )
if ( ds == nullptr )
{
error = "Datastore no longer exists";
return -1;
@ -1254,7 +1252,7 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
/* ---------------------------------------------------------------------- */
img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1291,9 +1289,11 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
img->set_target_snapshot(sid);
string img_tmpl;
string * drv_msg = format_message(img->to_xml(img_tmpl), ds_data, "");
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, ""));
imd->snapshot_revert(iid, *drv_msg);
image_msg_t msg(ImageManagerMessages::SNAP_REVERT, "", iid, *drv_msg);
imd->write(msg);
img->set_state(Image::LOCKED);
@ -1301,8 +1301,6 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
img->unlock();
delete drv_msg;
return 0;
}
@ -1311,9 +1309,9 @@ int ImageManager::revert_snapshot(int iid, int sid, string& error)
int ImageManager::flatten_snapshot(int iid, int sid, string& error)
{
const ImageManagerDriver* imd = get();
const auto* imd = get();
if ( imd == 0 )
if ( imd == nullptr )
{
error = "Could not get datastore driver";
NebulaLog::log("ImM",Log::ERROR, error);
@ -1323,7 +1321,7 @@ int ImageManager::flatten_snapshot(int iid, int sid, string& error)
Image * img = ipool->get_ro(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1340,7 +1338,7 @@ int ImageManager::flatten_snapshot(int iid, int sid, string& error)
Datastore * ds = dspool->get_ro(ds_id);
if ( ds == 0 )
if ( ds == nullptr )
{
error = "Datastore no longer exists";
return -1;
@ -1358,7 +1356,7 @@ int ImageManager::flatten_snapshot(int iid, int sid, string& error)
img = ipool->get(iid);
if ( img == 0 )
if ( img == nullptr )
{
error = "Image does not exist";
return -1;
@ -1388,9 +1386,11 @@ int ImageManager::flatten_snapshot(int iid, int sid, string& error)
img->set_target_snapshot(sid);
string img_tmpl;
string * drv_msg = format_message(img->to_xml(img_tmpl), ds_data, "");
unique_ptr<string> drv_msg(format_message(img->to_xml(img_tmpl), ds_data, ""));
imd->snapshot_flatten(iid, *drv_msg);
image_msg_t msg(ImageManagerMessages::SNAP_FLATTEN, "", iid, *drv_msg);
imd->write(msg);
img->set_state(Image::LOCKED);
@ -1398,8 +1398,6 @@ int ImageManager::flatten_snapshot(int iid, int sid, string& error)
img->unlock();
delete drv_msg;
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,737 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "ImageManager.h"
#include "ImagePool.h"
#include "DatastorePool.h"
#include "Quotas.h"
#include "TransferManager.h"
#include "VirtualMachine.h"
#include "VirtualMachinePool.h"
#include "Nebula.h"
/* ************************************************************************** */
/* Driver Protocol Implementation */
/* ************************************************************************** */
void ImageManager::_undefined(unique_ptr<image_msg_t> msg)
{
NebulaLog::warn("ImM", "Received UNDEFINED msg: " + msg->payload());
}
/* -------------------------------------------------------------------------- */
void ImageManager::_stat(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_stat: " + msg->payload());
if (msg->status() == "SUCCESS")
{
if (msg->payload().empty())
{
notify_request(msg->oid(), false, "Cannot get size from STAT");
}
notify_request(msg->oid(), true, msg->payload());
}
else
{
notify_request(msg->oid(), false, msg->payload());
}
}
/* -------------------------------------------------------------------------- */
void ImageManager::_cp(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_cp: " + msg->payload());
string source;
int ds_id = -1;
ostringstream oss;
istringstream is(msg->payload());
auto image = ipool->get(msg->oid());
if (image == nullptr)
{
if (msg->status() == "SUCCESS")
{
is >> source >> ws;
if (!source.empty())
{
oss << "CP operation succeeded but image no longer exists."
<< " Source image: " << source << ", may be left in datastore";
NebulaLog::log("ImM", Log::ERROR, oss);
}
}
return;
}
ds_id = image->get_ds_id();
if (msg->status() != "SUCCESS")
{
goto error;
}
is >> source >> ws;
if (is.fail())
{
goto error;
}
image->set_source(source);
image->set_state_unlock();
ipool->update(image);
image->unlock();
oss << "Image (" << msg->oid() << ") copied and ready to use.";
NebulaLog::log("ImM", Log::INFO, oss);
monitor_datastore(ds_id);
return;
error:
oss << "Error copying image in the datastore";
const auto& info = msg->payload();
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();
monitor_datastore(ds_id);
return;
}
/* -------------------------------------------------------------------------- */
void ImageManager::_clone(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_clone: " + msg->payload());
int cloning_id;
int ds_id = -1;
Image * image = ipool->get(msg->oid());
if (image == nullptr)
{
if (msg->status() == "SUCCESS")
{
ostringstream oss;
if (!msg->payload().empty())
{
oss << "CLONE operation succeeded but image no longer exists."
<< " Source image: " << msg->payload()
<< ", may be left in datastore";
NebulaLog::log("ImM", Log::ERROR, oss);
}
}
return;
}
ds_id = image->get_ds_id();
cloning_id = image->get_cloning_id();
if (msg->status() != "SUCCESS")
{
goto error;
}
image->set_source(msg->payload());
image->set_state_unlock();
image->clear_cloning_id();
ipool->update(image);
image->unlock();
NebulaLog::info("ImM", "Image cloned and ready to use.");
release_cloning_image(cloning_id, msg->oid());
monitor_datastore(ds_id);
return;
error:
ostringstream oss;
oss << "Error cloning from Image " << cloning_id;
const auto& info = msg->payload();
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);
image->clear_cloning_id();
ipool->update(image);
image->unlock();
release_cloning_image(cloning_id, msg->oid());
}
/* -------------------------------------------------------------------------- */
void ImageManager::_mkfs(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_mkfs: " + msg->payload());
int vm_id = -1;
int disk_id = -1;
VirtualMachine * vm = nullptr;
ostringstream oss;
Nebula& nd = Nebula::instance();
VirtualMachinePool * vmpool = nd.get_vmpool();
TransferManager * tm = nd.get_tm();
Image * image = ipool->get(msg->oid());
const string& source = msg->payload();
if (image == nullptr)
{
if (msg->status() == "SUCCESS")
{
ostringstream oss;
if (!source.empty())
{
oss << "MkFS operation succeeded but image no longer exists."
<< " Source image: " << source << ", may be left in datastore";
NebulaLog::log("ImM", Log::ERROR, oss);
}
}
return;
}
else if (image->get_state() == Image::DELETE)
{
NebulaLog::info("ImM", "Ignoring mkfs callback, image is "
"being deleted");
image->unlock();
return;
}
bool is_saving = image->is_saving();
int ds_id = image->get_ds_id();
if (is_saving)
{
image->get_template_attribute("SAVED_DISK_ID", disk_id);
image->get_template_attribute("SAVED_VM_ID", vm_id);
}
if (msg->status() != "SUCCESS")
{
goto error_img;
}
image->set_source(source);
if (!is_saving)
{
image->set_state_unlock();
}
ipool->update(image);
image->unlock();
if (!is_saving)
{
NebulaLog::info("ImM", "Image created and ready to use");
monitor_datastore(ds_id);
return;
}
vm = vmpool->get(vm_id);
if (vm == nullptr)
{
goto error_save_get;
}
if (vm->set_saveas_disk(disk_id, source, msg->oid()) == -1)
{
goto error_save_state;
}
tm->trigger(TMAction::SAVEAS_HOT, vm_id);
vmpool->update(vm);
vm->unlock();
monitor_datastore(ds_id);
return;
error_img:
oss << "Error creating datablock";
goto error;
error_save_get:
oss << "Image created to save as a disk but VM does not exist.";
goto error_save;
error_save_state:
vm->unlock();
oss << "Image created to save as disk but VM is no longer running";
error_save:
image = ipool->get(msg->oid());
if (image == nullptr)
{
NebulaLog::log("ImM", Log::ERROR, oss);
return;
}
error:
const auto& info = msg->payload();
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();
if (is_saving && vm_id != -1)
{
if ((vm = vmpool->get(vm_id)) != nullptr)
{
vm->clear_saveas_state();
vm->clear_saveas_disk();
vmpool->update(vm);
vm->unlock();
}
}
monitor_datastore(ds_id);
return;
}
/* -------------------------------------------------------------------------- */
void ImageManager::_rm(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_rm: " + msg->payload());
int ds_id = -1;
string tmp_error;
Image * image;
ostringstream oss;
image = ipool->get(msg->oid());
if (image == nullptr)
{
return;
}
ds_id = image->get_ds_id();
const auto& source = image->get_source();
int rc = ipool->drop(image, tmp_error);
image->unlock();
if (msg->status() != "SUCCESS")
{
goto error;
}
else if (rc < 0)
{
goto error_drop;
}
NebulaLog::info("ImM", "Image successfully removed.");
monitor_datastore(ds_id);
return;
error_drop:
oss << "Error removing image from DB: " << tmp_error
<< ". Remove image source " << source << " to completely delete image.";
NebulaLog::log("ImM", Log::ERROR, oss);
return;
error:
oss << "Error removing image from datastore. Manually remove image source "
<< source << " to completely delete the image";
const auto& info = msg->payload();
if (!info.empty() && (info[0] != '-'))
{
oss << ": " << info;
}
NebulaLog::log("ImM", Log::ERROR, oss);
return;
}
/* -------------------------------------------------------------------------- */
void ImageManager::_monitor(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_monitor: " + msg->payload());
ostringstream oss;
string dsinfo64 = msg->payload();
if (dsinfo64.empty())
{
oss << "Error monitoring datastore " << msg->oid()
<< ". Bad monitor data: " << dsinfo64;
NebulaLog::log("ImM", Log::ERROR, oss);
return;
}
unique_ptr<string> dsinfo(one_util::base64_decode(dsinfo64));
if (dsinfo == nullptr)
{
oss << "Error monitoring datastore " << msg->oid()
<< ". Bad monitor data: " << dsinfo64;
NebulaLog::log("ImM", Log::ERROR, oss);
return;
}
if (msg->status() != "SUCCESS")
{
oss << "Error monitoring datastore " << msg->oid() << ": " << dsinfo64;
if (!(*dsinfo).empty())
{
oss << ". Decoded info: " << *dsinfo;
}
NebulaLog::log("ImM", Log::ERROR, oss);
return;
}
Template monitor_data;
char* error_msg;
int rc = monitor_data.parse(*dsinfo, &error_msg);
if (rc != 0)
{
oss << "Error parsing datastore information: " << error_msg
<< ". Monitoring information: " << endl << *dsinfo;
NebulaLog::log("ImM", Log::ERROR, oss);
free(error_msg);
return;
}
float total, free, used;
string ds_name;
monitor_data.get("TOTAL_MB", total);
monitor_data.get("FREE_MB", free);
monitor_data.get("USED_MB", used);
Datastore * ds = dspool->get(msg->oid());
if (ds == nullptr)
{
return;
}
ds_name = ds->get_name();
ds->update_monitor(total, free, used);
dspool->update(ds);
ds->unlock();
oss << "Datastore " << ds_name << " (" << msg->oid()
<< ") successfully monitored.";
NebulaLog::debug("ImM", oss.str());
return;
}
/* -------------------------------------------------------------------------- */
void ImageManager::_snap_delete(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_snap_delete: " + msg->payload());
long long snap_size;
int ds_id, uid, gid;
Image * image = ipool->get(msg->oid());
if (image == nullptr)
{
return;
}
image->set_state_unlock();
int snap_id = image->get_target_snapshot();
if (snap_id == -1)
{
NebulaLog::error("ImM", "No target snapshot in callback");
ipool->update(image);
image->unlock();
return;
}
if (msg->status() == "SUCCESS")
{
ds_id = image->get_ds_id();
uid = image->get_uid();
gid = image->get_gid();
snap_size = (image->get_snapshots()).get_snapshot_size(snap_id);
image->delete_snapshot(snap_id);
}
else
{
ostringstream oss;
oss << "Error removing snapshot " << snap_id
<< " from image " << msg->oid();
const auto& info = msg->payload();
if (!info.empty() && (info[0] != '-'))
{
oss << ": " << info;
}
image->set_template_error_message(oss.str());
NebulaLog::log("ImM", Log::ERROR, oss);
}
image->clear_target_snapshot();
ipool->update(image);
image->unlock();
if (msg->status() == "SUCCESS")
{
Template quotas;
quotas.add("DATASTORE", ds_id);
quotas.add("SIZE", snap_size);
quotas.add("IMAGES", 0);
Quotas::ds_del(uid, gid, &quotas);
}
}
/* -------------------------------------------------------------------------- */
void ImageManager::_snap_revert(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_snap_revert: " + msg->payload());
Image * image = ipool->get(msg->oid());
if (image == nullptr)
{
return;
}
int snap_id = image->get_target_snapshot();
image->set_state_unlock();
if (snap_id == -1)
{
NebulaLog::error("ImM", "No target snapshot in callback");
ipool->update(image);
image->unlock();
return;
}
if (msg->status() == "SUCCESS")
{
image->revert_snapshot(snap_id);
}
else
{
ostringstream oss;
oss << "Error reverting image " << msg->oid()
<< " to snapshot " << snap_id;
const auto& info = msg->payload();
if (!info.empty() && (info[0] != '-'))
{
oss << ": " << info;
}
image->set_template_error_message(oss.str());
NebulaLog::log("ImM", Log::ERROR, oss);
}
image->clear_target_snapshot();
ipool->update(image);
image->unlock();
}
/* -------------------------------------------------------------------------- */
void ImageManager::_snap_flatten(unique_ptr<image_msg_t> msg)
{
NebulaLog::dddebug("ImM", "_snap_flatten: " + msg->payload());
long long snap_size;
int ds_id, uid, gid;
Image * image = ipool->get(msg->oid());
if (image == nullptr)
{
return;
}
if (msg->status() == "SUCCESS")
{
ds_id = image->get_ds_id();
uid = image->get_uid();
gid = image->get_gid();
snap_size = (image->get_snapshots()).get_total_size();
image->clear_snapshots();
}
else
{
ostringstream oss;
oss << "Error flattening image snapshot";
const auto& info = msg->payload();
if (!info.empty() && (info[0] != '-'))
{
oss << ": " << info;
}
image->set_template_error_message(oss.str());
NebulaLog::log("ImM", Log::ERROR, oss);
}
image->set_state_unlock();
image->clear_target_snapshot();
ipool->update(image);
image->unlock();
if (msg->status() == "SUCCESS")
{
Template quotas;
quotas.add("DATASTORE", ds_id);
quotas.add("SIZE", snap_size);
quotas.add("IMAGES", 0);
Quotas::ds_del(uid, gid, &quotas);
}
}
/* -------------------------------------------------------------------------- */
void ImageManager::_log(unique_ptr<image_msg_t> msg)
{
NebulaLog::log("ImM", log_type(msg->status()[0]), msg->payload());
}

View File

@ -43,7 +43,7 @@ ImagePool::ImagePool(
const string& __default_cdrom_dev_prefix,
vector<const SingleAttribute *>& restricted_attrs,
const vector<const SingleAttribute *>& _inherit_attrs)
:PoolSQL(db, Image::table)
: PoolSQL(db, one_db::image_table)
{
// Init static defaults
_default_type = __default_type;

View File

@ -24,7 +24,7 @@ lib_name='nebula_image'
source_files=[
'Image.cc',
'ImagePool.cc',
'ImageManagerDriver.cc',
'ImageManagerProtocol.cc',
'ImageManager.cc',
'ImageManagerActions.cc',
'ImageTemplate.cc'

View File

@ -29,7 +29,7 @@ extern "C" void * ipamm_action_loop(void *arg)
{
IPAMManager * ipamm;
if ( arg == 0 )
if ( arg == nullptr )
{
return 0;
}
@ -49,13 +49,35 @@ extern "C" void * ipamm_action_loop(void *arg)
int IPAMManager::start()
{
int rc;
pthread_attr_t pattr;
rc = MadManager::start();
using namespace std::placeholders; // for _1
if ( rc != 0 )
register_action(IPAMManagerMessages::UNDEFINED,
&IPAMManager::_undefined);
register_action(IPAMManagerMessages::REGISTER_ADDRESS_RANGE,
bind(&IPAMManager::_notify_request, this, _1));
register_action(IPAMManagerMessages::UNREGISTER_ADDRESS_RANGE,
bind(&IPAMManager::_notify_request, this, _1));
register_action(IPAMManagerMessages::GET_ADDRESS,
bind(&IPAMManager::_notify_request, this, _1));
register_action(IPAMManagerMessages::ALLOCATE_ADDRESS,
bind(&IPAMManager::_notify_request, this, _1));
register_action(IPAMManagerMessages::FREE_ADDRESS,
bind(&IPAMManager::_notify_request, this, _1));
register_action(IPAMManagerMessages::LOG,
&IPAMManager::_log);
string error;
if ( DriverManager::start(error) != 0 )
{
NebulaLog::error("IPM", error);
return -1;
}
@ -64,7 +86,7 @@ int IPAMManager::start()
pthread_attr_init(&pattr);
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
rc = pthread_create(&ipamm_thread, &pattr, ipamm_action_loop, (void *)this);
int rc = pthread_create(&ipamm_thread, &pattr, ipamm_action_loop, (void *)this);
return rc;
}
@ -78,7 +100,7 @@ void IPAMManager::user_action(const ActionRequest& ar)
IPAMRequest * request = ipam_ar.request();
if ( request == 0 )
if ( request == nullptr )
{
return;
}
@ -110,120 +132,79 @@ void IPAMManager::user_action(const ActionRequest& ar)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const IPAMManagerDriver * IPAMManager::setup_request(IPAMRequest * ir)
void IPAMManager::send_request(IPAMManagerMessages type, IPAMRequest * ir)
{
const IPAMManagerDriver * ipammd = get();
auto ipammd = get();
if (ipammd == 0)
if (ipammd == nullptr)
{
ir->result = false;
ir->message = "Could not find the IPAM driver";
ir->notify();
return 0;
return;
}
add_request(ir);
return ipammd;
string action_data;
ipam_msg_t msg(type, "", ir->id, ir->to_xml64(action_data));
ipammd->write(msg);
}
/* -------------------------------------------------------------------------- */
void IPAMManager::register_address_range_action(IPAMRequest * ir)
{
std::string action_data;
const IPAMManagerDriver * ipammd = setup_request(ir);
if (ipammd == 0)
{
return;
}
ipammd->register_address_range(ir->id, ir->to_xml64(action_data));
send_request(IPAMManagerMessages::REGISTER_ADDRESS_RANGE, ir);
}
/* -------------------------------------------------------------------------- */
void IPAMManager::unregister_address_range_action(IPAMRequest * ir)
{
std::string action_data;
const IPAMManagerDriver * ipammd = setup_request(ir);
if (ipammd == 0)
{
return;
}
ipammd->unregister_address_range(ir->id, ir->to_xml64(action_data));
send_request(IPAMManagerMessages::UNREGISTER_ADDRESS_RANGE, ir);
}
/* -------------------------------------------------------------------------- */
void IPAMManager::get_address_action(IPAMRequest * ir)
{
std::string action_data;
const IPAMManagerDriver * ipammd = setup_request(ir);
if (ipammd == 0)
{
return;
}
ipammd->get_address(ir->id, ir->to_xml64(action_data));
send_request(IPAMManagerMessages::GET_ADDRESS, ir);
}
/* -------------------------------------------------------------------------- */
void IPAMManager::allocate_address_action(IPAMRequest * ir)
{
std::string action_data;
const IPAMManagerDriver * ipammd = setup_request(ir);
if (ipammd == 0)
{
return;
}
ipammd->allocate_address(ir->id, ir->to_xml64(action_data));
send_request(IPAMManagerMessages::ALLOCATE_ADDRESS, ir);
}
/* -------------------------------------------------------------------------- */
void IPAMManager::free_address_action(IPAMRequest * ir)
{
std::string action_data;
const IPAMManagerDriver * ipammd = setup_request(ir);
if (ipammd == 0)
{
return;
}
ipammd->free_address(ir->id, ir->to_xml64(action_data));
send_request(IPAMManagerMessages::FREE_ADDRESS, ir);
}
/* ************************************************************************** */
/* MAD Loading */
/* ************************************************************************** */
int IPAMManager::load_mads(int uid)
int IPAMManager::load_drivers(const std::vector<const VectorAttribute*>& _mads)
{
const VectorAttribute * vattr;
IPAMManagerDriver * ipamm_driver;
if ( mad_conf.size() == 0 )
if ( _mads.size() == 0 )
{
return 0;
}
NebulaLog::log("IPM", Log::INFO, "Loading IPAM Manager driver.");
vattr = dynamic_cast<const VectorAttribute *>(mad_conf[0]);
const VectorAttribute * vattr = _mads[0];
if ( vattr == 0 )
if ( vattr == nullptr )
{
NebulaLog::log("IPM", Log::ERROR, "Failed to load IPAM Manager driver.");
return -1;
@ -233,10 +214,9 @@ int IPAMManager::load_mads(int uid)
ipam_conf.replace("NAME", ipam_driver_name);
ipamm_driver = new IPAMManagerDriver(uid, ipam_conf.value(), false, this);
if ( add(ipamm_driver) != 0 )
if ( load_driver(&ipam_conf) != 0 )
{
NebulaLog::error("ImM", "Unable to load IPAM Manager driver");
return -1;
}

View File

@ -14,84 +14,46 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "IPAMManagerDriver.h"
#include "IPAMManager.h"
#include "NebulaLog.h"
#include <sstream>
/* ************************************************************************** */
/* MAD Interface */
/* Driver Protocol Implementation */
/* ************************************************************************** */
void IPAMManagerDriver::protocol(const string& message) const
void IPAMManager::_undefined(unique_ptr<ipam_msg_t> msg)
{
NebulaLog::warn("IPM", "Received UNDEFINED msg: " + msg->payload());
}
/* -------------------------------------------------------------------------- */
void IPAMManager::_notify_request(unique_ptr<ipam_msg_t> msg)
{
istringstream is(message);
ostringstream os;
string action;
string result;
string info="";
os << "Message received: ";
msg->write_to(os);
int id;
os << "Message received: " << message;
NebulaLog::log("IPM", Log::DEBUG, os);
// Parse the driver message
if ( is.good() )
is >> action >> ws;
else
return;
if ( is.good() )
is >> result >> ws;
else
return;
if ( is.good() )
if (msg->status() == "SUCCESS")
{
is >> id >> ws;
if ( is.fail() )
{
if ( action == "LOG" )
{
is.clear();
getline(is,info);
NebulaLog::log("IPM", log_type(result[0]), info.c_str());
}
return;
}
}
else
return;
getline(is, info);
if (action == "LOG")
{
NebulaLog::log("IPM", Log::INFO, info.c_str());
}
else if (result == "SUCCESS")
{
ipamm->notify_request(id, true, info);
notify_request(msg->oid(), true, msg->payload());
}
else
{
ipamm->notify_request(id, false, info);
notify_request(msg->oid(), false, msg->payload());
}
return;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void IPAMManagerDriver::recover()
void IPAMManager::_log(unique_ptr<ipam_msg_t> msg)
{
NebulaLog::log("IPM",Log::INFO,"Recovering IPAM drivers");
NebulaLog::log("IPM", log_type(msg->status()[0]), msg->payload());
}

View File

@ -23,7 +23,7 @@ lib_name='nebula_ipamm'
# Sources to generate the library
source_files=[
'IPAMManager.cc',
'IPAMManagerDriver.cc',
'IPAMManagerProtocol.cc',
'IPAMRequest.cc'
]

View File

@ -1,323 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "Mad.h"
#include "NebulaLog.h"
#include "Nebula.h"
#include <sstream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <cerrno>
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
Mad::~Mad()
{
char buf[] = "FINALIZE\n";
bool success = false;
if ( pid==-1)
{
return;
}
// Finish the driver
::write(nebula_mad_pipe, buf, strlen(buf));
close(mad_nebula_pipe);
close(nebula_mad_pipe);
for (int i=0; i < 2; ++i)
{
int status;
if ( waitpid(pid, &status, WNOHANG) != 0 )
{
success = true;
break;
}
sleep(1);
}
if (!success)
{
kill(pid, SIGKILL);
};
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Mad::start()
{
int ne_mad_pipe[2];
int mad_ne_pipe[2];
map<string,string>::iterator it;
const char * owner = 0;
const char * executable = 0;
const char * arguments = 0;
string exec_path;
char buf[]="INIT\n";
char c;
stringbuf sbuf;
iostream mstream(&sbuf);
int rc;
string action;
string result;
string info;
ostringstream oss;
// Open communication pipes
if (pipe(ne_mad_pipe) == -1 ||
pipe(mad_ne_pipe) == -1)
{
goto error_pipes;
}
// Get the attributes for this driver
it = attributes.find("OWNER");
if ( it != attributes.end() )
{
owner = it->second.c_str();
}
it = attributes.find("EXECUTABLE");
if ( it != attributes.end() )
{
if ( it->second.empty() == false )
{
if (it->second[0] != '/') //Look in ONE_LOCATION/lib/mads or in "/usr/lib/one/mads"
{
Nebula& nd = Nebula::instance();
exec_path = nd.get_mad_location() + it->second;
executable= exec_path.c_str();
}
else //Absolute Path
{
executable = it->second.c_str();
}
}
}
it = attributes.find("ARGUMENTS");
if ( it != attributes.end() )
{
arguments = it->second.c_str();
}
if ( (sudo_execution == true && owner == 0) || executable == 0 )
{
goto error_attributes;
}
//Create a new process for the driver
pid = fork();
switch (pid)
{
case -1: // Error
goto error_fork;
case 0: // Child process (MAD)
close(ne_mad_pipe[1]);
close(mad_ne_pipe[0]);
if (dup2(ne_mad_pipe[0], 0) != 0 || // stdin and stdout redirection
dup2(mad_ne_pipe[1], 1) != 1)
{
goto error_dup2;
}
close(ne_mad_pipe[0]);
close(mad_ne_pipe[1]);
close(2);
if ( sudo_execution == true )
{
rc = execlp("sudo","sudo","-H","-u",owner,executable,arguments,
(char *) NULL);
}
else
{
rc = execlp(executable,executable,arguments,(char*)NULL);
}
goto error_exec;
default: // Parent process (SE)
fd_set rfds;
struct timeval tv;
close(ne_mad_pipe[0]);
close(mad_ne_pipe[1]);
nebula_mad_pipe = ne_mad_pipe[1];
mad_nebula_pipe = mad_ne_pipe[0];
// Close pipes in other MADs
fcntl(nebula_mad_pipe, F_SETFD, FD_CLOEXEC);
fcntl(mad_nebula_pipe, F_SETFD, FD_CLOEXEC);
::write(nebula_mad_pipe, buf, strlen(buf));
do
{
FD_ZERO(&rfds);
FD_SET(mad_nebula_pipe, &rfds);
// Wait up to 30 seconds
tv.tv_sec = 30;
tv.tv_usec = 0;
rc = select(mad_nebula_pipe+1,&rfds,0,0, &tv);
if ( rc <= 0 ) // MAD did not answered
{
goto error_mad_init;
}
rc = read(mad_nebula_pipe, (void *) &c, sizeof(char));
mstream.put(c);
}
while ( rc > 0 && c != '\n');
if ( rc <= 0 )
{
goto error_mad_init;
}
mstream >> action >> result >> ws;
getline(mstream,info);
if (action == "INIT")
{
if (result == "FAILURE")
{
goto error_mad_result;
}
}
else
{
goto error_mad_action;
}
break;
}
return 0;
error_exec:
oss.str("");
oss << "Cannot load driver " << executable << ", " << strerror(errno);
NebulaLog::log("MAD", Log::ERROR, oss);
exit(-1);
error_dup2:
oss.str("");
oss << "Cannot duplicate descriptors, " << strerror(errno);
NebulaLog::log("MAD", Log::ERROR, oss);
exit(-1);
error_mad_result:
oss.str("");
oss << "MAD initialization failed, " << info;
NebulaLog::log("MAD", Log::ERROR, oss);
return -1;
error_mad_action:
NebulaLog::log("MAD", Log::ERROR,"Wrong action in MAD response");
return -1;
error_mad_init:
NebulaLog::log("MAD", Log::ERROR, "MAD did not answer INIT command");
return -1;
error_fork:
oss.str("");
oss << "Error forking to start MAD, " << strerror(errno);
NebulaLog::log("MAD", Log::ERROR, oss);
return -1;
error_attributes:
NebulaLog::log("MAD", Log::ERROR, "Wrong attributes for the driver");
return -1;
error_pipes:
oss.str("");
oss << "Cannot create driver pipes, " << strerror(errno);
NebulaLog::log("MAD", Log::ERROR, oss);
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Mad::reload()
{
char buf[]="FINALIZE\n";
int status;
int rc;
pid_t rp;
// Finish the driver
::write(nebula_mad_pipe, buf, strlen(buf));
close(nebula_mad_pipe);
close(mad_nebula_pipe);
rp = waitpid(pid, &status, WNOHANG);
if ( rp == 0 )
{
sleep(1);
waitpid(pid, &status, WNOHANG);
}
// Start the MAD again
rc = start();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

Some files were not shown because too many files have changed in this diff Show More