minfra-cli 1.12.0 → 1.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b4880a484c8d4d18875f3b3c355162c0f49eb1fcae69f8a870ee4c5ad8d04d0
4
- data.tar.gz: 1d34cb69b8b8e5a5476dcec8d14463d02a00a2fbdc47ccb12f761a93413a3df9
3
+ metadata.gz: ca90567af36553b84c7a69020e85970c56591d65f7546d1c22c1969cc9bd58f3
4
+ data.tar.gz: 451949094ac8fe4a3bc502b10ad1f8924f2cb5688da1297594061ab4c4e4780d
5
5
  SHA512:
6
- metadata.gz: 851fbc24107b2efd086f2e69adaaf2580f1ceed98eee55a71067348d4283360be440b14bf94b586f56a3a5180d0432a76553602cad02b6aef3e2715441bef236
7
- data.tar.gz: cffdcda6203c63f9efcc12b7f57dea382caf82f2a2cd8cca605c14b36ef8fe77a82695c72a8cddac31113c311f24fc753de8df7506a03894474d83d360b282ee
6
+ metadata.gz: 6007ef02b33c582bb9bd002cb4b22adda6dd4e5ec32cf7646beb9597ef03181b8944a463fe5db943c84d19c6ed50c8b324a5bb383cfa975b0f4301e1ffb214d5
7
+ data.tar.gz: 1908c4e6f95565dc3ebdf2bf371808021166997691c0fcb3514a89b2d47cf379d551b1f5f1934032cfe08afcc180f6f4d75b39d694c584d036f46a5f86aa8f05
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 1.12.1
2
+ * replaced all relevant system calls with new runners (fixed apply and
3
+ generic_secrets)
1
4
  # 1.12.0
2
5
  * refactoring Runner
3
6
  * adding runner type system (popen is default)
@@ -1,5 +1,5 @@
1
1
  module Minfra
2
2
  module Cli
3
- VERSION = '1.12.0'.freeze
3
+ VERSION = '1.12.1'.freeze
4
4
  end
5
5
  end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
1
4
  require 'erb'
2
5
  require 'erubis'
3
6
  require 'open3'
@@ -10,34 +13,32 @@ require 'active_support/core_ext'
10
13
  module Orchparty
11
14
  module Services
12
15
  class Context
13
- include ::Minfra::Cli::Logging
16
+ include ::Minfra::Cli::Logging
14
17
 
15
- attr_accessor :cluster_name
16
- attr_accessor :namespace
17
- attr_accessor :dir_path
18
- attr_accessor :app_config
19
- attr_accessor :options
18
+ attr_accessor :cluster_name, :namespace, :dir_path, :app_config, :options
20
19
 
21
- def initialize(cluster_name: , namespace:, file_path: , app_config:, out_io: STDOUT, app: )
20
+ def initialize(cluster_name:, namespace:, file_path:, app_config:, app:, out_io: $stdout)
22
21
  self.cluster_name = cluster_name
23
22
  self.namespace = namespace
24
23
  self.dir_path = file_path
25
24
  self.app_config = app_config
26
25
  @app = app
27
26
  @out_io = out_io
28
- self.options=options
27
+ self.options = options
29
28
  end
30
29
 
31
- def template(file_path, helm, flag: "-f ", fix_file_path: nil)
32
- return "" unless file_path
30
+ def template(file_path, helm, flag: '-f ', fix_file_path: nil)
31
+ return '' unless file_path
32
+
33
33
  puts "Rendering: #{file_path}"
34
- file_path = File.join(self.dir_path, file_path)
35
- if(file_path.end_with?(".erb"))
34
+ file_path = File.join(dir_path, file_path)
35
+
36
+ if file_path.end_with?('.erb')
36
37
  helm.application = OpenStruct.new(cluster_name: cluster_name, namespace: namespace)
37
38
  template = Erubis::Eruby.new(File.read(file_path))
38
39
  template.filename = file_path
39
40
  yaml = template.result(helm.get_binding)
40
- file = Tempfile.new("kube-deploy.yaml")
41
+ file = Tempfile.new('kube-deploy.yaml')
41
42
  file.write(yaml)
42
43
  file.close
43
44
  file_path = file.path
@@ -46,11 +47,11 @@ module Orchparty
46
47
  end
47
48
 
48
49
  def print_install(helm)
