2017-10-13 13:21:23 -04:00
|
|
|
module Clusters
|
|
|
|
class Cluster < ActiveRecord::Base
|
|
|
|
include Presentable
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
self.table_name = 'clusters'
|
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
APPLICATIONS = {
|
2017-11-06 04:41:27 -05:00
|
|
|
Applications::Helm.application_name => Applications::Helm,
|
2017-12-22 12:23:43 -05:00
|
|
|
Applications::Ingress.application_name => Applications::Ingress,
|
2018-03-01 18:46:02 -05:00
|
|
|
Applications::Prometheus.application_name => Applications::Prometheus,
|
|
|
|
Applications::Runner.application_name => Applications::Runner
|
2017-11-02 12:08:56 -04:00
|
|
|
}.freeze
|
2018-03-21 10:59:40 -04:00
|
|
|
DEFAULT_ENVIRONMENT = '*'.freeze
|
2017-11-02 10:10:46 -04:00
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
has_many :cluster_projects, class_name: 'Clusters::Project'
|
2018-04-18 09:41:42 -04:00
|
|
|
has_many :projects, through: :cluster_projects, class_name: '::Project'
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-01 07:57:05 -04:00
|
|
|
# we force autosave to happen when we save `Cluster` model
|
|
|
|
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
|
|
|
|
|
2017-12-03 09:00:33 -05:00
|
|
|
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
has_one :application_helm, class_name: 'Clusters::Applications::Helm'
|
2017-11-06 04:41:27 -05:00
|
|
|
has_one :application_ingress, class_name: 'Clusters::Applications::Ingress'
|
2017-12-22 12:23:43 -05:00
|
|
|
has_one :application_prometheus, class_name: 'Clusters::Applications::Prometheus'
|
2018-03-01 18:46:02 -05:00
|
|
|
has_one :application_runner, class_name: 'Clusters::Applications::Runner'
|
2017-11-02 10:10:46 -04:00
|
|
|
|
2017-10-31 04:47:48 -04:00
|
|
|
accepts_nested_attributes_for :provider_gcp, update_only: true
|
2017-10-30 08:55:18 -04:00
|
|
|
accepts_nested_attributes_for :platform_kubernetes, update_only: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
validates :name, cluster_name: true
|
2017-10-13 13:21:23 -04:00
|
|
|
validate :restrict_modification, on: :update
|
|
|
|
|
2017-11-03 04:22:11 -04:00
|
|
|
delegate :status, to: :provider, allow_nil: true
|
2017-10-13 13:21:23 -04:00
|
|
|
delegate :status_reason, to: :provider, allow_nil: true
|
2017-10-23 04:36:35 -04:00
|
|
|
delegate :on_creation?, to: :provider, allow_nil: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-06 09:48:44 -05:00
|
|
|
delegate :active?, to: :platform_kubernetes, prefix: true, allow_nil: true
|
|
|
|
delegate :installed?, to: :application_helm, prefix: true, allow_nil: true
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
enum platform_type: {
|
|
|
|
kubernetes: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
enum provider_type: {
|
|
|
|
user: 0,
|
|
|
|
gcp: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :enabled, -> { where(enabled: true) }
|
|
|
|
scope :disabled, -> { where(enabled: false) }
|
2018-03-29 15:17:18 -04:00
|
|
|
scope :user_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:user]) }
|
|
|
|
scope :gcp_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:gcp]) }
|
2018-03-30 11:34:10 -04:00
|
|
|
scope :gcp_installed, -> { gcp_provided.includes(:provider_gcp).where(cluster_providers_gcp: { status: ::Clusters::Providers::Gcp.state_machines[:status].states[:created].value }) }
|
|
|
|
|
2018-03-21 10:59:40 -04:00
|
|
|
scope :default_environment, -> { where(environment_scope: DEFAULT_ENVIRONMENT) }
|
2017-10-29 14:48:45 -04:00
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
def status_name
|
|
|
|
if provider
|
|
|
|
provider.status_name
|
|
|
|
else
|
|
|
|
:created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 10:49:56 -05:00
|
|
|
def created?
|
|
|
|
status_name == :created
|
|
|
|
end
|
|
|
|
|
2017-11-02 12:57:50 -04:00
|
|
|
def applications
|
2017-11-03 05:02:30 -04:00
|
|
|
[
|
2017-11-06 04:41:27 -05:00
|
|
|
application_helm || build_application_helm,
|
2017-12-22 12:23:43 -05:00
|
|
|
application_ingress || build_application_ingress,
|
2018-03-01 18:46:02 -05:00
|
|
|
application_prometheus || build_application_prometheus,
|
|
|
|
application_runner || build_application_runner
|
2017-11-02 12:57:50 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def provider
|
2017-10-23 04:36:35 -04:00
|
|
|
return provider_gcp if gcp?
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def platform
|
2017-10-23 04:36:35 -04:00
|
|
|
return platform_kubernetes if kubernetes?
|
|
|
|
end
|
|
|
|
|
2017-11-02 06:30:07 -04:00
|
|
|
def managed?
|
|
|
|
!user?
|
|
|
|
end
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def first_project
|
|
|
|
return @first_project if defined?(@first_project)
|
|
|
|
|
|
|
|
@first_project = projects.first
|
|
|
|
end
|
2017-10-29 14:48:45 -04:00
|
|
|
alias_method :project, :first_project
|
2017-10-23 04:36:35 -04:00
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
def kubeclient
|
|
|
|
platform_kubernetes.kubeclient if kubernetes?
|
|
|
|
end
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def restrict_modification
|
|
|
|
if provider&.on_creation?
|
|
|
|
errors.add(:base, "cannot modify during creation")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
end
|