Ejemplo n.º 1
0
def edit_mirror(mirror_id: int):
    mirror_detail = PushMirror.query.filter_by(
        id=mirror_id, user=current_user).first_or_404()
    form = EditForm(flask.request.form,
                    id=mirror_detail.id,
                    project_mirror=mirror_detail.project_mirror,
                    note=mirror_detail.note,
                    is_force_update=mirror_detail.is_force_update,
                    is_prune_mirrors=mirror_detail.is_prune_mirrors,
                    project=mirror_detail.project.gitlab_id)
    if flask.request.method == 'POST' and form.validate():
        project_mirror_str = form.project_mirror.data.strip()
        project_mirror = GitRemote(project_mirror_str)
        target = GitRemote(project_mirror_str)
        if target.vcs_protocol == ProtocolEnum.SSH:
            # If protocol is SSH we need to convert URL to use USER RSA pair
            target = GitRemote(
                convert_url_for_user(project_mirror_str, current_user))

        # PullMirror
        mirror_detail.project_mirror = project_mirror_str
        mirror_detail.project = process_project(form.project.data)

        # Mirror
        mirror_detail.is_force_update = form.is_force_update.data
        mirror_detail.is_prune_mirrors = form.is_prune_mirrors.data
        mirror_detail.is_deleted = False
        mirror_detail.user = current_user
        mirror_detail.foreign_vcs_type = target.vcs_type
        mirror_detail.note = form.note.data
        mirror_detail.target = target
        mirror_detail.source = None  # We are getting source wia gitlab API

        db.session.add(mirror_detail)
        db.session.commit()
        if target.vcs_protocol == ProtocolEnum.SSH:
            # If source is SSH, create SSH Config for it also

            task_result = chain(
                create_ssh_config.si(current_user.id, target.hostname,
                                     project_mirror.hostname),
                save_push_mirror.si(mirror_detail.id)).apply_async()

            parent = log_task_pending(task_result.parent, mirror_detail,
                                      create_ssh_config, InvokedByEnum.MANUAL)
            log_task_pending(task_result, mirror_detail, save_push_mirror,
                             InvokedByEnum.MANUAL, parent)
        else:
            task = save_push_mirror.delay(mirror_detail.id)
            log_task_pending(task, mirror_detail, save_push_mirror,
                             InvokedByEnum.MANUAL)

        flask.flash('Push mirror was saved successfully.', 'success')
        return flask.redirect(flask.url_for('push_mirror.index.get_mirror'))

    return flask.render_template('push_mirror.index.edit.html',
                                 form=form,
                                 mirror_detail=mirror_detail)
Ejemplo n.º 2
0
def schedule_sync_mirror(mirror_id: int):
    # Check if mirror exists or throw 404
    found_mirror = PullMirror.query.filter_by(id=mirror_id, user=current_user).first_or_404()
    if not found_mirror.project_id:
        flask.flash('Project mirror is not created, cannot be synced', 'danger')
        return flask.redirect(flask.url_for('pull_mirror.index.get_mirror'))
    task = sync_pull_mirror.delay(mirror_id)
    log_task_pending(task, found_mirror, sync_pull_mirror, InvokedByEnum.MANUAL)

    flask.flash('Sync has been started with UUID: {}'.format(task.id), 'success')
    return flask.redirect(flask.url_for('pull_mirror.index.get_mirror'))
Ejemplo n.º 3
0
def new_mirror():
    form = NewForm(flask.request.form,
                   is_force_update=False,
                   is_prune_mirrors=False)
    if flask.request.method == 'POST' and form.validate():
        project_mirror_str = form.project_mirror.data.strip()
        project_mirror = GitRemote(project_mirror_str)
        target = GitRemote(project_mirror_str)
        if target.vcs_protocol == ProtocolEnum.SSH:
            # If protocol is SSH we need to convert URL to use USER RSA pair
            target = GitRemote(
                convert_url_for_user(project_mirror_str, current_user))

        mirror_new = PushMirror()
        # PushMirror
        mirror_new.project_mirror = project_mirror_str
        mirror_new.project = process_project(form.project.data)

        # Mirror
        mirror_new.is_force_update = form.is_force_update.data
        mirror_new.is_prune_mirrors = form.is_prune_mirrors.data
        mirror_new.is_deleted = False
        mirror_new.user = current_user
        mirror_new.foreign_vcs_type = target.vcs_type
        mirror_new.note = form.note.data
        mirror_new.target = target.url
        mirror_new.source = None  # We are getting source wia gitlab API
        mirror_new.last_sync = None
        mirror_new.hook_token = random_password()

        db.session.add(mirror_new)
        db.session.commit()

        if target.vcs_protocol == ProtocolEnum.SSH:
            # If target is SSH, create SSH Config for it also
            task_result = chain(
                create_ssh_config.si(current_user.id, target.hostname,
                                     project_mirror.hostname),
                save_push_mirror.si(mirror_new.id)).apply_async()

            parent = log_task_pending(task_result.parent, mirror_new,
                                      create_ssh_config, InvokedByEnum.MANUAL)
            log_task_pending(task_result, mirror_new, save_push_mirror,
                             InvokedByEnum.MANUAL, parent)
        else:
            task = save_push_mirror.delay(mirror_new.id)
            log_task_pending(task, mirror_new, save_push_mirror,
                             InvokedByEnum.MANUAL)

        flask.flash('New push mirror item was added successfully.', 'success')
        return flask.redirect(flask.url_for('push_mirror.index.get_mirror'))

    return flask.render_template('push_mirror.index.new.html', form=form)
