2019-10-16 14:08:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Deployments
|
2020-10-05 02:08:45 -04:00
|
|
|
class UpdateEnvironmentService
|
2019-10-16 14:08:01 -04:00
|
|
|
attr_reader :deployment
|
|
|
|
attr_reader :deployable
|
|
|
|
|
|
|
|
delegate :environment, to: :deployment
|
|
|
|
delegate :variables, to: :deployable
|
|
|
|
delegate :options, to: :deployable, allow_nil: true
|
|
|
|
|
2021-06-08 05:09:56 -04:00
|
|
|
EnvironmentUpdateFailure = Class.new(StandardError)
|
|
|
|
|
2019-10-16 14:08:01 -04:00
|
|
|
def initialize(deployment)
|
|
|
|
@deployment = deployment
|
|
|
|
@deployable = deployment.deployable
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
deployment.create_ref
|
|
|
|
deployment.invalidate_cache
|
|
|
|
|
|
|
|
update_environment(deployment)
|
|
|
|
|
|
|
|
deployment
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_environment(deployment)
|
2021-12-20 07:10:29 -05:00
|
|
|
ApplicationRecord.transaction do
|
2021-03-11 13:09:23 -05:00
|
|
|
# Renew attributes at update
|
|
|
|
renew_external_url
|
2019-12-10 02:53:40 -05:00
|
|
|
renew_auto_stop_in
|
2021-03-11 13:09:23 -05:00
|
|
|
renew_deployment_tier
|
2019-10-16 14:08:01 -04:00
|
|
|
environment.fire_state_event(action)
|
|
|
|
|
2021-06-08 05:09:56 -04:00
|
|
|
if environment.save
|
|
|
|
deployment.update_merge_request_metrics! unless environment.stopped?
|
|
|
|
else
|
|
|
|
# If there is a validation error on environment update, such as
|
|
|
|
# the external URL is malformed, the error message is recorded for debugging purpose.
|
|
|
|
# We should surface the error message to users for letting them to take an action.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/21182.
|
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
EnvironmentUpdateFailure.new,
|
|
|
|
project_id: deployment.project_id,
|
|
|
|
environment_id: environment.id,
|
|
|
|
reason: environment.errors.full_messages.to_sentence)
|
2019-10-16 14:08:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def environment_options
|
|
|
|
options&.dig(:environment) || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def expanded_environment_url
|
2022-04-14 11:08:59 -04:00
|
|
|
return unless environment_url
|
|
|
|
|
2022-07-26 02:11:51 -04:00
|
|
|
ExpandVariables.expand(environment_url, -> { variables.sort_and_expand_all })
|
2019-10-16 14:08:01 -04:00
|
|
|
end
|
|
|
|
|
2022-09-12 05:13:02 -04:00
|
|
|
def expanded_auto_stop_in
|
|
|
|
return unless auto_stop_in
|
|
|
|
|
|
|
|
ExpandVariables.expand(auto_stop_in, -> { variables.sort_and_expand_all })
|
|
|
|
end
|
|
|
|
|
2019-10-16 14:08:01 -04:00
|
|
|
def environment_url
|
|
|
|
environment_options[:url]
|
|
|
|
end
|
|
|
|
|
|
|
|
def action
|
|
|
|
environment_options[:action] || 'start'
|
|
|
|
end
|
2019-12-10 02:53:40 -05:00
|
|
|
|
2022-09-12 05:13:02 -04:00
|
|
|
def auto_stop_in
|
|
|
|
deployable&.environment_auto_stop_in
|
|
|
|
end
|
|
|
|
|
2021-03-11 13:09:23 -05:00
|
|
|
def renew_external_url
|
|
|
|
if (url = expanded_environment_url)
|
|
|
|
environment.external_url = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 02:53:40 -05:00
|
|
|
def renew_auto_stop_in
|
|
|
|
return unless deployable
|
|
|
|
|
2022-09-12 05:13:02 -04:00
|
|
|
if (value = expanded_auto_stop_in)
|
|
|
|
environment.auto_stop_in = value
|
|
|
|
end
|
2019-12-10 02:53:40 -05:00
|
|
|
end
|
2021-03-11 13:09:23 -05:00
|
|
|
|
|
|
|
def renew_deployment_tier
|
2021-03-15 14:09:05 -04:00
|
|
|
return unless deployable
|
2021-03-11 13:09:23 -05:00
|
|
|
|
2022-07-29 17:08:53 -04:00
|
|
|
if (tier = deployable.environment_tier_from_options)
|
2021-03-11 13:09:23 -05:00
|
|
|
environment.tier = tier
|
|
|
|
end
|
|
|
|
end
|
2019-10-16 14:08:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Deployments::UpdateEnvironmentService.prepend_mod_with('Deployments::UpdateEnvironmentService')
|