Module: Dapp::Kube::Dapp::Command::Common

Included in:
Dapp
Defined in:
lib/dapp/kube/dapp/command/common.rb

Instance Method Summary collapse

Instance Method Details

#context_optionObject



345
346
347
# File 'lib/dapp/kube/dapp/command/common.rb', line 345

def context_option
  options[:context]
end

#custom_kube_contextObject



349
350
351
# File 'lib/dapp/kube/dapp/command/common.rb', line 349

def custom_kube_context
  ENV["KUBECONTEXT"] || context_option
end

#helm_releaseObject



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
# File 'lib/dapp/kube/dapp/command/common.rb', line 10

def helm_release
  kube_check_helm!
  kube_check_helm_template_plugin!
  kube_check_helm_chart!

  repo = option_repo
  tag = begin
    raise ::Dapp::Error::Command, code: :expected_only_one_tag, data: { tags: option_tags.join(', ') } if option_tags.count > 1
    option_tags.first
  end
  validate_repo_name!(repo)
  validate_tag_name!(tag)

  with_kube_tmp_chart_dir do
    kube_copy_chart
    kube_helm_decode_secrets
    kube_generate_helm_chart_tpl

    release = Helm::Release.new(
        self,
        name: kube_release_name,
        repo: repo,
        docker_tag: tag,
        namespace: kube_namespace,
        kube_context: custom_kube_context,
        chart_path: kube_chart_path_for_helm,
        set: self.options[:helm_set_options],
        values: [*kube_values_paths, *kube_tmp_chart_secret_values_paths],
        deploy_timeout: self.options[:timeout] || 300,
        without_registry: self.options[:without_registry],
    )

    yield release
  end
end

#kube_chart_path(*path) ⇒ Object



303
304
305
# File 'lib/dapp/kube/dapp/command/common.rb', line 303

def kube_chart_path(*path)
  self.path('.helm', *path).expand_path
end

#kube_chart_path_for_helm(*path) ⇒ Object



313
314
315
316
317
318
# File 'lib/dapp/kube/dapp/command/common.rb', line 313

def kube_chart_path_for_helm(*path)
  chart_dir = ENV['DAPP_HELM_CHART_DIR'] || begin
    @kube_tmp_helm_chart_dir ||= Dir.mktmpdir('dapp-helm-chart-', tmp_base_dir)
  end
  make_path(chart_dir, *path).expand_path.tap { |p| p.parent.mkpath }
end

#kube_chart_secret_dir_nameObject



299
300
301
# File 'lib/dapp/kube/dapp/command/common.rb', line 299

def kube_chart_secret_dir_name
  'secret'
end

#kube_chart_secret_path(*path) ⇒ Object



295
296
297
# File 'lib/dapp/kube/dapp/command/common.rb', line 295

def kube_chart_secret_path(*path)
  kube_chart_path(kube_chart_secret_dir_name, *path).expand_path
end

#kube_chart_secret_values_pathObject



258
259
260
# File 'lib/dapp/kube/dapp/command/common.rb', line 258

def kube_chart_secret_values_path
  kube_chart_path('secret-values.yaml').expand_path
end

#kube_check_helm!Object



262
263
264
# File 'lib/dapp/kube/dapp/command/common.rb', line 262

def kube_check_helm!
  raise ::Dapp::Error::Command, code: :helm_not_found if shellout('which helm').exitstatus == 1
end

#kube_check_helm_chart!Object



46
47
48
# File 'lib/dapp/kube/dapp/command/common.rb', line 46

def kube_check_helm_chart!
  raise ::Dapp::Error::Command, code: :helm_directory_not_exist, data: { path: kube_chart_path } unless kube_chart_path.exist?
end

#kube_check_helm_template_plugin!Object



50
51
52
53
54
# File 'lib/dapp/kube/dapp/command/common.rb', line 50

def kube_check_helm_template_plugin!
  unless shellout!("helm plugin list | awk '{print $1}'").stdout.lines.map(&:strip).any? { |plugin| plugin == 'template' }
    raise ::Dapp::Error::Command, code: :helm_template_plugin_not_installed, data: { path: kube_chart_path }
  end
end

#kube_contextObject



353
354
355
# File 'lib/dapp/kube/dapp/command/common.rb', line 353

def kube_context
  custom_kube_context || kubernetes_config.current_context_name
