blackstack-deployer 1.2.14 → 1.2.17

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/blackstack-deployer.rb +94 -40
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b1cb9fcf8b593e26d0af9259d1f0c89c3ef3488424453e5532c31688e99576b
4
- data.tar.gz: c242909970936867f33ecad49d997a467fe8e04b0dd2903d0b90ecd29ab330ad
3
+ metadata.gz: 9e60ac86398b508a3c0262ee2a8d19d41e59994dbe293de5d155c68b0681d75a
4
+ data.tar.gz: 6054c1d54a2c3da6bea3b8e30ad1168c09450039918eeed55f1e1380deb7f01f
5
5
  SHA512:
6
- metadata.gz: 2b91301e161fe11577b77a78ceccfe8e160355ddf0118e6200ecb3137c21c8aa29456cbbc1f2d78210ae64efbb55f780517cd10b2c6302ba9219dadc20b27dd6
7
- data.tar.gz: bd821f6059852d9cc265250161a879d04f131b245244b46fc406bb357de49fb4bfd42e387ba8e0ca120ed354b559623713b1506584ff81d62b378aa539820410
6
+ metadata.gz: 441321acb984529b0d4fceb4b6b25cd6d7264731019d6076df6c8b5c65026345e9f7cab72c2b12df6843bedae5520b4b8dd6745d87e388057938a861a7ccd055
7
+ data.tar.gz: 5f487e1c15d30092ca92583eb98e0438334350cda2d719e77b18a03a416f7cfcb34fadd452de992c83b482f1c2b20c9c33cc1f9f0ea572825bf5f35348a1d1fd
@@ -5,14 +5,25 @@ module BlackStack
5
5
  # Deployer is a library that can be used to deploy a cluster of nodes.
6
6
  module Deployer
7
7
  @@logger = BlackStack::BaseLogger.new(nil)
8
+ @@show_output = false
8
9
  @@nodes = []
9
10
  @@routines = []
10
11
 
12
+ # set show_output
13
+ def self.set_show_output(value)
14
+ @@show_output = value
15
+ end
16
+
11
17
  # set the logger
12
18
  def self.set_logger(i_logger)
13
19
  @@logger = i_logger
14
20
  end
15
21
 
22
+ # get show_output
23
+ def self.show_output
24
+ @@show_output
25
+ end
26
+
16
27
  # get the logger assigned to the module
17
28
  def self.logger
18
29
  @@logger
@@ -155,7 +166,7 @@ module BlackStack
155
166
  def run(node)
156
167
  ret = []
157
168
  self.commands.each do |c|
158
- BlackStack::Deployer.logger.logs "Running command: #{c.command.to_s}... "
169
+ #BlackStack::Deployer.logger.logs "Running command: #{c.command.to_s}... "
159
170
  h = c.run(node)
160
171
  ret << h
161
172
 
@@ -163,10 +174,10 @@ module BlackStack
163
174
  #BlackStack::Deployer.logger.logf h.to_s
164
175
 
165
176
  if h[:errors].size == 0
166
- BlackStack::Deployer.logger.done
177
+ #BlackStack::Deployer.logger.done
167
178
  else
168
- BlackStack::Deployer.logger.logf('error: ' + h.to_s)
169
- raise "Error running command: #{h.to_s}"
179
+ #BlackStack::Deployer.logger.logf('error: ' + h.to_s)
180
+ raise "Error running command:\n#{h[:errors].uniq.join("\n")}"
170
181
  end
171
182
  end
172
183
  ret
@@ -175,7 +186,7 @@ module BlackStack
175
186
 
176
187
  # define attributes and methods of a routine's command
177
188
  module CommandModule
178
- attr_accessor :command, :matches, :nomatches, :sudo
189
+ attr_accessor :command, :matches, :nomatches, :sudo, :background
179
190
 
180
191
  def self.descriptor_errors(c)
181
192
  errors = []
@@ -200,8 +211,11 @@ module BlackStack
200
211
  # :reboot is a reserved word, so it is fine to call :reboot
