Ejemplo n.º 1
0
    def _save_data_to_db(db_url, inf_list, inf_id=None):
        db = DataBase(db_url)
        if db.connect():
            infs_to_save = inf_list
            if inf_id:
                infs_to_save = {inf_id: inf_list[inf_id]}

            for inf in infs_to_save.values():
                data = inf.serialize()
                if db.db_type == DataBase.MONGO:
                    res = db.replace("inf_list", {"id": inf.id}, {
                        "id": inf.id,
                        "deleted": int(inf.deleted),
                        "data": data,
                        "date": time.time()
                    })
                else:
                    res = db.execute(
                        "replace into inf_list (id, deleted, data, date) values (%s, %s, %s, now())",
                        (inf.id, int(inf.deleted), data))

            db.close()
            return res
        else:
            InfrastructureList.logger.error(
                "ERROR connecting with the database!.")
            return None
Ejemplo n.º 2
0
    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 []
Ejemplo n.º 3
0
	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 {}
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 def delete_data_from_db(db_url, date):
     db = DataBase(db_url)
     if db.connect():
         db.execute("DELETE FROM inf_list WHERE deleted = 1 and date < '%s';" % date)
         db.close()
     else:
         sys.stderr.write("ERROR connecting with the database!.")
         sys.exit(-1)
Ejemplo n.º 6
0
 def _reinit():
     """Restart the class attributes to initial values."""
     InfrastructureList.infrastructure_list = {}
     InfrastructureList._lock = threading.Lock()
     db = DataBase(Config.DATA_DB)
     if db.connect():
         db.execute("delete from inf_list")
         db.close()
Ejemplo n.º 7
0
Archivo: db.py Proyecto: vigial/im
 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()
Ejemplo n.º 8
0
    def init_table():
        """ Creates de database """
        db = DataBase(Config.DATA_DB)
        if db.connect():
            if not db.table_exists("inf_list"):
                db.execute(
                    "CREATE TABLE inf_list(id VARCHAR(255) PRIMARY KEY, deleted INTEGER,"
                    " date TIMESTAMP, data LONGBLOB)")
                db.close()
                return True
        else:
            InfrastructureList.logger.error(
                "ERROR connecting with the database!.")

        return False
Ejemplo n.º 9
0
	def save_data_to_db(db_url, inf_list, inf_id = None):
		db = DataBase(db_url)
		if db.connect():
			infs_to_save = inf_list
			if inf_id:
				infs_to_save = {inf_id: inf_list[inf_id]}
			
			for inf in infs_to_save.values():
				res = db.execute("replace into inf_list set id = %s, data = %s, date = now()", (inf.id, pickle.dumps(inf)))

			db.close()
			return res
		else:
			InfrastructureManager.logger.error("ERROR connecting with the database!.")
			return None
Ejemplo n.º 10
0
    def _save_data_to_db(db_url, inf_list, inf_id=None):
        db = DataBase(db_url)
        if db.connect():
            infs_to_save = inf_list
            if inf_id:
                infs_to_save = {inf_id: inf_list[inf_id]}

            for inf in infs_to_save.values():
                res = db.execute("replace into inf_list (id, deleted, data, date) values (%s, %s, %s, now())",
                                 (inf.id, int(inf.deleted), inf.serialize()))

            db.close()
            return res
        else:
            InfrastructureList.logger.error("ERROR connecting with the database!.")
            return None
Ejemplo n.º 11
0
    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 []
Ejemplo n.º 12
0
    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 {}
Ejemplo n.º 13
0
    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 {}
Ejemplo n.º 14
0
Archivo: db.py Proyecto: vigial/im
    def test_mysql_db(self, mdb_conn):
        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()