end

#kube_copy_chartObject



56
57
58
# File 'lib/dapp/kube/dapp/command/common.rb', line 56

def kube_copy_chart
  FileUtils.cp_r("#{kube_chart_path}/.", kube_chart_path_for_helm)
end

#kube_generate_helm_chart_tplObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dapp/kube/dapp/command/common.rb', line 96

def kube_generate_helm_chart_tpl
  cont = <<-EOF
{{- define "dapp_secret_file" -}}
{{-   $relative_file_path := index . 0 -}}
{{-   $context := index . 1 -}}
{{-   $context.Files.Get (print "#{kube_tmp_chart_secret_path.subpath_of(kube_chart_path_for_helm)}/" $relative_file_path) -}}
{{- end -}}

{{- define "_dimg" -}}
{{-   $context := index . 0 -}}
{{-   if not $context.Values.global.dapp.is_nameless_dimg -}}
{{-     required "No dimg specified for template" nil -}}
{{-   end -}}
{{    $context.Values.global.dapp.dimg.docker_image }}
{{- end -}}

{{- define "_dimg2" -}}
{{-   $name := index . 0 -}}
{{-   $context := index . 1 -}}
{{-   if $context.Values.global.dapp.is_nameless_dimg -}}
{{-     required (printf "No dimg should be specified for template, got `%s`" $name) nil -}}
{{-   end -}}
{{    index (required (printf "Unknown dimg `%s` specified for template" $name) (pluck $name $context.Values.global.dapp.dimg | first)) "docker_image" }}
{{- end -}}

{{- define "dimg" -}}
{{-   if eq (typeOf .) "chartutil.Values" -}}
{{-     $context := . -}}
{{      tuple $context | include "_dimg" }}
{{-   else if (ge (len .) 2) -}}
{{-     $name := index . 0 -}}
{{-     $context := index . 1 -}}
{{      tuple $name $context | include "_dimg2" }}
{{-   else -}}
{{-     $context := index . 0 -}}
{{      tuple $context | include "_dimg" }}
{{-   end -}}
{{- end -}}

{{- define "_dapp_container__imagePullPolicy" -}}
{{-   $context := index . 0 -}}
{{-   if $context.Values.global.dapp.ci.is_branch -}}
imagePullPolicy: Always
{{-   end -}}
{{- end -}}

{{- define "_dapp_container__image" -}}
{{-   $context := index . 0 -}}
image: {{ tuple $context | include "_dimg" }}
{{- end -}}

{{- define "_dapp_container__image2" -}}
{{-   $name := index . 0 -}}
{{-   $context := index . 1 -}}
image: {{ tuple $name $context | include "_dimg2" }}
{{- end -}}

{{- define "dapp_container_image" -}}
{{-   if eq (typeOf .) "chartutil.Values" -}}
{{-     $context := . -}}
{{      tuple $context | include "_dapp_container__image" }}
{{      tuple $context | include "_dapp_container__imagePullPolicy" }}
{{-   else if (ge (len .) 2) -}}
{{-     $name := index . 0 -}}
{{-     $context := index . 1 -}}
{{      tuple $name $context | include "_dapp_container__image2" }}
{{      tuple $context | include "_dapp_container__imagePullPolicy" }}
{{-   else -}}
{{-     $context := index . 0 -}}
{{      tuple $context | include "_dapp_container__image" }}
{{      tuple $context | include "_dapp_container__imagePullPolicy" }}
{{-   end -}}
{{- end -}}

{{- define "_dimg_id" -}}
{{-   $context := index . 0 -}}
{{-   if not $context.Values.global.dapp.is_nameless_dimg -}}
{{-     required "No dimg specified for template" nil -}}
{{-   end -}}
{{    $context.Values.global.dapp.dimg.docker_image_id }}
{{- end -}}

{{- define "_dimg_id2" -}}
{{-   $name := index . 0 -}}
{{-   $context := index . 1 -}}
{{-   if $context.Values.global.dapp.is_nameless_dimg -}}
{{-     required (printf "No dimg should be specified for template, got `%s`" $name) nil -}}
{{-   end -}}
{{    index (required (printf "Unknown dimg `%s` specified for template" $name) (pluck $name $context.Values.global.dapp.dimg | first)) "docker_image_id" }}
{{- end -}}