49
- @out_io.puts "---"
50
+ @out_io.puts '---'
50
51
  @out_io.puts install_cmd(helm, value_path(helm)).cmd
51
52
  @out_io.puts upgrade_cmd(helm, value_path(helm)).cmd
52
- @out_io.puts "---"
53
- @out_io.puts File.read(template(value_path(helm), helm, flag: "")) if value_path(helm)
53
+ @out_io.puts '---'
54
+ @out_io.puts File.read(template(value_path(helm), helm, flag: '')) if value_path(helm)
54
55
  end
55
56
 
56
57
  # On 05.02.2021 we have decided that it would be best to print both commands.
@@ -74,11 +75,15 @@ module Orchparty
74
75
  end
75
76
 
76
77
  def upgrade_cmd(helm, fix_file_path = nil)
77
- Minfra::Cli::HelmRunner.new("upgrade --namespace #{namespace} --kube-context #{cluster_name} --version #{helm.version} #{helm.name} #{helm.chart} #{template(value_path(helm), helm, fix_file_path: fix_file_path)}")
78
+ Minfra::Cli::HelmRunner.new("upgrade --namespace #{namespace} --kube-context #{cluster_name} --version #{helm.version} #{helm.name} #{helm.chart} #{template(
79
+ value_path(helm), helm, fix_file_path: fix_file_path
80
+ )}")
78
81
  end
79
82
 
80
83
  def install_cmd(helm, fix_file_path = nil)
81
- Minfra::Cli::HelmRunner.new("install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} --version #{helm.version} #{helm.name} #{helm.chart} #{template(value_path(helm), helm, fix_file_path: fix_file_path)}")
84
+ Minfra::Cli::HelmRunner.new("install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} --version #{helm.version} #{helm.name} #{helm.chart} #{template(
85
+ value_path(helm), helm, fix_file_path: fix_file_path
86
+ )}")
82
87
  end
83
88
  end
84
89
 
@@ -88,11 +93,15 @@ module Orchparty
88
93
  end
89
94
 
90
95
  def upgrade_cmd(apply, fix_file_path = nil)
91
- "kubectl apply --namespace #{namespace} --context #{cluster_name} #{template(value_path(apply), apply, fix_file_path: fix_file_path)}"
96
+ Minfra::Cli::KubeCtlRunner.new("apply --namespace #{namespace} --context #{cluster_name} #{template(
97
+ value_path(apply), apply, fix_file_path: fix_file_path
98
+ )}")
92
99
  end
93
100
 
94
101
  def install_cmd(apply, fix_file_path = nil)
95
- "kubectl apply --namespace #{namespace} --context #{cluster_name} #{template(value_path(apply), apply, fix_file_path: fix_file_path)}"
102
+ Minfra::Cli::KubeCtlRunner.new("apply --namespace #{namespace} --context #{cluster_name} #{template(
103
+ value_path(apply), apply, fix_file_path: fix_file_path
104
+ )}")
96
105
  end
97
106
  end
98
107
 
@@ -101,43 +110,51 @@ module Orchparty
101
110
  secret[:from_file]
102
111
  end
103
112
 
104
- def upgrade_cmd(secret, fix_file_path=nil)
105
- "kubectl --namespace #{namespace} --context #{cluster_name} create secret generic --dry-run -o yaml #{secret[:name]} #{template(value_path(secret), secret, flag: "--from-file=", fix_file_path: fix_file_path)} | kubectl --context #{cluster_name} apply -f -"
113
+ def upgrade_cmd(secret, fix_file_path = nil)
114
+ Minfra::Cli::KubeCtlRunner.new("--namespace #{namespace} --context #{cluster_name} create secret generic --dry-run -o yaml #{secret[:name]} #{template(
115
+ value_path(secret), secret, flag: '--from-file=', fix_file_path: fix_file_path
116
+ )} | #{apply_cmd(cluster_name).cmd}")
106
117
  end
107
118
 
