minfra-cli 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d8c4fc413b6865291c1f47e12581c0f11131008f4852742969fb49a8be4272d
4
- data.tar.gz: 7e1eb348156ba8adb74bf7f5bb5055853c31a3c2802fa77497c7a529573f9d23
3
+ metadata.gz: a79262f46479ee057b3285d260867512ab02a63ddfbf83f9d6a801854770a880
4
+ data.tar.gz: 8494a07eda5881a3c1b7aced8e4a4835d73ba3456180a12aad59d258e722cad6
5
5
  SHA512:
6
- metadata.gz: efa9ca0853ee602c708770a42cd6c1269f996dbcb70d4170cb630b815df161bc280bd5afb1ce608a4afefa699a6df78fa22c099260bfe3853ce4da7b8a601745
7
- data.tar.gz: 28e53a02daacbe1c3ccc33fa2e273a52ac04c7b9189c559a93b7d4fd5840bae0eb837ebbee16e74df533dd574b84eb350805ffca22bad2e4da09b209af7d916a
6
+ metadata.gz: 1b8e0cba7632dca57d50323eee59e30116d8685864c261466c1b85a13da05fdf4ca6b34ef883afdc8ef3d895c571441586aa93d14c82f467c97598475acfa950
7
+ data.tar.gz: 28e954fc5a49b5c140e77633f91774fcd16323c28c305909985bd15052b3975f50e2a09d2e6c96e80e8fbf36eeefa931a7d8dfeb1f9716c7272f73249a73c260
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.6.0
2
+ * collecting things in used_vars
3
+ * no overwriting of values.yaml between each char (as the helm template dir is shared)
4
+ # 1.5.1
5
+ * fixing hiera lookup caching error
1
6
  # 1.5.0
2
7
  * minfra project branch create now supports --prefix, keeps '-' (dash intact) and is config file configurable
3
8
  * Minfra::Cli::Config deep merges personal config (again)
data/bin/runner ADDED
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+
3
+ if [[ -f "$MINFRA_PATH/config/env_setup" ]]; then
4
+ source $MINFRA_PATH/config/env_setup
5
+ fi
6
+
7
+ MINFRA_SHELL=${MINFRA_SHELL:-"/bin/bash"}
8
+ CMD=${@:-$MINFRA_SHELL}
9
+
10
+ echo "running: '$CMD'"
11
+
12
+ exec $CMD
13
+
@@ -1,5 +1,5 @@
1
1
  module Minfra
2
2
  module Cli
3
- VERSION = '1.5.0'.freeze
3
+ VERSION = '1.6.0'.freeze
4
4
  end
5
5
  end
data/lib/minfra/cli.rb CHANGED
@@ -2,10 +2,10 @@ require 'thor'
2
2
  require 'open3'
3
3
  require 'json'
4
4
  require 'ostruct'
5
- require 'orchparty'
6
5
  require 'hiera'
7
6
 
8
7
  require_relative 'cli/logging'
8
+ require 'orchparty'
9
9
  require_relative 'cli/config'
10
10
  require_relative 'cli/version'
11
11
  require_relative 'cli/hook'
@@ -90,15 +90,15 @@ module Minfra
90
90
  return cache[value] if cache.has_key?(value)
91
91
 
92
92
  values=value.split(".")
93
- value=values.shift
93
+ fst_value=values.shift
94
94
 
95
- if special_lookups[value]
96
- lookup_type={ merge_behavior: special_lookups[value]["merge"].to_sym }
95
+ if special_lookups[fst_value]
96
+ lookup_type={ merge_behavior: special_lookups[fst_value]["merge"].to_sym }
97
97
  else
98
98
  lookup_type=:deep
99
99
  end
100
100
 
101
- result=hiera.lookup(value, default, scope, nil, lookup_type)
101
+ result=hiera.lookup(fst_value, default, scope, nil, lookup_type)
102
102
  if !values.empty? && result.kind_of?(Hash) # we return nil or the scalar value and only drill down on hashes
103
103
  result=result.dig(*values)
104
104
  end
data/lib/orchparty/ast.rb CHANGED
@@ -12,6 +12,18 @@ module Orchparty
12
12
  def get_binding
13
13
  binding
14
14
  end
15
+
16
+ def inspect(indent=0)
17
+ start="\n"
18
+ each_pair do |name, ast|
19
+ begin
20
+ start << "#{' ' * indent}#{name}: #{ast.inspect(indent+1)}\n"
21
+ rescue ArgumentError
22
+ start << "#{' ' * indent}#{name}: #{ast.inspect}\n"
23
+ end
24
+ end
25
+ start
26
+ end
15
27
  end
16
28
 
17
29
  def self.hash(args = {})
@@ -49,5 +61,6 @@ module Orchparty
49
61
  def self.chart(args = {})
