2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2017-08-17 22:00:37 +05:30
|
|
|
module Entry
|
2016-09-29 09:46:39 +05:30
|
|
|
##
|
|
|
|
# Entry that represents an environment.
|
|
|
|
#
|
2019-02-15 15:39:39 +05:30
|
|
|
class Environment < ::Gitlab::Config::Entry::Node
|
2020-01-01 13:55:28 +05:30
|
|
|
include ::Gitlab::Config::Entry::Configurable
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
ALLOWED_KEYS = %i[name url action on_stop auto_stop_in kubernetes deployment_tier].freeze
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
entry :kubernetes, Entry::Kubernetes, description: 'Kubernetes deployment configuration.'
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
validations do
|
|
|
|
validate do
|
|
|
|
unless hash? || string?
|
|
|
|
errors.add(:config, 'should be a hash or a string')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :name,
|
|
|
|
type: {
|
|
|
|
with: String,
|
2017-08-17 22:00:37 +05:30
|
|
|
message: Gitlab::Regex.environment_name_regex_message
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
validates :name,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.environment_name_regex,
|
2017-08-17 22:00:37 +05:30
|
|
|
message: Gitlab::Regex.environment_name_regex_message
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
with_options if: :hash? do
|
|
|
|
validates :config, allowed_keys: ALLOWED_KEYS
|
|
|
|
|
|
|
|
validates :url,
|
2019-07-31 22:56:46 +05:30
|
|
|
type: String,
|
2016-09-29 09:46:39 +05:30
|
|
|
length: { maximum: 255 },
|
|
|
|
allow_nil: true
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
validates :action,
|
2019-07-31 22:56:46 +05:30
|
|
|
type: String,
|
2020-07-28 23:09:34 +05:30
|
|
|
inclusion: { in: %w[start stop prepare], message: 'should be start, stop or prepare' },
|
2016-11-03 12:29:30 +05:30
|
|
|
allow_nil: true
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
validates :deployment_tier,
|
|
|
|
type: String,
|
|
|
|
inclusion: { in: ::Environment.tiers.keys, message: "must be one of #{::Environment.tiers.keys.join(', ')}" },
|
|
|
|
allow_nil: true
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
validates :on_stop, type: String, allow_nil: true
|
2020-01-01 13:55:28 +05:30
|
|
|
validates :kubernetes, type: Hash, allow_nil: true
|
|
|
|
validates :auto_stop_in, duration: true, allow_nil: true
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash?
|
|
|
|
@config.is_a?(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def string?
|
|
|
|
@config.is_a?(String)
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
value[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
value[:url]
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def action
|
|
|
|
value[:action] || 'start'
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_stop
|
|
|
|
value[:on_stop]
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def kubernetes
|
|
|
|
value[:kubernetes]
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_stop_in
|
|
|
|
value[:auto_stop_in]
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def deployment_tier
|
|
|
|
value[:deployment_tier]
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def value
|
|
|
|
case @config
|
2016-11-03 12:29:30 +05:30
|
|
|
when String then { name: @config, action: 'start' }
|
2016-09-29 09:46:39 +05:30
|
|
|
when Hash then @config
|
|
|
|
else {}
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
def skip_config_hash_validation?
|
|
|
|
true
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|