Exemple #1
0
    def _clean_db_relations(self):

        CreateDB()._purge_relations()

        sql = "delete from relation where details like '%UNIT_TEST%'"
        db_session.execute(sql)
        db_session.commit()
Exemple #2
0
def add(ipaddress, mac, uniqueid):

    from phenome_core.core.database.model.api import create_object, get_objectmodel_by_name, get_object_by_ip_and_model_id
    model = get_objectmodel_by_name(_CORE_AGENT_MODEL_CLASSNAME_)

    if model is None:
        logger.error("Model not initialized. Please check JSON/config and DB.")
        return None

    obj = get_object_by_ip_and_model_id(ipaddress, model.id)

    if obj is None:

        try:

            from sqlalchemy.exc import IntegrityError

            # create a new core agent
            obj = create_object(model.id, ipaddress, mac, uniqueid)

            # commit
            db_session.add(obj)
            db_session.commit()

        except IntegrityError as e:
            db_session.rollback()

    return obj
Exemple #3
0
    def relate(self, miner):

        """
        Creates a relationship between the Miner and the Mining Pool

        Returns:
                None
        """

        try:

            from phenome_core.core.database.db import db_session

            # by default we will not update db
            updated_db = False

            # find out if there is a relationship from this miner to this pool
            if miner.has_relation(self._pool_object) == False:

                # get the "MINING_ON" relationship
                rtype_id = get_relation_type_id('MINING_ON')

                # add the relation
                miner.add_relation(self._pool_object, rtype_id, None)

                # commit so the model will fill in the relation objects, etc.
                db_session.commit()

            # are there any relations of classtype MINING POOL
            relations = miner.get_relations_by_classtype(get_model_classtype_id_by_name(self.CLASSTYPE))

            if relations:

                # reset the flag
                updated_db = False

                # iterate through relations to pools, and disable all relations except for the one to the current pool
                for r in relations:
                    if r.object_to is not None and r.object_to.id == self._pool_object.id:
                        if r.enabled == False:
                            r.enabled = True
                            updated_db = True
                    elif r.enabled == True:
                        r.enabled = False
                        updated_db = True

                if updated_db:
                    db_session.commit()

        except:
            logger.error("ERROR creating relationship between miner '{}' to pool '{}'".format(miner.id, self._pool_object.unique_id))
Exemple #4
0
    def _clean_db_model_helper_objects(self):

        from phenome_core.core.database.model.api import get_object_by_ip, get_object_by_mac

        # get our objects from all helper tests
        obj_ip = get_object_by_ip(CONST_TEST_IPADDRESS_ADD_DELETE_ONLY)
        if obj_ip:
            obj_ip.remove_relations()
            db_session.commit()

        obj_mac = get_object_by_mac(CONST_TEST_MACADDRESS_ADD_DELETE_ONLY)
        if obj_mac:
            obj_mac.remove_relations()
            db_session.commit()

        # delete the objects we will create (just in case) - but do not delete the "SELF" object
        sql = "delete from object where " \
              "(ip = '" + CONST_TEST_IPADDRESS_ADD_DELETE_ONLY + "' " \
                "or mac = '" + CONST_TEST_MACADDRESS_ADD_DELETE_ONLY + "' " \
                "or unique_id = '" + CONST_TEST_IPADDRESS_ADD_DELETE_ONLY + "' " \
                "or unique_id = '" + CONST_TEST_MACADDRESS_ADD_DELETE_ONLY + "')" \
                " and unique_id <> 'SELF'"

        db_session.execute(sql)
        db_session.commit()
Exemple #5
0
    def _get_api_key_and_user_id(self):

        from phenome import flask_app
        api_key = flask_app.config.get("MINING_POOL_HUB_API_KEY")
        user_id = flask_app.config.get("MINING_POOL_HUB_USER_ID")

        if api_key is None or len(api_key)==0:
            # try to get from object
            api_key = self._pool_object.api_key
            user_id = self._pool_object.user_id

        if api_key is not None and len(api_key) > 0:
            # we have an API key, does it match the current one in the pool object?
            try:
                if self._pool_object.api_key is None or (self._pool_object.api_key is not None and self._pool_object.api_key != api_key):
                    from phenome_core.core.database.db import db_session
                    self._pool_object.api_key = api_key
                    self._pool_object.user_id = user_id
                    db_session.commit()
            except:
                logger.error("Cannot commit API parameters for Mining Pool Hub Pool object")

        return api_key, user_id
Exemple #6
0
    def _clean_db_objects_FULL(self):

        from phenome_core.core.database.model.api import get_object_by_ip, get_object_by_mac
        from phenome_core.core.database.model.object_model import ObjectModel

        try:
            # get our objects from test 4 and 5
            obj_ip = get_object_by_ip(CONST_TEST_IPADDRESS)
            if obj_ip:
                obj_ip.remove_relations()
                # when operating directly on the object, must commit immediately
                db_session.commit()

        except Exception as ex:
            print(ex)

        try:
            obj_mac = get_object_by_mac(CONST_TEST_MACADDRESS)
            if obj_mac:
                obj_mac.remove_relations()
                # when operating directly on the object, must commit immediately
                db_session.commit()
        except:
            pass

        try:
            obj_model_test = ObjectModel.query.filter_by(
                model=CONST_TEST_MODEL_TAG).first()

            if obj_model_test:
                obj_model_id = obj_model_test.id

                sql = "delete from property where (object_model_id = " + str(
                    obj_model_id) + ")"
                db_session.execute(sql)

                sql = "delete from property_model where (object_model_id = " + str(
                    obj_model_id) + ")"
                db_session.execute(sql)

                db_session.commit()

        except:
            pass

        # delete the RELATIONSHIPS of the objects we will create
        self._clean_db_relations()

        # delete the objects we will create (just in case) - but do not delete the "SELF" object

        sql = "delete from object where " \
              "(ip = '" + CONST_TEST_IPADDRESS + "' " \
                "or mac = '" + CONST_TEST_MACADDRESS + "' " \
                "or unique_id = '" + CONST_TEST_IPADDRESS + "' " \
                "or unique_id = '" + CONST_TEST_MACADDRESS + "')" \
                " and unique_id <> 'SELF'"

        db_session.execute(sql)
        db_session.commit()

        sql = "delete from object where " \
              "(ip = '" + CONST_TEST_IPADDRESS_2 + "' " \
                "or mac = '" + CONST_TEST_MACADDRESS_2 + "' " \
                "or unique_id = '" + CONST_TEST_IPADDRESS_2 + "' " \
                "or unique_id = '" + CONST_TEST_MACADDRESS_2 + "')" \
                " and unique_id <> 'SELF'"

        db_session.execute(sql)
        db_session.commit()