def main():
    global MONGO_SERVER, MONGO_CLIENT, STATE_CONFIG

    if MONGO_CLIENT is None:
        log("Establishing Mongo connection...")

        # read the configuration file from next to this file
        with open(ROOT_PATH + "mongo.json") as json_data:
            raw_data = json_data.read().replace('\\"', '\"')
            mongo_config = loads(raw_data)

        # connect to the mongo database using that configuration
        MONGO_SERVER = SSHTunnelForwarder(
            (mongo_config["mongo_ip"], int(mongo_config["ssh_port"])),
            ssh_pkey=(ROOT_PATH + mongo_config["ssh_key"]),
            ssh_username=mongo_config["ssh_username"],
            remote_bind_address=('127.0.0.1', 27017),
        )
        MONGO_SERVER.start()  # open the SSH tunnel to the mongo server
        MONGO_CLIENT = PyMongoClient(
            '127.0.0.1', MONGO_SERVER.local_bind_port)  # connect to mongo

        with open(ROOT_PATH + "instance.json") as json_data:
            raw_data = json_data.read().replace('\\"', '\"')
            STATE_CONFIG = loads(raw_data)

        log("Mongo connection established!")

    # implicitly a fetch job call from EM
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = (ROOT_PATH + "gauth.json")
    fetch_job()

    log("Closing mongo connection...")
    MONGO_SERVER.stop()
    log("Mongo connection terminated.")
Beispiel #2
0
    def mongo_connect(self):
        ROOT_PATH = "./"

        # read the configuration file from next to this file
        with open(ROOT_PATH + "mongo.json") as json_data:
            mongo_config = loads(json_data.read())

        # connect to the mongo database using that configuration
        self.MONGO_SERVER = SSHTunnelForwarder((mongo_config["mongo_ip"], int(mongo_config["ssh_port"])),
                                          ssh_pkey=(ROOT_PATH + mongo_config["ssh_key"]),
                                          ssh_username=mongo_config["ssh_username"],
                                          remote_bind_address=('localhost', 27017),
                                          )
        self.MONGO_SERVER.start()  # open the SSH tunnel to the mongo server
        self.MONGO_CLIENT = PyMongoClient('localhost', self.MONGO_SERVER.local_bind_port)  # connect to mongo
Beispiel #3
0
def main(command):
    global MONGO_SERVER, MONGO_CLIENT, STATE_CONFIG

    if MONGO_CLIENT is None:
        log("Establishing Mongo connection...")

        # read the configuration file from next to this file
        with open(ROOT_PATH + "mongo.json") as json_data:
            raw_data = json_data.read().replace('\\"', '\"')
            mongo_config = loads(raw_data)

        # connect to the mongo database using that configuration
        MONGO_SERVER = SSHTunnelForwarder((mongo_config["mongo_ip"], int(mongo_config["ssh_port"])),
                                          ssh_pkey=(ROOT_PATH + mongo_config["ssh_key"]),
                                          ssh_username=mongo_config["ssh_username"],
                                          remote_bind_address=('localhost', 27017),
                                          )
        MONGO_SERVER.start()  # open the SSH tunnel to the mongo server
        MONGO_CLIENT = PyMongoClient('localhost', MONGO_SERVER.local_bind_port)  # connect to mongo

        with open(ROOT_PATH + "instance.json") as json_data:
            raw_data = json_data.read().replace('\\"', '\"')
            STATE_CONFIG = loads(raw_data)

        log("Mongo connection established!")

    log("Received command:\n{0}".format(command))
    if command == "fetch_job":
        fetch_job()
    elif command == "finish_job":
        finish_job()
    else:
        log("Unrecognized command: {0}".format(command))  # shouldn't happen, but just to be safe...

    log("Closing mongo connection...")
    MONGO_SERVER.stop()
    log("Mongo connection terminated.")
Beispiel #4
0
 def __init__(self, addr="mender-mongo:27017"):
     self.client = PyMongoClient(addr)
end_time = 1630457220

if MONGO_CLIENT is None:

    # read the configuration file from next to this file
    with open(ROOT_PATH + "mongo.json") as json_data:
        mongo_config = loads(json_data.read())

    # connect to the mongo database using that configuration
    MONGO_SERVER = SSHTunnelForwarder((mongo_config["mongo_ip"], int(mongo_config["ssh_port"])),
                                      ssh_pkey=(ROOT_PATH + mongo_config["ssh_key"]),
                                      ssh_username=mongo_config["ssh_username"],
                                      remote_bind_address=('localhost', 27017),
                                      )
    MONGO_SERVER.start()  # open the SSH tunnel to the mongo server
    MONGO_CLIENT = PyMongoClient('localhost', MONGO_SERVER.local_bind_port)  # connect to mongo

    # with open(ROOT_PATH + "instance.json") as json_data:
    #     STATE_CONFIG = loads(json_data.read())

    DS_RESULTS = MONGO_CLIENT["ds_results"]
    dsfr_list = DS_RESULTS.dsfr.find({"model_type": model_type, "timestamp": {"$gte": start_time, "$lte": end_time}})
    locations = dict()
    data_insert = []

    for each in dsfr_list:
        node_id = each["observation"][0]
        timestamp = each["timestamp"]
        parent = each["parent"]
        coordinate = each["coordinate"]
        tmp = dict()