gitlab-org--gitlab-foss/app/controllers/projects/environments_controller.rb

159 lines
4.6 KiB
Ruby
Raw Normal View History

2016-04-29 09:14:38 -04:00
class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project'
2016-06-10 17:36:54 -04:00
before_action :authorize_read_environment!
2016-10-17 16:01:56 -04:00
before_action :authorize_create_environment!, only: [:new, :create]
2016-10-17 15:06:10 -04:00
before_action :authorize_create_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update]
2016-12-14 19:59:04 -05:00
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
2016-12-14 19:59:04 -05:00
before_action :verify_api_request!, only: :terminal_websocket_authorize
2016-04-29 09:14:38 -04:00
def index
@environments = project.environments
.with_state(params[:scope] || :available)
respond_to do |format|
format.html
format.json do
2017-07-06 09:31:24 -04:00
Gitlab::PollingInterval.set_header(response, interval: 3_000)
render json: {
environments: EnvironmentSerializer
2017-05-09 00:15:34 -04:00
.new(project: @project, current_user: @current_user)
.with_pagination(request, response)
.within_folders
.represent(@environments),
available_count: project.environments.available.count,
stopped_count: project.environments.stopped.count
}
2016-10-18 06:02:50 -04:00
end
end
2016-04-29 09:14:38 -04:00
end
def folder
2017-02-15 13:59:30 -05:00
folder_environments = project.environments.where(environment_type: params[:id])
@environments = folder_environments.with_state(params[:scope] || :available)
.order(:name)
2017-11-23 07:04:03 -05:00
@folder = params[:id]
respond_to do |format|
format.html
format.json do
render json: {
environments: EnvironmentSerializer
2017-05-09 00:15:34 -04:00
.new(project: @project, current_user: @current_user)
.with_pagination(request, response)
.represent(@environments),
2017-02-15 13:59:30 -05:00
available_count: folder_environments.available.count,
stopped_count: folder_environments.stopped.count
}
end
end
end
2016-04-29 09:14:38 -04:00
def show
2016-06-15 06:07:06 -04:00
@deployments = environment.deployments.order(id: :desc).page(params[:page])
2016-06-10 18:15:53 -04:00
end
def new
@environment = project.environments.new
end
def edit
end
2016-06-10 18:15:53 -04:00
def create
@environment = project.environments.create(environment_params)
2016-06-14 12:34:48 -04:00
if @environment.persisted?
redirect_to project_environment_path(project, @environment)
2016-06-14 12:34:48 -04:00
else
render :new
end
end
def update
if @environment.update(environment_params)
redirect_to project_environment_path(project, @environment)
else
render :edit
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
2017-02-06 10:50:03 -05:00
stop_action = @environment.stop_with_action!(current_user)
2017-05-09 00:15:34 -04:00
action_or_env_url =
if stop_action
polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
else
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
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
terminal = environment.terminals.try(:first)
if terminal
set_workhorse_internal_api_content_type
render json: Gitlab::Workhorse.terminal_websocket(terminal)
else
render text: 'Not found', status: 404
end
end
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
def additional_metrics
respond_to do |format|
format.json do
additional_metrics = environment.additional_metrics || {}
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
end
end
end
2016-06-10 17:36:54 -04:00
private
2016-12-14 19:59:04 -05:00
def verify_api_request!
Gitlab::Workhorse.verify_api_request!(request.headers)
end
def environment_params
params.require(:environment).permit(:name, :external_url)
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
end