Beispiel #1
0
def biomaj_daemon_bank_update(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user:
        abort(401, 'This action requires authentication with api key')
    options.update = True
    options.bank = bank
    params = request.get_json()
    if params is None:
        params = {}
    if 'publish' in params:
        options.publish = params['publish']
    if 'fromscratch' in params:
        options.fromscratch = params['fromscratch']
    if 'stop_before' in params:
        options.stop_before = params['stop_before']
    if 'stop_after' in params:
        options.stop_after = params['stop_after']
    if 'from_task' in params:
        options.from_task = params['from_task']
    if 'process' in params:
        options.process = params['process']
    if 'release' in params:
        options.release = params['release']
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #2
0
def biomaj_daemon_bank_remove(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user:
        abort(401, 'This action requires authentication with api key')
    options.bank = bank
    params = request.get_json()
    if params is None:
        params = {}
    if 'all' in params and params['all']:
        options.removeall = True
        if 'force' in params and params['force']:
            options.force = params['force']
    elif 'pending' in params and params['pending']:
        options.removepending = True
    else:
        options.remove = True
        if 'release' in params:
            options.release = params['release']
        else:
            abort(400, 'missing release')

    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #3
0
def biomaj_daemon():
    '''
    Execute a command request (bank update, removal, ...)
    '''
    apikey = request.headers.get('Authorization')
    token = None

    if apikey:
        bearer = apikey.split()
        if bearer[0] == 'APIKEY':
            token = bearer[1]
    try:
        params = request.get_json()
        options = params['options']
        options_object = Options(options)
        options_object.token = token
        options_object.user = None
        options_object.redis_host = config['redis']['host']
        options_object.redis_port = config['redis']['port']
        options_object.redis_db = config['redis']['db']
        options_object.redis_prefix = config['redis']['prefix']

        user = None
        if token:
            proxy = Utils.get_service_endpoint(config, 'user')
            r = requests.get(proxy + '/api/user/info/apikey/' + token)
            if not r.status_code == 200:
                abort(404, {'message': 'Invalid API Key or connection issue'})
            user = r.json()['user']
            if user:
                options_object.user = user['id']

        if options_object.maintenance in ['on', 'off']:
            if not options_object.user or 'admin' not in config[
                    'biomaj'] or options_object.user not in config['biomaj'][
                        'admin']:
                abort(401, {
                    'message':
                    'This action requires authentication with api key'
                })

        if options_object.bank:
            bmaj_options = BmajOptions(options_object)
            BiomajConfig(options_object.bank, bmaj_options)

            if not options_object.search and not options_object.show and not options_object.check and not options_object.status:
                if not user:
                    abort(
                        401, {
                            'message':
                            'This action requires authentication with api key'
                        })

        (res, msg) = biomaj_client_action(options_object, config)
    except Exception as e:
        logging.exception(e)
        return jsonify({'status': False, 'msg': str(e)})
    return jsonify({'status': res, 'msg': msg})
Beispiel #4
0
def biomaj_daemon_bank_status_ko():
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    options.statusko = True
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #5
0
def biomaj_daemon_history():
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)

    options.history = True
    options.historyLimit = request.args.get('limit', 100)
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #6
0
def biomaj_daemon_bank_unpublish(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user:
        abort(401, 'This action requires authentication with api key')
    options.bank = bank
    options.unpublish = True
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #7
0
def biomaj_daemon_maintenance_off():
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user or 'admin' not in config[
            'biomaj'] or options.user not in config['biomaj']['admin']:
        abort(401, 'This action requires authentication with api key')
    options.maintenance = 'off'
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #8
0
def biomaj_daemon_bank_update_directory(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user:
        abort(401, 'This action requires authentication with api key')
    params = request.get_json()
    options.bank = bank
    try:
        options.newdir = params['path']
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #9
0
def biomaj_daemon_aboutme():
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)

    options.aboutme = True
    params = request.get_json()
    try:
        options.userlogin = params['login']
        options.userpassword = params['password']
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #10
0
def biomaj_daemon_bank_publish(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    if not options.user:
        abort(401, 'This action requires authentication with api key')
    options.bank = bank
    options.publish = True
    params = request.get_json()
    if params is None:
        params = {}
    if 'release' in params:
        options.release = params['release']
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #11
0
def biomaj_daemon_bank_search(bank):
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    options.search = True
    params = request.get_json()
    if params is None:
        params = {}
    if 'formats' in params and params['formats']:
        options.formats = ','.join(params['formats'])
    if 'types' in params and params['types']:
        options.types = ','.join(params['types'])
    if 'query' in params and params['query']:
        options.query = ','.join(params['query'])
    try:
        (res, msg) = biomaj_client_action(options, config)
        if res:
            if isinstance(msg, dict):
                return jsonify(msg)
            else:
                return jsonify({'msg': msg})
    except Exception as e:
        abort(500, str(e))
Beispiel #12
0
def main():

    parser = argparse.ArgumentParser(add_help=False)
    Utils.set_args(parser)

    options = Options()
    parser.parse_args(namespace=options)

    options.no_log = False

    if options.help:
        print('''
    --config: global.properties file path (local install only)

    --proxy: BioMAJ daemon url (http://x.y.z)

    --trace: Trace workflow in Zipkin server

    --api-key: User API key to authenticate against proxy

    --whatsup: Get info on what biomaj is doing

    --last-log: Get log file of last session
        [MANDATORY]
        --proxy http://x.y.z
        [OPTIONAL]
        --tail X number of lines to tail from log file

    --about-me: Get my info
        [MANDATORY]
        --proxy http://x.y.z
        --user-login XX
        --user-password XX

    --update-status: get status of an update
        [MANDATORY]
        --bank xx: name of the bank to check
        --proxy http://x.y.z

    --update-cancel: cancel current update
        [MANDATORY]
        --bank xx: name of the bank to cancel
        --proxy http://x.y.z

    --status: list of banks with published release
        [OPTIONAL]
        --bank xx / bank: Get status details of bank

    --status-ko: list of banks in error status (last run)

    --log DEBUG|INFO|WARN|ERR  [OPTIONAL]: set log level in logs for this run, default is set in global.properties file

    --check: Check bank property file
        [MANDATORY]
        --bank xx: name of the bank to check (will check xx.properties)

    --owner yy: Change owner of the bank (user id)
        [MANDATORY]
        --bank xx: name of the bank

    --visibility public|private: change visibility public/private of a bank
        [MANDATORY]
        --bank xx: name of the bank

    --change-dbname yy: Change name of the bank to this new name
        [MANDATORY]
        --bank xx: current name of the bank

    --move-production-directories yy: Change bank production directories location to this new path, path must exists
        [MANDATORY]
        --bank xx: current name of the bank

    --update: Update bank
        [MANDATORY]
        --bank xx: name of the bank(s) to update, comma separated
        [OPTIONAL]
        --publish: after update set as *current* version
        --from-scratch: force a new update cycle, even if release is identical, release will be incremented like (myrel_1)
        --stop-before xx: stop update cycle before the start of step xx
        --stop-after xx: stop update cycle after step xx has completed
        --from-task xx --release yy: Force an re-update cycle for bank release *yy* or from current cycle (in production directories), skipping steps up to *xx*
        --process xx: linked to from-task, optionally specify a block, meta or process name to start from
        --release xx: release to update

    --publish: Publish bank as current release to use
        [MANDATORY]
        --bank xx: name of the bank to update
        --release xx: release of the bank to publish

    --unpublish: Unpublish bank (remove current)
        [MANDATORY]
        --bank xx: name of the bank to update

    --remove-all: Remove all bank releases and database records
        [MANDATORY]
        --bank xx: name of the bank to update
        [OPTIONAL]
        --force: remove freezed releases

    --remove-pending: Remove pending releases
        [MANDATORY]
        --bank xx: name of the bank to update

    --remove: Remove bank release (files and database release)
        [MANDATORY]
        --bank xx: name of the bank to update
        --release xx: release of the bank to remove

        Release must not be the *current* version. If this is the case, publish a new release before.

    --freeze: Freeze bank release (cannot be removed)
        [MANDATORY]
        --bank xx: name of the bank to update
        --release xx: release of the bank to remove

    --unfreeze: Unfreeze bank release (can be removed)
        [MANDATORY]
        --bank xx: name of the bank to update
        --release xx: release of the bank to remove

    --search: basic search in bank production releases, return list of banks
       --formats xx,yy : list of comma separated format
      AND/OR
       --types xx,yy : list of comma separated type

       --query "LUCENE query syntax": search in index (if activated)

    --show: Show bank files per format
      [MANDATORY]
      --bank xx: name of the bank to show
      [OPTIONAL]
      --release xx: release of the bank to show

    --maintenance on/off/status: (un)set biomaj in maintenance mode to prevent updates/removal

        ''')
        return

    proxy = options.proxy

    if 'BIOMAJ_PROXY' in os.environ:
        proxy = os.environ['BIOMAJ_PROXY']
        options.proxy = proxy

    if 'BIOMAJ_APIKEY' in os.environ:
        apikey = os.environ['BIOMAJ_APIKEY']
        options.apikey = apikey

    if not proxy:
        try:
            from biomaj_daemon.daemon.utils import biomaj_client_action
        except Exception as e:
            print(
                'Failed to import biomaj libraries. Either you forgot the --proxy option, either you use a local biomaj install and did not installed it (biomaj-daemon package)'
            )

    try:
        if not proxy:
            from biomaj_daemon.daemon.utils import biomaj_client_action
            options.user = getpass.getuser()
            BiomajConfig.load_config(options.config)
            (status, msg) = biomaj_client_action(options)
        else:
            headers = {}
            if options.apikey:
                headers = {'Authorization': 'APIKEY ' + options.apikey}

            if options.lastlog:
                if not options.bank:
                    print("--bank is missing\n")
                    sys.exit(1)
                if options.tail:
                    r = requests.get(proxy + '/api/daemon/bank/' +
                                     options.bank + '/log/' + options.tail,
                                     headers=headers)
                    print(r.text)
                else:
                    r = requests.get(proxy + '/api/daemon/bank/' +
                                     options.bank + '/log',
                                     headers=headers)
                    print(r.text)
                sys.exit(0)

            r = requests.post(proxy + '/api/daemon',
                              headers=headers,
                              json={'options': options.__dict__})
            if not r.status_code == 200:
                print('Failed to contact BioMAJ daemon')
                sys.exit(1)
            result = r.json()
            status = result['status']
            msg = result['msg']
        if not status:
            print('An error occured:\n')
            print(str(msg))
        else:
            if msg:
                print(str(msg))
            else:
                print('Done.')
    except Exception as e:
        logging.exception(e)
        print('Error:' + str(e))