BigW Consortium Gitlab

environments_controller.rb 4.32 KB
Newer Older
Kamil Trzcinski committed
1 2
class Projects::EnvironmentsController < Projects::ApplicationController
  layout 'project'
3
  before_action :authorize_read_environment!
Kamil Trzcinski committed
4
  before_action :authorize_create_environment!, only: [:new, :create]
5 6
  before_action :authorize_create_deployment!, only: [:stop]
  before_action :authorize_update_environment!, only: [:edit, :update]
7
  before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
8
  before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
9
  before_action :verify_api_request!, only: :terminal_websocket_authorize
Kamil Trzcinski committed
10 11

  def index
12 13
    @environments = project.environments
      .with_state(params[:scope] || :available)
14

15 16 17
    respond_to do |format|
      format.html
      format.json do
18 19
        render json: {
          environments: EnvironmentSerializer
20
            .new(project: @project, current_user: @current_user)
21 22 23 24 25 26
            .with_pagination(request, response)
            .within_folders
            .represent(@environments),
          available_count: project.environments.available.count,
          stopped_count: project.environments.stopped.count
        }
27
      end
28
    end
Kamil Trzcinski committed
29 30
  end

31
  def folder
32 33
    folder_environments = project.environments.where(environment_type: params[:id])
    @environments = folder_environments.with_state(params[:scope] || :available)
34
      .order(:name)
35 36 37 38 39 40

    respond_to do |format|
      format.html
      format.json do
        render json: {
          environments: EnvironmentSerializer
41
            .new(project: @project, current_user: @current_user)
42 43
            .with_pagination(request, response)
            .represent(@environments),
44 45
          available_count: folder_environments.available.count,
          stopped_count: folder_environments.stopped.count
46 47 48 49 50
        }
      end
    end
  end

Kamil Trzcinski committed
51
  def show
52
    @deployments = environment.deployments.order(id: :desc).page(params[:page])
Kamil Trzcinski committed
53 54 55 56 57 58
  end

  def new
    @environment = project.environments.new
  end

59 60 61
  def edit
  end

Kamil Trzcinski committed
62
  def create
63
    @environment = project.environments.create(environment_params)
64 65 66 67

    if @environment.persisted?
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
68 69 70 71 72 73 74 75 76
      render :new
    end
  end

  def update
    if @environment.update(environment_params)
      redirect_to namespace_project_environment_path(project.namespace, project, @environment)
    else
      render :edit
Kamil Trzcinski committed
77 78 79
    end
  end

80
  def stop
81
    return render_404 unless @environment.available?
82

Kamil Trzcinski committed
83 84
    stop_action = @environment.stop_with_action!(current_user)

85 86 87 88 89 90 91 92 93 94
    action_or_env_url =
      if stop_action
        polymorphic_url([project.namespace.becomes(Namespace), project, stop_action])
      else
        namespace_project_environment_url(project.namespace, 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 } }
95
    end
Kamil Trzcinski committed
96 97
  end

98 99 100 101 102 103 104 105 106 107 108
  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
109
    # should retry those errors
110 111 112 113 114 115 116 117 118
    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

119 120 121 122 123 124 125 126 127 128 129 130 131
  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

132 133
  private

134 135 136 137
  def verify_api_request!
    Gitlab::Workhorse.verify_api_request!(request.headers)
  end

138 139
  def environment_params
    params.require(:environment).permit(:name, :external_url)
Kamil Trzcinski committed
140 141
  end

142
  def environment
143
    @environment ||= project.environments.find(params[:id])
Kamil Trzcinski committed
144 145
  end
end