Ejemplo n.º 4
0
def schedule_sync_push_mirror(mirror_id: int):
    hook_token = request.args.get('token')
    if not hook_token:
        return jsonify({'message': 'Token was not provided'}), 400

    found_mirror = PushMirror.query.filter_by(id=mirror_id).first()
    if not found_mirror:
        return jsonify({'message': 'Mirror not found'}), 404

    if found_mirror.hook_token != hook_token:
        return jsonify({'message': 'Supplied token was incorrect'}), 400

    if not found_mirror.project_id:
        return jsonify(
            {'message':
             'Project mirror is not created, cannot be synced'}), 400

    task = sync_push_mirror.delay(found_mirror.id)
    log_task_pending(task, found_mirror, sync_push_mirror, InvokedByEnum.HOOK)

    return jsonify({'message': 'Sync task started', 'uuid': task.id}), 200
Ejemplo n.º 5
0
def edit_mirror(mirror_id: int):
    mirror_detail = PullMirror.query.filter_by(id=mirror_id, user=current_user).first_or_404()
    form = EditForm(
        flask.request.form,
        id=mirror_detail.id,
        project_name=mirror_detail.project_name,
        project_mirror=mirror_detail.project_mirror,
        note=mirror_detail.note,
        is_no_create=mirror_detail.is_no_create,
        is_force_create=mirror_detail.is_force_create,
        is_no_remote=mirror_detail.is_no_remote,
        is_issues_enabled=mirror_detail.is_issues_enabled,
        is_wall_enabled=mirror_detail.is_wall_enabled,
        is_wiki_enabled=mirror_detail.is_wiki_enabled,
        is_snippets_enabled=mirror_detail.is_snippets_enabled,
        is_merge_requests_enabled=mirror_detail.is_merge_requests_enabled,
        visibility=mirror_detail.visibility,
        is_force_update=mirror_detail.is_force_update,
        is_prune_mirrors=mirror_detail.is_prune_mirrors,
        group=mirror_detail.group.gitlab_id,
        periodic_sync=mirror_detail.periodic_sync,
        is_jobs_enabled=mirror_detail.is_jobs_enabled
    )
    if flask.request.method == 'POST' and form.validate():
        project_mirror_str = form.project_mirror.data.strip()
        project_mirror = GitRemote(project_mirror_str)
        source = GitRemote(project_mirror_str)
        if source.vcs_protocol == ProtocolEnum.SSH:
            # If protocol is SSH we need to convert URL to use USER RSA pair
            source = GitRemote(convert_url_for_user(project_mirror_str, current_user))

        # PullMirror
        mirror_detail.project_name = form.project_name.data
        mirror_detail.project_mirror = project_mirror_str
        mirror_detail.is_no_create = form.is_no_create.data
        mirror_detail.is_force_create = form.is_force_create.data
        mirror_detail.is_no_remote = form.is_no_remote.data
        mirror_detail.is_issues_enabled = form.is_issues_enabled.data
        mirror_detail.is_wall_enabled = form.is_wall_enabled.data
        mirror_detail.is_wiki_enabled = form.is_wiki_enabled.data
        mirror_detail.is_snippets_enabled = form.is_snippets_enabled.data
        mirror_detail.is_jobs_enabled = form.is_jobs_enabled.data
        mirror_detail.is_merge_requests_enabled = form.is_merge_requests_enabled.data
        mirror_detail.visibility = form.visibility.data
        mirror_detail.group = process_group(form.group.data)
        mirror_detail.periodic_sync = form.periodic_sync.data

        # Mirror
        mirror_detail.is_force_update = form.is_force_update.data
        mirror_detail.is_prune_mirrors = form.is_prune_mirrors.data
        mirror_detail.is_deleted = False
        mirror_detail.user = current_user
        mirror_detail.foreign_vcs_type = source.vcs_type
        mirror_detail.note = form.note.data
        mirror_detail.target = None  # We are getting target wia gitlab API
        mirror_detail.source = source.url

        if process_cron_expression(mirror_detail):
            PeriodicTasks.changed()

        db.session.add(mirror_detail)
        db.session.flush()
        db.session.commit()

        if source.vcs_protocol == ProtocolEnum.SSH:
            # If source is SSH, create SSH Config for it also
            task_result = chain(
                create_ssh_config.si(
                    current_user.id,
                    source.hostname,
                    project_mirror.hostname
                ),
                save_pull_mirror.si(
                    mirror_detail.id
                )
            ).apply_async()

            parent = log_task_pending(task_result.parent, mirror_detail, create_ssh_config, InvokedByEnum.MANUAL)
            log_task_pending(task_result, mirror_detail, save_pull_mirror, InvokedByEnum.MANUAL, parent)
        else:
            task = save_pull_mirror.delay(mirror_detail.id)
            log_task_pending(task, mirror_detail, save_pull_mirror, InvokedByEnum.MANUAL)

        flask.flash('Pull mirror was saved successfully.', 'success')
        return flask.redirect(flask.url_for('pull_mirror.index.get_mirror'))

    return flask.render_template('pull_mirror.index.edit.html', form=form, mirror_detail=mirror_detail)