108
- def install_cmd(secret, fix_file_path=nil)
109
- "kubectl --namespace #{namespace} --context #{cluster_name} create secret generic --dry-run -o yaml #{secret[:name]} #{template(value_path(secret), secret, flag: "--from-file=", fix_file_path: fix_file_path)} | kubectl --context #{cluster_name} apply -f -"
119
+ def install_cmd(secret, fix_file_path = nil)
120
+ Minfra::Cli::KubeCtlRunner.new("--namespace #{namespace} --context #{cluster_name} create secret generic --dry-run -o yaml #{secret[:name]} #{template(
121
+ value_path(secret), secret, flag: '--from-file=', fix_file_path: fix_file_path
122
+ )} | #{apply_cmd(cluster_name).cmd}")
123
+ end
124
+
125
+ def apply_cmd(cluster_name)
126
+ Minfra::Cli::KubeCtlRunner.new("--context #{cluster_name} apply -f -")
110
127
  end
111
128
  end
112
129
 
113
130
  class Label < Context
114
131
  def print_install(label)
115
- @out_io.puts "---"
132
+ @out_io.puts '---'
116
133
  @out_io.puts install_cmd(label)
117
134
  end
118
135
 
119
136
  def print_upgrade(label)
120
- @out_io.puts "---"
137
+ @out_io.puts '---'
121
138
  @out_io.puts upgrade_cmd(label)
122
139
  end
123
140
 
124
141
  def upgrade_cmd(label)
125
- "kubectl --namespace #{namespace} --context #{cluster_name} label --overwrite #{label[:resource]} #{label[:name]} #{label["value"]}"
142
+ Minfra::Cli::KubeCtlRunner.new("--namespace #{namespace} --context #{cluster_name} label --overwrite #{label[:resource]} #{label[:name]} #{label['value']}")
126
143
  end
127
144
 
128
145
  def install_cmd(label)
129
- "kubectl --namespace #{namespace} --context #{cluster_name} label --overwrite #{label[:resource]} #{label[:name]} #{label["value"]}"
146
+ Minfra::Cli::KubeCtlRunner.new("--namespace #{namespace} --context #{cluster_name} label --overwrite #{label[:resource]} #{label[:name]} #{label['value']}")
130
147
  end
131
148
  end
132
149
 
133
150
  class Wait < Context
134
151
  def print_install(wait)
135
- @out_io.puts "---"
152
+ @out_io.puts '---'
136
153
  @out_io.puts wait.cmd
137
154
  end
138
155
 
139
156
  def print_upgrade(wait)
140
- @out_io.puts "---"
157
+ @out_io.puts '---'
141
158
  @out_io.puts wait.cmd
142
159
  end
143
160
 
@@ -159,20 +176,19 @@ module Orchparty
159
176
  end
160
177
  end
161
178
 
162
-
163
179
  def print_install(chart)
164
-
165
180
  build_chart(chart) do |chart_path|
166
- cmd="helm template --namespace #{namespace} --debug --kube-context #{cluster_name} --output-dir #{chart_path.join('..','helm_expanded')} #{chart.name} #{chart_path}"
167
- @out_io.puts `$cmd`
168
- if system("#{cmd} > /dev/null")
181
+ cmd = "helm template --namespace #{namespace} --debug --kube-context #{cluster_name} --output-dir #{chart_path.join(
182
+ '..', 'helm_expanded'
183
+ )} #{chart.name} #{chart_path}"
184
+ @out_io.puts `$cmd`
185
+ if system("#{cmd} > /dev/null")
169
186
 
170
- info("helm template check: OK")
187
+ info('helm template check: OK')
171
188
  else
172
- error("helm template check: FAIL")
173
- end
189
+ error('helm template check: FAIL')
190
+ end
174
191
  end
175
-
176
192
  end
177
193
 
178
194
  def print_upgrade(chart)
@@ -182,7 +198,7 @@ module Orchparty
182
198
  def install(chart)
183
199
  info("Install: #{chart.name}")
184
200
  build_chart(chart) do |chart_path|
185
- res=Minfra::Cli::HelmRunner.new("install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}").run
201
+ res = Minfra::Cli::HelmRunner.new("install --create-namespace --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}").run
186
202
  @out_io.puts res.stdout
187
203
  end
188
204
  end
@@ -190,44 +206,41 @@ module Orchparty
190
206
  def upgrade(chart)
191
207
  info("Upgrade: #{chart}")
192
208
  build_chart(chart) do |chart_path|
193
- res=Minfra::Cli::HelmRunner.new("upgrade --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}").run
209
+ res = Minfra::Cli::HelmRunner.new("upgrade --namespace #{namespace} --kube-context #{cluster_name} #{chart.name} #{chart_path}").run
194
210
  @out_io.puts res.stdout