50
62
  Node.new(_mix:[], _variables: {}, _services: []).merge(args)
51
63
  end
64
+
52
65
  end
53
66
  end
@@ -10,6 +10,8 @@ require 'active_support/core_ext'
10
10
  module Orchparty
11
11
  module Services
12
12
  class Context
13
+ include ::Minfra::Cli::Logging
14
+
13
15
  attr_accessor :cluster_name
14
16
  attr_accessor :namespace
15
17
  attr_accessor :dir_path
@@ -157,35 +159,79 @@ module Orchparty
157
159
  end
158
160
  end
159
161
 
162
+
163
+ def print_install(chart)
164
+
165
+ build_chart(chart) do |chart_path|
166
+ cmd="helm template --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}"
167
+ @out_io.puts `$cmd`
168
+ if system("#{cmd} > /dev/null")
169
+ info("helm template check: OK")
170
+ else
171
+ error("helm template check: FAIL")
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+ def print_upgrade(chart)
178
+ print_install(chart)
179
+ end
180
+
181
+ def install(chart)
182
+ info("Install: #{chart}")
183
+ build_chart(chart) do |chart_path|
184
+ @out_io.puts system("helm install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}")
185
+ end
186
+ end
187
+
188
+ def upgrade(chart)
189
+ info("Upgrade: #{chart}")
190
+ build_chart(chart) do |chart_path|
191
+ @out_io.puts system("helm upgrade --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}")
192
+ end
193
+ end
194
+ private
195
+
160
196
  def build_chart(chart)
197
+
198
+
199
+ dir = @app.status_dir.join('helm') # duplication
200
+
161
201
  params = chart._services.map {|s| app_config.services[s.to_sym] }.map{|s| [s.name, s]}.to_h
162
- dir = @app.status_dir.join('helm')
163
- dir.mkpath
164
202
  run(templates_path: File.expand_path(chart.template, self.dir_path), params: params, output_chart_path: dir, chart: chart)
165
203
  yield dir
166
204
  end
167
205
 
168
- def run(templates_path:, params:, output_chart_path:, chart: )
169
- system("mkdir -p #{File.join(output_chart_path, 'templates')}")
170
206
 
171
- system("cp #{File.join(templates_path, 'values.yaml')} #{File.join(output_chart_path, 'values.yaml')}")
172
- system("cp #{File.join(templates_path, '.helmignore')} #{File.join(output_chart_path, '.helmignore')}")
173
- system("cp #{File.join(templates_path, 'templates/_helpers.tpl')} #{File.join(output_chart_path, 'templates/_helpers.tpl')}")
207
+
208
+ # remember:
209
+ # this is done for an app
210
+ # that app can have multiple charts with multiple services!
211
+
212
+ def run(templates_path:, params:, output_chart_path:, chart: )
174
213
 
175
214
  generate_chart_yaml(
176
215
  templates_path: templates_path,
177
216
  output_chart_path: output_chart_path,
178
217
  chart_name: chart.name,
179
218
  )
180
- params.each do |app_name, subparams|
181
- subparams[:chart] = chart
182
- generate_documents_from_erbs(
183
- templates_path: templates_path,
184
- app_name: app_name,
185
- params: subparams,
186
- output_chart_path: output_chart_path
187
- )
219
+
220
+ File.open(File.join(output_chart_path, 'values.yaml'),'a') do |helm_values|
221
+ params.each do |app_name, subparams|
222
+ subparams[:chart] = chart
223
+ used_vars=generate_documents_from_erbs(
224
+ templates_path: templates_path,
225
+ app_name: app_name,
226
+ params: subparams,
227
+ output_chart_path: output_chart_path
228
+ )
229
+ used_vars.each do |variable,value|
230
+ helm_values.puts "#{variable}: #{value}"
231
+ end
232
+ end
188
233
  end
234
+
189
235
  end
190
236
 
191
237
  def generate_documents_from_erbs(templates_path:, app_name:, params:, output_chart_path:)
@@ -195,23 +241,28 @@ module Orchparty
195
241
  end
196
242
 
197
243
  kind = params.fetch(:kind)
244
+ params._used_vars = {} #here we'll collect all used vars
245
+
198
246
  Dir[File.join(templates_path, kind, '*.erb')].each do |template_path|
199
- puts "Rendering Template: #{template_path}"
247
+ info("Rendering Template: #{template_path}")
200
248
  template_name = File.basename(template_path, '.erb')
201
249
  output_path = File.join(output_chart_path, 'templates', "#{app_name}-#{template_name}")
202
-
250
+
203
251
  template = Erubis::Eruby.new(File.read(template_path))
204
252
  template.filename = template_path
253
+
254
+ params.app = @app
205
255
  params.app_name = app_name
206
256
  params.templates_path = templates_path
