1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
# frozen_string_literal: true
class Projects::EnvironmentsController < Projects::ApplicationController
include ProductAnalyticsTracking
include KasCookie
MIN_SEARCH_LENGTH = 3
ACTIVE_STATES = %i[available stopping].freeze
SCOPES_TO_STATES = { "active" => ACTIVE_STATES, "stopped" => %i[stopped] }.freeze
layout 'project'
before_action only: [:folder] do
push_frontend_feature_flag(:environments_folder_new_look, project)
end
before_action only: [:show] do
push_frontend_feature_flag(:k8s_tree_view, project)
push_frontend_feature_flag(:use_websocket_for_k8s_watch, project)
end
before_action :authorize_read_environment!
before_action :authorize_create_environment!, only: [:new, :create]
before_action :authorize_stop_environment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update, :cancel_auto_stop]
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
before_action :environment,
only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :cancel_auto_stop, :k8s]
before_action :verify_api_request!, only: :terminal_websocket_authorize
before_action :expire_etag_cache, only: [:index], unless: -> { request.format.json? }
before_action :set_kas_cookie, only: [:edit, :new, :show, :k8s], if: -> { current_user && request.format.html? }
after_action :expire_etag_cache, only: [:cancel_auto_stop]
track_event :index, :folder, :show, :new, :edit, :create, :update, :stop, :cancel_auto_stop, :terminal, :k8s,
name: 'users_visiting_environments_pages'
feature_category :continuous_delivery
urgency :low
def index
@project = ProjectPresenter.new(project, current_user: current_user)
respond_to do |format|
format.html
format.json do
states = SCOPES_TO_STATES.fetch(params[:scope], ACTIVE_STATES)
@environments = search_environments.with_state(states)
environments_count_by_state = search_environments.count_by_state
Gitlab::PollingInterval.set_header(response, interval: 3_000)
render json: {
environments: serialize_environments(request, response, params[:nested]),
review_app: serialize_review_app,
can_stop_stale_environments: can?(current_user, :stop_environment, @project),
available_count: environments_count_by_state[:available],
active_count: environments_count_by_state[:available] + environments_count_by_state[:stopping],
stopped_count: environments_count_by_state[:stopped]
}
end
end
end
# Returns all environments for a given folder
# rubocop: disable CodeReuse/ActiveRecord
def folder
@folder = params[:id]
respond_to do |format|
format.html
format.json do
states = SCOPES_TO_STATES.fetch(params[:scope], ACTIVE_STATES)
folder_environments = search_environments(type: params[:id])
@environments = folder_environments.with_state(states)
.order(:name)
render json: {
environments: serialize_environments(request, response),
available_count: folder_environments.available.count,
active_count: folder_environments.active.count,
stopped_count: folder_environments.stopped.count
}
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
def show; end
def new
@environment = project.environments.new
end
def edit; end
def k8s
render action: :show
end
def create
@environment = project.environments.create(environment_params)
if @environment.persisted?
render json: { environment: @environment, path: project_environment_path(project, @environment) }
else
render json: { message: @environment.errors.full_messages }, status: :bad_request
end
end
def update
if @environment.update(environment_params)
render json: { environment: @environment, path: project_environment_path(project, @environment) }
else
render json: { message: @environment.errors.full_messages }, status: :bad_request
end
end
def stop
return render_404 unless @environment.available?
service_response = Environments::StopService.new(project, current_user).execute(@environment)
return render_403 unless service_response.success?
job = service_response[:actions].first if service_response[:actions]&.count == 1
action_or_env_url =
if job
project_job_url(project, job)
else
project_environment_url(project, @environment)
end
respond_to do |format|
format.html { redirect_to action_or_env_url }
format.json { render json: { redirect_url: action_or_env_url } }
end
end
def cancel_auto_stop
result = Environments::ResetAutoStopService.new(project, current_user)
.execute(environment)
if result[:status] == :success
respond_to do |format|
message = _('Auto stop successfully canceled.')
format.html { redirect_back_or_default(default: { action: 'show' }, options: { notice: message }) }
format.json { render json: { message: message }, status: :ok }
end
else
respond_to do |format|
message = result[:message]
format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: message }) }
format.json { render json: { message: message }, status: :unprocessable_entity }
end
end
end
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
# should retry those errors
terminal = environment.terminals.try(:first)
if terminal
set_workhorse_internal_api_content_type
render json: Gitlab::Workhorse.channel_websocket(terminal)
else
render html: 'Not found', status: :not_found
end
end
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
private
def deployments
environment
.deployments
.with_environment_page_associations
.ordered
.page(params[:page])
end
def verify_api_request!
Gitlab::Workhorse.verify_api_request!(request.headers)
end
def expire_etag_cache
# this forces to reload json content
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(project_environments_path(project, format: :json))
end
end
def allowed_environment_attributes
attributes = [:external_url]
attributes << :name if action_name == "create"
attributes
end
def environment_params
params.require(:environment).permit(allowed_environment_attributes)
end
def environment
@environment ||= project.environments.find(params[:id])
end
def search_environments(type: nil)
search = params[:search] if params[:search] && params[:search].length >= MIN_SEARCH_LENGTH
@search_environments ||= Environments::EnvironmentsFinder.new(
project,
current_user,
type: type,
search: search
).execute
end
def include_all_dashboards?
!params[:embedded]
end
def search_environment_names
return [] unless params[:query]
project.environments.for_name_like(params[:query]).pluck_names
end
def serialize_environments(request, response, nested = false)
EnvironmentSerializer
.new(project: @project, current_user: @current_user)
.tap { |serializer| serializer.within_folders if nested }
.with_pagination(request, response)
.represent(@environments)
end
def serialize_review_app
ReviewAppSetupSerializer.new(current_user: @current_user).represent(@project)
end
def authorize_stop_environment!
access_denied! unless can?(current_user, :stop_environment, environment)
end
def authorize_update_environment!
access_denied! unless can?(current_user, :update_environment, environment)
end
end
Projects::EnvironmentsController.prepend_mod_with('Projects::EnvironmentsController')
|