environments_controller.rb 4.58 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
  before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
8
  before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
9
  before_action :verify_api_request!, only: :terminal_websocket_authorize
Kamil Trzcinski's avatar
Kamil Trzcinski committed
10 11

  def index
12 13
    @environments = project.environments
      .with_state(params[:scope] || :available)
14

15 16 17
    respond_to do |format|
      format.html
      format.json do
18 19
        Gitlab::PollingInterval.set_header(response, interval: 3_000)

20 21
        render json: {
          environments: EnvironmentSerializer
Fatih Acet's avatar
Fatih Acet committed
22
            .new(project: @project, current_user: @current_user)
23 24 25 26 27 28
            .with_pagination(request, response)
            .within_folders
            .represent(@environments),
          available_count: project.environments.available.count,
          stopped_count: project.environments.stopped.count
        }
29
      end
30
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
31 32
  end

33
  def folder
34 35
    folder_environments = project.environments.where(environment_type: params[:id])
    @environments = folder_environments.with_state(params[:scope] || :available)
36
      .order(:name)
37
    @folder = params[:id]
38 39 40 41 42 43

    respond_to do |format|
      format.html
      format.json do
        render json: {
          environments: EnvironmentSerializer
Fatih Acet's avatar
Fatih Acet committed
44
            .new(project: @project, current_user: @current_user)
45 46
            .with_pagination(request, response)
            .represent(@environments),
47 48
          available_count: folder_environments.available.count,
          stopped_count: folder_environments.stopped.count
49 50 51 52 53
        }
      end
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
54
  def show
Kamil Trzcinski's avatar
Kamil Trzcinski committed
55
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
56 57 58 59 60 61
  end

  def new
    @environment = project.environments.new
  end

62 63 64
  def edit
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
65
  def create
66
    @environment = project.environments.create(environment_params)
67 68

    if @environment.persisted?
69
      redirect_to project_environment_path(project, @environment)
70
    else
71 72 73 74 75 76
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
77
      redirect_to project_environment_path(project, @environment)
78 79
    else
      render :edit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
80 81 82
    end
  end

83
  def stop
84
    return render_404 unless @environment.available?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
85

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

Fatih Acet's avatar
Fatih Acet committed
88 89 90 91
    action_or_env_url =
      if stop_action
        polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
      else
92
        project_environment_url(project, @environment)
Fatih Acet's avatar
Fatih Acet committed
93 94 95 96 97
      end

    respond_to do |format|
      format.html { redirect_to action_or_env_url }
      format.json { render json: { redirect_url: action_or_env_url } }
98
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
99 100
  end

101 102 103 104 105 106 107 108 109 110 111
  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
112
    # should retry those errors
113 114 115 116 117 118 119 120 121
    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

122 123 124 125 126 127 128 129 130 131 132 133 134
  def metrics
    # Currently, this acts as a hint to load the metrics details into the cache
    # if they aren't there already
    @metrics = environment.metrics || {}

    respond_to do |format|
      format.html
      format.json do
        render json: @metrics, status: @metrics.any? ? :ok : :no_content
      end
    end
  end

135
  def additional_metrics
136 137 138
    respond_to do |format|
      format.json do
        additional_metrics = environment.additional_metrics || {}
139

140 141 142
        render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
      end
    end
143 144
  end

145 146
  private

147 148 149 150
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

151 152
  def environment_params
    params.require(:environment).permit(:name, :external_url)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
153 154
  end

155
  def environment
Kamil Trzcinski's avatar
Kamil Trzcinski committed
156
    @environment ||= project.environments.find(params[:id])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
157 158
  end
end