Beispiel #1
0
def overrides_delete(path):
    if not request.user.can_manage:
        return abort(403)

    separator = "/"

    repo_path, overrides_path = file_utils.split_url_path(path)
    repo = get_target_for(repo_path)
    if not isinstance(repo, Repository):
        return abort(404)

    return_path_parts = path.split(separator)
    return_path = separator.join(return_path_parts[:-1])
    cwd = os.path.join(utils.get_override_path(repo),
                       overrides_path.replace('/', os.sep))

    session['errors'] = []
    try:
        file_utils.delete(cwd)
        utils.flash('Object was deleted.')
    except BaseException as exc:
        app.logger.info(exc)
        session['errors'].append('Could not delete \'' +
                                 return_path_parts[-1] + '\'.')

    return redirect(url_for('overrides_list', path=return_path))
Beispiel #2
0
def view_repo(path):
    repo = get_target_for(path)
    if not isinstance(repo, Repository):
        return abort(404)

    context = {}
    page_size = 10

    try:
        context['page_number'] = int(request.args.get('page', 1))
    except ValueError:
        context['page_number'] = 1

    page_from = (context['page_number'] - 1) * page_size
    page_to = page_from + page_size

    context['next_page'] = None if context[
        'page_number'] <= 1 else context['page_number'] - 1
    context['previous_page'] = None if len(
        repo.builds) <= page_to else context['page_number'] + 1
    context['builds'] = repo.builds.select().order_by(desc(
        Build.date_queued))[page_from:page_to]
    return render_template('view_repo.html',
                           user=request.user,
                           repo=repo,
                           **context)
Beispiel #3
0
def view_build(path):
    build = get_target_for(path)
    if not isinstance(build, Build):
        return abort(404)

    restart = request.args.get('restart', '').strip().lower() == 'true'
    if restart:
        if build.status != Build.Status_Building:
            build.delete_build()
            build.status = Build.Status_Queued
            build.date_started = None
            build.date_finished = None
            models.commit()
            enqueue(build)
        return redirect(build.url())

    stop = request.args.get('stop', '').strip().lower() == 'true'
    if stop:
        if build.status == Build.Status_Queued:
            build.status = Build.Status_Stopped
        elif build.status == Build.Status_Building:
            terminate_build(build)
        return redirect(build.url())

    return render_template('view_build.html', user=request.user, build=build)
Beispiel #4
0
def overrides_edit(path):
  if not request.user.can_manage:
    return abort(403)

  separator = "/"
  context = {}
  errors = []

  repo_path, context['overrides_path'] = file_utils.split_url_path(path)
  context['repo'] = get_target_for(repo_path)
  if not isinstance(context['repo'], Repository):
    return abort(404)

  file_path = os.path.join(utils.get_override_path(context['repo']), context['overrides_path'].replace('/', os.sep))

  if request.method == 'POST':
    override_content = request.form.get('override_content')
    try:
      file_utils.write_file(file_path, override_content)
      utils.flash('Changes in file was saved.')
    except BaseException as exc:
      app.logger.info(exc)
      errors.append('Could not write into file.')

  dir_path_parts = path.split("/")
  context['dir_path'] = separator.join(dir_path_parts[:-1])

  try:
    context['content'] = file_utils.read_file(file_path)
  except BaseException as exc:
    app.logger.info(exc)
    errors.append('Could not read file.')

  return render_template('overrides_edit.html', user=request.user, **context, errors=errors)
Beispiel #5
0
def overrides_list(path):
  if not request.user.can_manage:
    return abort(403)

  separator = "/"
  errors = []
  errors = errors + session.pop('errors', [])
  context = {}

  repo_path, context['overrides_path'] = file_utils.split_url_path(path)
  context['repo'] = get_target_for(repo_path)
  context['list_path'] = url_for('overrides_list', path = path)
  context['edit_path'] = url_for('overrides_edit', path = path)
  context['delete_path'] = url_for('overrides_delete', path = path)
  context['download_path'] = url_for('overrides_download', path = path)
  context['upload_path'] = url_for('overrides_upload', path = path)
  if context['overrides_path'] != '' and context['overrides_path'] != None:
    context['parent_name'] = separator.join(context['overrides_path'].split(separator)[:-1])
    context['parent_path'] = url_for('overrides_list', path = separator.join(path.split(separator)[:-1]))

  if not isinstance(context['repo'], Repository):
    return abort(404)

  try:
    cwd = os.path.join(utils.get_override_path(context['repo']), context['overrides_path'].replace('/', os.sep))
    utils.makedirs(os.path.dirname(cwd))
    context['files'] = file_utils.list_folder(cwd)
  except BaseException as exc:
    app.logger.info(exc)
    errors.append('Could not read overrides for this repository.')

  return render_template('overrides_list.html', user=request.user, **context, errors=errors)
Beispiel #6
0
def overrides_download(path):
  if not request.user.can_manage:
    return abort(403)

  repo_path, overrides_path = file_utils.split_url_path(path)
  repo = get_target_for(repo_path)
  if not isinstance(repo, Repository):
    return abort(404)

  file_path = os.path.join(utils.get_override_path(repo), overrides_path.replace('/', os.sep))
  return utils.stream_file(file_path, mime='application/octet-stream')
Beispiel #7
0
def overrides_upload(path):
    if not request.user.can_manage:
        return abort(403)

    separator = "/"
    context = {}
    errors = [] + session.pop('errors', [])

    repo_path, context['overrides_path'] = file_utils.split_url_path(path)
    repo = get_target_for(repo_path)
    if not isinstance(repo, Repository):
        return abort(404)

    context['list_path'] = url_for('overrides_list', path=path)
    cwd = os.path.join(utils.get_override_path(repo),
                       context['overrides_path'].replace('/', os.sep))

    if request.method == 'POST':
        session['errors'] = []
        files = request.files.getlist('upload_file')
        if not files:
            utils.flash('No file was uploaded.')
        else:
            file_uploads = []
            for file in files:
                filepath = os.path.join(cwd, secure_filename(file.filename))
                try:
                    file.save(filepath)
                    file_uploads.append("File '{}' was uploaded.".format(
                        file.filename))
                except BaseException as exc:
                    app.logger.info(exc)
                    session['errors'].append("Could not upload '{}'.".format(
                        file.filename))
            utils.flash(" ".join(file_uploads))
            if not session['errors']:
                return redirect(url_for('overrides_list', path=path))

    dir_path_parts = path.split("/")
    context['dir_path'] = separator.join(dir_path_parts[:-1])

    return render_template('overrides_upload.html',
                           user=request.user,
                           **context,
                           errors=errors)
Beispiel #8
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))
Beispiel #9
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))