{{- define "dimg_id" -}}
{{-   if eq (typeOf .) "chartutil.Values" -}}
{{-     $context := . -}}
{{      tuple $context | include "_dimg_id" }}
{{-   else if (ge (len .) 2) -}}
{{-     $name := index . 0 -}}
{{-     $context := index . 1 -}}
{{      tuple $name $context | include "_dimg_id2" }}
{{-   else -}}
{{-     $context := index . 0 -}}
{{      tuple $context | include "_dimg_id" }}
{{-   end -}}
{{- end -}}

{{- define "_dapp_container_env" -}}
{{-   $context := index . 0 -}}
{{-   if $context.Values.global.dapp.ci.is_branch -}}
- name: DOCKER_IMAGE_ID
  value: {{ tuple $context | include "_dimg_id" }}
{{-   end -}}
{{- end -}}

{{- define "_dapp_container_env2" -}}
{{-   $name := index . 0 -}}
{{-   $context := index . 1 -}}
{{-   if $context.Values.global.dapp.ci.is_branch -}}
- name: DOCKER_IMAGE_ID
  value: {{ tuple $name $context | include "_dimg_id2" }}
{{-   end -}}
{{- end -}}

{{- define "dapp_container_env" -}}
{{-   if eq (typeOf .) "chartutil.Values" -}}
{{-     $context := . -}}
{{      tuple $context | include "_dapp_container_env" }}
{{-   else if (ge (len .) 2) -}}
{{-     $name := index . 0 -}}
{{-     $context := index . 1 -}}
{{      tuple $name $context | include "_dapp_container_env2" }}
{{-   else -}}
{{-     $context := index . 0 -}}
{{      tuple $context | include "_dapp_container_env" }}
{{-   end -}}
{{- end -}}
  EOF
  kube_chart_path_for_helm('templates/_dapp_helpers.tpl').write(cont)
end

#kube_helm_decode_secret_filesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dapp/kube/dapp/command/common.rb', line 79

def kube_helm_decode_secret_files
  return unless kube_chart_secret_path.directory?
  Dir.glob(kube_chart_secret_path.join('**/*')).each do |entry|
    next if File.directory?(entry)

    secret_relative_path = Pathname(entry).subpath_of(kube_chart_secret_path)

    if secret.nil?
      File.open(kube_tmp_chart_secret_path(secret_relative_path), 'wb:ASCII-8BIT', 0600) {|f| f.write ""}
    else
      kube_secret_file_validate!(entry)
      secret_data = secret.extract(IO.binread(entry).chomp("\n"))
      File.open(kube_tmp_chart_secret_path(secret_relative_path), 'wb:ASCII-8BIT', 0600) {|f| f.write secret_data}
    end
  end
end

#kube_helm_decode_secret_valuesObject



72
73
74
75
76
77
# File 'lib/dapp/kube/dapp/command/common.rb', line 72

def kube_helm_decode_secret_values
  kube_secret_values_paths.each_with_index do |secret_values_file, index|
    decoded_data = kube_helm_decode_json(secret, yaml_load_file(secret_values_file))
    kube_tmp_chart_secret_values_paths[index].write(decoded_data.to_yaml)
  end
end

#kube_helm_decode_secretsObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dapp/kube/dapp/command/common.rb', line 60

def kube_helm_decode_secrets
  if secret.nil?
    log_warning(desc: {
      code: :dapp_secret_key_not_found,
      data: {not_found_in: secret_key_not_found_in.join(', ')}
    }) if !kube_secret_values_paths.empty? || kube_chart_secret_path.directory?
  end

  kube_helm_decode_secret_files
  kube_helm_decode_secret_values
end

#kube_namespaceObject



357
358
359
360
361
# File 'lib/dapp/kube/dapp/command/common.rb', line 357

def kube_namespace
  namespace_option ||
    kubernetes_config.namespace(kube_context) ||
      "default"
end

#kube_release_nameObject



266
267
268
# File 'lib/dapp/kube/dapp/command/common.rb', line 266

def kube_release_name
  ENV["DAPP_HELM_RELEASE_NAME"] || "#{name}-#{kube_namespace}"
end

#kube_secret_file_validate!(file_path) ⇒ Object



286
287
288
289
# File 'lib/dapp/kube/dapp/command/common.rb', line 286

