Example #1
0
 def connect(self, config=None, timeoutInMS=500):
     """
     Connect the mongo client to the database using the login provided and ssl certificates if ssl is activated.
     Args:
         config: A dictionnary with client.cfg config values (host, mongo_port, password, user, ssl).
                 Default to None. If None, the client.cfg file will be read.
         timeoutInMs: milliseconds to wait before timeout. Default to 500ms.
     Raises:
         ServerSelectionTimeoutError: if unable to connect to the mongo database
         OperationFailure: if unable to authenticate using user/password.
     Returns:
         None if not connected
         False if connection failed
         True if connected succeeded
     """
     if self.client is not None:
         return
     dir_path = os.path.dirname(os.path.realpath(__file__))
     cfg = config if config is not None else Utils.loadCfg(
         os.path.join(dir_path, "../../config/client.cfg"))
     try:
         self.host = str(cfg["host"])
         self.port = str(cfg.get("mongo_port", 27017))
         self.password = str(cfg["password"])
         self.user = str(cfg["user"])
         self.ssl = str(cfg["ssl"])
         connectionString = ""
         if self.user != "":
             connectionString = self.user + ':' + self.password + '@'
         self.calendarName = None
         try:
             if cfg["ssl"] == "True":
                 self.ssldir = os.path.abspath(
                     os.path.join(dir_path, "../../ssl/"))
                 self.client = MongoClient(
                     'mongodb://' + connectionString + self.host + ":" +
                     self.port,
                     ssl=True,
                     ssl_certfile=os.path.join(self.ssldir, "client.pem"),
                     ssl_cert_reqs=ssl.CERT_REQUIRED,
                     ssl_ca_certs=os.path.join(self.ssldir, "ca.pem"),
                     serverSelectionTimeoutMS=timeoutInMS,
                     socketTimeoutMS=2000,
                     connectTimeoutMS=2000)
             else:
                 self.client = MongoClient(
                     'mongodb://' + connectionString + self.host + ":" +
                     self.port,
                     serverSelectionTimeoutMS=timeoutInMS)
             server_info = self.client.server_info()
             return True and self.client is not None and server_info is not None
         except ServerSelectionTimeoutError as e:  # Unable to connect
             raise e
         except OperationFailure as e:  #  Authentication failed
             raise e
     except KeyError as e:
         raise e
     return False
Example #2
0
 def __init__(self, cfg=None):
     """
     Constructor
     Args:
         cfg: a dict with keys host, sftp_port, sftp_user, sftp_password.
             If None, reads configuration file in config/client.cfg
             Default to None.
     """
     # /home/barre/Documents/Pollenisator/core/Components/FileStorage.py
     if cfg is None:
         dir_path = os.path.dirname(os.path.realpath(__file__))
         dir_path = os.path.join(dir_path, "../../config/client.cfg")
         cfg = Utils.loadCfg(dir_path)
     self.hostname = cfg["host"]
     self.port = int(cfg["sftp_port"])
     self.username = cfg["sftp_user"]
     self.password = cfg["sftp_password"]
     self.sftp_connection = None
Example #3
0
dir_path = os.path.dirname(os.path.realpath(__file__))  # fullpath to this file
ssldir = os.path.join(dir_path, "./ssl/")  # fullepath to ssl directory
certs = {
    'keyfile': ssldir + 'client.pem',
    'certfile': ssldir + 'server.pem',
    'ca_certs': ssldir + 'ca.pem',
    'cert_reqs': ssl.CERT_REQUIRED
}
config_dir = os.path.join(dir_path, "./config/")
if not os.path.isfile(os.path.join(config_dir, "client.cfg")):
    if os.path.isfile(os.path.join(config_dir, "clientSample.cfg")):
        copyfile(os.path.join(config_dir, "clientSample.cfg"),
                 os.path.join(config_dir, "client.cfg"))

if os.path.isfile(os.path.join(config_dir, "client.cfg")):
    cfg = Utils.loadCfg(os.path.join(config_dir, "client.cfg"))
else:
    print("No client config file found under " + str(config_dir))
    sys.exit(1)
user_string = cfg["user"]+':'+cfg["password"] + \
    '@' if cfg['user'].strip() != "" else ""
if cfg["ssl"] == "True":
    app = Celery(
        'tasks',
        broker='mongodb://' + user_string + cfg["host"] + ':' +
        cfg["mongo_port"] +
        '/broker_pollenisator?authSource=admin&ssl=true&ssl_ca_certs=' +
        certs["ca_certs"] + '&ssl_certfile=' + certs["keyfile"])
else:
    app = Celery('tasks',
                 broker='mongodb://' + user_string + cfg["host"] + ':' +
Example #4
0
from core.Models.Interval import Interval
from core.Models.Tool import Tool
from core.Models.Wave import Wave
from core.Models.Command import Command
from core.Components.Worker import Worker
# Module variables
dir_path = os.path.dirname(os.path.realpath(__file__))  # fullpath to this file
ssldir = os.path.join(dir_path, "./ssl/")  # fullepath to ssl directory
certs = {
    'keyfile': ssldir+'client.pem',
    'certfile': ssldir+'server.pem',
    'ca_certs': ssldir+'ca.pem',
    'cert_reqs': ssl.CERT_REQUIRED
}
try:
    cfg = Utils.loadCfg(os.path.join(dir_path, "./config/client.cfg"))
except FileNotFoundError:
    print("No client config was found under Pollenisator/config/client.cfg. Create one from the sample provided in this directory.")
    sys.exit(0)
user_string = cfg["user"]+':'+cfg["password"] + \
    '@' if cfg['user'].strip() != "" else ""
if cfg["ssl"] == "True":
    app = Celery('tasks', broker='mongodb://'+user_string+cfg["host"] + ':' + cfg["mongo_port"] +
                 '/broker_pollenisator?authSource=admin&ssl=true&ssl_ca_certs='+certs["ca_certs"]+'&ssl_certfile='+certs["keyfile"])
else:
    app = Celery('tasks', broker='mongodb://' + user_string +
                 cfg["host"] + ':'+cfg["mongo_port"] + '/broker_pollenisator?authSource=admin')


"""FIX MULTIPROCESING INSIDE CELERY TASK"""
from celery.signals import worker_process_init