Skip to content
environments_controller.rb 5.05 KiB
Newer Older
Kamil Trzciński's avatar
Kamil Trzciński committed
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
  before_action :authorize_read_environment!
Kamil Trzciński's avatar
Kamil Trzciński committed
  before_action :authorize_create_environment!, only: [:new, :create]
Kamil Trzciński's avatar
Kamil Trzciński committed
  before_action :authorize_create_deployment!, only: [:stop]
  before_action :authorize_update_environment!, only: [:edit, :update]
  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
  before_action :expire_etag_cache, only: [:index]
Kamil Trzciński's avatar
Kamil Trzciński committed

  def index
    @environments = project.environments
      .with_state(params[:scope] || :available)
    respond_to do |format|
      format.html
      format.json do
        Gitlab::PollingInterval.set_header(response, interval: 3_000)

        render json: {
          environments: EnvironmentSerializer
            .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
        }
Kamil Trzciński's avatar
Kamil Trzciński committed
  end

    folder_environments = project.environments.where(environment_type: params[:id])
    @environments = folder_environments.with_state(params[:scope] || :available)
    @folder = params[:id]

    respond_to do |format|
      format.html
      format.json do
        render json: {
          environments: EnvironmentSerializer
            .new(project: @project, current_user: @current_user)
            .with_pagination(request, response)
            .represent(@environments),
          available_count: folder_environments.available.count,
          stopped_count: folder_environments.stopped.count
Kamil Trzciński's avatar
Kamil Trzciński committed
  def show
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzciński's avatar
Kamil Trzciński committed
  end

  def new
    @environment = project.environments.new
  end

Kamil Trzciński's avatar
Kamil Trzciński committed
  def create
    @environment = project.environments.create(environment_params)

    if @environment.persisted?
      redirect_to project_environment_path(project, @environment)
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
      redirect_to project_environment_path(project, @environment)
    else
      render :edit
    return render_404 unless @environment.available?
Kamil Trzciński's avatar
Kamil Trzciński committed
    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
        project_environment_url(project, @environment)
      end

    respond_to do |format|
      format.html { redirect_to action_or_env_url }
      format.json { render json: { redirect_url: action_or_env_url } }
Kamil Trzciński's avatar
Kamil Trzciński committed
  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
    # 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
Lin Jen-Shin's avatar
Lin Jen-Shin committed
      render text: 'Not found', status: :not_found
    environment = project.default_environment
    if environment
      redirect_to environment_metrics_path(environment)
    else
      render :empty
    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

    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
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

  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

  def environment_params
    params.require(:environment).permit(:name, :external_url)
  def environment
    @environment ||= project.environments.find(params[:id])
Kamil Trzciński's avatar
Kamil Trzciński committed
  end
end