summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFatih Acet <acetfatih@gmail.com>2016-12-15 00:59:04 +0000
committerNick Thomas <nick@gitlab.com>2016-12-19 21:27:08 +0000
commit3db5b7033b13c21b904a21f751bc0f19156ea155 (patch)
tree0f5cfa65887744bb352dded9960217ae6df86ff5
parentc3d972f4e861059312c2708dacb57999416fcc70 (diff)
downloadgitlab-ce-3db5b7033b13c21b904a21f751bc0f19156ea155.tar.gz
Add terminal UI and controller actions
-rw-r--r--app/assets/javascripts/environments/components/environment.js.es63
-rw-r--r--app/assets/javascripts/environments/components/environment_item.js.es616
-rw-r--r--app/assets/javascripts/environments/components/environment_terminal_button.js.es627
-rw-r--r--app/assets/stylesheets/framework/buttons.scss7
-rw-r--r--app/assets/stylesheets/pages/pipelines.scss20
-rw-r--r--app/controllers/projects/environments_controller.rb14
-rw-r--r--app/serializers/environment_entity.rb10
-rw-r--r--app/serializers/request_aware_entity.rb4
-rw-r--r--app/views/projects/environments/_terminal_button.html.haml3
-rw-r--r--app/views/projects/environments/index.html.haml1
-rw-r--r--app/views/projects/environments/show.html.haml1
-rw-r--r--app/views/projects/environments/terminal.html.haml22
-rw-r--r--app/views/shared/icons/_icon_terminal.svg1
-rw-r--r--config/routes/project.rb2
-rw-r--r--spec/controllers/projects/environments_controller_spec.rb1
-rw-r--r--spec/factories/projects.rb10
-rw-r--r--spec/features/environment_spec.rb32
-rw-r--r--spec/features/environments_spec.rb28
18 files changed, 189 insertions, 13 deletions
diff --git a/app/assets/javascripts/environments/components/environment.js.es6 b/app/assets/javascripts/environments/components/environment.js.es6
index d04adecd207..facd653fd72 100644
--- a/app/assets/javascripts/environments/components/environment.js.es6
+++ b/app/assets/javascripts/environments/components/environment.js.es6
@@ -76,6 +76,7 @@
helpPagePath: environmentsData.helpPagePath,
commitIconSvg: environmentsData.commitIconSvg,
playIconSvg: environmentsData.playIconSvg,
+ terminalIconSvg: environmentsData.terminalIconSvg,
};
},
@@ -230,6 +231,7 @@
:can-create-deployment="canCreateDeploymentParsed"
:can-read-environment="canReadEnvironmentParsed"
:play-icon-svg="playIconSvg"
+ :terminal-icon-svg="terminalIconSvg"
:commit-icon-svg="commitIconSvg"></tr>
<tr v-if="model.isOpen && model.children && model.children.length > 0"
@@ -240,6 +242,7 @@
:can-create-deployment="canCreateDeploymentParsed"
:can-read-environment="canReadEnvironmentParsed"
:play-icon-svg="playIconSvg"
+ :terminal-icon-svg="terminalIconSvg"
:commit-icon-svg="commitIconSvg">
</tr>
diff --git a/app/assets/javascripts/environments/components/environment_item.js.es6 b/app/assets/javascripts/environments/components/environment_item.js.es6
index 4674d5202e6..b26a40aa268 100644
--- a/app/assets/javascripts/environments/components/environment_item.js.es6
+++ b/app/assets/javascripts/environments/components/environment_item.js.es6
@@ -8,6 +8,7 @@
/*= require ./environment_external_url */
/*= require ./environment_stop */
/*= require ./environment_rollback */
+/*= require ./environment_terminal_button */
(() => {
/**
@@ -33,6 +34,7 @@
'external-url-component': window.gl.environmentsList.ExternalUrlComponent,
'stop-component': window.gl.environmentsList.StopComponent,
'rollback-component': window.gl.environmentsList.RollbackComponent,
+ 'terminal-button-component': window.gl.environmentsList.TerminalButtonComponent,
},
props: {
@@ -68,6 +70,12 @@
type: String,
required: false,
},
+
+ terminalIconSvg: {
+ type: String,
+ required: false,
+ },
+
},
data() {
@@ -506,6 +514,14 @@
</stop-component>
</div>
+ <div v-if="model.terminal_path"
+ class="inline js-terminal-button-container">
+ <terminal-button-component
+ :terminal-icon-svg="terminalIconSvg"
+ :terminal-path="model.terminal_path">
+ </terminal-button-component>
+ </div>
+
<div v-if="canRetry && canCreateDeployment"
class="inline js-rollback-component-container">
<rollback-component
diff --git a/app/assets/javascripts/environments/components/environment_terminal_button.js.es6 b/app/assets/javascripts/environments/components/environment_terminal_button.js.es6
new file mode 100644
index 00000000000..25e6ac7f3c9
--- /dev/null
+++ b/app/assets/javascripts/environments/components/environment_terminal_button.js.es6
@@ -0,0 +1,27 @@
+/*= require vue */
+/* global Vue */
+
+(() => {
+ window.gl = window.gl || {};
+ window.gl.environmentsList = window.gl.environmentsList || {};
+
+ window.gl.environmentsList.TerminalButtonComponent = Vue.component('terminal-button-component', {
+ props: {
+ terminalPath: {
+ type: String,
+ default: '',
+ },
+ terminalIconSvg: {
+ type: String,
+ default: '',
+ },
+ },
+
+ template: `
+ <a class="btn terminal-button"
+ :href="terminalPath">
+ <span class="js-terminal-icon-container" v-html="terminalIconSvg"></span>
+ </a>
+ `,
+ });
+})();
diff --git a/app/assets/stylesheets/framework/buttons.scss b/app/assets/stylesheets/framework/buttons.scss
index 59ff17ad2c1..a11f1cd7735 100644
--- a/app/assets/stylesheets/framework/buttons.scss
+++ b/app/assets/stylesheets/framework/buttons.scss
@@ -230,6 +230,13 @@
}
}
+.btn-terminal {
+ svg {
+ height: 14px;
+ width: 18px;
+ }
+}
+
.btn-lg {
padding: 12px 20px;
}
diff --git a/app/assets/stylesheets/pages/pipelines.scss b/app/assets/stylesheets/pages/pipelines.scss
index be22e7bdc79..7905d2f2e7c 100644
--- a/app/assets/stylesheets/pages/pipelines.scss
+++ b/app/assets/stylesheets/pages/pipelines.scss
@@ -734,3 +734,23 @@
padding: 5px 5px 5px 7px;
}
}
+
+.terminal-icon {
+ margin-left: 3px;
+}
+
+.terminal-container {
+ .content-block {
+ border-bottom: none;
+ }
+
+ #terminal {
+ margin-top: 10px;
+ min-height: 450px;
+ box-sizing: border-box;
+
+ > div {
+ min-height: 450px;
+ }
+ }
+}
diff --git a/app/controllers/projects/environments_controller.rb b/app/controllers/projects/environments_controller.rb
index a1b39c6a78a..87cc36253f1 100644
--- a/app/controllers/projects/environments_controller.rb
+++ b/app/controllers/projects/environments_controller.rb
@@ -4,7 +4,9 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :authorize_create_environment!, only: [:new, :create]
before_action :authorize_create_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update]
- before_action :environment, only: [:show, :edit, :update, :stop]
+ before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
+ before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize]
+ before_action :verify_api_request!, only: :terminal_websocket_authorize
def index
@scope = params[:scope]
@@ -14,7 +16,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
format.html
format.json do
render json: EnvironmentSerializer
- .new(project: @project)
+ .new(project: @project, user: current_user)
.represent(@environments)
end
end
@@ -65,11 +67,9 @@ class Projects::EnvironmentsController < Projects::ApplicationController
# GET .../terminal.ws : implemented in gitlab-workhorse
def terminal_websocket_authorize
- Gitlab::Workhorse.verify_api_request!(request.headers)
-
# 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
+ # should retry those errors
terminal = environment.terminals.try(:first)
if terminal
set_workhorse_internal_api_content_type
@@ -81,6 +81,10 @@ class Projects::EnvironmentsController < Projects::ApplicationController
private
+ def verify_api_request!
+ Gitlab::Workhorse.verify_api_request!(request.headers)
+ end
+
def environment_params
params.require(:environment).permit(:name, :external_url)
end
diff --git a/app/serializers/environment_entity.rb b/app/serializers/environment_entity.rb
index e7ef01258ef..5d15eb8d3d3 100644
--- a/app/serializers/environment_entity.rb
+++ b/app/serializers/environment_entity.rb
@@ -8,7 +8,6 @@ class EnvironmentEntity < Grape::Entity
expose :environment_type
expose :last_deployment, using: DeploymentEntity
expose :stoppable?
- expose :has_terminals?, as: :has_terminals
expose :environment_path do |environment|
namespace_project_environment_path(
@@ -25,10 +24,11 @@ class EnvironmentEntity < Grape::Entity
end
expose :terminal_path, if: ->(environment, _) { environment.has_terminals? } do |environment|
- terminal_namespace_project_environment_path(
- environment.project.namespace,
- environment.project,
- environment)
+ can?(request.user, :admin_environment, environment.project) &&
+ terminal_namespace_project_environment_path(
+ environment.project.namespace,
+ environment.project,
+ environment)
end
expose :created_at, :updated_at
diff --git a/app/serializers/request_aware_entity.rb b/app/serializers/request_aware_entity.rb
index ff8c1142abc..e159d750cb7 100644
--- a/app/serializers/request_aware_entity.rb
+++ b/app/serializers/request_aware_entity.rb
@@ -8,4 +8,8 @@ module RequestAwareEntity
def request
@options.fetch(:request)
end
+
+ def can?(object, action, subject)
+ Ability.allowed?(object, action, subject)
+ end
end
diff --git a/app/views/projects/environments/_terminal_button.html.haml b/app/views/projects/environments/_terminal_button.html.haml
new file mode 100644
index 00000000000..97de9c95de7
--- /dev/null
+++ b/app/views/projects/environments/_terminal_button.html.haml
@@ -0,0 +1,3 @@
+- if environment.has_terminals? && can?(current_user, :admin_environment, @project)
+ = link_to terminal_namespace_project_environment_path(@project.namespace, @project, environment), class: 'btn terminal-button' do
+ = icon('terminal')
diff --git a/app/views/projects/environments/index.html.haml b/app/views/projects/environments/index.html.haml
index 6aae035b3e0..0c6f696f5b9 100644
--- a/app/views/projects/environments/index.html.haml
+++ b/app/views/projects/environments/index.html.haml
@@ -15,4 +15,5 @@
"help-page-path" => help_page_path("ci/environments"),
"css-class" => container_class,
"commit-icon-svg" => custom_icon("icon_commit"),
+ "terminal-icon-svg" => custom_icon("icon_terminal"),
"play-icon-svg" => custom_icon("icon_play")}}
diff --git a/app/views/projects/environments/show.html.haml b/app/views/projects/environments/show.html.haml
index bcac73d3698..6e0d9456900 100644
--- a/app/views/projects/environments/show.html.haml
+++ b/app/views/projects/environments/show.html.haml
@@ -8,6 +8,7 @@
%h3.page-title= @environment.name.capitalize
.col-md-3
.nav-controls
+ = render 'projects/environments/terminal_button', environment: @environment
= render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
diff --git a/app/views/projects/environments/terminal.html.haml b/app/views/projects/environments/terminal.html.haml
new file mode 100644
index 00000000000..a6726e509e0
--- /dev/null
+++ b/app/views/projects/environments/terminal.html.haml
@@ -0,0 +1,22 @@
+- @no_container = true
+- page_title "Terminal for environment", @environment.name
+= render "projects/pipelines/head"
+
+- content_for :page_specific_javascripts do
+ = stylesheet_link_tag "xterm/xterm"
+ = page_specific_javascript_tag("terminal/terminal_bundle.js")
+
+%div{class: container_class}
+ .top-area
+ .row
+ .col-sm-6
+ %h3.page-title
+ Terminal for environment
+ = @environment.name
+
+ .col-sm-6
+ .nav-controls
+ = render 'projects/deployments/actions', deployment: @environment.last_deployment
+
+.terminal-container{class: container_class}
+ #terminal{data:{project_path: "#{terminal_namespace_project_environment_path(@project.namespace, @project, @environment)}.ws"}}
diff --git a/app/views/shared/icons/_icon_terminal.svg b/app/views/shared/icons/_icon_terminal.svg
new file mode 100644
index 00000000000..c80f44c3edf
--- /dev/null
+++ b/app/views/shared/icons/_icon_terminal.svg
@@ -0,0 +1 @@
+<svg width="19" height="14" viewBox="0 0 19 14" xmlns="http://www.w3.org/2000/svg"><rect fill="#848484" x="7.2" y="9.25" width="6.46" height="1.5" rx=".5"/><path d="M5.851 7.016L3.81 9.103a.503.503 0 0 0 .017.709l.35.334c.207.198.524.191.717-.006l2.687-2.748a.493.493 0 0 0 .137-.376.493.493 0 0 0-.137-.376L4.894 3.892a.507.507 0 0 0-.717-.006l-.35.334a.503.503 0 0 0-.017.709L5.85 7.016z"/><path d="M1.25 11.497c0 .691.562 1.253 1.253 1.253h13.994c.694 0 1.253-.56 1.253-1.253V2.503c0-.691-.562-1.253-1.253-1.253H2.503c-.694 0-1.253.56-1.253 1.253v8.994zM2.503 0h13.994A2.504 2.504 0 0 1 19 2.503v8.994A2.501 2.501 0 0 1 16.497 14H2.503A2.504 2.504 0 0 1 0 11.497V2.503A2.501 2.501 0 0 1 2.503 0z"/></svg>
diff --git a/config/routes/project.rb b/config/routes/project.rb
index e17d6bae10c..80cc47ab9a8 100644
--- a/config/routes/project.rb
+++ b/config/routes/project.rb
@@ -148,6 +148,8 @@ constraints(ProjectUrlConstrainer.new) do
resources :environments, except: [:destroy] do
member do
post :stop
+ get :terminal
+ get '/terminal.ws/authorize', to: 'environments#terminal_websocket_authorize', constraints: { format: nil }
end
end
diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb
index 7afa8b1bc28..7ac1d62d1b1 100644
--- a/spec/controllers/projects/environments_controller_spec.rb
+++ b/spec/controllers/projects/environments_controller_spec.rb
@@ -123,6 +123,7 @@ describe Projects::EnvironmentsController do
context 'and invalid id' do
it 'returns 404' do
get :terminal_websocket_authorize, environment_params(id: 666)
+
expect(response).to have_http_status(404)
end
end
diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb
index c941fb5ef4b..f7fa834d7a2 100644
--- a/spec/factories/projects.rb
+++ b/spec/factories/projects.rb
@@ -42,6 +42,12 @@ FactoryGirl.define do
end
end
+ trait :test_repo do
+ after :create do |project|
+ TestEnv.copy_repo(project)
+ end
+ end
+
# Nest Project Feature attributes
transient do
wiki_access_level ProjectFeature::ENABLED
@@ -91,9 +97,7 @@ FactoryGirl.define do
factory :project, parent: :empty_project do
path { 'gitlabhq' }
- after :create do |project|
- TestEnv.copy_repo(project)
- end
+ test_repo
end
factory :forked_project_with_submodules, parent: :empty_project do
diff --git a/spec/features/environment_spec.rb b/spec/features/environment_spec.rb
index 0c1939fd885..56f6cd2e095 100644
--- a/spec/features/environment_spec.rb
+++ b/spec/features/environment_spec.rb
@@ -38,6 +38,10 @@ feature 'Environment', :feature do
scenario 'does not show a re-deploy button for deployment without build' do
expect(page).not_to have_link('Re-deploy')
end
+
+ scenario 'does not show terminal button' do
+ expect(page).not_to have_terminal_button
+ end
end
context 'with related deployable present' do
@@ -60,6 +64,10 @@ feature 'Environment', :feature do
expect(page).not_to have_link('Stop')
end
+ scenario 'does not show terminal button' do
+ expect(page).not_to have_terminal_button
+ end
+
context 'with manual action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'deploy to production') }
@@ -84,6 +92,26 @@ feature 'Environment', :feature do
end
end
+ context 'with terminal' do
+ let(:project) { create(:kubernetes_project, :test_repo) }
+
+ context 'for project master' do
+ let(:role) { :master }
+
+ scenario 'it shows the terminal button' do
+ expect(page).to have_terminal_button
+ end
+ end
+
+ context 'for developer' do
+ let(:role) { :developer }
+
+ scenario 'does not show terminal button' do
+ expect(page).not_to have_terminal_button
+ end
+ end
+ end
+
context 'with stop action' do
given(:manual) { create(:ci_build, :manual, pipeline: pipeline, name: 'close_app') }
given(:deployment) { create(:deployment, environment: environment, deployable: build, on_stop: 'close_app') }
@@ -158,4 +186,8 @@ feature 'Environment', :feature do
environment.project,
environment)
end
+
+ def have_terminal_button
+ have_link(nil, href: terminal_namespace_project_environment_path(project.namespace, project, environment))
+ end
end
diff --git a/spec/features/environments_spec.rb b/spec/features/environments_spec.rb
index e1b97b31e5d..72b984cfab8 100644
--- a/spec/features/environments_spec.rb
+++ b/spec/features/environments_spec.rb
@@ -113,6 +113,10 @@ feature 'Environments page', :feature, :js do
expect(page).not_to have_css('external-url')
end
+ scenario 'does not show terminal button' do
+ expect(page).not_to have_terminal_button
+ end
+
context 'with external_url' do
given(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
given(:build) { create(:ci_build, pipeline: pipeline) }
@@ -145,6 +149,26 @@ feature 'Environments page', :feature, :js do
end
end
end
+
+ context 'with terminal' do
+ let(:project) { create(:kubernetes_project, :test_repo) }
+
+ context 'for project master' do
+ let(:role) { :master }
+
+ scenario 'it shows the terminal button' do
+ expect(page).to have_terminal_button
+ end
+ end
+
+ context 'for developer' do
+ let(:role) { :developer }
+
+ scenario 'does not show terminal button' do
+ expect(page).not_to have_terminal_button
+ end
+ end
+ end
end
end
end
@@ -195,6 +219,10 @@ feature 'Environments page', :feature, :js do
end
end
+ def have_terminal_button
+ have_link(nil, href: terminal_namespace_project_environment_path(project.namespace, project, environment))
+ end
+
def visit_environments(project)
visit namespace_project_environments_path(project.namespace, project)
end