2017-10-02 08:58:50 -04:00
|
|
|
module Gcp
|
2017-09-25 03:10:25 -04:00
|
|
|
class Cluster < ActiveRecord::Base
|
2017-10-02 08:58:50 -04:00
|
|
|
extend Gitlab::Gcp::Model
|
2017-10-05 13:45:15 -04:00
|
|
|
include Presentable
|
2017-09-25 03:10:25 -04:00
|
|
|
|
2017-10-02 08:58:50 -04:00
|
|
|
belongs_to :project, inverse_of: :cluster
|
2017-09-28 11:08:11 -04:00
|
|
|
belongs_to :user
|
2017-09-26 04:46:09 -04:00
|
|
|
belongs_to :service
|
2017-09-25 03:10:25 -04:00
|
|
|
|
2017-10-11 04:32:58 -04:00
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :disabled, -> { where(enabled: false) }
|
|
|
|
|
2017-10-03 18:20:47 -04:00
|
|
|
default_value_for :gcp_cluster_zone, 'us-central1-a'
|
|
|
|
default_value_for :gcp_cluster_size, 3
|
|
|
|
default_value_for :gcp_machine_type, 'n1-standard-4'
|
|
|
|
|
2017-09-28 11:08:11 -04:00
|
|
|
attr_encrypted :password,
|
2017-10-02 08:58:50 -04:00
|
|
|
mode: :per_attribute_iv,
|
2017-09-30 11:54:22 -04:00
|
|
|
key: Gitlab::Application.secrets.db_key_base,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
|
|
|
attr_encrypted :kubernetes_token,
|
2017-10-02 08:58:50 -04:00
|
|
|
mode: :per_attribute_iv,
|
2017-09-30 11:54:22 -04:00
|
|
|
key: Gitlab::Application.secrets.db_key_base,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
|
|
|
attr_encrypted :gcp_token,
|
2017-10-02 08:58:50 -04:00
|
|
|
mode: :per_attribute_iv,
|
2017-09-30 11:54:22 -04:00
|
|
|
key: Gitlab::Application.secrets.db_key_base,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
2017-10-03 18:20:47 -04:00
|
|
|
validates :gcp_project_id,
|
|
|
|
length: 1..63,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.kubernetes_namespace_regex,
|
|
|
|
message: Gitlab::Regex.kubernetes_namespace_regex_message
|
|
|
|
}
|
|
|
|
|
|
|
|
validates :gcp_cluster_name,
|
|
|
|
length: 1..63,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.kubernetes_namespace_regex,
|
|
|
|
message: Gitlab::Regex.kubernetes_namespace_regex_message
|
|
|
|
}
|
|
|
|
|
|
|
|
validates :gcp_cluster_zone, presence: true
|
2017-10-04 07:27:25 -04:00
|
|
|
|
|
|
|
validates :gcp_cluster_size,
|
|
|
|
presence: true,
|
2017-10-04 05:21:01 -04:00
|
|
|
numericality: {
|
|
|
|
only_integer: true,
|
|
|
|
greater_than: 0
|
|
|
|
}
|
2017-10-03 18:20:47 -04:00
|
|
|
|
|
|
|
validates :project_namespace,
|
|
|
|
allow_blank: true,
|
|
|
|
length: 1..63,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.kubernetes_namespace_regex,
|
|
|
|
message: Gitlab::Regex.kubernetes_namespace_regex_message
|
|
|
|
}
|
|
|
|
|
|
|
|
# if we do not do status transition we prevent change
|
|
|
|
validate :restrict_modification, on: :update, unless: :status_changed?
|
|
|
|
|
2017-10-03 17:26:26 -04:00
|
|
|
state_machine :status, initial: :scheduled do
|
2017-10-04 16:13:10 -04:00
|
|
|
state :scheduled, value: 1
|
|
|
|
state :creating, value: 2
|
|
|
|
state :created, value: 3
|
|
|
|
state :errored, value: 4
|
|
|
|
|
2017-10-04 09:14:01 -04:00
|
|
|
event :make_creating do
|
2017-10-03 17:26:26 -04:00
|
|
|
transition any - [:creating] => :creating
|
|
|
|
end
|
|
|
|
|
2017-10-04 09:14:01 -04:00
|
|
|
event :make_created do
|
2017-10-03 17:26:26 -04:00
|
|
|
transition any - [:created] => :created
|
|
|
|
end
|
|
|
|
|
2017-10-04 09:14:01 -04:00
|
|
|
event :make_errored do
|
2017-10-03 17:26:26 -04:00
|
|
|
transition any - [:errored] => :errored
|
|
|
|
end
|
|
|
|
|
|
|
|
before_transition any => [:errored, :created] do |cluster|
|
|
|
|
cluster.gcp_token = nil
|
|
|
|
cluster.gcp_operation_id = nil
|
|
|
|
end
|
|
|
|
|
2017-10-04 09:40:12 -04:00
|
|
|
before_transition any => [:errored] do |cluster, transition|
|
2017-10-03 17:26:26 -04:00
|
|
|
status_reason = transition.args.first
|
2017-10-04 16:13:10 -04:00
|
|
|
cluster.status_reason = status_reason if status_reason
|
2017-10-03 17:26:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-03 18:20:47 -04:00
|
|
|
def project_namespace_placeholder
|
|
|
|
"#{project.path}-#{project.id}"
|
|
|
|
end
|
2017-10-02 08:58:50 -04:00
|
|
|
|
2017-09-30 11:54:22 -04:00
|
|
|
def on_creation?
|
|
|
|
scheduled? || creating?
|
2017-09-28 05:11:17 -04:00
|
|
|
end
|
2017-10-01 04:48:21 -04:00
|
|
|
|
|
|
|
def api_url
|
2017-10-05 03:34:24 -04:00
|
|
|
'https://' + endpoint if endpoint
|
2017-10-01 04:48:21 -04:00
|
|
|
end
|
2017-10-01 07:30:32 -04:00
|
|
|
|
|
|
|
def restrict_modification
|
|
|
|
if on_creation?
|
|
|
|
errors.add(:base, "cannot modify during creation")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
end
|