201
212
  else
202
213
  # validate: existis a routine with a the value c[:command].to_s on its :name key
203
- errors << "The routine with the name #{c[:command].to_s} does not exist" unless BlackStack::Deployer::routines.select { |r| r.name == c[:command].to_s }.size > 0
214
+ errors << "The routine with the name #{c[:command].to_s} does not exist" unless BlackStack::Deployer::deployment_routines.select { |r| r.name == c[:command].to_s }.size > 0
204
215
  end
216
+ else
217
+ # validate: each line of the :command value must finish with ;
218
+ errors << "Each line in the :command value must finish with `;`.\nCommand: #{c[:command]}.\nRefer https://github.com/leandrosardi/blackstack-deployer#67-running-commands-in-background for more details." unless c[:command].strip.split("\n").select { |l| l.strip[-1,1] != ';' }.size == 0
205
219
  end
206
220
 
207
221
  # if c[:matches] exists
@@ -229,6 +243,12 @@ module BlackStack
229
243
  end # each
230
244
  end # if c[:matches].is_a?(Array)
231
245
  end # if :matches exists
246
+
247
+ # if c[:background] exists, it must be a boolean
248
+ if c.has_key?(:background)
249
+ errors << "The value of the key :background is not a boolean" unless c[:background].is_a?(TrueClass) || c[:background].is_a?(FalseClass)
250
+ end
251
+
232
252
  #
233
253
  errors.uniq
234
254
  end # def self.descriptor_error(h)
@@ -257,7 +277,12 @@ module BlackStack
257
277
  self.nomatches << BlackStack::Deployer::NoMatch.new(m)
258
278
  end
259
279
  end
260
- end
280
+ end
281
+ if h.has_key?(:background)
282
+ self.background = h[:background]
283
+ else
284
+ self.background = false
285
+ end
261
286
  end # def initialize(h)
262
287
 
263
288
  def to_hash
@@ -275,49 +300,78 @@ module BlackStack
275
300
  h
276
301
  end # def to_hash
277
302
 
303
+ # return the code to exectute the command,
304
+ # after applying modifications requested by
305
+ # some parameters like `:show_outut` or `:background`.
306
+ def code(node)
307
+ ret = self.command
308
+ # replacing parameters
309
+ ret.scan(/%[a-zA-Z0-9\_]+%/).each do |p|
310
+ if p == '%eth0_ip%' # reserved parameter
311
+ # TODO: move the method eth0_ip to the blackstack-nodes library
312
+ ret.gsub!(p, node.eth0_ip)
313
+ elsif p == '%timestamp%' # reserved parameter
314
+ # TODO: move this to a timestamp function on blackstack-core
315
+ ret.gsub!(p, Time.now.to_s.gsub(/\D/, ''))
316
+ else
317
+ if node.parameters.has_key?(p.gsub(/%/, '').to_sym)
318
+ ret.gsub!(p, node.parameters[p.gsub(/%/, '').to_sym].to_s)
319
+ else
320
+ raise "The parameter #{p} does not exist in the node descriptor #{node.parameters.to_s}"
321
+ end
322
+ end
323
+ end
324
+ # if the command is configured to run in background, and the flag show_ouput is off, then modify the ret to run in background
325
+ if self.background && !BlackStack::Deployer.show_output
326
+ lines = ret.strip.lines
327
+ total = lines.size
328
+ i = 0
329
+ lines.each { |l|
330
+ i += 1
331
+ if i == total
332
+ l.gsub!(/;$/, ' > /dev/null 2>&1 &')
333
+ else
334
+ l.gsub!(/;$/, ' > /dev/null 2>&1;')
335
+ end
336
+ }
337
+ ret = lines.join("\n")
338
+ end
339
+ # return the code
340
+ ret
341
+ end
342
+
278
343
  def run(node)
344
+ l = BlackStack::Deployer.logger
279
345
  errors = []
280
- code = self.command
281
346
  output = nil
347
+ s = self.code(node)
282
348
 
