debian-mirror-gitlab/app/controllers/projects/environments_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

281 lines
8.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
class Projects::EnvironmentsController < Projects::ApplicationController
2020-07-28 23:09:34 +05:30
# 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.
2019-12-21 20:55:43 +05:30
include MetricsDashboard
layout 'project'
2020-05-24 23:13:21 +05:30
before_action only: [:metrics, :additional_metrics, :metrics_dashboard] do
authorize_metrics_dashboard!
push_frontend_feature_flag(:prometheus_computed_alerts)
2020-10-24 23:57:45 +05:30
push_frontend_feature_flag(:disable_metric_dashboard_refresh_rate)
2020-05-24 23:13:21 +05:30
end
2021-09-04 01:27:46 +05:30
2020-06-23 00:09:42 +05:30
before_action :authorize_read_environment!, except: [:metrics, :additional_metrics, :metrics_dashboard, :metrics_redirect]
before_action :authorize_create_environment!, only: [:new, :create]
2018-11-18 11:00:15 +05:30
before_action :authorize_stop_environment!, only: [:stop]
2020-01-01 13:55:28 +05:30
before_action :authorize_update_environment!, only: [:edit, :update, :cancel_auto_stop]
2017-08-17 22:00:37 +05:30
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
2020-01-01 13:55:28 +05:30
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics, :cancel_auto_stop]
2017-08-17 22:00:37 +05:30
before_action :verify_api_request!, only: :terminal_websocket_authorize
2020-01-01 13:55:28 +05:30
before_action :expire_etag_cache, only: [:index], unless: -> { request.format.json? }
after_action :expire_etag_cache, only: [:cancel_auto_stop]
2021-01-03 14:25:43 +05:30
feature_category :continuous_delivery
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-03 14:25:43 +05:30
def index
2020-03-13 15:44:24 +05:30
@project = ProjectPresenter.new(project, current_user: current_user)
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html
format.json do
2022-05-07 20:08:51 +05:30
@environments = project.environments
.with_state(params[:scope] || :available)
2017-09-10 17:25:29 +05:30
Gitlab::PollingInterval.set_header(response, interval: 3_000)
2020-05-24 23:13:21 +05:30
environments_count_by_state = project.environments.count_by_state
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
render json: {
2019-03-02 22:35:43 +05:30
environments: serialize_environments(request, response, params[:nested]),
2020-03-13 15:44:24 +05:30
review_app: serialize_review_app,
2020-05-24 23:13:21 +05:30
available_count: environments_count_by_state[:available],
stopped_count: environments_count_by_state[:stopped]
2017-08-17 22:00:37 +05:30
}
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
2019-03-02 22:35:43 +05:30
# Returns all environments for a given folder
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def folder
2018-03-17 18:26:18 +05:30
@folder = params[:id]
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html
format.json do
2022-05-07 20:08:51 +05:30
folder_environments = project.environments.where(environment_type: params[:id])
@environments = folder_environments.with_state(params[:scope] || :available)
.order(:name)
2017-08-17 22:00:37 +05:30
render json: {
2019-03-02 22:35:43 +05:30
environments: serialize_environments(request, response),
2017-08-17 22:00:37 +05:30
available_count: folder_environments.available.count,
stopped_count: folder_environments.stopped.count
}
end
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
def show
2022-07-16 23:28:13 +05:30
@deployments = deployments
end
def new
@environment = project.environments.new
end
2016-09-13 17:45:13 +05:30
def edit
end
def create
2016-09-13 17:45:13 +05:30
@environment = project.environments.create(environment_params)
if @environment.persisted?
2021-10-27 15:23:28 +05:30
render json: { environment: @environment, path: project_environment_path(project, @environment) }
else
2021-10-27 15:23:28 +05:30
render json: { message: @environment.errors.full_messages }, status: :bad_request
2016-09-13 17:45:13 +05:30
end
end
def update
if @environment.update(environment_params)
2021-10-27 15:23:28 +05:30
render json: { environment: @environment, path: project_environment_path(project, @environment) }
2016-09-13 17:45:13 +05:30
else
2021-10-27 15:23:28 +05:30
render json: { message: @environment.errors.full_messages }, status: :bad_request
end
end
2016-11-03 12:29:30 +05:30
def stop
2017-08-17 22:00:37 +05:30
return render_404 unless @environment.available?
2022-06-21 17:19:12 +05:30
stop_actions = @environment.stop_with_actions!(current_user)
2017-08-17 22:00:37 +05:30
action_or_env_url =
2022-06-21 17:19:12 +05:30
if stop_actions&.count == 1
polymorphic_url([project, stop_actions.first])
2017-08-17 22:00:37 +05:30
else
2017-09-10 17:25:29 +05:30
project_environment_url(project, @environment)
2017-08-17 22:00:37 +05:30
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
2020-01-01 13:55:28 +05:30
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
2017-08-17 22:00:37 +05:30
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
2019-07-07 11:18:12 +05:30
render json: Gitlab::Workhorse.channel_websocket(terminal)
2017-08-17 22:00:37 +05:30
else
2019-02-15 15:39:39 +05:30
render html: 'Not found', status: :not_found
2017-08-17 22:00:37 +05:30
end
end
2018-11-08 19:23:39 +05:30
def metrics_redirect
2020-10-24 23:57:45 +05:30
redirect_to project_metrics_dashboard_path(project)
2018-11-08 19:23:39 +05:30
end
2017-08-17 22:00:37 +05:30
def metrics
respond_to do |format|
2020-10-24 23:57:45 +05:30
format.html do
redirect_to project_metrics_dashboard_path(project, environment: environment )
end
2017-08-17 22:00:37 +05:30
format.json do
2019-07-31 22:56:46 +05:30
# Currently, this acts as a hint to load the metrics details into the cache
# if they aren't there already
@metrics = environment.metrics || {}
2017-08-17 22:00:37 +05:30
render json: @metrics, status: @metrics.any? ? :ok : :no_content
end
end
end
2017-09-10 17:25:29 +05:30
def additional_metrics
respond_to do |format|
format.json do
2019-07-07 11:18:12 +05:30
additional_metrics = environment.additional_metrics(*metrics_params) || {}
2017-09-10 17:25:29 +05:30
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
end
end
end
2019-03-02 22:35:43 +05:30
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
2022-07-16 23:28:13 +05:30
def deployments
2022-07-23 23:45:48 +05:30
environment
.deployments
.with_environment_page_associations
.ordered
.page(params[:page])
2022-07-16 23:28:13 +05:30
end
2017-08-17 22:00:37 +05:30
def verify_api_request!
Gitlab::Workhorse.verify_api_request!(request.headers)
end
2018-11-08 19:23:39 +05:30
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-11-11 11:23:49 +05:30
def allowed_environment_attributes
attributes = [:external_url]
attributes << :name if action_name == "create"
attributes
end
2016-09-13 17:45:13 +05:30
def environment_params
2021-11-11 11:23:49 +05:30
params.require(:environment).permit(allowed_environment_attributes)
end
def environment
@environment ||= project.environments.find(params[:id])
end
2018-11-18 11:00:15 +05:30
2019-07-07 11:18:12 +05:30
def metrics_params
params.require([:start, :end])
end
2019-12-21 20:55:43 +05:30
def metrics_dashboard_params
params
2020-04-22 19:07:51 +05:30
.permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics, :embed_json)
2019-12-21 20:55:43 +05:30
.merge(dashboard_path: params[:dashboard], environment: environment)
2019-10-12 21:52:04 +05:30
end
2019-12-21 20:55:43 +05:30
def include_all_dashboards?
!params[:embedded]
2019-07-31 22:56:46 +05:30
end
2019-03-02 22:35:43 +05:30
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
2020-03-13 15:44:24 +05:30
def serialize_review_app
ReviewAppSetupSerializer.new(current_user: @current_user).represent(@project)
end
2018-11-18 11:00:15 +05:30
def authorize_stop_environment!
access_denied! unless can?(current_user, :stop_environment, environment)
end
2020-01-01 13:55:28 +05:30
def authorize_update_environment!
access_denied! unless can?(current_user, :update_environment, environment)
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Projects::EnvironmentsController.prepend_mod_with('Projects::EnvironmentsController')