environments_controller.rb 2.92 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
3
  before_action :authorize_read_environment!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
4
  before_action :authorize_create_environment!, only: [:new, :create]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
5 6
  before_action :authorize_create_deployment!, only: [:stop]
  before_action :authorize_update_environment!, only: [:edit, :update]
7 8 9
  before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
  before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize]
  before_action :verify_api_request!, only: :terminal_websocket_authorize
Kamil Trzcinski's avatar
Kamil Trzcinski committed
10 11

  def index
12
    @scope = params[:scope]
13
    @environments = project.environments
14

15 16 17
    respond_to do |format|
      format.html
      format.json do
18
        render json: EnvironmentSerializer
19
          .new(project: @project, user: current_user)
20
          .represent(@environments)
21
      end
22
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
23 24 25
  end

  def show
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
27 28 29 30 31 32
  end

  def new
    @environment = project.environments.new
  end

33 34 35
  def edit
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
36
  def create
37
    @environment = project.environments.create(environment_params)
38 39 40 41

    if @environment.persisted?
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
42 43 44 45 46 47 48 49 50
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
      render :edit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
51 52 53
    end
  end

54
  def stop
55
    return render_404 unless @environment.available?
56

Kamil Trzcinski's avatar
Kamil Trzcinski committed
57 58
    stop_action = @environment.stop_with_action!(current_user)

59 60 61 62 63
    if stop_action
      redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, stop_action])
    else
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
64 65
  end

66 67 68 69 70 71 72 73 74 75 76
  def terminal
    # Currently, this acts as a hint to load the terminal details into the cache
    # if they aren't there already. In the future, users will need these details
    # to choose between terminals to connect to.
    @terminals = environment.terminals
  end

  # GET .../terminal.ws : implemented in gitlab-workhorse
  def terminal_websocket_authorize
    # Just return the first terminal for now. If the list is in the process of
    # being looked up, this may result in a 404 response, so the frontend
77
    # should retry those errors
78 79 80 81 82 83 84 85 86
    terminal = environment.terminals.try(:first)
    if terminal
      set_workhorse_internal_api_content_type
      render json: Gitlab::Workhorse.terminal_websocket(terminal)
    else
      render text: 'Not found', status: 404
    end
  end

87 88
  private

89 90 91 92
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

93 94
  def environment_params
    params.require(:environment).permit(:name, :external_url)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
95 96
  end

97
  def environment
Kamil Trzcinski's avatar
Kamil Trzcinski committed
98
    @environment ||= project.environments.find(params[:id])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
99 100
  end
end