Commit 6cdbb27e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Refactor code to use available and stopped statuses and refactor views to use separate renders

parent 7aea16b6
...@@ -10,8 +10,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -10,8 +10,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
@all_environments = project.environments @all_environments = project.environments
@environments = @environments =
case @scope case @scope
when 'closed' then @all_environments.closed when 'stopped' then @all_environments.stopped
else @all_environments.opened else @all_environments.available
end end
end end
...@@ -45,7 +45,9 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -45,7 +45,9 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end end
def stop def stop
action = @environment.stop_action
new_action = action.active? ? action : action.play(current_user)
redirect_to [project.namespace.become(Namespace), project, new_action]
end end
def destroy def destroy
......
...@@ -416,7 +416,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -416,7 +416,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
id: environment.id, id: environment.id,
name: environment.name, name: environment.name,
url: namespace_project_environment_path(project.namespace, project, environment), url: namespace_project_environment_path(project.namespace, project, environment),
stop_url: (stop_namespace_project_environment_path(project.namespace, project, environment) if environment.closeable?), stop_url: (stop_namespace_project_environment_path(project.namespace, project, environment) if environment.stoppable?),
external_url: environment.external_url, external_url: environment.external_url,
external_url_formatted: environment.formatted_external_url, external_url_formatted: environment.formatted_external_url,
deployed_at: deployment.try(:created_at), deployed_at: deployment.try(:created_at),
......
...@@ -106,14 +106,6 @@ module Ci ...@@ -106,14 +106,6 @@ module Ci
project.builds_enabled? && commands.present? && manual? && skipped? project.builds_enabled? && commands.present? && manual? && skipped?
end end
def close_environment?
options.fetch(:environment, {}).fetch(:close, false)
end
def closes_environment?(name)
environment == name && close_environment?
end
def play(current_user = nil) def play(current_user = nil)
# Try to queue a current build # Try to queue a current build
if self.enqueue if self.enqueue
......
...@@ -11,6 +11,8 @@ class Deployment < ActiveRecord::Base ...@@ -11,6 +11,8 @@ class Deployment < ActiveRecord::Base
delegate :name, to: :environment, prefix: true delegate :name, to: :environment, prefix: true
store :properties, accessors: [:on_stop]
after_save :create_ref after_save :create_ref
def commit def commit
...@@ -34,7 +36,7 @@ class Deployment < ActiveRecord::Base ...@@ -34,7 +36,7 @@ class Deployment < ActiveRecord::Base
end end
def manual_actions def manual_actions
deployable.try(:other_actions) @manual_actions ||= deployable.try(:other_actions)
end end
def includes_commit?(commit) def includes_commit?(commit)
...@@ -84,17 +86,14 @@ class Deployment < ActiveRecord::Base ...@@ -84,17 +86,14 @@ class Deployment < ActiveRecord::Base
take take
end end
def close_action def stop_action
return nil unless manual_actions return nil unless manual_actions
@close_action ||= @stop_action ||= manual_actions.find_by(name: on_stop)
manual_actions.find do |manual_action|
manual_action.try(:closes_environment?, deployable.environment)
end
end end
def closeable? def stoppable?
close_action.present? on_stop.present? && stop_action.present?
end end
def formatted_deployment_time def formatted_deployment_time
......
...@@ -19,22 +19,22 @@ class Environment < ActiveRecord::Base ...@@ -19,22 +19,22 @@ class Environment < ActiveRecord::Base
allow_nil: true, allow_nil: true,
addressable_url: true addressable_url: true
delegate :closeable?, :close_action, to: :last_deployment, allow_nil: true delegate :stoppable?, :stop_action, to: :last_deployment, allow_nil: true
scope :opened, -> { where(state: [:opened]) } scope :available, -> { where(state: [:available]) }
scope :closed, -> { where(state: [:closed]) } scope :stopped, -> { where(state: [:stopped]) }
state_machine :state, initial: :opened do state_machine :state, initial: :available do
event :close do event :start do
transition opened: :closed transition stopped: :available
end end
event :reopen do event :stop do
transition closed: :opened transition available: :stopped
end end
state :opened state :available
state :closed state :stopped
end end
def last_deployment def last_deployment
......
...@@ -1292,7 +1292,7 @@ class Project < ActiveRecord::Base ...@@ -1292,7 +1292,7 @@ class Project < ActiveRecord::Base
environment_ids.where(ref: ref) environment_ids.where(ref: ref)
end end
environments.opened.where(id: environment_ids).select do |environment| environments.available.where(id: environment_ids).select do |environment|
environment.includes_commit?(commit) environment.includes_commit?(commit)
end end
end end
......
...@@ -8,12 +8,12 @@ class CreateDeploymentService < BaseService ...@@ -8,12 +8,12 @@ class CreateDeploymentService < BaseService
@deployable = deployable @deployable = deployable
@environment = prepare_environment @environment = prepare_environment
if close? if stop?
@environment.close @environment.stop
return return
end end
@environment.reopen @environment.start
deploy.tap do |deployment| deploy.tap do |deployment|
deployment.update_merge_request_metrics! deployment.update_merge_request_metrics!
...@@ -61,10 +61,6 @@ class CreateDeploymentService < BaseService ...@@ -61,10 +61,6 @@ class CreateDeploymentService < BaseService
options[:url] options[:url]
end end
def close?
options[:close]
end
def options def options
params[:options] || {} params[:options] || {}
end end
...@@ -72,4 +68,8 @@ class CreateDeploymentService < BaseService ...@@ -72,4 +68,8 @@ class CreateDeploymentService < BaseService
def variables def variables
params[:variables] || [] params[:variables] || []
end end
def stop?
params[:options].fetch(:stop, false)
end
end end
- if can?(current_user, :create_deployment, deployment) && deployment.deployable - if can?(current_user, :create_deployment, deployment)
.pull-right - actions = deployment.manual_actions
- if actions.present?
- external_url = deployment.environment.external_url .inline
- if external_url .dropdown
= link_to external_url, target: '_blank', class: 'btn external-url' do %a.dropdown-new.btn.btn-default{type: 'button', 'data-toggle' => 'dropdown'}
= icon('external-link') = custom_icon('icon_play')
= icon('caret-down')
- actions = deployment.manual_actions %ul.dropdown-menu.dropdown-menu-align-right
- if actions.present? - actions.each do |action|
.inline %li
.dropdown = link_to [:play, @project.namespace.becomes(Namespace), @project, action], method: :post, rel: 'nofollow' do
%a.dropdown-new.btn.btn-default{type: 'button', 'data-toggle' => 'dropdown'} = custom_icon('icon_play')
= custom_icon('icon_play') %span= action.name.humanize
= icon('caret-down')
%ul.dropdown-menu.dropdown-menu-align-right
- actions.each do |action|
%li
= link_to [:play, @project.namespace.becomes(Namespace), @project, action], method: :post, rel: 'nofollow' do
= custom_icon('icon_play')
%span= action.name.humanize
- if local_assigns.fetch(:allow_close, false) && deployment.closeable?
.inline
= link_to [:play, @project.namespace.becomes(Namespace), @project, deployment.close_action], method: :post, class: 'btn close-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to close this environment?' } do
= icon('stop', class: 'close-env-icon')
- if local_assigns.fetch(:allow_rollback, false)
= link_to [:retry, @project.namespace.becomes(Namespace), @project, deployment.deployable], method: :post, class: 'btn' do
- if deployment.last?
Re-deploy
- else
Rollback
...@@ -17,4 +17,6 @@ ...@@ -17,4 +17,6 @@
#{time_ago_with_tooltip(deployment.created_at)} #{time_ago_with_tooltip(deployment.created_at)}
%td.hidden-xs %td.hidden-xs
= render 'projects/deployments/actions', deployment: deployment, allow_rollback: true .pull-right
= render 'projects/deployments/actions', deployment: deployment
= render 'projects/deployments/rollback', deployment: deployment
- if can?(current_user, :create_deployment, deployment) && deployment.deployable
= link_to [:retry, @project.namespace.becomes(Namespace), @project, deployment.deployable], method: :post, class: 'btn btn-build' do
- if deployment.last?
Re-deploy
- else
Rollback
...@@ -28,4 +28,8 @@ ...@@ -28,4 +28,8 @@
#{time_ago_with_tooltip(last_deployment.created_at)} #{time_ago_with_tooltip(last_deployment.created_at)}
%td.hidden-xs %td.hidden-xs
= render 'projects/deployments/actions', deployment: last_deployment, allow_close: environment.opened? .pull-right
= render 'projects/environments/external_url', environment: environment
= render 'projects/deployments/actions', deployment: last_deployment
= render 'projects/environments/stop', environment: environment
= render 'projects/deployments/rollback', deployment: last_deployment
- if environment.external_url
= link_to environment.external_url, target: '_blank', class: 'btn external-url' do
= icon('external-link')
- if environment.available? && environment.stoppable?
.inline
= link_to stop_namespace_project_environment_path(@project.namespace, @project, environment), method: :post,
class: 'btn close-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to close this environment?' } do
= icon('stop', class: 'close-env-icon')
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
%li{class: ('active' if @scope.nil?)} %li{class: ('active' if @scope.nil?)}
= link_to project_environments_path(@project) do = link_to project_environments_path(@project) do
Available Available
%span.badge.js-avaibale-environments-count %span.badge.js-available-environments-count
= number_with_delimiter(@all_environments.opened.count) = number_with_delimiter(@all_environments.available.count)
%li{class: ('active' if @scope == 'closed')} %li{class: ('active' if @scope == 'stopped')}
= link_to project_environments_path(@project, scope: :closed) do = link_to project_environments_path(@project, scope: :stopped) do
Stopped Stopped
%span.badge.js-stopped-environments-count %span.badge.js-stopped-environments-count
= number_with_delimiter(@all_environments.closed.count) = number_with_delimiter(@all_environments.stopped.count)
.nav-controls .nav-controls
- if can?(current_user, :create_environment, @project) && !@all_environments.blank? - if can?(current_user, :create_environment, @project) && !@all_environments.blank?
......
...@@ -2,18 +2,20 @@ ...@@ -2,18 +2,20 @@
- page_title "Environments" - page_title "Environments"
= render "projects/pipelines/head" = render "projects/pipelines/head"
- last_deployment = @environment.last_deployment
%div{ class: container_class } %div{ class: container_class }
.top-area .top-area
.col-md-9 .col-md-9
%h3.page-title= @environment.name.capitalize %h3.page-title= @environment.name.capitalize
.col-md-3 .col-md-3
.nav-controls .nav-controls
- if can?(current_user, :read_environmnet, @environment)
= render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment) - if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn' = link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
- if @environment.opened? && last_deployment.try(:close_action) - if @environment.available? && @environment.stoppable?
= link_to 'Close', [:play, @project.namespace.becomes(Namespace), @project, last_deployment.close_action], data: { confirm: 'Are you sure you want to close this environment?' }, class: 'btn btn-danger', method: :post = link_to 'Stop', stop_namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to close this environment?' }, class: 'btn btn-danger', method: :post
= link_to 'Destroy', namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to delete this environment?' }, class: 'btn btn-danger', method: :delete
.deployments-container .deployments-container
- if @deployments.blank? - if @deployments.blank?
......
...@@ -16,8 +16,8 @@ class Gitlab::Seeder::Pipelines ...@@ -16,8 +16,8 @@ class Gitlab::Seeder::Pipelines
{ name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending }, { name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending },
{ name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running }, { name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running },
{ name: 'env:gamma', stage: 'deploy', environment: 'gamma', status: :canceled }, { name: 'env:gamma', stage: 'deploy', environment: 'gamma', status: :canceled },
{ name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success }, { name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success, options: { environment: { on_stop: 'stop staging' } } },
{ name: 'close staging', stage: 'deploy', environment: 'staging', when: 'manual', status: :skipped, options: { environment: { close: true } } }, { name: 'stop staging', stage: 'deploy', environment: 'staging', when: 'manual', status: :skipped },
{ name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped }, { name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
{ name: 'slack', stage: 'notify', when: 'manual', status: :created }, { name: 'slack', stage: 'notify', when: 'manual', status: :created },
] ]
......
...@@ -6,7 +6,7 @@ class AddStateToEnvironment < ActiveRecord::Migration ...@@ -6,7 +6,7 @@ class AddStateToEnvironment < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
def up def up
add_column_with_default(:environments, :state, :string, default: :opened) add_column_with_default(:environments, :state, :string, default: :available)
end end
def down def down
......
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddPropertiesToDeployment < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
# When a migration requires downtime you **must** uncomment the following
# constant and define a short and easy to understand explanation as to why the
# migration requires downtime.
# DOWNTIME_REASON = ''
# When using the methods "add_concurrent_index" or "add_column_with_default"
# you must disable the use of transactions as these methods can not run in an
# existing transaction. When using "add_concurrent_index" make sure that this
# method is the _only_ method called in the migration, any other changes
# should go in a separate migration. This ensures that upon failure _only_ the
# index creation fails and can be retried or reverted easily.
#
# To disable transactions uncomment the following line and remove these
# comments:
# disable_ddl_transaction!
def change
add_column :deployments, :properties, :text
end
end
...@@ -398,22 +398,13 @@ ActiveRecord::Schema.define(version: 20161007133303) do ...@@ -398,22 +398,13 @@ ActiveRecord::Schema.define(version: 20161007133303) do
add_index "emails", ["user_id"], name: "index_emails_on_user_id", using: :btree add_index "emails", ["user_id"], name: "index_emails_on_user_id", using: :btree
create_table "environments", force: :cascade do |t| create_table "environments", force: :cascade do |t|
<<<<<<< HEAD
t.integer "project_id"
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "external_url"
t.string "environment_type"
t.string "state", default: "opened", null: false
=======
t.integer "project_id" t.integer "project_id"
t.string "name", null: false t.string "name", null: false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "external_url" t.string "external_url"
t.string "environment_type" t.string "environment_type"
>>>>>>> origin/master t.string "state", default: "available", null: false
end end
add_index "environments", ["project_id", "name"], name: "index_environments_on_project_id_and_name", using: :btree add_index "environments", ["project_id", "name"], name: "index_environments_on_project_id_and_name", using: :btree
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment