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



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

def context_option
  options[:context]
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
# 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,
        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



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

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

#kube_chart_path_for_helm(*path) ⇒ Object



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

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



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

def kube_chart_secret_dir_name
  'secret'
end

#kube_chart_secret_path(*path) ⇒ Object



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

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

#kube_chart_secret_values_pathObject



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

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

#kube_check_helm!Object



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

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

#kube_check_helm_chart!Object



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

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



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

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



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

def kube_context
  ENV["KUBECONTEXT"] ||
    context_option ||
      kubernetes_config.current_context_name
end

#kube_copy_chartObject



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

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

#kube_generate_helm_chart_tplObject



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

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



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

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



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

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



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

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



354
355
356
357
358
# File 'lib/dapp/kube/dapp/command/common.rb', line 354

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

#kube_release_nameObject



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

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

#kube_secret_file_validate!(file_path) ⇒ Object



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

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



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

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



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

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



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

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



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

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



364
365
366
367
368
369
370
371
# File 'lib/dapp/kube/dapp/command/common.rb', line 364

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

#kubernetes_configObject



360
361
362
# File 'lib/dapp/kube/dapp/command/common.rb', line 360

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



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

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

#secretObject



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

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



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

def secret_key_not_found_in
  @secret_key_not_found_in ||= []
end

#secret_key_should_exist!Object



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

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



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

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