207
257
  begin
208
258
  document = template.result(CleanBinding.new.get_binding(params))
209
259
  rescue Exception
210
- puts "#{template_path} has a problem: #{$!.inspect}"
260
+ error "#{template_path} has a problem: #{$!.inspect}"
211
261
  raise
212
262
  end
213
263
  File.write(output_path, document)
214
264
  end
265
+ params._used_vars
215
266
  end
216
267
 
217
268
  def generate_chart_yaml(templates_path:, output_chart_path:, chart_name: )
@@ -224,34 +275,13 @@ module Orchparty
224
275
  document = template.result(CleanBinding.new.get_binding(params))
225
276
  File.write(output_path, document)
226
277
  end
227
-
228
- def print_install(chart)
229
- build_chart(chart) do |chart_path|
230
- @out_io.puts `helm template --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}`
231
- end
232
- end
233
-
234
- def print_upgrade(chart)
235
- print_install(chart)
236
- end
237
-
238
- def install(chart)
239
- build_chart(chart) do |chart_path|
240
- @out_io.puts system("helm install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}")
241
- end
242
- end
243
-
244
- def upgrade(chart)
245
- build_chart(chart) do |chart_path|
246
- system("cp -a #{chart_path} /tmp/latest-chart")
247
- @out_io.puts system("helm upgrade --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}")
248
- end
249
- end
250
278
  end
251
279
  end
252
280
  end
253
281
 
254
282
  class KubernetesApplication
283
+ include Minfra::Cli::Logging
284
+
255
285
  attr_accessor :cluster_name
256
286
  attr_accessor :file_path
257
287
  attr_accessor :namespace
@@ -259,7 +289,7 @@ class KubernetesApplication
259
289
  attr_reader :status_dir
260
290
 
261
291
  def initialize(app_config: [], namespace:, cluster_name:, file_name:, status_dir:, out_io: STDOUT)
262
- self.file_path = Pathname.new(file_name).parent.expand_path
292
+ self.file_path = Pathname.new(file_name).parent.expand_path #path of the stack
263
293
  self.cluster_name = cluster_name
264
294
  self.namespace = namespace
265
295
  self.app_config = app_config
@@ -279,6 +309,22 @@ class KubernetesApplication
279
309
  each_service("print_#{method}".to_sym)
280
310
  end
281
311
 
312
+ private
313
+ def prepare
314
+ output_chart_path = @status_dir.join('helm')
315
+ output_chart_path.rmtree
316
+ output_chart_path.mkpath
317
+ templates_path = file_path.join('../../chart-templates').expand_path #don't ask. the whole concept of multiple charts in an app stinks...
318
+
319
+ info("generating base helm structure from: #{output_chart_path} from #{templates_path}")
320
+ system("mkdir -p #{File.join(output_chart_path, 'templates')}")
321
+
322
+ system("cp #{File.join(templates_path, 'values.yaml')} #{File.join(output_chart_path, 'values.yaml')}")
323
+ system("cp #{File.join(templates_path, '.helmignore')} #{File.join(output_chart_path, '.helmignore')}")
324
+ system("cp #{File.join(templates_path, 'templates/_helpers.tpl')} #{File.join(output_chart_path, 'templates/_helpers.tpl')}")
325
+
326
+ end
327
+
282
328
  def combine_charts(app_config)
283
329
  services = app_config._service_order.map(&:to_s)
284
330
  app_config._service_order.each do |name|
@@ -293,11 +339,20 @@ class KubernetesApplication
293
339
  end
294
340
 
295
341
  def each_service(method)
342
+ prepare
296
343
  services = combine_charts(app_config)
297
344
  services.each do |name|
298
345
  service = app_config[:services][name]
299
- puts "Service: #{name}(#{service._type}) #{method}"
300
- "::Orchparty::Services::#{service._type.classify}".constantize.new(cluster_name: cluster_name, namespace: namespace, file_path: file_path, app_config: app_config, out_io: @out_io, app: self).send(method, service)
346
+ info "Service: #{name}(#{service._type}) #{method}"
347
+ deployable_class="::Orchparty::Services::#{service._type.classify}".constantize
348
+ deployable=deployable_class.new(cluster_name: cluster_name,
349
+ namespace: namespace,
350
+ file_path: file_path,
351
+ app_config: app_config,
352
+ out_io: @out_io,
353
+ app: self)
354
+
355
+ deployable.send(method, service)
301
356
  end
302
357
  end
303
358
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minfra-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Schrammel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-24 00:00:00.000000000 Z
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -162,6 +162,7 @@ files:
162
162
  - bin/console
163
163
  - bin/container_exec
164
164
  - bin/run_tests
165
+ - bin/runner
165
166
  - bin/setup.sh
166
167
  - exe/minfra
167
168
  - lib/deep_merge.rb