diff --git a/include/Nebula.h b/include/Nebula.h index 77790f436c..0d7ab522b8 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -17,7 +17,7 @@ #ifndef NEBULA_H_ #define NEBULA_H_ -#include +#include "SqlDB.h" #include "Log.h" #include "NebulaTemplate.h" @@ -38,29 +38,29 @@ class Nebula { public: - - static Nebula& instance() + + static Nebula& instance() { static Nebula nebulad; - + return nebulad; }; - + // --------------------------------------------------------------- // Logging // --------------------------------------------------------------- - + static void log( const char * module, const Log::MessageType type, const ostringstream& message, const char * filename = 0, Log::MessageType clevel = Log::ERROR) - { + { static Log nebula_log(filename,clevel,ios_base::trunc); static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&log_mutex); + pthread_mutex_lock(&log_mutex); nebula_log.log(module,type,message); pthread_mutex_unlock(&log_mutex); }; @@ -78,23 +78,23 @@ public: // -------------------------------------------------------------- // Pool Accessors - // -------------------------------------------------------------- - + // -------------------------------------------------------------- + VirtualMachinePool * get_vmpool() { return vmpool; - }; + }; HostPool * get_hpool() { return hpool; - }; - + }; + VirtualNetworkPool * get_vnpool() { return vnpool; }; - + UserPool * get_upool() { return upool; @@ -102,8 +102,8 @@ public: // -------------------------------------------------------------- // Manager Accessors - // -------------------------------------------------------------- - + // -------------------------------------------------------------- + VirtualMachineManager * get_vmm() { return vmm; @@ -113,7 +113,7 @@ public: { return lcm; }; - + InformationManager * get_im() { return im; @@ -128,29 +128,29 @@ public: { return dm; }; - + HookManager * get_hm() { return hm; }; - + // -------------------------------------------------------------- // Environment & Configuration // -------------------------------------------------------------- - + /** - * Returns the value of ONE_LOCATION env variable. When this variable is + * Returns the value of ONE_LOCATION env variable. When this variable is * not defined the nebula location is "/". * @return the nebula location. - */ + */ const string& get_nebula_location() { return nebula_location; }; /** - * Returns the path where mad executables are stored, if ONE_LOCATION is - * defined this path points to $ONE_LOCATION/bin, otherwise it is + * Returns the path where mad executables are stored, if ONE_LOCATION is + * defined this path points to $ONE_LOCATION/bin, otherwise it is * /usr/lib/one/mads. * @return the mad execs location. */ @@ -160,7 +160,7 @@ public: }; /** - * Returns the path where defaults for mads are stored, if ONE_LOCATION is + * Returns the path where defaults for mads are stored, if ONE_LOCATION is * defined this path points to $ONE_LOCATION/etc, otherwise it is /etc/one * @return the mad defaults location. */ @@ -168,10 +168,10 @@ public: { return etc_location; }; - + /** * Returns the path where logs (oned.log, schedd.log,...) are generated - * if ONE_LOCATION is defined this path points to $ONE_LOCATION/var, + * if ONE_LOCATION is defined this path points to $ONE_LOCATION/var, * otherwise it is /var/log/one. * @return the log location. */ @@ -181,8 +181,8 @@ public: }; /** - * Returns the path where the OpenNebula DB and the VM local directories - * are stored. When ONE_LOCATION is defined this path points to + * Returns the path where the OpenNebula DB and the VM local directories + * are stored. When ONE_LOCATION is defined this path points to * $ONE_LOCATION/var, otherwise it is /var/lib/one. * @return the log location. */ @@ -190,7 +190,7 @@ public: { return var_location; }; - + /** * Returns the path of the log file for a VM, depending where OpenNebula is * installed, @@ -202,46 +202,46 @@ public: string get_vm_log_filename(int oid) { ostringstream oss; - + if (nebula_location == "/") { - oss << log_location << oid << ".log"; + oss << log_location << oid << ".log"; } else { - oss << nebula_location << "var/" << oid << "/vm.log"; + oss << nebula_location << "var/" << oid << "/vm.log"; } - + return oss.str(); }; - + const string& get_nebula_hostname() { return hostname; }; - + static string version() { - return "OpenNebula 1.5.0"; + return "OpenNebula 1.5.0"; }; - + void start(); - + void get_configuration_attribute( - const char * name, + const char * name, string& value) const { string _name(name); - - nebula_configuration->Template::get(_name,value); + + nebula_configuration->Template::get(_name,value); }; - + private: - - // ----------------------------------------------------------------------- + + // ----------------------------------------------------------------------- //Constructors and = are private to only access the class through instance // ----------------------------------------------------------------------- - + Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0),upool(0), lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0) { @@ -250,7 +250,7 @@ private: if (nl == 0) //OpenNebula installed under root directory { nebula_location = "/"; - + mad_location = "/usr/lib/one/mads/"; etc_location = "/etc/one/"; log_location = "/var/log/one/"; @@ -259,19 +259,19 @@ private: else { nebula_location = nl; - + if ( nebula_location.at(nebula_location.size()-1) != '/' ) { nebula_location += "/"; } - - mad_location = nebula_location + "lib/mads/"; - etc_location = nebula_location + "etc/"; + + mad_location = nebula_location + "lib/mads/"; + etc_location = nebula_location + "etc/"; log_location = nebula_location + "var/"; - var_location = nebula_location + "var/"; - } + var_location = nebula_location + "var/"; + } }; - + ~Nebula() { if ( vmpool != 0) @@ -303,7 +303,7 @@ private: { delete lcm; } - + if ( im != 0) { delete im; @@ -313,69 +313,69 @@ private: { delete tm; } - + if ( dm != 0) { delete dm; } - + if ( rm != 0) { delete rm; } - + if ( hm != 0) { delete hm; } - + if ( nebula_configuration != 0) { delete nebula_configuration; } - + if ( db != 0 ) { delete db; } }; - + Nebula(Nebula const&){}; - - Nebula& operator=(Nebula const&){return *this;}; - + + Nebula& operator=(Nebula const&){return *this;}; + // --------------------------------------------------------------- // Environment variables // --------------------------------------------------------------- - + string nebula_location; - + string mad_location; string etc_location; string log_location; string var_location; string hostname; - + // --------------------------------------------------------------- // Configuration // --------------------------------------------------------------- NebulaTemplate * nebula_configuration; - + // --------------------------------------------------------------- // Nebula Pools // --------------------------------------------------------------- - - SqliteDB * db; + + SqlDB * db; VirtualMachinePool * vmpool; HostPool * hpool; VirtualNetworkPool * vnpool; UserPool * upool; - + // --------------------------------------------------------------- // Nebula Managers // --------------------------------------------------------------- - + LifeCycleManager * lcm; VirtualMachineManager * vmm; InformationManager * im; @@ -383,11 +383,11 @@ private: DispatchManager * dm; RequestManager * rm; HookManager * hm; - + // --------------------------------------------------------------- // Implementation functions // --------------------------------------------------------------- - + friend void nebula_signal_handler (int sig); }; diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 491401c662..eb29848734 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -16,6 +16,7 @@ #include "Nebula.h" #include "VirtualMachine.h" +#include "SqliteDB.h" #include #include @@ -39,31 +40,31 @@ void Nebula::start() sigset_t mask; int signal; char hn[80]; - + if ( gethostname(hn,79) != 0 ) { throw runtime_error("Error getting hostname"); } - + hostname = hn; - // ----------------------------------------------------------- - // Configuration - // ----------------------------------------------------------- + // ----------------------------------------------------------- + // Configuration + // ----------------------------------------------------------- nebula_configuration = new NebulaTemplate(etc_location, var_location); - + rc = nebula_configuration->load_configuration(); - + if ( rc != 0 ) { throw runtime_error("Could not load nebula configuration file."); } - - // ----------------------------------------------------------- - // Log system - // ----------------------------------------------------------- - + + // ----------------------------------------------------------- + // Log system + // ----------------------------------------------------------- + ostringstream os; try @@ -71,26 +72,26 @@ void Nebula::start() string log_fname; int log_level_int; Log::MessageType clevel = Log::ERROR; - + log_fname = log_location + "oned.log"; - + nebula_configuration->get("DEBUG_LEVEL", log_level_int); - + if (0 <= log_level_int && log_level_int <= 3 ) { clevel = static_cast(log_level_int); } os << "Init OpenNebula Log system"; - + // Initializing ONE Daemon log system - + Nebula::log("ONE", Log::INFO, os, log_fname.c_str(), clevel); - + os.str(""); os << "Log Level: " << clevel << " [0=ERROR,1=WARNING,2=INFO,3=DEBUG]"; @@ -100,120 +101,120 @@ void Nebula::start() Log::INFO, os, log_fname.c_str(), - clevel); + clevel); } catch(runtime_error&) { throw; } - + Nebula::log("ONE",Log::INFO,"----------------------------------------------"); Nebula::log("ONE",Log::INFO," OpenNebula Configuration File "); Nebula::log("ONE",Log::INFO,"----------------------------------------------"); os.str(""); - + os << "\n--------------------------------------------"; os << *nebula_configuration; os << "\n--------------------------------------------"; - + Nebula::log("ONE",Log::INFO,os); - - // ----------------------------------------------------------- + + // ----------------------------------------------------------- // Pools - // ----------------------------------------------------------- + // ----------------------------------------------------------- try { string db_name = var_location + "one.db"; struct stat db_stat; bool db_bootstrap = stat(db_name.c_str(), &db_stat) != 0; - + db = new SqliteDB(db_name,Nebula::log); - if (db_bootstrap) - { - Nebula::log("ONE",Log::INFO,"Bootstraping OpenNebula database."); - - VirtualMachinePool::bootstrap(db); - HostPool::bootstrap(db); - VirtualNetworkPool::bootstrap(db); - UserPool::bootstrap(db); - } + if (db_bootstrap) + { + Nebula::log("ONE",Log::INFO,"Bootstraping OpenNebula database."); + + VirtualMachinePool::bootstrap(db); + HostPool::bootstrap(db); + VirtualNetworkPool::bootstrap(db); + UserPool::bootstrap(db); + } } catch (exception&) { throw; } - + try - { + { string mac_prefix; int size; - + vector vm_hooks; - + nebula_configuration->get("VM_HOOK", vm_hooks); - + vmpool = new VirtualMachinePool(db, vm_hooks); hpool = new HostPool(db); - + nebula_configuration->get("MAC_PREFIX", mac_prefix); nebula_configuration->get("NETWORK_SIZE", size); - + vnpool = new VirtualNetworkPool(db,mac_prefix,size); - + upool = new UserPool(db); } catch (exception&) { throw; } - - // ----------------------------------------------------------- - // Close stds, we no longer need them - // ----------------------------------------------------------- - + + // ----------------------------------------------------------- + // Close stds, we no longer need them + // ----------------------------------------------------------- + fd = open("/dev/null", O_RDWR|O_CREAT); - + dup2(fd,0); - dup2(fd,1); + dup2(fd,1); dup2(fd,2); - - close(fd); - + + close(fd); + fcntl(0,F_SETFD,0); // Keep them open across exec funcs fcntl(1,F_SETFD,0); fcntl(2,F_SETFD,0); - - // ----------------------------------------------------------- - // Block all signals before creating any Nebula thread - // ----------------------------------------------------------- - + + // ----------------------------------------------------------- + // Block all signals before creating any Nebula thread + // ----------------------------------------------------------- + sigfillset(&mask); - - pthread_sigmask(SIG_BLOCK, &mask, NULL); - - // ----------------------------------------------------------- + + pthread_sigmask(SIG_BLOCK, &mask, NULL); + + // ----------------------------------------------------------- //Managers // ----------------------------------------------------------- - + MadManager::mad_manager_system_init(); time_t timer_period; - + nebula_configuration->get("MANAGER_TIMER", timer_period); - - // ---- Virtual Machine Manager ---- + + // ---- Virtual Machine Manager ---- try { time_t poll_period; vector vmm_mads; nebula_configuration->get("VM_POLLING_INTERVAL", poll_period); - + nebula_configuration->get("VM_MAD", vmm_mads); - + vmm = new VirtualMachineManager( vmpool, hpool, @@ -225,15 +226,15 @@ void Nebula::start() { throw; } - + rc = vmm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Virtual Machine Manager"); - } - - // ---- Life-cycle Manager ---- + } + + // ---- Life-cycle Manager ---- try { lcm = new LifeCycleManager(vmpool,hpool); @@ -244,22 +245,22 @@ void Nebula::start() } rc = lcm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Life-cycle Manager"); } - + // ---- Information Manager ---- try { vector im_mads; time_t monitor_period; - + nebula_configuration->get("HOST_MONITORING_INTERVAL", monitor_period); - + nebula_configuration->get("IM_MAD", im_mads); - + im = new InformationManager(hpool,timer_period,monitor_period,im_mads); } catch (bad_alloc&) @@ -268,7 +269,7 @@ void Nebula::start() } rc = im->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Information Manager"); @@ -278,9 +279,9 @@ void Nebula::start() try { vector tm_mads; - + nebula_configuration->get("TM_MAD", tm_mads); - + tm = new TransferManager(vmpool, hpool, tm_mads); } catch (bad_alloc&) @@ -289,24 +290,24 @@ void Nebula::start() } rc = tm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Transfer Manager"); } - + // ---- Dispatch Manager ---- try - { + { dm = new DispatchManager(vmpool,hpool); } catch (bad_alloc&) { throw; } - + rc = dm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Dispatch Manager"); @@ -314,11 +315,11 @@ void Nebula::start() // ---- Request Manager ---- try - { + { int rm_port = 0; nebula_configuration->get("PORT", rm_port); - + rm = new RequestManager( vmpool, hpool, @@ -332,21 +333,21 @@ void Nebula::start() Nebula::log("ONE", Log::ERROR, "Error starting RM"); throw; } - + rc = rm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Request Manager"); } - // ---- Hook Manager ---- + // ---- Hook Manager ---- try { vector hm_mads; - + nebula_configuration->get("HM_MAD", hm_mads); - + hm = new HookManager(hm_mads,vmpool); } catch (bad_alloc&) @@ -355,12 +356,12 @@ void Nebula::start() } rc = hm->start(); - + if ( rc != 0 ) { throw runtime_error("Could not start the Hook Manager"); } - + // ----------------------------------------------------------- // Load mads // ----------------------------------------------------------- @@ -372,43 +373,43 @@ void Nebula::start() im->load_mads(0); tm->load_mads(0); hm->load_mads(0); - + // ----------------------------------------------------------- // Wait for a SIGTERM or SIGINT signal // ----------------------------------------------------------- - + sigemptyset(&mask); - + sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); - + sigwait(&mask, &signal); - // ----------------------------------------------------------- + // ----------------------------------------------------------- // Stop the managers & free resources // ----------------------------------------------------------- - + vmm->trigger(VirtualMachineManager::FINALIZE,0); - lcm->trigger(LifeCycleManager::FINALIZE,0); + lcm->trigger(LifeCycleManager::FINALIZE,0); tm->trigger(TransferManager::FINALIZE,0); dm->trigger(DispatchManager::FINALIZE,0); - - im->finalize(); + + im->finalize(); rm->finalize(); hm->finalize(); //sleep to wait drivers??? - + pthread_join(vmm->get_thread_id(),0); pthread_join(lcm->get_thread_id(),0); pthread_join(tm->get_thread_id(),0); pthread_join(dm->get_thread_id(),0); - + pthread_join(im->get_thread_id(),0); pthread_join(rm->get_thread_id(),0); pthread_join(hm->get_thread_id(),0); - + Nebula::log("ONE", Log::INFO, "All modules finalized, exiting.\n"); } diff --git a/src/vnm/RangedLeases.cc b/src/vnm/RangedLeases.cc index ac19d3fe99..19e8141547 100644 --- a/src/vnm/RangedLeases.cc +++ b/src/vnm/RangedLeases.cc @@ -24,7 +24,7 @@ /* ************************************************************************** */ RangedLeases::RangedLeases( - SqliteDB * db, + SqlDB * db, int _oid, unsigned long _size, unsigned int _mac_prefix, @@ -32,12 +32,12 @@ RangedLeases::RangedLeases( Leases(db,_oid,_size),mac_prefix(_mac_prefix),current(0) { unsigned int net_addr; - + Leases::Lease::ip_to_number(_network_address,net_addr); - + //size is the number of hosts in the network size = _size + 2; - + network_address = 0xFFFFFFFF << (int) ceil(log(size)/log(2)); network_address &= net_addr; @@ -49,33 +49,33 @@ RangedLeases::RangedLeases( int RangedLeases::get(int vid, string& ip, string& mac) { - unsigned int num_ip; - int rc = -1; - - for (unsigned int i=0; iexec(oss); - + if ( rc == 0 ) { leases.insert(make_pair(ip,new Lease(ip,mac,vid,used))); @@ -161,7 +161,7 @@ int RangedLeases::del(const string& ip) { unsigned int _ip; ostringstream oss; - int rc; + int rc; map::iterator it_ip; // Remove lease from leases map