diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index a3f87f48e4..4ac8b877cd 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -102,6 +102,15 @@ public: return static_cast(PoolSQL::get(oid,lock)); }; + /** + * Gets a VM ID by its deploy_id, the dedploy_id - VM id mapping is keep + * in the import_table. + * @param deploy_id to search the id for + * @return -1 if not found or VMID + * + */ + int get_vmid(const string& deploy_id); + /** * Function to get the IDs of running VMs * @param oids a vector that contains the IDs @@ -197,8 +206,14 @@ public: * @return 0 on success */ static int bootstrap(SqlDB * _db) - { - return VirtualMachine::bootstrap(_db); + { + int rc; + ostringstream oss_import(import_db_bootstrap); + + rc = VirtualMachine::bootstrap(_db); + rc += _db->exec(oss_import); + + return rc; }; /** @@ -279,6 +294,32 @@ private: * True or false whether to submit new VM on HOLD or not */ static bool _submit_on_hold; + + /** + * Callback used to get an int in the DB it is used by VM Pool in: + * - calculate_showback (min_stime) + * - get_vmid (vmid) + */ + int db_int_cb(void * _min_stime, int num, char **values, char **names); + + // ------------------------------------------------------------------------- + // Virtual Machine ID - Deploy ID index for imported VMs + // The index is managed by the VirtualMachinePool + // ------------------------------------------------------------------------- + static const char * import_table; + + static const char * import_db_names; + + static const char * import_db_bootstrap; + + /** + * Insert deploy_id - vmid index. + * @param replace will replace and not insert + * @return 0 on success + */ + int insert_index(const string& deploy_id, int vm_id, bool replace); + + void drop_index(const string& deploy_id); }; #endif /*VIRTUAL_MACHINE_POOL_H_*/ diff --git a/src/host/Host.cc b/src/host/Host.cc index 1633a59d7d..e07adb06f1 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -345,6 +345,14 @@ int Host::update_info(Template &tmpl, rc = vatt->vector_value("ID", vmid); + if (rc == 0 && vmid == -1) //Check if it is an imported + { + Nebula& nd = Nebula::instance(); + VirtualMachinePool * vmpool = nd.get_vmpool(); + + vmid = vmpool->get_vmid(vatt->vector_value("DEPLOY_ID")); + } + if (rc == 0 && vmid != -1) { if (tmp_lost_vms.erase(vmid) == 1) //Good, known diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 9e123e5ca3..fbcf10243b 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -402,6 +402,20 @@ int VirtualMachine::insert(SqlDB * db, string& error_str) goto error_graphics; } + // ------------------------------------------------------------------------- + // Get and set DEPLOY_ID for imported VMs + // ------------------------------------------------------------------------- + + user_obj_template->get("IMPORT_VM_ID", value); + user_obj_template->erase("IMPORT_VM_ID"); + + if (!value.empty()) + { + deploy_id = value; + } + + // ------------------------------------------------------------------------ + parse_well_known_attributes(); // ------------------------------------------------------------------------ diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index d0920ea693..16106b4752 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -18,6 +18,7 @@ #include "VirtualMachineHook.h" #include "NebulaLog.h" +#include "Nebula.h" #include @@ -27,6 +28,16 @@ time_t VirtualMachinePool::_monitor_expiration; bool VirtualMachinePool::_submit_on_hold; + +const char * VirtualMachinePool::import_table = "vm_import"; + +const char * VirtualMachinePool::import_db_names = "deploy_id, vmid"; + +const char * VirtualMachinePool::import_db_bootstrap = + "CREATE TABLE IF NOT EXISTS vm_import " + "(deploy_id VARCHAR(128), vmid INTEGER, PRIMARY KEY(deploy_id))"; + + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -240,6 +251,54 @@ VirtualMachinePool::VirtualMachinePool( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VirtualMachinePool::insert_index(const string& deploy_id, int vmid, + bool replace) +{ + ostringstream oss; + char * deploy_name = db->escape_str(deploy_id.c_str()); + + if (deploy_name == 0) + { + return -1; + } + + if (replace) + { + oss << "REPLACE "; + } + else + { + oss << "INSERT "; + } + + oss << "INTO " << import_table << " ("<< import_db_names <<") " + << " VALUES ('" << deploy_name << "'," << vmid << ")"; + + db->free_str(deploy_name); + + return db->exec(oss); +}; + +/* -------------------------------------------------------------------------- */ + +void VirtualMachinePool::drop_index(const string& deploy_id) +{ + ostringstream oss; + char * deploy_name = db->escape_str(deploy_id.c_str()); + + if (deploy_name == 0) + { + return; + } + + oss << "DELETE FROM " << import_table << " WHERE deploy_id='" + << deploy_name << "'"; + + db->exec(oss); +} + +/* -------------------------------------------------------------------------- */ + int VirtualMachinePool::allocate ( int uid, int gid, @@ -252,13 +311,17 @@ int VirtualMachinePool::allocate ( bool on_hold) { VirtualMachine * vm; + + string deploy_id; // ------------------------------------------------------------------------ // Build a new Virtual Machine object // ------------------------------------------------------------------------ vm = new VirtualMachine(-1, uid, gid, uname, gname, umask, vm_template); - if ( _submit_on_hold == true || on_hold ) + vm->user_obj_template->get("IMPORT_VM_ID", deploy_id); + + if ( _submit_on_hold == true || on_hold || !deploy_id.empty()) { vm->state = VirtualMachine::HOLD; } @@ -267,12 +330,33 @@ int VirtualMachinePool::allocate ( vm->state = VirtualMachine::PENDING; } + if (insert_index(deploy_id, -1, false) == -1) //Mark import as in progress + { + error_str = "Virtual Machine " + deploy_id + " already imported."; + return -1; + } // ------------------------------------------------------------------------ // Insert the Object in the pool // ------------------------------------------------------------------------ *oid = PoolSQL::allocate(vm, error_str); + // ------------------------------------------------------------------------ + // Insert the deploy_id - vmid index for imported VMs + // ------------------------------------------------------------------------ + + if (!deploy_id.empty()) + { + if (*oid >= 0) + { + insert_index(deploy_id, *oid, true); + } + else + { + drop_index(deploy_id); + } + } + return *oid; } @@ -413,3 +497,47 @@ int VirtualMachinePool::dump_monitoring( return PoolSQL::dump(oss, "MONITORING_DATA", cmd); } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachinePool::db_int_cb(void * _min_stime, int num, char **values, char **names) +{ + if ( num == 0 || values == 0 || values[0] == 0 ) + { + return -1; + } + + *static_cast(_min_stime) = atoi(values[0]); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachinePool::get_vmid (const string& deploy_id) +{ + int vmid, rc; + ostringstream oss; + + set_callback(static_cast(&VirtualMachinePool::db_int_cb), + static_cast(&vmid)); + + oss << "SELECT vmid FROM " << import_table + << "WHERE deploy_id = " << db->escape_str(deploy_id.c_str()); + + rc = db->exec(oss, this); + + unset_callback(); + + if (rc != 0 ) + { + return -1; + } + + return vmid; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */