Beispiel #1
0
def biomaj_status(options):
    '''
    Get bank status information
    '''
    msg = ''
    if options.bank:
        bank = Bank(options.bank, options=options, no_log=True)
        if bank.bank['properties']['visibility'] != 'public' and not bank.is_owner():
            return (False, 'Access forbidden')
        info = bank.get_bank_release_info(full=True)
        msg += tabulate(info['info'], headers='firstrow', tablefmt='psql') + '\n'
        msg += tabulate(info['prod'], headers='firstrow', tablefmt='psql') + '\n'
        # do we have some pending release(s)
        if 'pend' in info and len(info['pend']) > 1:
            msg += tabulate(info['pend'], headers='firstrow', tablefmt='psql') + '\n'
    else:
        banks = Bank.list()
        # Headers of output table
        banks_list = [["Name", "Type(s)", "Release", "Visibility", "Last update"]]
        for bank in sorted(banks, key=lambda k: k['name']):
            bank = Bank(bank['name'], options=options, no_log=True)
            if bank.bank['properties']['visibility'] == 'public' or bank.is_owner():
                banks_list.append(bank.get_bank_release_info()['info'])
        msg += tabulate(banks_list, headers="firstrow", tablefmt="psql") + '\n'
    return (True, msg)
Beispiel #2
0
def biomaj_daemon_bank_isowner(bank):
    '''
    Checks that logged user is admin or owner of the bank
    '''
    (http_code, options, error) = daemon_api_auth(request)
    if error:
        abort(http_code, error)
    bank_log = Bank(bank, options=options, no_log=True)
    if bank_log.is_owner():
        return jsonify({'is_admin_or_owner': True})
    else:
        return jsonify({'is_admin_or_owner': False})
Beispiel #3
0
def biomaj_bank_log_tail(bank, tail=100):
    apikey = request.headers.get('Authorization')
    token = None

    if apikey:
        bearer = apikey.split()
        if bearer[0] == 'APIKEY':
            token = bearer[1]
    log_file = None
    try:
        user = None
        options_object = Options(OPTIONS_PARAMS)
        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']
            options_object = Options({'user': user['id']})

        bank_log = Bank(bank, options=options_object, no_log=True)
        if bank_log.bank['properties'][
                'visibility'] != 'public' and not bank_log.is_owner():
            abort(403, {'message': 'not authorized to access this bank'})
        if 'status' not in bank_log.bank or 'log_file' not in bank_log.bank[
                'status'] or not bank_log.bank['status']['log_file']['status']:
            return "No log file available"
        log_file = bank_log.bank['status']['log_file']['status']
    except Exception as e:
        logging.exception(e)
        return "Failed to access log file: " + str(e)
    if not log_file or not os.path.exists(log_file):
        return "Cannot access log file %s" % (str(log_file))

    def generate():
        with open(log_file) as fp:
            tail_l = int(tail)
            if tail_l == 0:
                for line in fp:
                    yield line
            else:
                dq = deque(fp, maxlen=tail_l)
                for line in dq:
                    yield line
        yield "##END_OF_LOG"

    return Response(generate(), mimetype='text/plain')
Beispiel #4
0
def biomaj_status(options, config):
    '''
    Get bank status information
    '''
    msg = ''
    if options.bank:
        bank = Bank(options.bank, options=options, no_log=True)
        if bank.bank['properties'][
                'visibility'] != 'public' and not bank.is_owner():
            return (False, 'Access forbidden')
        info = bank.get_bank_release_info(full=True)
        if options.json:
            bank_info = []
            headers = []
            if info['info'] and len(info['info']) > 1:
                headers = info['info'][0]
                bank_info = info['info'][1:]
            prod_info = []
            prod_headers = []
            if info['prod'] and len(info['prod']) > 1:
                prod_headers = info['prod'][0]
                prod_info = info['prod'][1:]
            msg = {
                'bank': {
                    'info': {
                        'headers': headers,
                        'details': bank_info
                    },
                    'production': {
                        'headers': prod_headers,
                        'details': prod_info
                    },
                    'pending': []
                }
            }
        else:
            msg += tabulate(info['info'], headers='firstrow',
                            tablefmt='psql') + '\n'
            msg += tabulate(info['prod'], headers='firstrow',
                            tablefmt='psql') + '\n'
        # do we have some pending release(s)
        if 'pend' in info and len(info['pend']) > 1:
            if options.json:
                msg['bank']['pending'] = info['pend']
            else:
                msg += tabulate(
                    info['pend'], headers='firstrow', tablefmt='psql') + '\n'
    else:
        banks = Bank.list()
        # Headers of output table
        banks_list = []
        headers = ["Name", "Type(s)", "Release", "Visibility", "Last update"]
        if not options.json:
            banks_list.append(headers)
        for bank in sorted(banks, key=lambda k: k['name']):
            try:
                bank = Bank(bank['name'], options=options, no_log=True)
                if bank.bank['properties'][
                        'visibility'] == 'public' or bank.is_owner():
                    banks_list.append(bank.get_bank_release_info()['info'])
            except Exception as e:
                logging.error('Failed to load bank %s: %s' %
                              (bank['name'], str(e)))
        if options.json:
            msg = {'headers': headers, 'banks': banks_list}
        else:
            msg += tabulate(banks_list, headers="firstrow",
                            tablefmt="psql") + '\n'
    return (True, msg)