environments_controller.rb 7.5 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]
13
  before_action only: [:metrics, :additional_metrics, :metrics_dashboard] do
14
    push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint, default_enabled: true)
15
    push_frontend_feature_flag(:environment_metrics_show_multiple_dashboards)
16
    push_frontend_feature_flag(:environment_metrics_additional_panel_types)
17
    push_frontend_feature_flag(:prometheus_computed_alerts)
18
    push_frontend_feature_flag(:export_metrics_to_csv_enabled)
19
  end
K
Kamil Trzcinski 已提交
20 21

  def index
22 23
    @environments = project.environments
      .with_state(params[:scope] || :available)
24

25 26 27
    respond_to do |format|
      format.html
      format.json do
28 29
        Gitlab::PollingInterval.set_header(response, interval: 3_000)

30
        render json: {
31
          environments: serialize_environments(request, response, params[:nested]),
32 33 34
          available_count: project.environments.available.count,
          stopped_count: project.environments.stopped.count
        }
K
Kamil Trzcinski 已提交
35
      end
36
    end
K
Kamil Trzcinski 已提交
37 38
  end

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

    respond_to do |format|
      format.html
      format.json do
        render json: {
51
          environments: serialize_environments(request, response),
52 53
          available_count: folder_environments.available.count,
          stopped_count: folder_environments.stopped.count
54 55 56 57
        }
      end
    end
  end
58
  # rubocop: enable CodeReuse/ActiveRecord
59

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

  def new
    @environment = project.environments.new
  end

70 71 72
  def edit
  end

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

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

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

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

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

F
Fatih Acet 已提交
96 97 98 99
    action_or_env_url =
      if stop_action
        polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
      else
100
        project_environment_url(project, @environment)
F
Fatih Acet 已提交
101 102 103 104 105
      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 已提交
106
    end
K
Kamil Trzcinski 已提交
107 108
  end

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

130
  def metrics_redirect
131
    environment = project.default_environment
132

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

140 141 142 143
  def metrics
    respond_to do |format|
      format.html
      format.json do
144 145 146 147
        # Currently, this acts as a hint to load the metrics details into the cache
        # if they aren't there already
        @metrics = environment.metrics || {}

148 149 150 151 152
        render json: @metrics, status: @metrics.any? ? :ok : :no_content
      end
    end
  end

153
  def additional_metrics
154 155
    respond_to do |format|
      format.json do
156
        additional_metrics = environment.additional_metrics(*metrics_params) || {}
157

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

S
syasonik 已提交
163
  def metrics_dashboard
164
    if params[:embedded]
165 166 167 168
      result = dashboard_finder.find(
        project,
        current_user,
        environment,
169 170
        dashboard_path: params[:dashboard],
        **dashboard_params.to_h.symbolize_keys
171
      )
172 173 174 175 176 177 178
    elsif Feature.enabled?(:environment_metrics_show_multiple_dashboards, project)
      result = dashboard_finder.find(
        project,
        current_user,
        environment,
        dashboard_path: params[:dashboard]
      )
179

180
      result[:all_dashboards] = dashboard_finder.find_all_paths(project)
181 182 183
    else
      result = dashboard_finder.find(project, current_user, environment)
    end
S
syasonik 已提交
184 185

    respond_to do |format|
186
      if result[:status] == :success
187 188 189
        format.json do
          render status: :ok, json: result.slice(:all_dashboards, :dashboard, :status)
        end
190
      else
191 192 193 194 195 196
        format.json do
          render(
            status: result[:http_status],
            json: result.slice(:all_dashboards, :message, :status)
          )
        end
S
syasonik 已提交
197 198 199 200
      end
    end
  end

201 202 203 204 205 206 207 208 209 210
  def search
    respond_to do |format|
      format.json do
        environment_names = search_environment_names

        render json: environment_names, status: environment_names.any? ? :ok : :no_content
      end
    end
  end

211 212
  private

213 214 215 216
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

217 218 219 220 221 222 223 224 225
  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

226 227
  def environment_params
    params.require(:environment).permit(:name, :external_url)
K
Kamil Trzcinski 已提交
228 229
  end

230
  def environment
K
Kamil Trzcinski 已提交
231
    @environment ||= project.environments.find(params[:id])
K
Kamil Trzcinski 已提交
232
  end
233

234
  def metrics_params
235
    params.require([:start, :end])
236 237
  end

238 239 240 241
  def dashboard_params
    params.permit(:embedded, :group, :title, :y_label)
  end

242 243 244 245
  def dashboard_finder
    Gitlab::Metrics::Dashboard::Finder
  end

246 247 248 249 250 251
  def search_environment_names
    return [] unless params[:query]

    project.environments.for_name_like(params[:query]).pluck_names
  end

252
  def serialize_environments(request, response, nested = false)
S
syasonik 已提交
253
    EnvironmentSerializer
254
      .new(project: @project, current_user: @current_user)
S
syasonik 已提交
255
      .tap { |serializer| serializer.within_folders if nested }
256
      .with_pagination(request, response)
S
syasonik 已提交
257
      .represent(@environments)
258 259
  end

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