2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-29 09:14:38 -04:00
|
|
|
class Projects::EnvironmentsController < Projects::ApplicationController
|
2020-07-05 23:09:07 -04:00
|
|
|
# Metrics dashboard code is getting decoupled from environments and is being moved
|
|
|
|
# into app/controllers/projects/metrics_dashboard_controller.rb
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/226002 for more details.
|
|
|
|
|
2022-09-12 23:12:37 -04:00
|
|
|
MIN_SEARCH_LENGTH = 3
|
|
|
|
|
2019-10-04 05:06:35 -04:00
|
|
|
include MetricsDashboard
|
2022-09-09 11:13:16 -04:00
|
|
|
include ProductAnalyticsTracking
|
2019-10-04 05:06:35 -04:00
|
|
|
|
2016-04-29 09:14:38 -04:00
|
|
|
layout 'project'
|
2020-04-30 05:09:39 -04:00
|
|
|
|
|
|
|
before_action only: [:metrics, :additional_metrics, :metrics_dashboard] do
|
|
|
|
authorize_metrics_dashboard!
|
|
|
|
|
|
|
|
push_frontend_feature_flag(:prometheus_computed_alerts)
|
2020-07-17 11:09:13 -04:00
|
|
|
push_frontend_feature_flag(:disable_metric_dashboard_refresh_rate)
|
2020-04-30 05:09:39 -04:00
|
|
|
end
|
2021-06-16 23:09:59 -04:00
|
|
|
|
2020-05-21 14:08:27 -04:00
|
|
|
before_action :authorize_read_environment!, except: [:metrics, :additional_metrics, :metrics_dashboard, :metrics_redirect]
|
2016-10-17 16:01:56 -04:00
|
|
|
before_action :authorize_create_environment!, only: [:new, :create]
|
2018-07-10 04:11:04 -04:00
|
|
|
before_action :authorize_stop_environment!, only: [:stop]
|
2019-12-10 02:53:40 -05:00
|
|
|
before_action :authorize_update_environment!, only: [:edit, :update, :cancel_auto_stop]
|
2016-12-14 19:59:04 -05:00
|
|
|
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
|
2019-12-10 02:53:40 -05:00
|
|
|
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics, :cancel_auto_stop]
|
2016-12-14 19:59:04 -05:00
|
|
|
before_action :verify_api_request!, only: :terminal_websocket_authorize
|
2019-12-10 02:53:40 -05:00
|
|
|
before_action :expire_etag_cache, only: [:index], unless: -> { request.format.json? }
|
|
|
|
after_action :expire_etag_cache, only: [:cancel_auto_stop]
|
2016-04-29 09:14:38 -04:00
|
|
|
|
2022-09-09 11:13:16 -04:00
|
|
|
track_event :index,
|
|
|
|
:folder,
|
|
|
|
:show,
|
|
|
|
:new,
|
|
|
|
:edit,
|
|
|
|
:create,
|
|
|
|
:update,
|
|
|
|
:stop,
|
|
|
|
:cancel_auto_stop,
|
|
|
|
:terminal,
|
|
|
|
name: 'users_visiting_environments_pages'
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :continuous_delivery
|
2022-04-29 08:10:13 -04:00
|
|
|
urgency :low
|
2020-10-08 14:08:32 -04:00
|
|
|
|
2016-04-29 09:14:38 -04:00
|
|
|
def index
|
2020-01-09 07:08:03 -05:00
|
|
|
@project = ProjectPresenter.new(project, current_user: current_user)
|
2016-11-22 14:55:56 -05:00
|
|
|
|
2016-10-14 08:14:41 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2022-09-15 17:12:27 -04:00
|
|
|
@environments = search_environments.with_state(params[:scope] || :available)
|
|
|
|
environments_count_by_state = search_environments.count_by_state
|
2017-07-06 09:31:24 -04:00
|
|
|
|
2022-09-12 23:12:37 -04:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
2017-02-04 04:38:16 -05:00
|
|
|
render json: {
|
2019-01-16 15:05:19 -05:00
|
|
|
environments: serialize_environments(request, response, params[:nested]),
|
2020-01-09 07:08:03 -05:00
|
|
|
review_app: serialize_review_app,
|
2020-04-28 05:09:34 -04:00
|
|
|
available_count: environments_count_by_state[:available],
|
|
|
|
stopped_count: environments_count_by_state[:stopped]
|
2017-02-04 04:38:16 -05:00
|
|
|
}
|
2016-10-18 06:02:50 -04:00
|
|
|
end
|
2016-10-14 08:14:41 -04:00
|
|
|
end
|
2016-04-29 09:14:38 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 15:05:19 -05:00
|
|
|
# Returns all environments for a given folder
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-07 09:06:31 -05:00
|
|
|
def folder
|
2017-11-23 07:04:03 -05:00
|
|
|
@folder = params[:id]
|
2017-02-07 09:06:31 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2022-09-15 17:12:27 -04:00
|
|
|
folder_environments = search_environments(type: params[:id])
|
2022-09-12 23:12:37 -04:00
|
|
|
|
2022-03-15 14:08:10 -04:00
|
|
|
@environments = folder_environments.with_state(params[:scope] || :available)
|
|
|
|
.order(:name)
|
|
|
|
|
2017-02-07 09:06:31 -05:00
|
|
|
render json: {
|
2019-01-16 15:05:19 -05:00
|
|
|
environments: serialize_environments(request, response),
|
2017-02-15 13:59:30 -05:00
|
|
|
available_count: folder_environments.available.count,
|
|
|
|
stopped_count: folder_environments.stopped.count
|
2017-02-07 09:06:31 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-07 09:06:31 -05:00
|
|
|
|
2016-04-29 09:14:38 -04:00
|
|
|
def show
|
2022-04-28 08:09:06 -04:00
|
|
|
@deployments = deployments
|
2016-06-10 18:15:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@environment = project.environments.new
|
|
|
|
end
|
|
|
|
|
2016-07-26 03:35:47 -04:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2016-06-10 18:15:53 -04:00
|
|
|
def create
|
2016-07-26 03:35:47 -04:00
|
|
|
@environment = project.environments.create(environment_params)
|
2016-06-14 12:34:48 -04:00
|
|
|
|
|
|
|
if @environment.persisted?
|
2021-07-21 08:09:35 -04:00
|
|
|
render json: { environment: @environment, path: project_environment_path(project, @environment) }
|
2016-06-14 12:34:48 -04:00
|
|
|
else
|
2021-07-21 08:09:35 -04:00
|
|
|
render json: { message: @environment.errors.full_messages }, status: :bad_request
|
2016-07-26 03:35:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @environment.update(environment_params)
|
2021-07-23 05:08:49 -04:00
|
|
|
render json: { environment: @environment, path: project_environment_path(project, @environment) }
|
2016-07-26 03:35:47 -04:00
|
|
|
else
|
2021-07-23 05:08:49 -04:00
|
|
|
render json: { message: @environment.errors.full_messages }, status: :bad_request
|
2016-06-10 18:15:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-17 05:33:24 -04:00
|
|
|
def stop
|
2016-11-10 07:59:26 -05:00
|
|
|
return render_404 unless @environment.available?
|
2016-10-17 10:13:19 -04:00
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
stop_actions = @environment.stop_with_actions!(current_user)
|
2017-02-06 10:50:03 -05:00
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
action_or_env_url =
|
2022-04-14 02:08:29 -04:00
|
|
|
if stop_actions&.count == 1
|
|
|
|
polymorphic_url([project, stop_actions.first])
|
2017-05-09 00:15:34 -04:00
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
project_environment_url(project, @environment)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to action_or_env_url }
|
|
|
|
format.json { render json: { redirect_url: action_or_env_url } }
|
2016-11-10 07:59:26 -05:00
|
|
|
end
|
2016-04-29 09:14:38 -04:00
|
|
|
end
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
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
|
|
|
|
|
2016-11-22 14:55:56 -05:00
|
|
|
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
|
2016-12-14 19:59:04 -05:00
|
|
|
# should retry those errors
|
2016-11-22 14:55:56 -05:00
|
|
|
terminal = environment.terminals.try(:first)
|
|
|
|
if terminal
|
|
|
|
set_workhorse_internal_api_content_type
|
2019-04-04 14:32:02 -04:00
|
|
|
render json: Gitlab::Workhorse.channel_websocket(terminal)
|
2016-11-22 14:55:56 -05:00
|
|
|
else
|
2018-11-28 13:08:21 -05:00
|
|
|
render html: 'Not found', status: :not_found
|
2016-11-22 14:55:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-28 05:54:02 -04:00
|
|
|
def metrics_redirect
|
2020-08-12 23:10:13 -04:00
|
|
|
redirect_to project_metrics_dashboard_path(project)
|
2018-06-28 05:54:02 -04:00
|
|
|
end
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
def metrics
|
|
|
|
respond_to do |format|
|
2020-08-12 23:10:13 -04:00
|
|
|
format.html do
|
|
|
|
redirect_to project_metrics_dashboard_path(project, environment: environment )
|
|
|
|
end
|
2017-03-07 11:57:42 -05:00
|
|
|
format.json do
|
2019-04-29 06:25:31 -04:00
|
|
|
# Currently, this acts as a hint to load the metrics details into the cache
|
|
|
|
# if they aren't there already
|
|
|
|
@metrics = environment.metrics || {}
|
|
|
|
|
2017-03-07 11:57:42 -05:00
|
|
|
render json: @metrics, status: @metrics.any? ? :ok : :no_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-10 05:25:30 -04:00
|
|
|
def additional_metrics
|
2017-06-20 09:53:05 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2019-03-15 07:20:59 -04:00
|
|
|
additional_metrics = environment.additional_metrics(*metrics_params) || {}
|
2017-05-10 05:25:30 -04:00
|
|
|
|
2017-06-20 09:53:05 -04:00
|
|
|
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
|
|
|
|
end
|
|
|
|
end
|
2017-05-10 05:25:30 -04:00
|
|
|
end
|
|
|
|
|
2019-02-05 02:14:30 -05:00
|
|
|
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
|
|
|
|
|
2016-06-10 17:36:54 -04:00
|
|
|
private
|
|
|
|
|
2022-04-28 08:09:06 -04:00
|
|
|
def deployments
|
2022-05-18 17:07:37 -04:00
|
|
|
environment
|
|
|
|
.deployments
|
|
|
|
.with_environment_page_associations
|
|
|
|
.ordered
|
|
|
|
.page(params[:page])
|
2022-04-28 08:09:06 -04:00
|
|
|
end
|
|
|
|
|
2016-12-14 19:59:04 -05:00
|
|
|
def verify_api_request!
|
|
|
|
Gitlab::Workhorse.verify_api_request!(request.headers)
|
|
|
|
end
|
|
|
|
|
2018-05-22 09:12:47 -04:00
|
|
|
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
|
|
|
|
|
2021-08-26 17:11:25 -04:00
|
|
|
def allowed_environment_attributes
|
|
|
|
attributes = [:external_url]
|
|
|
|
attributes << :name if action_name == "create"
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2016-07-26 03:35:47 -04:00
|
|
|
def environment_params
|
2021-08-26 17:11:25 -04:00
|
|
|
params.require(:environment).permit(allowed_environment_attributes)
|
2016-06-10 18:15:53 -04:00
|
|
|
end
|
|
|
|
|
2016-06-10 17:36:54 -04:00
|
|
|
def environment
|
2016-06-15 06:07:06 -04:00
|
|
|
@environment ||= project.environments.find(params[:id])
|
2016-04-29 09:14:38 -04:00
|
|
|
end
|
2018-07-10 04:11:04 -04:00
|
|
|
|
2022-09-12 23:12:37 -04:00
|
|
|
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
|
|
|
|
|
2019-03-15 07:20:59 -04:00
|
|
|
def metrics_params
|
2019-04-09 05:54:17 -04:00
|
|
|
params.require([:start, :end])
|
2019-03-15 07:20:59 -04:00
|
|
|
end
|
|
|
|
|
2019-10-04 05:06:35 -04:00
|
|
|
def metrics_dashboard_params
|
|
|
|
params
|
2020-04-07 20:09:30 -04:00
|
|
|
.permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics, :embed_json)
|
2019-10-04 05:06:35 -04:00
|
|
|
.merge(dashboard_path: params[:dashboard], environment: environment)
|
2019-08-07 12:17:35 -04:00
|
|
|
end
|
|
|
|
|
2019-10-04 05:06:35 -04:00
|
|
|
def include_all_dashboards?
|
|
|
|
!params[:embedded]
|
2019-05-01 06:16:03 -04:00
|
|
|
end
|
|
|
|
|
2019-02-05 02:14:30 -05:00
|
|
|
def search_environment_names
|
|
|
|
return [] unless params[:query]
|
|
|
|
|
|
|
|
project.environments.for_name_like(params[:query]).pluck_names
|
|
|
|
end
|
|
|
|
|
2019-01-16 15:05:19 -05:00
|
|
|
def serialize_environments(request, response, nested = false)
|
2019-01-29 20:58:04 -05:00
|
|
|
EnvironmentSerializer
|
2019-01-16 15:05:19 -05:00
|
|
|
.new(project: @project, current_user: @current_user)
|
2019-01-29 20:58:04 -05:00
|
|
|
.tap { |serializer| serializer.within_folders if nested }
|
2019-01-16 15:05:19 -05:00
|
|
|
.with_pagination(request, response)
|
2019-01-29 20:58:04 -05:00
|
|
|
.represent(@environments)
|
2019-01-16 15:05:19 -05:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:08:03 -05:00
|
|
|
def serialize_review_app
|
|
|
|
ReviewAppSetupSerializer.new(current_user: @current_user).represent(@project)
|
|
|
|
end
|
|
|
|
|
2018-07-10 04:11:04 -04:00
|
|
|
def authorize_stop_environment!
|
|
|
|
access_denied! unless can?(current_user, :stop_environment, environment)
|
|
|
|
end
|
2019-12-10 02:53:40 -05:00
|
|
|
|
|
|
|
def authorize_update_environment!
|
|
|
|
access_denied! unless can?(current_user, :update_environment, environment)
|
|
|
|
end
|
2016-04-29 09:14:38 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Projects::EnvironmentsController.prepend_mod_with('Projects::EnvironmentsController')
|