environments_controller.rb 5.4 KB
Newer Older
1 2
# frozen_string_literal: true

K
Kamil Trzcinski 已提交
3 4
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
5
  before_action :authorize_read_environment!
K
Kamil Trzcinski 已提交
6
  before_action :authorize_create_environment!, only: [:new, :create]
7
  before_action :authorize_stop_environment!, only: [:stop]
K
Kamil Trzcinski 已提交
8
  before_action :authorize_update_environment!, only: [:edit, :update]
9
  before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
10
  before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
11
  before_action :verify_api_request!, only: :terminal_websocket_authorize
12
  before_action :expire_etag_cache, only: [:index]
K
Kamil Trzcinski 已提交
13 14

  def index
15 16
    @environments = project.environments
      .with_state(params[:scope] || :available)
17

18 19 20
    respond_to do |format|
      format.html
      format.json do
21 22
        Gitlab::PollingInterval.set_header(response, interval: 3_000)

23 24
        render json: {
          environments: EnvironmentSerializer
F
Fatih Acet 已提交
25
            .new(project: @project, current_user: @current_user)
26 27 28 29 30 31
            .with_pagination(request, response)
            .within_folders
            .represent(@environments),
          available_count: project.environments.available.count,
          stopped_count: project.environments.stopped.count
        }
K
Kamil Trzcinski 已提交
32
      end
33
    end
K
Kamil Trzcinski 已提交
34 35
  end

36
  # rubocop: disable CodeReuse/ActiveRecord
37
  def folder
38 39
    folder_environments = project.environments.where(environment_type: params[:id])
    @environments = folder_environments.with_state(params[:scope] || :available)
40
      .order(:name)
F
Filipa Lacerda 已提交
41
    @folder = params[:id]
42 43 44 45 46 47

    respond_to do |format|
      format.html
      format.json do
        render json: {
          environments: EnvironmentSerializer
F
Fatih Acet 已提交
48
            .new(project: @project, current_user: @current_user)
49 50
            .with_pagination(request, response)
            .represent(@environments),
51 52
          available_count: folder_environments.available.count,
          stopped_count: folder_environments.stopped.count
53 54 55 56
        }
      end
    end
  end
57
  # rubocop: enable CodeReuse/ActiveRecord
58

59
  # rubocop: disable CodeReuse/ActiveRecord
K
Kamil Trzcinski 已提交
60
  def show
S
Shinya Maeda 已提交
61
    @deployments = environment.deployments.deployed.order(id: :desc).page(params[:page])
K
Kamil Trzcinski 已提交
62
  end
63
  # rubocop: enable CodeReuse/ActiveRecord
K
Kamil Trzcinski 已提交
64 65 66 67 68

  def new
    @environment = project.environments.new
  end

69 70 71
  def edit
  end

K
Kamil Trzcinski 已提交
72
  def create
73
    @environment = project.environments.create(environment_params)
K
Kamil Trzcinski 已提交
74 75

    if @environment.persisted?
76
      redirect_to project_environment_path(project, @environment)
K
Kamil Trzcinski 已提交
77
    else
78 79 80 81 82 83
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
84
      redirect_to project_environment_path(project, @environment)
85 86
    else
      render :edit
K
Kamil Trzcinski 已提交
87 88 89
    end
  end

90
  def stop
K
Kamil Trzcinski 已提交
91
    return render_404 unless @environment.available?
K
Kamil Trzcinski 已提交
92

K
Kamil Trzcinski 已提交
93 94
    stop_action = @environment.stop_with_action!(current_user)

F
Fatih Acet 已提交
95 96 97 98
    action_or_env_url =
      if stop_action
        polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
      else
99
        project_environment_url(project, @environment)
F
Fatih Acet 已提交
100 101 102 103 104
      end

    respond_to do |format|
      format.html { redirect_to action_or_env_url }
      format.json { render json: { redirect_url: action_or_env_url } }
K
Kamil Trzcinski 已提交
105
    end
K
Kamil Trzcinski 已提交
106 107
  end

108 109 110 111 112 113 114 115 116 117 118
  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
119
    # should retry those errors
120 121 122 123 124
    terminal = environment.terminals.try(:first)
    if terminal
      set_workhorse_internal_api_content_type
      render json: Gitlab::Workhorse.terminal_websocket(terminal)
    else
L
Lin Jen-Shin 已提交
125
      render text: 'Not found', status: :not_found
126 127 128
    end
  end

129
  def metrics_redirect
130
    environment = project.default_environment
131

132 133 134 135 136
    if environment
      redirect_to environment_metrics_path(environment)
    else
      render :empty
    end
137 138
  end

139 140 141 142 143 144 145 146 147 148 149 150 151
  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

152
  def additional_metrics
153 154 155
    respond_to do |format|
      format.json do
        additional_metrics = environment.additional_metrics || {}
156

157 158 159
        render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
      end
    end
160 161
  end

162 163
  private

164 165 166 167
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

168 169 170 171 172 173 174 175 176
  def expire_etag_cache
    return if request.format.json?

    # this forces to reload json content
    Gitlab::EtagCaching::Store.new.tap do |store|
      store.touch(project_environments_path(project, format: :json))
    end
  end

177 178
  def environment_params
    params.require(:environment).permit(:name, :external_url)
K
Kamil Trzcinski 已提交
179 180
  end

181
  def environment
K
Kamil Trzcinski 已提交
182
    @environment ||= project.environments.find(params[:id])
K
Kamil Trzcinski 已提交
183
  end
184 185 186 187

  def authorize_stop_environment!
    access_denied! unless can?(current_user, :stop_environment, environment)
  end
K
Kamil Trzcinski 已提交
188
end