195
211
  end
196
212
  end
213
+
197
214
  private
198
-
215
+
199
216
  def build_chart(chart)
200
217
  dir = @app.status_dir.join('helm') # duplication
201
-
202
- params = chart._services.map {|s| app_config.services[s.to_sym] }.map{|s| [s.name, s]}.to_h
203
- run(templates_path: File.expand_path(chart.template, self.dir_path), params: params, output_chart_path: dir, chart: chart)
218
+
219
+ params = chart._services.map { |s| app_config.services[s.to_sym] }.map { |s| [s.name, s] }.to_h
220
+ run(templates_path: File.expand_path(chart.template, dir_path), params: params, output_chart_path: dir,
221
+ chart: chart)
204
222
  yield dir
205
223
  end
206
224
 
207
-
208
-
209
225
  # remember:
210
226
  # this is done for an app
211
227
  # that app can have multiple charts with multiple services!
212
-
213
- def run(templates_path:, params:, output_chart_path:, chart: )
214
228
 
215
-
216
- File.open(File.join(output_chart_path, 'values.yaml'),'a') do |helm_values|
229
+ def run(templates_path:, params:, output_chart_path:, chart:)
230
+ File.open(File.join(output_chart_path, 'values.yaml'), 'a') do |helm_values|
217
231
  params.each do |app_name, subparams|
218
232
  subparams[:chart] = chart
219
- used_vars=generate_documents_from_erbs(
233
+ used_vars = generate_documents_from_erbs(
220
234
  templates_path: templates_path,
221
235
  app_name: app_name,
222
236
  params: subparams,
223
237
  output_chart_path: output_chart_path
224
238
  )
225
- used_vars.each do |variable,value|
239
+ used_vars.each do |variable, value|
226
240
  helm_values.puts "#{variable}: \"#{value}\""
227
241
  end
228
242
  end
229
243
  end
230
-
231
244
  end
232
245
 
233
246
  def generate_documents_from_erbs(templates_path:, app_name:, params:, output_chart_path:)
@@ -237,13 +250,13 @@ module Orchparty
237
250
  end
238
251
 
239
252
  kind = params.fetch(:kind)
240
- params._used_vars = {} #here we'll collect all used vars
253
+ params._used_vars = {} # here we'll collect all used vars
241
254
 
242
255
  Dir[File.join(templates_path, kind, '*.erb')].each do |template_path|
243
256
  info("Rendering Template: #{template_path}")
244
257
  template_name = File.basename(template_path, '.erb')
245
258
  output_path = File.join(output_chart_path, 'templates', "#{app_name}-#{template_name}")
246
-
259
+
247
260
  template = Erubis::Eruby.new(File.read(template_path))
248
261
  template.filename = template_path
249
262
 
@@ -253,14 +266,13 @@ module Orchparty
253
266
  begin
254
267
  document = template.result(CleanBinding.new.get_binding(params))
255
268
  rescue Exception
256
- error "#{template_path} has a problem: #{$!.inspect}"
269
+ error "#{template_path} has a problem: #{$ERROR_INFO.inspect}"
257
270
  raise
258
271
  end
259
272
  File.write(output_path, document)
260
273
  end
261
274
  params._used_vars
262
275
  end
263
-
264
276
  end
265
277
  end
266
278
  end
@@ -268,19 +280,16 @@ end
268
280
  class KubernetesApplication
269
281
  include Minfra::Cli::Logging
270
282
 
271
- attr_accessor :cluster_name
272
- attr_accessor :file_path
273
- attr_accessor :namespace
274
- attr_accessor :app_config
283
+ attr_accessor :cluster_name, :file_path, :namespace, :app_config
275
284
  attr_reader :status_dir
276
285
 
277
- def initialize(app_config: [], namespace:, cluster_name:, file_name:, status_dir:, out_io: STDOUT)
278
- self.file_path = Pathname.new(file_name).parent.expand_path #path of the stack
286
+ def initialize(namespace:, cluster_name:, file_name:, status_dir:, app_config: [], out_io: $stdout)
287
+ self.file_path = Pathname.new(file_name).parent.expand_path # path of the stack
279
288
  self.cluster_name = cluster_name
280
289
  self.namespace = namespace
281
290
  self.app_config = app_config
282
291
  @status_dir = status_dir
283
- @out_io= out_io
292
+ @out_io = out_io
284
293
  end
285
294
 
286
295
  def install
@@ -296,17 +305,17 @@ class KubernetesApplication
296
305
  end
297
306
 
298
307
  private
308
+
299
309
  def prepare
300
-
301
310
  output_chart_path = @status_dir.join('helm')
302
311
  output_chart_path.rmtree if output_chart_path.exist?
303
312
  output_chart_path.mkpath
304
- templates_path = file_path.join('../../chart-templates').expand_path #don't ask. the whole concept of multiple charts in an app stinks...
305
-
313
+ templates_path = file_path.join('../../chart-templates').expand_path # don't ask. the whole concept of multiple charts in an app stinks...
314
+
306
315
  generate_chart_yaml(
307
- templates_path: templates_path,
308
- output_chart_path: output_chart_path,
309
- chart_name: namespace,
316
+ templates_path: templates_path,
317
+ output_chart_path: output_chart_path,
318
+ chart_name: namespace
310
319
  )
311
320
 
312
321
  info("generating base helm structure from: #{output_chart_path} from #{templates_path}")
@@ -314,26 +323,26 @@ class KubernetesApplication
314
323
 
315
324
  system("cp #{File.join(templates_path, 'values.yaml')} #{File.join(output_chart_path, 'values.yaml')}")
316
325
  system("cp #{File.join(templates_path, '.helmignore')} #{File.join(output_chart_path, '.helmignore')}")
317
- system("cp #{File.join(templates_path, 'templates/_helpers.tpl')} #{File.join(output_chart_path, 'templates/_helpers.tpl')}")
318
-
326
+ system("cp #{File.join(templates_path,
327
+ 'templates/_helpers.tpl')} #{File.join(output_chart_path, 'templates/_helpers.tpl')}")
319
328
  end
