summaryrefslogtreecommitdiff
path: root/app/models/clusters/concerns/application_core.rb
blob: 760576ea1eb054598f24b33140095628c58086b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

module Clusters
  module Concerns
    module ApplicationCore
      extend ActiveSupport::Concern

      included do
        belongs_to :cluster, class_name: 'Clusters::Cluster', foreign_key: :cluster_id

        validates :cluster, presence: true

        after_initialize :set_initial_status

        def set_initial_status
          return unless not_installable?

          self.status = status_states[:installable]
        end

        def can_uninstall?
          allowed_to_uninstall?
        end

        # All new applications should uninstall by default
        # Override if there's dependencies that needs to be uninstalled first
        def allowed_to_uninstall?
          true
        end

        def self.application_name
          self.to_s.demodulize.underscore
        end

        def self.association_name
          :"application_#{application_name}"
        end

        def name
          self.class.application_name
        end

        def schedule_status_update
          # Override if you need extra data synchronized
          # from K8s after installation
        end

        def update_command
          install_command.tap do |command|
            command.version = version
          end
        end

        def prepare_uninstall
          # Override if your application needs any action before
          # being uninstalled by Helm
        end

        def post_uninstall
          # Override if your application needs any action after
          # being uninstalled by Helm
        end

        def logger
          @logger ||= Gitlab::Kubernetes::Logger.build
        end

        def log_exception(error, event)
          logger.error({
            exception: error.class.name,
            status_code: error.error_code,
            cluster_id: cluster&.id,
            application_id: id,
            class_name: self.class.name,
            event: event,
            message: error.message
          })

          Gitlab::ErrorTracking.track_exception(error, cluster_id: cluster&.id, application_id: id)
        end
      end
    end
  end
end

Clusters::Concerns::ApplicationCore.prepend_if_ee('EE::Clusters::Concerns::ApplicationCore')