Ejemplo n.º 15
0
    def init_table():
        """ Creates de database """
        db = DataBase(Config.DATA_DB)
        if db.connect():
            if not db.table_exists("inf_list"):
                InfrastructureList.logger.debug("Creating the IM database!.")
                if db.db_type == DataBase.MYSQL:
                    db.execute("CREATE TABLE inf_list(rowid INTEGER NOT NULL AUTO_INCREMENT UNIQUE,"
                               " id VARCHAR(255) PRIMARY KEY, deleted INTEGER, date TIMESTAMP, data LONGBLOB)")
                elif db.db_type == DataBase.SQLITE:
                    db.execute("CREATE TABLE inf_list(id VARCHAR(255) PRIMARY KEY, deleted INTEGER,"
                               " date TIMESTAMP, data LONGBLOB)")
                db.close()
            return True
        else:
            InfrastructureList.logger.error("ERROR connecting with the database!.")

        return False
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
Archivo: db.py Proyecto: vigial/im
    def test_mongo_db(self, mongo):
        client = MagicMock()
        mongo.return_value = client
        database = MagicMock()
        client.__getitem__.return_value = database
        database.client = client
        table = MagicMock()
        database.__getitem__.return_value = table
        table.database = database

        db_url = "mongodb://*****:*****@server/db_name"
        db = DataBase(db_url)
        self.assertTrue(db.connect())

        database.collection_names.return_value = ['table1']
        res = db.table_exists("test")
        self.assertFalse(res)
        res = db.table_exists("table1")
        self.assertTrue(res)

        res = db.replace('table', {}, {'id': 1, 'data': 'test1'})
        self.assertTrue(res)
        self.assertEqual(table.replace_one.call_args_list[0][0], ({}, {'data': 'test1', 'id': 1}, True))

        table.find.return_value = [{'id': 2, 'data': 'test2', '_id': 2}]
        res = db.find('table', {'id': 2}, {'data': True})
        self.assertEqual(len(res), 1)
        self.assertEqual(table.find.call_args_list[0][0], ({'id': 2}, {'_id': False, 'data': True}))

        del_res = MagicMock()
        del_res.deleted_count = 1
        table.delete_many.return_value = del_res
        res = db.delete('table', {'id': 1})
        self.assertEqual(res, 1)
        self.assertEqual(table.delete_many.call_args_list[0][0], ({'id': 1},))

        db.close()
Ejemplo n.º 18
0
 def rename_old_data():
     db = DataBase(Config.DATA_DB)
     if db.connect():
         if db.table_exists("inf_list"):
             now = str(int(time.time() * 100))
             if db.db_type == DataBase.SQLITE:
                 db.execute('ALTER TABLE inf_list RENAME TO inf_list_%s;' % now)
                 db.close()
             elif db.db_type == DataBase.MYSQL:
                 db.execute('RENAME TABLE inf_list TO inf_list_%s;' % now)
                 db.close()
             else:
                 db.close()
                 sys.stderr.write("ERROR connecting with the database!.")
                 sys.exit(-1)
         else:
             db.close()
     else:
         sys.stderr.write("ERROR connecting with the database!.")
         sys.exit(-1)
Ejemplo n.º 19
0
sys.path.append("..")
sys.path.append(".")

from IM.config import Config
from IM.db import DataBase

if __name__ == "__main__":
    if not Config.DATA_DB:
        sys.stderr.write("No DATA_DB defined in the im.cfg file!!")
        sys.exit(-1)

    db = DataBase(Config.DATA_DB)
    if db.connect():
        if db.table_exists("inf_list"):
            if db.db_type == DataBase.MYSQL:
                sys.stdout.write("Updating DB: %s.\n" % Config.DATA_DB)
                db.execute(
                    "ALTER TABLE `inf_list` ADD COLUMN `rowid` INT AUTO_INCREMENT UNIQUE FIRST;"
                )
            else:
                sys.stdout.write("SQLite DB does not need to be updated.")
            db.close()
        else:
            sys.stdout.write(
                "There are no inf_list table. Do not need to update.")
    else:
        sys.stderr.write("Error connecting with DB: %s\n" % Config.DATA_DB)
        sys.exit(-1)

    sys.exit(0)