283
- # if code is a symbol
284
- if code.is_a?(Symbol)
349
+ # if self.command is a symbol
350
+ if self.command.is_a?(Symbol)
285
351
 
286
- # if code is equel than :reboot
287
- if code == :reboot
352
+ # if self.command is equel than :reboot
353
+ if self.command == :reboot
288
354
  # call the node reboot method
289
355
  node.reboot
290
356
  else
291
357
  # look for a routine with this name
292
- r = BlackStack::Deployer.routines.select { |r| r.name == code.to_s }.first
358
+ r = BlackStack::Deployer.routines.select { |r| r.name == self.command.to_s }.first
293
359
  if !r.nil?
294
360
  r.run(node)
295
361
  else
296
- raise "The routine #{code.to_s} does not exist"
362
+ raise "The routine #{self.command.to_s} does not exist"
297
363
  end
298
364
  end
299
365
 
300
- # if code is a string
301
- elsif code.is_a?(String)
302
- # replacing parameters
303
- code.scan(/%[a-zA-Z0-9\_]+%/).each do |p|
304
- if p == '%eth0_ip%' # reserved parameter
305
- # TODO: move the method eth0_ip to the blackstack-nodes library
306
- code.gsub!(p, node.eth0_ip)
307
- elsif p == '%timestamp%' # reserved parameter
308
- # TODO: move this to a timestamp function on blackstack-core
309
- code.gsub!(p, Time.now.to_s.gsub(/\D/, ''))
310
- else
311
- if node.parameters.has_key?(p.gsub(/%/, '').to_sym)
312
- code.gsub!(p, node.parameters[p.gsub(/%/, '').to_sym].to_s)
313
- else
314
- raise "The parameter #{p} does not exist in the node descriptor #{node.parameters.to_s}"
315
- end
316
- end
317
- end
366
+ # if self.command is a string
367
+ elsif self.command.is_a?(String)
318
368
 
319
369
  # running the command
320
- output = node.exec(code, self.sudo)
370
+ l.logs "Show command output... " if BlackStack::Deployer.show_output
371
+ l.log "\n\nCommand:\n--------\n\n#{s} " if BlackStack::Deployer.show_output
372
+ output = node.exec(s, self.sudo)
373
+ l.log "\n\nOutput:\n-------\n\n#{output}" if BlackStack::Deployer.show_output
374
+ l.logf('done tracing.') if BlackStack::Deployer.show_output
321
375
 
322
376
  # validation: at least one of the matches should happen
323
377
  if self.matches.size > 0
@@ -339,7 +393,7 @@ module BlackStack
339
393
  # return a hash descriptor of the command result
340
394
  {
341
395
  :command => self.command,
342
- :code => code,
396
+ :code => s,
343
397
  :output => output,
344
398
  :errors => errors,
345
399
  }
@@ -514,19 +568,19 @@ module BlackStack
514
568
  raise "The routine #{routine_name} cannot be run on the node #{node_name}: #{errors.uniq.join(".\n")}" if errors.length > 0
515
569
 
516
570
  # connect the node
517
- self.logger.logs "Connecting to node #{n.name}... "
571
+ #self.logger.logs "Connecting to node #{n.name}... "
518
572
  n.connect
519
- self.logger.done
573
+ #self.logger.done
520
574
 
521
575
  # run the routine
522
- self.logger.logs "Running routine #{r.name}... "
576
+ #self.logger.logs "Running routine #{r.name}... "
523
577
  r.run(n)
524
- self.logger.done
578
+ #self.logger.done
525
579
 
526
580
  # disconnect the node
527
- self.logger.logs "Disconnecting from node #{n.name}... "
581
+ #self.logger.logs "Disconnecting from node #{n.name}... "
528
582
  n.disconnect
529
- self.logger.done
583
+ #self.logger.done
530
584
 
531
585
  end # def
532
586
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blackstack-deployer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.14
4
+ version: 1.2.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Daniel Sardi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-30 00:00:00.000000000 Z
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: blackstack-nodes