cluster.rb 3.41 KB
Newer Older
1 2 3 4
module Clusters
  class Cluster < ActiveRecord::Base
    include Presentable

Shinya Maeda's avatar
Shinya Maeda committed
5 6
    self.table_name = 'clusters'

7
    APPLICATIONS = {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
8
      Applications::Helm.application_name => Applications::Helm,
9
      Applications::Ingress.application_name => Applications::Ingress,
10 11
      Applications::Prometheus.application_name => Applications::Prometheus,
      Applications::Runner.application_name => Applications::Runner
Alessio Caiazza's avatar
Alessio Caiazza committed
12
    }.freeze
13
    DEFAULT_ENVIRONMENT = '*'.freeze
14

15 16
    belongs_to :user

Shinya Maeda's avatar
Shinya Maeda committed
17
    has_many :cluster_projects, class_name: 'Clusters::Project'
18
    has_many :projects, through: :cluster_projects, class_name: '::Project'
19

20 21 22
    # we force autosave to happen when we save `Cluster` model
    has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true

23
    has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true
24

25
    has_one :application_helm, class_name: 'Clusters::Applications::Helm'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
    has_one :application_ingress, class_name: 'Clusters::Applications::Ingress'
27
    has_one :application_prometheus, class_name: 'Clusters::Applications::Prometheus'
28
    has_one :application_runner, class_name: 'Clusters::Applications::Runner'
29

30
    accepts_nested_attributes_for :provider_gcp, update_only: true
31
    accepts_nested_attributes_for :platform_kubernetes, update_only: true
32

Shinya Maeda's avatar
Shinya Maeda committed
33
    validates :name, cluster_name: true
34 35
    validate :restrict_modification, on: :update

Kamil Trzcinski's avatar
Kamil Trzcinski committed
36
    delegate :status, to: :provider, allow_nil: true
37
    delegate :status_reason, to: :provider, allow_nil: true
Shinya Maeda's avatar
Shinya Maeda committed
38
    delegate :on_creation?, to: :provider, allow_nil: true
39

40 41 42
    delegate :active?, to: :platform_kubernetes, prefix: true, allow_nil: true
    delegate :installed?, to: :application_helm, prefix: true, allow_nil: true

43 44 45 46 47 48 49 50 51 52 53
    enum platform_type: {
      kubernetes: 1
    }

    enum provider_type: {
      user: 0,
      gcp: 1
    }

    scope :enabled, -> { where(enabled: true) }
    scope :disabled, -> { where(enabled: false) }
54 55
    scope :user_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:user]) }
    scope :gcp_provided, -> { where(provider_type: ::Clusters::Cluster.provider_types[:gcp]) }
56 57
    scope :gcp_installed, -> { gcp_provided.includes(:provider_gcp).where(cluster_providers_gcp: { status: ::Clusters::Providers::Gcp.state_machines[:status].states[:created].value }) }

58
    scope :default_environment, -> { where(environment_scope: DEFAULT_ENVIRONMENT) }
59

60 61 62 63 64 65 66 67
    def status_name
      if provider
        provider.status_name
      else
        :created
      end
    end

68 69 70 71
    def created?
      status_name == :created
    end

72
    def applications
73
      [
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
        application_helm || build_application_helm,
75
        application_ingress || build_application_ingress,
76 77
        application_prometheus || build_application_prometheus,
        application_runner || build_application_runner
78 79 80
      ]
    end

81
    def provider
Shinya Maeda's avatar
Shinya Maeda committed
82
      return provider_gcp if gcp?
83 84 85
    end

    def platform
Shinya Maeda's avatar
Shinya Maeda committed
86 87 88
      return platform_kubernetes if kubernetes?
    end

89 90 91 92
    def managed?
      !user?
    end

93 94 95 96 97
    def first_project
      return @first_project if defined?(@first_project)

      @first_project = projects.first
    end
98
    alias_method :project, :first_project
Shinya Maeda's avatar
Shinya Maeda committed
99

100 101 102 103
    def kubeclient
      platform_kubernetes.kubeclient if kubernetes?
    end

Shinya Maeda's avatar
Shinya Maeda committed
104 105 106 107 108 109 110 111 112 113
    private

    def restrict_modification
      if provider&.on_creation?
        errors.add(:base, "cannot modify during creation")
        return false
      end

      true
    end
114 115
  end
end