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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
3 4
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
5
  before_action :authorize_read_environment!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
6
  before_action :authorize_create_environment!, only: [:new, :create]
7
  before_action :authorize_stop_environment!, only: [:stop]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13

Adriel Santiago's avatar
Adriel Santiago committed
14 15 16 17
  before_action do
    push_frontend_feature_flag(:area_chart, project)
  end

18
  # Returns all environments or all folders based on the :nested param
Kamil Trzcinski's avatar
Kamil Trzcinski committed
19
  def index
20 21
    @environments = project.environments
      .with_state(params[:scope] || :available)
22

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

28
        render json: {
29
          environments: serialize_environments(request, response, params[:nested]),
30 31 32
          available_count: project.environments.available.count,
          stopped_count: project.environments.stopped.count
        }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
      end
34
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
35 36
  end

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

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

58
  # rubocop: disable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
59
  def show
Kamil Trzcinski's avatar
Kamil Trzcinski committed
60
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
61
  end
62
  # rubocop: enable CodeReuse/ActiveRecord
Kamil Trzcinski's avatar
Kamil Trzcinski committed
63 64 65 66 67

  def new
    @environment = project.environments.new
  end

68 69 70
  def edit
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
71
  def create
72
    @environment = project.environments.create(environment_params)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
73 74

    if @environment.persisted?
75
      redirect_to project_environment_path(project, @environment)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
    else
77 78 79 80 81 82
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
83
      redirect_to project_environment_path(project, @environment)
84 85
    else
      render :edit
Kamil Trzcinski's avatar
Kamil Trzcinski committed
86 87 88
    end
  end

89
  def stop
90
    return render_404 unless @environment.available?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
91

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

Fatih Acet's avatar
Fatih Acet committed
94 95 96 97
    action_or_env_url =
      if stop_action
        polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
      else
98
        project_environment_url(project, @environment)
Fatih Acet's avatar
Fatih Acet committed
99 100 101 102 103
      end

    respond_to do |format|
      format.html { redirect_to action_or_env_url }
      format.json { render json: { redirect_url: action_or_env_url } }
104
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
105 106
  end

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

128
  def metrics_redirect
129
    environment = project.default_environment
130

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

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

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

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

161 162 163 164 165 166 167 168 169 170
  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

171 172
  private

173 174 175 176
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

177 178 179 180 181 182 183 184 185
  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

186 187
  def environment_params
    params.require(:environment).permit(:name, :external_url)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
188 189
  end

190
  def environment
Kamil Trzcinski's avatar
Kamil Trzcinski committed
191
    @environment ||= project.environments.find(params[:id])
Kamil Trzcinski's avatar
Kamil Trzcinski committed
192
  end
193

194 195 196 197 198 199
  def search_environment_names
    return [] unless params[:query]

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

200
  def serialize_environments(request, response, nested = false)
syasonik's avatar
syasonik committed
201
    EnvironmentSerializer
202
      .new(project: @project, current_user: @current_user)
syasonik's avatar
syasonik committed
203
      .tap { |serializer| serializer.within_folders if nested }
204
      .with_pagination(request, response)
syasonik's avatar
syasonik committed
205
      .represent(@environments)
206 207
  end

208 209 210
  def authorize_stop_environment!
    access_denied! unless can?(current_user, :stop_environment, environment)
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
211
end