Exemple #1
0
def main():
    boot_time = time.time()
    config = load_config()

    metrics.init(config, 'iris-owa-sync', default_metrics)

    owaconfig = config.get('owa')

    if not owaconfig:
        logger.critical('Missing OWA configs')
        sys.exit(1)

    api_host = owaconfig.get('api_host', 'http://localhost:16649')
    iris_client = IrisClient(api_host, 0, owaconfig['iris_app'],
                             owaconfig['iris_app_key'])

    proxies = owaconfig.get('proxies')

    # only way to configure a proxy is to monkey-patch (http adapter) a monkey-patch (baseprotocol) :/
    if proxies:
        UseProxyHttpAdapter._my_proxies = proxies
        exchangelib.protocol.BaseProtocol.HTTP_ADAPTER_CLS = UseProxyHttpAdapter

    creds = exchangelib.Credentials(**owaconfig['credentials'])

    try:
        nap_time = int(owaconfig.get('sleep_interval', 60))
    except ValueError:
        nap_time = 60

    while True:
        start_time = time.time()
        message_count = 0

        try:
            config = exchangelib.Configuration(credentials=creds,
                                               **owaconfig['config'])
            account = exchangelib.Account(config=config,
                                          access_type=exchangelib.DELEGATE,
                                          **owaconfig['account'])
        except (exchangelib.errors.EWSError,
                requests.exceptions.RequestException):
            logger.exception('Failed authenticating to OWA365')
            metrics.incr('owa_api_failure_count')
        else:
            logger.info('Receiving mail on behalf of %s',
                        owaconfig['account'].get('primary_smtp_address'))
            message_count = poll(account, iris_client)

        now = time.time()
        run_time = now - start_time
        logger.info(
            'Last run took %2.f seconds and processed %s messages. Waiting %s seconds until next poll..',
            run_time, message_count, nap_time)
        metrics.set('uptime', now - boot_time)
        metrics.emit()
        sleep(nap_time)
Exemple #2
0
def main():
    iris_config = load_config(
        os.environ.get('IRIS_CFG_PATH', '/home/iris/config/config.yaml'))
    mysql_config = iris_config['db']['conn']['kwargs']

    # It often takes several seconds for MySQL to start up. iris dies upon start
    # if it can't immediately connect to MySQL, so we have to wait for it.
    wait_for_mysql(mysql_config)

    if 'DOCKER_DB_BOOTSTRAP' in os.environ:
        if not os.path.exists(initializedfile):
            initialize_mysql_schema(mysql_config)

    os.execv('/usr/bin/uwsgi',
             ['', '--yaml', '/home/iris/daemons/uwsgi.yaml:prod'])
Exemple #3
0
def main():
    iris_config = load_config(
        os.environ.get('IRIS_CFG_PATH', '/home/iris/config/config.yaml'))
    mysql_config = iris_config['db']['conn']['kwargs']

    # It often takes several seconds for MySQL to start up. iris dies upon start
    # if it can't immediately connect to MySQL, so we have to wait for it.
    wait_for_mysql(mysql_config)

    if 'DOCKER_DB_BOOTSTRAP' in os.environ:
        if not os.path.exists(initializedfile):
            initialize_mysql_schema(mysql_config)

    os.execv(
        '/usr/bin/uwsgi',
        # first array element is ARGV0, since python 3.6 it cannot be empty, using space
        # https://bugs.python.org/issue28732
        [
            ' ', '--yaml',
            os.environ.get('UWSGI_CONFIG',
                           '/home/iris/daemons/uwsgi.yaml:prod')
        ])
Exemple #4
0
def main():
    config = load_config()

    metrics.init(config, 'iris-owa-sync', default_metrics)

    owaconfig = config.get('owa')

    if not owaconfig:
        logger.critical('Missing OWA configs')
        sys.exit(1)

    api_host = owaconfig.get('api_host', 'http://localhost:16649')
    iris_client = IrisClient(api_host, 0, owaconfig['iris_app'],
                             owaconfig['iris_app_key'])

    spawn(metrics.emit_forever)

    creds = Credentials(**owaconfig['credentials'])

    account = Account(primary_smtp_address=owaconfig['smtp_address'],
                      credentials=creds,
                      autodiscover=True,
                      access_type=DELEGATE)
    logger.info('Receiving mail on behalf of %s', owaconfig['smtp_address'])

    try:
        nap_time = int(owaconfig.get('sleep_interval', 60))
    except ValueError:
        nap_time = 60

    while True:
        start_time = time.time()
        message_count = poll(account, iris_client)
        run_time = time.time() - start_time
        logger.info(
            'Last run took %2.f seconds and processed %s messages. Waiting %s seconds until next poll..',
            run_time, message_count, nap_time)
        sleep(nap_time)
Exemple #5
0
def test_load_config(mocker):
    import os
    from tempfile import NamedTemporaryFile
    from iris.config import load_config

    mocker.patch.dict(os.environ, {'IRIS_CFG_DB_USER': '******'})

    with NamedTemporaryFile() as temp_config:
        temp_config.write(b'''
db:
  conn:
    kwargs:
      scheme: mysql+pymysql
      user: iris
      password: iris
      host: 127.0.0.1
      database: iris
      charset: utf8
    str: "%(scheme)s://%(user)s:%(password)s@%(host)s/%(database)s?charset=%(charset)s"
''')
        temp_config.flush()
        config = load_config(temp_config.name)
        assert config['db']['conn']['kwargs']['user'] == 'iris_dev'
Exemple #6
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from gevent import monkey
monkey.patch_all()  # NOQA

from gevent.pywsgi import WSGIServer
import sys
from iris.api import get_api
from iris.config import load_config

if __name__ == '__main__':

    def clean_exit(a, b):
        import sys
        sys.exit(0)

    import signal
    signal.signal(signal.SIGTERM, clean_exit)

    config = load_config(sys.argv[1])
    addr = (config['server']['host'], config['server']['port'])
    print('Listening on %s...' % (addr, ))
    application = get_api(config)
    WSGIServer(addr, application).serve_forever()
Exemple #7
0
import os
import logging
from iris.config import load_config
from iris.api import get_api

logging.basicConfig(format='[%(asctime)s] [%(process)d] [%(levelname)s] %(name)s %(message)s',
                    level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S %z')
application = get_api(load_config(os.environ['CONFIG']))