2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
class Projects::EnvironmentsController < Projects::ApplicationController
|
|
|
|
layout 'project'
|
|
|
|
before_action :authorize_read_environment!
|
|
|
|
before_action :authorize_create_environment!, only: [:new, :create]
|
2018-11-18 11:00:15 +05:30
|
|
|
before_action :authorize_stop_environment!, only: [:stop]
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :authorize_update_environment!, only: [:edit, :update]
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
|
|
|
|
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
|
|
|
|
before_action :verify_api_request!, only: :terminal_websocket_authorize
|
2018-11-08 19:23:39 +05:30
|
|
|
before_action :expire_etag_cache, only: [:index]
|
2019-07-31 22:56:46 +05:30
|
|
|
before_action only: [:metrics, :additional_metrics, :metrics_dashboard] do
|
2019-07-07 11:18:12 +05:30
|
|
|
push_frontend_feature_flag(:metrics_time_window)
|
2019-07-31 22:56:46 +05:30
|
|
|
push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint)
|
|
|
|
push_frontend_feature_flag(:environment_metrics_show_multiple_dashboards)
|
|
|
|
push_frontend_feature_flag(:grafana_dashboard_link)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
def index
|
2017-08-17 22:00:37 +05:30
|
|
|
@environments = project.environments
|
|
|
|
.with_state(params[:scope] || :available)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
|
|
|
|
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]),
|
2017-08-17 22:00:37 +05:30
|
|
|
available_count: project.environments.available.count,
|
|
|
|
stopped_count: project.environments.stopped.count
|
|
|
|
}
|
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
|
|
|
|
folder_environments = project.environments.where(environment_type: params[:id])
|
|
|
|
@environments = folder_environments.with_state(params[:scope] || :available)
|
2017-09-10 17:25:29 +05:30
|
|
|
.order(:name)
|
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
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-16 23:09:34 +05:30
|
|
|
def show
|
|
|
|
@deployments = environment.deployments.order(id: :desc).page(params[:page])
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
def new
|
|
|
|
@environment = project.environments.new
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def create
|
2016-09-13 17:45:13 +05:30
|
|
|
@environment = project.environments.create(environment_params)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
if @environment.persisted?
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_environment_path(project, @environment)
|
2016-06-16 23:09:34 +05:30
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @environment.update(environment_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_environment_path(project, @environment)
|
2016-09-13 17:45:13 +05:30
|
|
|
else
|
|
|
|
render :edit
|
2016-06-16 23:09:34 +05:30
|
|
|
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?
|
|
|
|
|
|
|
|
stop_action = @environment.stop_with_action!(current_user)
|
|
|
|
|
|
|
|
action_or_env_url =
|
|
|
|
if stop_action
|
|
|
|
polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
|
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
|
|
|
|
environment = project.default_environment
|
|
|
|
|
|
|
|
if environment
|
|
|
|
redirect_to environment_metrics_path(environment)
|
|
|
|
else
|
|
|
|
render :empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def metrics
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
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-07-31 22:56:46 +05:30
|
|
|
def metrics_dashboard
|
|
|
|
return render_403 unless Feature.enabled?(:environment_metrics_use_prometheus_endpoint, project)
|
|
|
|
|
|
|
|
if Feature.enabled?(:environment_metrics_show_multiple_dashboards, project)
|
|
|
|
result = dashboard_finder.find(project, current_user, environment, params[:dashboard])
|
|
|
|
|
|
|
|
result[:all_dashboards] = project.repository.metrics_dashboard_paths
|
|
|
|
else
|
|
|
|
result = dashboard_finder.find(project, current_user, environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if result[:status] == :success
|
|
|
|
format.json do
|
|
|
|
render status: :ok, json: result.slice(:all_dashboards, :dashboard, :status)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json do
|
|
|
|
render(
|
|
|
|
status: result[:http_status],
|
|
|
|
json: result.slice(:all_dashboards, :message, :status)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
private
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def environment_params
|
|
|
|
params.require(:environment).permit(:name, :external_url)
|
2016-06-16 23:09:34 +05:30
|
|
|
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
|
|
|
|
return unless Feature.enabled?(:metrics_time_window, project)
|
|
|
|
return unless params[:start].present? || params[:end].present?
|
|
|
|
|
|
|
|
params.require([:start, :end])
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def dashboard_finder
|
|
|
|
Gitlab::Metrics::Dashboard::Finder
|
|
|
|
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
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def authorize_stop_environment!
|
|
|
|
access_denied! unless can?(current_user, :stop_environment, environment)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|