install_command.rb 2.4 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Kubernetes
    module Helm
6 7
      class InstallCommand
        include BaseCommand
8
        include ClientCommand
9

10 11
        attr_reader :name, :files, :chart, :repository, :preinstall, :postinstall
        attr_accessor :version
12

A
Amit Rathi 已提交
13
        def initialize(name:, chart:, files:, rbac:, version: nil, repository: nil, preinstall: nil, postinstall: nil)
14 15
          @name = name
          @chart = chart
16
          @version = version
17
          @rbac = rbac
18
          @files = files
19
          @repository = repository
20 21
          @preinstall = preinstall
          @postinstall = postinstall
22 23
        end

24 25 26
        def generate_script
          super + [
            init_command,
27
            wait_for_tiller_command,
28
            repository_command,
29
            repository_update_command,
30 31 32
            preinstall_command,
            install_command,
            postinstall_command
33
          ].compact.join("\n")
34 35
        end

36 37 38 39
        def rbac?
          @rbac
        end

40 41
        private

42
        def repository_update_command
43
          'helm repo update' if repository
44 45
        end

46 47
        # Uses `helm upgrade --install` which means we can use this for both
        # installation and uprade of applications
C
Chris Baumbauer 已提交
48
        def install_command
49 50 51 52 53 54 55 56
          command = ['helm', 'upgrade', name, chart] +
            install_flag +
            reset_values_flag +
            optional_tls_flags +
            optional_version_flag +
            rbac_create_flag +
            namespace_flag +
            value_flag
57

58
          command.shelljoin
59 60
        end

61
        def preinstall_command
C
Chris Baumbauer 已提交
62
          preinstall.join("\n") if preinstall
63 64 65
        end

        def postinstall_command
C
Chris Baumbauer 已提交
66
          postinstall.join("\n") if postinstall
67 68
        end

69 70 71
        def install_flag
          ['--install']
        end
72

73 74 75 76 77 78 79 80 81 82
        def reset_values_flag
          ['--reset-values']
        end

        def value_flag
          ['-f', "/data/helm/#{name}/config/values.yaml"]
        end

        def namespace_flag
          ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
83 84
        end

85 86 87 88 89 90
        def rbac_create_flag
          if rbac?
            %w[--set rbac.create=true,rbac.enabled=true]
          else
            %w[--set rbac.create=false,rbac.enabled=false]
          end
91
        end
92 93

        def optional_version_flag
94 95 96
          return [] unless version

          ['--version', version]
97
        end
98 99 100 101
      end
    end
  end
end