2018-09-26 03:45:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-29 13:14:38 +00:00
|
|
|
class Projects::EnvironmentsController < Projects::ApplicationController
|
|
|
|
layout 'project'
|
2016-06-10 21:36:54 +00:00
|
|
|
before_action :authorize_read_environment!
|
2016-10-17 20:01:56 +00:00
|
|
|
before_action :authorize_create_environment!, only: [:new, :create]
|
2018-07-10 08:11:04 +00:00
|
|
|
before_action :authorize_stop_environment!, only: [:stop]
|
2016-10-17 19:06:10 +00:00
|
|
|
before_action :authorize_update_environment!, only: [:edit, :update]
|
2016-12-15 00:59:04 +00:00
|
|
|
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
|
2017-03-07 16:57:42 +00:00
|
|
|
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
|
2016-12-15 00:59:04 +00:00
|
|
|
before_action :verify_api_request!, only: :terminal_websocket_authorize
|
2018-05-22 13:12:47 +00:00
|
|
|
before_action :expire_etag_cache, only: [:index]
|
2016-04-29 13:14:38 +00:00
|
|
|
|
|
|
|
def index
|
2017-02-04 09:38:16 +00:00
|
|
|
@environments = project.environments
|
|
|
|
.with_state(params[:scope] || :available)
|
2016-11-22 19:55:56 +00:00
|
|
|
|
2016-10-14 12:14:41 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2017-07-06 13:31:24 +00:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
|
|
|
|
2017-02-04 09:38:16 +00:00
|
|
|
render json: {
|
2019-01-16 20:05:19 +00:00
|
|
|
environments: serialize_environments(request, response, params[:nested]),
|
2017-02-04 09:38:16 +00:00
|
|
|
available_count: project.environments.available.count,
|
|
|
|
stopped_count: project.environments.stopped.count
|
|
|
|
}
|
2016-10-18 10:02:50 +00:00
|
|
|
end
|
2016-10-14 12:14:41 +00:00
|
|
|
end
|
2016-04-29 13:14:38 +00:00
|
|
|
end
|
|
|
|
|
2019-01-16 20:05:19 +00:00
|
|
|
# Returns all environments for a given folder
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-07 14:06:31 +00:00
|
|
|
def folder
|
2017-02-15 18:59:30 +00:00
|
|
|
folder_environments = project.environments.where(environment_type: params[:id])
|
|
|
|
@environments = folder_environments.with_state(params[:scope] || :available)
|
2017-05-17 11:36:19 +00:00
|
|
|
.order(:name)
|
2017-11-23 12:04:03 +00:00
|
|
|
@folder = params[:id]
|
2017-02-07 14:06:31 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: {
|
2019-01-16 20:05:19 +00:00
|
|
|
environments: serialize_environments(request, response),
|
2017-02-15 18:59:30 +00:00
|
|
|
available_count: folder_environments.available.count,
|
|
|
|
stopped_count: folder_environments.stopped.count
|
2017-02-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-07 14:06:31 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-04-29 13:14:38 +00:00
|
|
|
def show
|
2016-06-15 10:07:06 +00:00
|
|
|
@deployments = environment.deployments.order(id: :desc).page(params[:page])
|
2016-06-10 22:15:53 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-10 22:15:53 +00:00
|
|
|
|
|
|
|
def new
|
|
|
|
@environment = project.environments.new
|
|
|
|
end
|
|
|
|
|
2016-07-26 07:35:47 +00:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2016-06-10 22:15:53 +00:00
|
|
|
def create
|
2016-07-26 07:35:47 +00:00
|
|
|
@environment = project.environments.create(environment_params)
|
2016-06-14 16:34:48 +00:00
|
|
|
|
|
|
|
if @environment.persisted?
|
2017-06-29 17:06:35 +00:00
|
|
|
redirect_to project_environment_path(project, @environment)
|
2016-06-14 16:34:48 +00:00
|
|
|
else
|
2016-07-26 07:35:47 +00:00
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @environment.update(environment_params)
|
2017-06-29 17:06:35 +00:00
|
|
|
redirect_to project_environment_path(project, @environment)
|
2016-07-26 07:35:47 +00:00
|
|
|
else
|
|
|
|
render :edit
|
2016-06-10 22:15:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-17 09:33:24 +00:00
|
|
|
def stop
|
2016-11-10 12:59:26 +00:00
|
|
|
return render_404 unless @environment.available?
|
2016-10-17 14:13:19 +00:00
|
|
|
|
2017-02-06 15:50:03 +00:00
|
|
|
stop_action = @environment.stop_with_action!(current_user)
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
action_or_env_url =
|
|
|
|
if stop_action
|
|
|
|
polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
|
|
|
|
else
|
2017-06-29 17:06:35 +00:00
|
|
|
project_environment_url(project, @environment)
|
2017-05-09 04:15:34 +00: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 12:59:26 +00:00
|
|
|
end
|
2016-04-29 13:14:38 +00:00
|
|
|
end
|
|
|
|
|
2016-11-22 19:55:56 +00: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-15 00:59:04 +00:00
|
|
|
# should retry those errors
|
2016-11-22 19:55:56 +00:00
|
|
|
terminal = environment.terminals.try(:first)
|
|
|
|
if terminal
|
|
|
|
set_workhorse_internal_api_content_type
|
|
|
|
render json: Gitlab::Workhorse.terminal_websocket(terminal)
|
|
|
|
else
|
2018-11-28 18:08:21 +00:00
|
|
|
render html: 'Not found', status: :not_found
|
2016-11-22 19:55:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-28 09:54:02 +00:00
|
|
|
def metrics_redirect
|
2018-07-03 09:17:07 +00:00
|
|
|
environment = project.default_environment
|
2018-06-28 09:54:02 +00:00
|
|
|
|
2018-06-29 08:02:32 +00:00
|
|
|
if environment
|
|
|
|
redirect_to environment_metrics_path(environment)
|
|
|
|
else
|
|
|
|
render :empty
|
|
|
|
end
|
2018-06-28 09:54:02 +00:00
|
|
|
end
|
|
|
|
|
2017-03-07 16:57:42 +00:00
|
|
|
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
|
|
|
|
|
2017-05-10 09:25:30 +00:00
|
|
|
def additional_metrics
|
2017-06-20 13:53:05 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
additional_metrics = environment.additional_metrics || {}
|
2017-05-10 09:25:30 +00:00
|
|
|
|
2017-06-20 13:53:05 +00:00
|
|
|
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
|
|
|
|
end
|
|
|
|
end
|
2017-05-10 09:25:30 +00:00
|
|
|
end
|
|
|
|
|
2019-02-05 07:14:30 +00: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 21:36:54 +00:00
|
|
|
private
|
|
|
|
|
2016-12-15 00:59:04 +00:00
|
|
|
def verify_api_request!
|
|
|
|
Gitlab::Workhorse.verify_api_request!(request.headers)
|
|
|
|
end
|
|
|
|
|
2018-05-22 13:12:47 +00:00
|
|
|
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-07-26 07:35:47 +00:00
|
|
|
def environment_params
|
|
|
|
params.require(:environment).permit(:name, :external_url)
|
2016-06-10 22:15:53 +00:00
|
|
|
end
|
|
|
|
|
2016-06-10 21:36:54 +00:00
|
|
|
def environment
|
2016-06-15 10:07:06 +00:00
|
|
|
@environment ||= project.environments.find(params[:id])
|
2016-04-29 13:14:38 +00:00
|
|
|
end
|
2018-07-10 08:11:04 +00:00
|
|
|
|
2019-02-05 07:14:30 +00:00
|
|
|
def search_environment_names
|
|
|
|
return [] unless params[:query]
|
|
|
|
|
|
|
|
project.environments.for_name_like(params[:query]).pluck_names
|
|
|
|
end
|
|
|
|
|
2019-01-16 20:05:19 +00:00
|
|
|
def serialize_environments(request, response, nested = false)
|
2019-01-30 01:58:04 +00:00
|
|
|
EnvironmentSerializer
|
2019-01-16 20:05:19 +00:00
|
|
|
.new(project: @project, current_user: @current_user)
|
2019-01-30 01:58:04 +00:00
|
|
|
.tap { |serializer| serializer.within_folders if nested }
|
2019-01-16 20:05:19 +00:00
|
|
|
.with_pagination(request, response)
|
2019-01-30 01:58:04 +00:00
|
|
|
.represent(@environments)
|
2019-01-16 20:05:19 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 08:11:04 +00:00
|
|
|
def authorize_stop_environment!
|
|
|
|
access_denied! unless can?(current_user, :stop_environment, environment)
|
|
|
|
end
|
2016-04-29 13:14:38 +00:00
|
|
|
end
|