Ejemplo n.º 1
0
def gitlab_job(token):
    payload = request.get_json()
    object_type = payload.get('object_type')
    tag = payload.get('tag')
    job_id = payload.get('job_id')
    project_id = payload.get('project_id')
    job_status = payload.get('job_status')
    repository = payload.get('repository')
    record = Job().collection.find_one({'token': token})
    if not record:
        return jsonify({'message': 'invalid token', 'code': 104010}), 401

    if object_type != 'job':
        return jsonify({
            'message': 'only job event allow',
            'code': 104031
        }), 403

    if not job_id or not project_id or job_status != 'success':
        return jsonify({'message': 'invalid params', 'code': 104000}), 400

    if not tag:
        return jsonify({'message': 'only tag allow', 'code': 104032}), 403

    app_info = db.collection('apps').find_one(
        {'_id': ObjectId(record.get('app_id'))})
    if not app_info:
        return jsonify({
            'message': 'job must be bind with gitlab app',
            'code': 104002
        }), 400
    params = app_info.get('params') or {}
    if params.get('project_id') != project_id:
        return jsonify({'message': 'illegal project', 'code': 104003}), 400

    # if params.get('extract') == 'artifacts':
    #     wk = Workspace()
    #     filename = wk.get_gitlab_artifacts_file(record.get('name'), project_id, job_id)
    #     gitlab = GitlabApi().dowload_artifact(project_id, job_id, filename)
    #
    # if params.get('extract') == 'docker':
    #     pass
    integration = Integration(app_info.get('type'), params)
    integration.install()

    ansible_params = load_ansible_playbook(record)
    if ansible_params.get('message') is not 'ok':
        return jsonify(payload), 400

    data = ansible_params.get('data')
    wk = Workspace()
    res = wk.load_book_from_db(name=data.get('book_name'),
                               roles=data.get('roles'))
    if not res:
        return jsonify({
            'message': 'load book failed',
            'code': 104000,
        }), 400

    return jsonify({'message': 'ok', 'code': 0, 'data': 1})
Ejemplo n.º 2
0
def job_webhook():
    query = request.args
    token = query.get('token')
    payload = request.get_json()
    if not payload or not token:
        return jsonify({'message': 'invalid params', 'code': 104000}), 400

    record = Job().collection.find_one({'token': token})
    if not record:
        return jsonify({'message': 'illegal token', 'code': 104010}), 401

    if record.get('type') == 'adhoc':
        task_id = run_job(str(record.get('_id')))
        if not task_id:
            return jsonify({
                'message': 'try to queue task faield',
                'code': 104008
            }), 400

        return jsonify({'message': 'ok', 'code': 0, 'data': task_id})

    template = record.get('template')
    app_id = template.get('app')
    income_params = {'cache': True}
    if app_id:
        app_info = Application.find_by_id(app_id)
        if not app_info:
            return jsonify({
                'message': 'app not found, please check your app',
                'code': 104001
            }), 400

        app_params = app_info.get('params')
        income = app_params.get('income')
        if income and payload.get('income'):
            income = Template(income)
            tpl = income.render(**payload.get('income'))
            tpl = yaml.safe_load(tpl)
            if tpl:
                income_params.update(tpl)

    task_id = run_job(str(record.get('_id')), **income_params)
    if not task_id:
        return jsonify({
            'message': 'put job enqueue failed',
            'code': 104002
        }), 400

    # if app_type == 'jenkins':
    #     build_id = '19'
    #     job_name = 'upward'
    #     run_job(_id, job_name, build_id)
    # elif app_type == 'gitlab':
    #     project_id = '13539397'
    #     job_id = '261939258'
    #     run_job(_id, project_id, job_id)
    # else:
    #     run_job(_id)

    # logger.error('test', extra={'a': {'b': 1}})

    return jsonify({'message': 'ok', 'code': 0, 'data': task_id})