summaryrefslogtreecommitdiff
path: root/app/models/environment.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/environment.rb')
-rw-r--r--app/models/environment.rb48
1 files changed, 47 insertions, 1 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 4f7f688a040..d89909a71a2 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -39,6 +39,7 @@ class Environment < ApplicationRecord
before_validation :generate_slug, if: ->(env) { env.slug.blank? }
before_save :set_environment_type
+ before_save :ensure_environment_tier
after_save :clear_reactive_cache!
validates :name,
@@ -87,6 +88,7 @@ class Environment < ApplicationRecord
end
scope :for_project, -> (project) { where(project_id: project) }
+ scope :for_tier, -> (tier) { where(tier: tier).where('tier IS NOT NULL') }
scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) }
scope :unfoldered, -> { where(environment_type: nil) }
scope :with_rank, -> do
@@ -94,6 +96,30 @@ class Environment < ApplicationRecord
end
scope :for_id, -> (id) { where(id: id) }
+ scope :stopped_review_apps, -> (before, limit) do
+ stopped
+ .in_review_folder
+ .where("created_at < ?", before)
+ .order("created_at ASC")
+ .limit(limit)
+ end
+
+ scope :scheduled_for_deletion, -> do
+ where.not(auto_delete_at: nil)
+ end
+
+ scope :not_scheduled_for_deletion, -> do
+ where(auto_delete_at: nil)
+ end
+
+ enum tier: {
+ production: 0,
+ staging: 1,
+ testing: 2,
+ development: 3,
+ other: 4
+ }
+
state_machine :state, initial: :available do
event :start do
transition stopped: :available
@@ -137,6 +163,10 @@ class Environment < ApplicationRecord
self.state_machine.states.map(&:name)
end
+ def self.schedule_to_delete(at_time = 1.week.from_now)
+ update_all(auto_delete_at: at_time)
+ end
+
class << self
##
# This method returns stop actions (jobs) for multiple environments within one
@@ -242,7 +272,7 @@ class Environment < ApplicationRecord
def cancel_deployment_jobs!
jobs = active_deployments.with_deployable
jobs.each do |deployment|
- Gitlab::OptimisticLocking.retry_lock(deployment.deployable) do |deployable|
+ Gitlab::OptimisticLocking.retry_lock(deployment.deployable, name: 'environment_cancel_deployment_jobs') do |deployable|
deployable.cancel! if deployable&.cancelable?
end
rescue => e
@@ -429,6 +459,22 @@ class Environment < ApplicationRecord
def generate_slug
self.slug = Gitlab::Slug::Environment.new(name).generate
end
+
+ def ensure_environment_tier
+ self.tier ||= guess_tier
+ end
+
+ # Guessing the tier of the environment if it's not explicitly specified by users.
+ # See https://en.wikipedia.org/wiki/Deployment_environment for industry standard deployment environments
+ def guess_tier
+ case name
+ when %r{dev|review|trunk}i then self.class.tiers[:development]
+ when %r{test|qc}i then self.class.tiers[:testing]
+ when %r{st(a|)g|mod(e|)l|pre|demo}i then self.class.tiers[:staging]
+ when %r{pr(o|)d|live}i then self.class.tiers[:production]
+ else self.class.tiers[:other]
+ end
+ end
end
Environment.prepend_if_ee('EE::Environment')