def kube_secret_file_validate!(file_path)
  raise ::Dapp::Error::Command, code: :secret_file_not_found, data: { path: File.expand_path(file_path) } unless File.exist?(file_path)
  raise ::Dapp::Error::Command, code: :secret_file_empty, data: { path: File.expand_path(file_path) }     if File.read(file_path).strip.empty?
end

#kube_secret_values_pathsObject



249
250
251
252
253
254
255
256
# File 'lib/dapp/kube/dapp/command/common.rb', line 249

def kube_secret_values_paths
  @kube_chart_secret_values_files ||= [].tap do |files|
    files << kube_chart_secret_values_path if kube_chart_secret_values_path.file?
    files.concat(options[:helm_secret_values_options].map { |p| Pathname(p).expand_path }.each do |f|
      kube_secret_file_validate!(f)
    end)
  end
end

#kube_tmp_chart_secret_path(*path) ⇒ Object



235
236
237
# File 'lib/dapp/kube/dapp/command/common.rb', line 235

def kube_tmp_chart_secret_path(*path)
  kube_chart_path_for_helm('decoded-secret', *path).tap { |p| p.parent.mkpath }
end

#kube_tmp_chart_secret_values_pathsObject



245
246
247
# File 'lib/dapp/kube/dapp/command/common.rb', line 245

def kube_tmp_chart_secret_values_paths
  @kube_tmp_chart_secret_values_paths ||= kube_secret_values_paths.each_with_index.map { |f, i| kube_chart_path_for_helm( "decoded-secret-values-#{i}.yaml") }
end

#kube_values_pathsObject



239
240
241
242
243
# File 'lib/dapp/kube/dapp/command/common.rb', line 239

def kube_values_paths
  self.options[:helm_values_options].map { |p| Pathname(p).expand_path }.each do |f|
    raise ::Dapp::Error::Command, code: :values_file_not_found, data: { path: f } unless f.file?
  end
end

#kubernetesObject



367
368
369
370
371
372
373
374
# File 'lib/dapp/kube/dapp/command/common.rb', line 367

def kubernetes
  @kubernetes ||= Kubernetes::Client.new(
    kubernetes_config,
    kube_context,
    kube_namespace,
    timeout: options[:kubernetes_timeout],
  )
end

#kubernetes_configObject



363
364
365
# File 'lib/dapp/kube/dapp/command/common.rb', line 363

def kubernetes_config
  @kubernetes_config ||= Kubernetes::Config.new_auto
end

#lock_helm_release(&blk) ⇒ Object



6
7
8
# File 'lib/dapp/kube/dapp/command/common.rb', line 6

def lock_helm_release(&blk)
  lock("helm_release.#{kube_release_name}", &blk)
end

#namespace_optionObject



341
342
343
# File 'lib/dapp/kube/dapp/command/common.rb', line 341

def namespace_option
  options[:namespace].nil? ? nil : consistent_uniq_slugify(options[:namespace])
end

#secretObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dapp/kube/dapp/command/common.rb', line 320

def secret
  @secret ||= begin
    unless (secret_key = ENV['DAPP_SECRET_KEY'])
      secret_key_not_found_in << '`DAPP_SECRET_KEY`'

      file_path = path('.dapp_secret_key')
      if file_path.file?
        secret_key = path('.dapp_secret_key').read.chomp
      else
        secret_key_not_found_in << "`#{file_path}`"
      end
    end

    Secret.new(secret_key) if secret_key
  end
end

#secret_key_not_found_inObject



337
338
339
# File 'lib/dapp/kube/dapp/command/common.rb', line 337

def secret_key_not_found_in
  @secret_key_not_found_in ||= []
end

#secret_key_should_exist!Object



291
292
293
# File 'lib/dapp/kube/dapp/command/common.rb', line 291

def secret_key_should_exist!
  raise ::Dapp::Error::Command, code: :secret_key_not_found, data: { not_found_in: secret_key_not_found_in.join(', ') } if secret.nil?
end

#with_kube_tmp_chart_dirObject



307
308
309
310
311
# File 'lib/dapp/kube/dapp/command/common.rb', line 307

def with_kube_tmp_chart_dir
  yield if block_given?
ensure
  FileUtils.rm_rf(@kube_tmp_helm_chart_dir) if @kube_tmp_helm_chart_dir
end