320
329
 
321
- def generate_chart_yaml(templates_path:, output_chart_path:, chart_name: )
330
+ def generate_chart_yaml(templates_path:, output_chart_path:, chart_name:)
322
331
  template_path = File.join(templates_path, 'Chart.yaml.erb')
323
332
  output_path = File.join(output_chart_path, 'Chart.yaml')
324
333
 
325
- res=Minfra::Cli::Templater.read(template_path, params: {chart_name: chart_name})
334
+ res = Minfra::Cli::Templater.read(template_path, params: { chart_name: chart_name })
326
335
  File.write(output_path, res)
327
336
  end
328
-
337
+
329
338
  def combine_charts(app_config)
330
339
  services = app_config._service_order.map(&:to_s)
331
340
  app_config._service_order.each do |name|
332
341
  current_service = app_config[:services][name]
333
- if current_service._type == "chart"
334
- current_service._services.each do |n|
335
- services.delete n.to_s
336
- end
342
+ next unless current_service._type == 'chart'
343
+
344
+ current_service._services.each do |n|
345
+ services.delete n.to_s
337
346
  end
338
347
  end
339
348
  services
@@ -345,14 +354,14 @@ class KubernetesApplication
345
354
  services.each do |name|
346
355
  service = app_config[:services][name]
347
356
  info "Service: #{name}(#{service._type}) #{method}"
348
- deployable_class="::Orchparty::Services::#{service._type.classify}".constantize
349
- deployable=deployable_class.new(cluster_name: cluster_name,
350
- namespace: namespace,
351
- file_path: file_path,
352
- app_config: app_config,
353
- out_io: @out_io,
354
- app: self)
355
-
357
+ deployable_class = "::Orchparty::Services::#{service._type.classify}".constantize
358
+ deployable = deployable_class.new(cluster_name: cluster_name,
359
+ namespace: namespace,
360
+ file_path: file_path,
361
+ app_config: app_config,
362
+ out_io: @out_io,
363
+ app: self)
364
+
356
365
  deployable.send(method, service)
357
366
  end
358
367
  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.12.0
4
+ version: 1.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Schrammel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-01 00:00:00.000000000 Z
11
+ date: 2023-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  - !ruby/object:Gem::Version
240
240
  version: '0'
241
241
  requirements: []
242
- rubygems_version: 3.4.10
242
+ rubygems_version: 3.3.7
243
243
  signing_key:
244
244
  specification_version: 4
245
245
  summary: A cli framework for k8s based development and deployment.