Ejemplo n.º 6
0
def new_mirror():
    form = NewForm(
        flask.request.form,
        is_no_create=False,
        is_force_create=False,
        is_no_remote=False,
        is_issues_enabled=False,
        is_wall_enabled=False,
        is_wiki_enabled=False,
        is_snippets_enabled=False,
        is_merge_requests_enabled=False,
        visibility=PullMirror.VISIBILITY_PRIVATE,
        is_force_update=False,
        is_prune_mirrors=False,
        is_jobs_enabled=True
    )
    if flask.request.method == 'POST' and form.validate():
        project_mirror_str = form.project_mirror.data.strip()
        project_mirror = GitRemote(project_mirror_str)
        source = GitRemote(project_mirror_str)
        if source.vcs_protocol == ProtocolEnum.SSH:
            # If protocol is SSH we need to convert URL to use USER RSA pair
            source = GitRemote(convert_url_for_user(project_mirror_str, current_user))

        mirror_new = PullMirror()
        # PullMirror
        mirror_new.project_name = form.project_name.data
        mirror_new.project_mirror = project_mirror_str
        mirror_new.is_no_create = form.is_no_create.data
        mirror_new.is_force_create = form.is_force_create.data
        mirror_new.is_no_remote = form.is_no_remote.data
        mirror_new.is_issues_enabled = form.is_issues_enabled.data
        mirror_new.is_wall_enabled = form.is_wall_enabled.data
        mirror_new.is_wiki_enabled = form.is_wiki_enabled.data
        mirror_new.is_snippets_enabled = form.is_snippets_enabled.data
        mirror_new.is_jobs_enabled = form.is_jobs_enabled.data
        mirror_new.is_merge_requests_enabled = form.is_merge_requests_enabled.data
        mirror_new.visibility = form.visibility.data
        mirror_new.group = process_group(form.group.data)
        mirror_new.periodic_sync = form.periodic_sync.data

        # Mirror
        mirror_new.is_force_update = form.is_force_update.data
        mirror_new.is_prune_mirrors = form.is_prune_mirrors.data
        mirror_new.is_deleted = False
        mirror_new.user = current_user
        mirror_new.foreign_vcs_type = source.vcs_type
        mirror_new.note = form.note.data
        mirror_new.target = None  # We are getting target wia gitlab API
        mirror_new.source = source.url
        mirror_new.last_sync = None
        mirror_new.hook_token = random_password()

        if process_cron_expression(mirror_new):
            PeriodicTasks.changed()

        db.session.add(mirror_new)
        db.session.commit()

        if source.vcs_protocol == ProtocolEnum.SSH:
            # If source is SSH, create SSH Config for it also
            task_result = chain(
                create_ssh_config.si(
                    current_user.id,
                    source.hostname,
                    project_mirror.hostname
                ),
                save_pull_mirror.si(
                    mirror_new.id
                )
            ).apply_async()

            parent = log_task_pending(task_result.parent, mirror_new, create_ssh_config, InvokedByEnum.MANUAL)
            log_task_pending(task_result, mirror_new, save_pull_mirror, InvokedByEnum.MANUAL, parent)
        else:
            task = save_pull_mirror.delay(mirror_new.id)
            log_task_pending(task, mirror_new, save_pull_mirror, InvokedByEnum.MANUAL)

        flask.flash('New pull mirror item was added successfully.', 'success')
        return flask.redirect(flask.url_for('pull_mirror.index.get_mirror'))

    return flask.render_template('pull_mirror.index.new.html', form=form)
Ejemplo n.º 7
0
def sync_pull_mirror_cron(pull_mirror_id: int) -> None:
    pull_mirror = PullMirror.query.filter_by(id=pull_mirror_id).first()

    task = sync_pull_mirror.delay(pull_mirror_id)
    log_task_pending(task, pull_mirror, sync_pull_mirror, InvokedByEnum.SCHEDULER)