def getRecipes(): """ Static method to get the list of recipes """ if not DataBase.db_available: return [] else: try: db = DataBase(Config.RECIPES_DB_FILE) db.connect() res = [] result = db.select('select * from recipes') for d in result: name = d[0] version = d[1] modules = d[2].split(",") recipe = d[3] isapp = d[4] galaxy_module = d[5] desc = d[6] requirements = d[7] res.append( Recipe(name, version, modules, recipe, desc, requirements, galaxy_module, isapp)) return res except Exception: return []
def get_data_from_db(db_url): db = DataBase(db_url) if db.connect(): if not db.table_exists("inf_list"): db.execute("CREATE TABLE inf_list(id VARCHAR(255) PRIMARY KEY, date TIMESTAMP, data LONGBLOB)") db.close() return {} else: inf_list = {} res = db.select("select * from inf_list order by id desc") if len(res) > 0: for elem in res: #inf_id = elem[0] #date = elem[1] try: inf = pickle.loads(elem[2]) if not inf.deleted: inf_list[inf.id] = inf except: InfrastructureManager.logger.exception("ERROR reading infrastructure %d from database, ignoring it!." % inf.id) else: InfrastructureManager.logger.error("ERROR getting inf_list from database!.") db.close() return inf_list else: InfrastructureManager.logger.error("ERROR connecting with the database!.") return {}
def get_data_from_db(db_url): db = DataBase(db_url) if db.connect(): if not db.table_exists("inf_list"): return {} else: inf_list = {} res = db.select("select * from inf_list order by id desc") if len(res) > 0: for elem in res: # inf_id = elem[0] # date = elem[1] try: inf = pickle.loads(elem[2]) inf_list[inf.id] = inf except: sys.stderr.write( "ERROR reading infrastructure from database, ignoring it!." ) sys.exit(-1) else: sys.stderr.write("ERROR getting inf_list from database!.") sys.exit(-1) db.close() return inf_list else: sys.stderr.write("ERROR connecting with the database!.") sys.exit(-1)
def _get_inf_ids_from_db(): try: db = DataBase(Config.DATA_DB) if db.connect(): inf_list = [] if db.db_type == DataBase.MONGO: res = db.find("inf_list", {"deleted": 0}, {"id": True}, [('id', -1)]) else: res = db.select( "select id from inf_list where deleted = 0 order by rowid desc" ) for elem in res: if db.db_type == DataBase.MONGO: inf_list.append(elem['id']) else: inf_list.append(elem[0]) db.close() return inf_list else: InfrastructureList.logger.error( "ERROR connecting with the database!.") return [] except Exception: InfrastructureList.logger.exception( "ERROR loading data. Correct or delete it!!") return []
def _get_data_from_db(db_url, inf_id=None, auth=None): """ Get data from DB. If no inf_id specified all Infrastructures are loaded. If auth is specified only auth data will be loaded. """ if InfrastructureList.init_table(): db = DataBase(db_url) if db.connect(): inf_list = {} if inf_id: if db.db_type == DataBase.MONGO: res = db.find("inf_list", {"id": inf_id}, {"data": True}) else: res = db.select("select data from inf_list where id = %s", (inf_id,)) else: if db.db_type == DataBase.MONGO: res = db.find("inf_list", {"deleted": 0}, {"data": True}, [('_id', -1)]) else: res = db.select("select data from inf_list where deleted = 0 order by rowid desc") if len(res) > 0: for elem in res: if db.db_type == DataBase.MONGO: data = elem['data'] else: data = elem[0] try: if auth: inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth(data) else: inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize(data) inf_list[inf.id] = inf except Exception: InfrastructureList.logger.exception( "ERROR reading infrastructure from database, ignoring it!.") else: InfrastructureList.logger.warn("No data in database!.") db.close() return inf_list else: InfrastructureList.logger.error("ERROR connecting with the database!.") return {} else: InfrastructureList.logger.error("ERROR connecting with the database!.") return {}
def _get_data_from_db(db_url, inf_id=None, auth=None): """ Get data from DB. If no inf_id specified all Infrastructures are loaded. If auth is specified only auth data will be loaded. """ if InfrastructureList.init_table(): return {} else: db = DataBase(db_url) if db.connect(): inf_list = {} if inf_id: res = db.select("select * from inf_list where id = '%s'" % inf_id) else: res = db.select( "select * from inf_list where deleted = 0 order by id desc" ) if len(res) > 0: for elem in res: # inf_id = elem[0] # date = elem[1] # deleted = elem[2] try: if auth: inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth( elem[3]) else: inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize( elem[3]) inf_list[inf.id] = inf except: InfrastructureList.logger.exception( "ERROR reading infrastructure from database, ignoring it!." ) else: InfrastructureList.logger.warn("No data in database!.") db.close() return inf_list else: InfrastructureList.logger.error( "ERROR connecting with the database!.") return {}
def test_db(self, mdb_conn): filename = "/tmp/inf.dat" if os.path.exists(filename): os.unlink(filename) db_url = "sqlite://" + filename db = DataBase(db_url) self.assertTrue(db.connect()) if not db.table_exists("test"): db.execute( "CREATE TABLE test(id int PRIMARY KEY, date TIMESTAMP, data LONGBLOB)" ) self.assertTrue(db.table_exists("test")) db.execute("insert into test (id, data, date) values (%s, %s, now())", (1, "Data")) res = db.select("select data from test where id = %s", (1, )) self.assertEqual(res, [("Data", )]) db.close() connection = MagicMock() mdb_conn.return_value = connection db_url = "mysql://*****:*****@server/db_name" db = DataBase(db_url) self.assertTrue(db.connect()) if not db.table_exists("test"): db.execute( "CREATE TABLE test(id int PRIMARY KEY, date TIMESTAMP, data LONGBLOB)" ) db.execute("insert into test (id, data, date) values (%s, %s, now())", (1, "Data")) cursor = MagicMock() cursor.fetchall.return_value = [("Data", )] connection.cursor.return_value = cursor res = db.select("select data from test where id = %s", (1, )) self.assertEqual(res, [("Data", )]) db.close()
def test_sqlite_db(self): filename = "/tmp/inf.dat" if os.path.exists(filename): os.unlink(filename) db_url = "sqlite://" + filename db = DataBase(db_url) self.assertTrue(db.connect()) if not db.table_exists("test"): db.execute("CREATE TABLE test(id int PRIMARY KEY, date TIMESTAMP, data LONGBLOB)") self.assertTrue(db.table_exists("test")) db.execute("insert into test (id, data, date) values (%s, %s, now())", (1, "Data")) res = db.select("select data from test where id = %s", (1,)) self.assertEqual(res, [("Data",)]) db.close()
def _get_inf_ids_from_db(): try: db = DataBase(Config.DATA_DB) if db.connect(): inf_list = [] res = db.select("select id from inf_list where deleted = 0 order by rowid desc") for elem in res: inf_list.append(elem[0]) db.close() return inf_list else: InfrastructureList.logger.error("ERROR connecting with the database!.") return [] except Exception: InfrastructureList.logger.exception("ERROR loading data. Correct or delete it!!") return []
def get_data_from_db(db_url): db = DataBase(db_url) if db.connect(): inf_list = {} res = db.select("select * from inf_list where deleted = 0 order by id desc") if len(res) > 0: for elem in res: try: inf = DB150to151.deserialize_info(elem[3]) inf_list[inf.id] = inf except: sys.stderr.write("ERROR reading infrastructure from database, ignoring it!.") else: sys.stderr.write("No data in database!.") db.close() return inf_list else: sys.stderr.write("ERROR connecting with the database!.") sys.exit(-1)
def getInstallableApps(): """ Static method to get the list of avalible apps """ if not DataBase.db_available: return [] else: try: db = DataBase(Config.RECIPES_DB_FILE) db.connect() res = [] result = db.select('select * from recipes where isapp = 1') for d in result: name = d[0] version = d[1] module = d[2] recipe = d[3] galaxy_module = d[5] requirements = d[7] res.append((FeaturesApp.from_str(name, version), module, galaxy_module, recipe, requirements)) return res except Exception: return []