install_command.rb 2.6 KB
Newer Older
1 2 3
module Gitlab
  module Kubernetes
    module Helm
4 5
      class InstallCommand
        include BaseCommand
6

A
Amit Rathi 已提交
7
        attr_reader :name, :files, :chart, :version, :repository, :preinstall, :postinstall, :application_flags
8

A
Amit Rathi 已提交
9
        def initialize(name:, chart:, files:, rbac:, version: nil, repository: nil, preinstall: nil, postinstall: nil, application_flags: [])
10 11
          @name = name
          @chart = chart
12
          @version = version
13
          @rbac = rbac
14
          @files = files
15
          @repository = repository
16 17
          @preinstall = preinstall
          @postinstall = postinstall
A
Amit Rathi 已提交
18
          @application_flags = application_flags
19 20
        end

21 22 23 24
        def generate_script
          super + [
            init_command,
            repository_command,
25
            repository_update_command,
26 27 28
            preinstall_command,
            install_command,
            postinstall_command
29
          ].compact.join("\n")
30 31
        end

32 33 34 35
        def rbac?
          @rbac
        end

36 37 38
        private

        def init_command
39
          'helm init --client-only'
40 41
        end

42
        def repository_command
43
          ['helm', 'repo', 'add', name, repository].shelljoin if repository
44 45
        end

46
        def repository_update_command
47
          'helm repo update' if repository
48 49
        end

C
Chris Baumbauer 已提交
50
        def install_command
51 52
          command = ['helm', 'install', chart] + install_command_flags

53
          command.shelljoin
54 55
        end

56
        def preinstall_command
C
Chris Baumbauer 已提交
57
          preinstall.join("\n") if preinstall
58 59 60
        end

        def postinstall_command
C
Chris Baumbauer 已提交
61
          postinstall.join("\n") if postinstall
62 63 64 65 66 67
        end

        def install_command_flags
          name_flag      = ['--name', name]
          namespace_flag = ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
          value_flag     = ['-f', "/data/helm/#{name}/config/values.yaml"]
68

69 70 71
          name_flag +
            optional_tls_flags +
            optional_version_flag +
72
            rbac_create_flag +
73
            namespace_flag +
A
Amit Rathi 已提交
74 75
            value_flag +
            application_flags
76 77
        end

78 79 80 81 82 83
        def rbac_create_flag
          if rbac?
            %w[--set rbac.create=true,rbac.enabled=true]
          else
            %w[--set rbac.create=false,rbac.enabled=false]
          end
84
        end
85 86

        def optional_version_flag
87 88 89
          return [] unless version

          ['--version', version]
90
        end
91 92

        def optional_tls_flags
93
          return [] unless files.key?(:'ca.pem')
94

95 96 97 98 99 100
          [
            '--tls',
            '--tls-ca-cert', "#{files_dir}/ca.pem",
            '--tls-cert', "#{files_dir}/cert.pem",
            '--tls-key', "#{files_dir}/key.pem"
          ]
101
        end
102 103 104 105
      end
    end
  end
end