Example #1
0
def edit_repo(repo_id):
    if not request.user.can_manage:
        return abort(403)
    if repo_id is not None:
        repo = Repository.get(id=repo_id)
    else:
        repo = None

    errors = []

    context = {}
    if repo and repo.name:
        if os.path.isfile(utils.get_repo_public_key_path(repo)):
            try:
                context['public_key'] = file_utils.read_file(
                    utils.get_repo_public_key_path(repo))
            except BaseException as exc:
                app.logger.info(exc)
                errors.append('Could not read public key for this repository.')

    if request.method == 'POST':
        secret = request.form.get('repo_secret', '')
        clone_url = request.form.get('repo_clone_url', '')
        repo_name = request.form.get('repo_name', '').strip()
        ref_whitelist = request.form.get('repo_ref_whitelist', '')
        build_script = request.form.get('repo_build_script', '')
        if len(repo_name) < 3 or repo_name.count('/') != 1:
            errors.append('Invalid repository name. Format must be owner/repo')
        if not clone_url:
            errors.append('No clone URL specified')
        other = Repository.get(name=repo_name)
        if (other and not repo) or (other and other.id != repo.id):
            errors.append('Repository {!r} already exists'.format(repo_name))
        if not errors:
            if not repo:
                repo = Repository(name=repo_name,
                                  clone_url=clone_url,
                                  secret=secret,
                                  build_count=0,
                                  ref_whitelist=ref_whitelist)
            else:
                repo.name = repo_name
                repo.clone_url = clone_url
                repo.secret = secret
                repo.ref_whitelist = ref_whitelist
            try:
                utils.write_override_build_script(repo, build_script)
            except BaseException as exc:
                app.logger.info(exc)
                errors.append('Could not make change on build script')
            if not errors:
                return redirect(repo.url())

    return render_template('edit_repo.html',
                           user=request.user,
                           repo=repo,
                           errors=errors,
                           **context)
Example #2
0
def generate_keypair(path):
    if not request.user.can_manage:
        return abort(403)

    repo = get_target_for(path)
    if not isinstance(repo, Repository):
        return abort(404)

    session['errors'] = []

    utils.makedirs(utils.get_customs_path(repo))

    try:
        private_key, public_key = utils.generate_ssh_keypair(repo.name +
                                                             '@FluxCI')
        private_key_path = utils.get_repo_private_key_path(repo)
        public_key_path = utils.get_repo_public_key_path(repo)

        try:
            file_utils.create_file_path(private_key_path)
            file_utils.create_file_path(public_key_path)

            file_utils.write_file(private_key_path, private_key)
            file_utils.write_file(public_key_path, public_key)

            try:
                os.chmod(private_key_path, 0o600)
                utils.flash('SSH keypair generated.')
            except BaseException as exc:
                app.logger.info(exc)
                session['errors'].append(
                    'Could not set permissions to newly generated private key.'
                )
        except BaseException as exc:
            app.logger.info(exc)
            session['errors'].append('Could not save generated SSH keypair.')
    except BaseException as exc:
        app.logger.info(exc)
        session['errors'].append('Could not generate new SSH keypair.')

    return redirect(url_for('edit_repo', repo_id=repo.id))
Example #3
0
def remove_keypair(path):
    if not request.user.can_manage:
        return abort(403)

    repo = get_target_for(path)
    if not isinstance(repo, Repository):
        return abort(404)

    session['errors'] = []

    private_key_path = utils.get_repo_private_key_path(repo)
    public_key_path = utils.get_repo_public_key_path(repo)

    try:
        file_utils.delete(private_key_path)
        file_utils.delete(public_key_path)
        utils.flash('SSH keypair removed.')
    except BaseException as exc:
        app.logger.info(exc)
        session['errors'].append('Could not remove SSH keypair.')

    return redirect(url_for('edit_repo', repo_id=repo.id))