diff options
Diffstat (limited to 'app/models/environment.rb')
-rw-r--r-- | app/models/environment.rb | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb index 652abf18a8a..bf33010fd21 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -1,11 +1,13 @@ class Environment < ActiveRecord::Base # Used to generate random suffixes for the slug + LETTERS = 'a'..'z' NUMBERS = '0'..'9' - SUFFIX_CHARS = ('a'..'z').to_a + NUMBERS.to_a + SUFFIX_CHARS = LETTERS.to_a + NUMBERS.to_a belongs_to :project, required: true, validate: true - has_many :deployments + has_many :deployments, dependent: :destroy + has_one :last_deployment, -> { order('deployments.id DESC') }, class_name: 'Deployment' before_validation :nullify_external_url before_validation :generate_slug, if: ->(env) { env.slug.blank? } @@ -36,6 +38,13 @@ class Environment < ActiveRecord::Base scope :available, -> { with_state(:available) } scope :stopped, -> { with_state(:stopped) } + scope :order_by_last_deployed_at, -> do + max_deployment_id_sql = + Deployment.select(Deployment.arel_table[:id].maximum). + where(Deployment.arel_table[:environment_id].eq(arel_table[:id])). + to_sql + order(Gitlab::Database.nulls_first_order("(#{max_deployment_id_sql})", 'ASC')) + end state_machine :state, initial: :available do event :start do @@ -61,10 +70,6 @@ class Environment < ActiveRecord::Base ref.to_s == last_deployment.try(:ref) end - def last_deployment - deployments.last - end - def nullify_external_url self.external_url = nil if self.external_url.blank? end @@ -86,6 +91,10 @@ class Environment < ActiveRecord::Base last_deployment.includes_commit?(commit) end + def last_deployed_at + last_deployment.try(:created_at) + end + def update_merge_request_metrics? (environment_type || name) == "production" end @@ -109,15 +118,15 @@ class Environment < ActiveRecord::Base external_url.gsub(/\A.*?:\/\//, '') end - def stoppable? + def stop_action? available? && stop_action.present? end - def stop!(current_user) - return unless stoppable? + def stop_with_action!(current_user) + return unless available? - stop - stop_action.play(current_user) + stop! + stop_action&.play(current_user) end def actions_for(environment) @@ -136,6 +145,14 @@ class Environment < ActiveRecord::Base project.deployment_service.terminals(self) if has_terminals? end + def has_metrics? + project.monitoring_service.present? && available? && last_deployment.present? + end + + def metrics + project.monitoring_service.metrics(self) if has_metrics? + end + # An environment name is not necessarily suitable for use in URLs, DNS # or other third-party contexts, so provide a slugified version. A slug has # the following properties: @@ -148,21 +165,37 @@ class Environment < ActiveRecord::Base slugified = name.to_s.downcase.gsub(/[^a-z0-9]/, '-') # Must start with a letter - slugified = "env-" + slugified if NUMBERS.cover?(slugified[0]) + slugified = 'env-' + slugified unless LETTERS.cover?(slugified[0]) + + # Repeated dashes are invalid (OpenShift limitation) + slugified.gsub!(/\-+/, '-') # Maximum length: 24 characters (OpenShift limitation) slugified = slugified[0..23] - # Cannot end with a "-" character (Kubernetes label limitation) - slugified = slugified[0..-2] if slugified[-1] == "-" + # Cannot end with a dash (Kubernetes label limitation) + slugified.chop! if slugified.end_with?('-') # Add a random suffix, shortening the current string if necessary, if it # has been slugified. This ensures uniqueness. - slugified = slugified[0..16] + "-" + random_suffix if slugified != name + if slugified != name + slugified = slugified[0..16] + slugified << '-' unless slugified.end_with?('-') + slugified << random_suffix + end self.slug = slugified end + def external_url_for(path, commit_sha) + return unless self.external_url + + public_path = project.public_path_for_source_path(path, commit_sha) + return unless public_path + + [external_url, public_path].join('/') + end + private # Slugifying a name may remove the uniqueness guarantee afforded by it being |