nuri 0.5.2 → 0.5.3

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
  SHA1:
3
- metadata.gz: 5713187406a70381abeffa8e382892172e1cab3a
4
- data.tar.gz: 589c3291070bf16e6ee05e4846192ae3d0ddca81
3
+ metadata.gz: 52435655f176b90c016bb5f1ae94348f03c480c7
4
+ data.tar.gz: 12e8e06d2689353bb6fd3dfe2b8047b709ba8d20
5
5
  SHA512:
6
- metadata.gz: 5917ac035100aeb2e1dc75ff326034d9bbc7b5ef8a8679307581d389cca20dc8b9138bafddcdddccfbe9e6457184418eaa1ffb42ea6ddfd6c5fed6d8d30b8728
7
- data.tar.gz: d14d4cfd96af587cdf02a4f622e1ad7773db83052eef68de3e304f1196313814aca4e0673d3069d721f662fba89db8158bef6a91b1b2f09eeb3e0ee13248c01a
6
+ metadata.gz: fbe8ac30498b9f0c672dcf429d6c7a5e7178e00a24c50c725cd95d1472d06d570e0a35121913d266c112b797c4af141ad16b2bd0870c7899006484102093dadf
7
+ data.tar.gz: da39e1869ccf6eae68622497fe2f99c6143079dc57aa8675a6a6ebc2584f28c51d4477587e86359273c8c763537657bc4bf3c32c4dd0220e52ed6b0c9a5c9684
@@ -7,6 +7,7 @@ rvm:
7
7
  - 1.9.2
8
8
  - 1.9.3
9
9
  - 2.0.0
10
+ - jruby-19mode
10
11
 
11
12
  notifications:
12
13
  - email: false
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.5.3
data/bin/nuri CHANGED
@@ -8,11 +8,6 @@ About = "Nuri #{Version} (c) 2013"
8
8
 
9
9
  Nuri.init
10
10
 
11
- def console(*args)
12
- print Time.now.strftime('%Y-%m-%d %H:%M:%S '.yellow)
13
- $stdout.puts(args)
14
- end
15
-
16
11
  class Nuri::Console
17
12
  include Nuri::Helper
18
13
 
@@ -37,7 +32,7 @@ where [options] are:
37
32
  EOS
38
33
  opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
39
34
  opt :json, 'print output in JSON'
40
- opt :plain, 'print output in plain text'
35
+ opt :color, 'enable colorized output'
41
36
  opt :no_push_module, 'disable automatic push module'
42
37
  end
43
38
  help, args = check_help(args)
@@ -54,9 +49,10 @@ EOS
54
49
  state.accept(Sfp::Helper::Sfp2Ruby)
55
50
 
56
51
  if opts[:json]
57
- puts (opts[:plain] ? JSON.generate(state) : CodeRay.encode(JSON.pretty_generate(state), :json, :terminal))
52
+ puts (!opts[:color] ? JSON.generate(state) : CodeRay.encode(JSON.pretty_generate(state), :json, :terminal))
58
53
  else
59
- puts (opts[:plain] ? YAML.dump(state) : CodeRay.encode(YAML.dump(state), :yaml, :terminal))
54
+ puts (!opts[:color] ? YAML.dump(state) : CodeRay.encode(YAML.dump(state), :yaml, :terminal))
55
+
60
56
  end
61
57
  else
62
58
  $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details.".red
@@ -73,7 +69,8 @@ EOS
73
69
  opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
74
70
  opt :parallel, 'generate a parallel plan', :short => '-l'
75
71
  opt :apply, 'generate and execute a plan', :short => '-a'
76
- opt :plain, 'print output in plain JSON'
72
+ opt :json, 'print the plan in JSON'
73
+ opt :color, 'enable colorized output'
77
74
  opt :no_interactive, 'disable interactive input'
78
75
  opt :no_push_module, 'disable automatic push module'
79
76
  opt :image, 'image graph of the generated plan', :default => Dir.home + '/.nuri/plan.png'
@@ -83,6 +80,7 @@ EOS
83
80
 
84
81
  if help
85
82
  parser.educate(STDOUT)
83
+
86
84
  elsif File.exist?(opts[:model_file])
87
85
  opts[:plan] = true
88
86
  opts[:push_modules] = true if !opts[:no_push_module]
@@ -95,22 +93,25 @@ EOS
95
93
  File.open(plan_file, 'w') { |f| f.write(JSON.generate(plan)) }
96
94
 
97
95
  if opts[:image]
98
- system "#{File.dirname(__FILE__)}/sfw2graph #{plan_file} #{opts[:image]}"
96
+ system "#{File.dirname(__FILE__)}/nuri-sfwgraph #{plan_file} #{opts[:image]}"
99
97
  end
100
98
 
101
- if opts[:plain]
102
- puts JSON.generate(plan)
99
+ if opts[:json]
100
+ json = JSON.pretty_generate(plan)
101
+ puts (opts[:color] ? CodeRay.encode(json, :json, :terminal) : json)
102
+
103
103
  else
104
104
  actions = plan['workflow']
105
105
  if plan['type'] == 'sequential'
106
- p = 'Sequential Plan:'.yellow
106
+ p = 'Sequential Plan:'
107
107
  actions.each_index { |i| p += "\n#{i+1}. #{actions[i]['name']} #{JSON.generate(actions[i]['parameters'])}" }
108
108
  puts p
109
109
  else
110
- p = 'Partial-Order Plan:'.yellow
110
+ p = 'Partial-Order Plan:'
111
111
  actions.each { |op| p += "\n#{op['id']+1}. #{op['name']} #{JSON.generate(op['parameters'])} (#{op['predecessors'].map{|i|i+1}}, #{op['successors'].map{|i|i+1}})" }
112
112
  puts p
113
113
  end
114
+
114
115
  end
115
116
 
116
117
  if not opts[:apply] and not opts[:no_interactive]
@@ -119,23 +120,23 @@ EOS
119
120
  end
120
121
 
121
122
  if opts[:apply]
122
- puts 'Executing the plan: '.yellow
123
+ puts 'Executing the plan:'
123
124
  opts[:plan] = plan
124
125
  if master.execute_plan(opts)
125
- puts "Execution success!".green
126
+ puts (opts[:color] ? "Execution success.".green : "Execution success.")
126
127
  else
127
- puts "Execution failed!".red
128
+ puts (opts[:color] ? "Execution failed.".red : "Execution failed.")
128
129
  end
129
130
  end
130
131
 
131
132
  elsif plan['workflow'].length == 0
132
- puts (opts[:plain] ? 'Goal state has been achieved.' : 'Goal state has been achieved.').green
133
+ puts (opts[:color] ? "Goal state has been achieved.".green : "Goal state has been achieved.")
133
134
  end
134
135
  else
135
- $stderr.puts (opts[:plain] ? "No solution!".yellow : "No solution!".red)
136
+ $stderr.puts (opts[:color] ? "No solution.".red : "No solution.")
136
137
  end
137
138
  else
138
- $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details.".red
139
+ $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details."
139
140
  end
140
141
  end
141
142
 
@@ -148,7 +149,7 @@ EOS
148
149
  opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
149
150
  opt :purge, 'purge existing BSig model', :short => '-g'
150
151
  opt :deploy, 'generate and then deploy a BSig model'
151
- opt :plain, 'print output in plain JSON'
152
+ opt :color, 'enable colorized output'
152
153
  opt :no_interactive, 'disable interactive input'
153
154
  end
154
155
  help, args = check_help(args)
@@ -163,21 +164,20 @@ EOS
163
164
  master.set_model(opts)
164
165
 
165
166
  if opts[:purge]
167
+ print "Purging Behavioural Signature model "
166
168
  if master.purge_bsig(opts)
167
- puts "Purging Behavioural Signature model [OK]".green
169
+ puts (opts[:color] ? "[OK]".green : "[OK]")
168
170
  else
169
- puts "Purging Behavioural Signature model [Failed]".red
171
+ puts (opts[:color] ? "[Failed]".red : "[Failed]")
170
172
  end
173
+
171
174
  else
172
175
  bsig = master.get_bsig(opts)
173
176
  if bsig.is_a?(Hash) and bsig.length > 0
174
177
  empty_local_bsig = bsig.select { |name,local_bsig| local_bsig['operators'].length <= 0 }
175
178
  if empty_local_bsig.length != bsig.length
176
- if opts[:plain]
177
- puts JSON.generate(bsig)
178
- else
179
- puts CodeRay.encode(JSON.pretty_generate(bsig), :json, :terminal)
180
- end
179
+ json = JSON.pretty_generate(bsig)
180
+ puts (opts[:color] ? CodeRay.encode(json, :json, :terminal) : json)
181
181
 
182
182
  if not opts[:no_interactive] and not opts[:deploy]
183
183
  print "Deploy the BSig model [y/N]? "
@@ -185,23 +185,23 @@ EOS
185
185
  end
186
186
 
187
187
  if opts[:deploy]
188
- puts 'Deploying the BSig model: '.yellow
188
+ print 'Deploying the BSig model '
189
189
  opts[:bsig] = bsig
190
190
  if master.deploy_bsig(opts)
191
- puts "Deployment success!".green
191
+ puts (opts[:color] ? "[OK]".green : "[OK]")
192
192
  else
193
- puts "Deployment failed!".red
193
+ puts (opts[:color] ? "[Failed]".red : "[Failed]")
194
194
  end
195
195
  end
196
196
  else
197
- puts (opts[:plain] ? 'Goal state has been achieved.' : 'Goal state has been achieved.').green
197
+ puts (opts[:color] ? "Goal state has been achieved.".green : "Goal state has been achieved.")
198
198
  end
199
199
  else
200
- $stderr.puts (opts[:plain] ? "No solution!".yellow : "No solution!".red)
200
+ $stderr.puts (opts[:color] ? "No solution.".red : "No solution.")
201
201
  end
202
202
  end
203
203
  else
204
- $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details.".red
204
+ $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details."
205
205
  end
206
206
  end
207
207
 
@@ -216,13 +216,15 @@ where <subcommand> is:
216
216
  restart restart the agent (use SSH for remode node)
217
217
  stop stop the agent (use SSH for remote node)
218
218
  status get status (use SSH for remote node)
219
+ reset delete all modules, model (desired state), bsig-model,
220
+ and agents list
219
221
  state get current state
220
222
  model get current model
221
223
  bsig get current Behavioural Signature model
222
224
  exec <action> execute given action description
223
225
  <action> := <path> [param1=value1 param2=value2 ...]
224
226
  module get modules list
225
- log get last 100 lines of logs
227
+ log get last 100 lines of agent's logs file
226
228
  list list of available agents from model file
227
229
 
228
230
  where [options] are:
@@ -233,6 +235,7 @@ EOS
233
235
  opt :ssh_user, "SSH username", :short => '-u', :default => 'root'
234
236
  opt :ssh_port, "SSH port", :short => '-p', :default => '22'
235
237
  opt :raw, "print a RAW output"
238
+ opt :color, "enable colorized output"
236
239
  end
237
240
  help, args = check_help(args)
238
241
  subcommand = (args.length > 0 ? args.shift : '')
@@ -248,26 +251,26 @@ EOS
248
251
  parser.educate(STDOUT)
249
252
  elsif `which sfpagent`.strip.length > 0 or ssh_opt.length > 0
250
253
  case subcommand
251
- when 'install'
254
+ when 'i', 'install'
252
255
  system("#{ssh_opt} sudo gem install sfpagent --no-ri --no-rdoc")
253
256
 
254
- when 'start'
257
+ when 's', 'start'
255
258
  system "#{ssh_opt} sfpagent -s && sleep 5"
256
259
  system "#{ssh_opt} sfpagent -a"
257
260
 
258
- when 'restart'
261
+ when 'r', 'restart'
259
262
  system "#{ssh_opt} sfpagent -r"
260
263
 
261
- when 'stop'
264
+ when 't', 'stop'
262
265
  system("#{ssh_opt} sfpagent -t")
263
266
 
264
- when 'status'
267
+ when 'a', 'status'
265
268
  system("#{ssh_opt} sfpagent -a")
266
269
 
267
270
  when 'upgrade'
268
271
  system("#{ssh_opt} sudo gem update sfpagent --no-ri --no-rdoc")
269
272
 
270
- when 'state'
273
+ when 'e', 'state'
271
274
  code, state = get_data(opts[:address], 1314, "/state")
272
275
  if code == '200' and state =~ /{.*}/
273
276
  state = JSON[state]['state']
@@ -275,47 +278,51 @@ EOS
275
278
  state.keys.each { |key| state.delete(key) if state[key]['_context'] != 'object' }
276
279
  state.accept(Sfp::Helper::Sfp2Ruby)
277
280
  end
278
- puts CodeRay.encode(YAML.dump(state), :yaml, :terminal)
281
+ puts (opts[:color] ? CodeRay.encode(YAML.dump(state), :yaml, :terminal) : YAML.dump(state))
282
+
279
283
  else
280
284
  puts state
285
+
281
286
  end
282
287
 
283
- when 'model'
288
+ when 'm', 'model'
284
289
  code, model = get_data(opts[:address], 1314, "/model")
285
- if code == '200' and model =~ /{.*}/
290
+ if code == '200' and model =~ /{.+}/
286
291
  model = JSON[model]
287
292
  if not opts[:raw]
288
293
  model.select! { |k,v| k[0,1] != '_' and (not v.is_a?(Hash) or v['_context'] == 'object') }
289
294
  model.accept(Sfp::Helper::Sfp2Ruby)
290
295
  end
291
- puts CodeRay.encode(YAML.dump(model), :yaml, :terminal)
292
- elsif code == '404'
293
- puts "The model of desired state is not exist!"
296
+ puts (opts[:color] ? CodeRay.encode(YAML.dump(model), :yaml, :terminal) : YAML.dump(model))
297
+ elsif code == '404' or model == '{}'
298
+ puts "Model of desired state is not exist."
294
299
  else
295
300
  $stderr.puts "Error: #{code}"
296
301
  end
297
302
 
298
- when 'bsig'
299
- code, json = get_data(opts[:address], 1314, "/bsig")
300
- if code == '200' and json.length > 0
301
- puts CodeRay.encode(JSON.pretty_generate(JSON[json]), :json, :terminal)
302
- elsif code == '404'
303
- puts 'Behavioural Signature model is not exist!'
303
+ when 'b', 'bsig'
304
+ code, bsig = get_data(opts[:address], 1314, "/bsig")
305
+ if code == '200' and bsig =~ /{.+}/
306
+ json = JSON.pretty_generate(JSON[json])
307
+ puts (opts[:color] ? CodeRay.encode(json, :json, :terminal) : json)
308
+ elsif code == '404' or bsig == '{}'
309
+ puts 'Behavioural Signature model is not exist.'
304
310
  else
305
311
  $stderr.puts "Error: #{code}"
306
312
  end
307
313
 
308
- when 'module'
309
- code, json = get_data(opts[:address], 1314, "/modules")
310
- if code == '200' and json.length >= 2
311
- puts CodeRay.encode(YAML.dump(JSON[json]), :yaml, :terminal)
312
- elsif code == '404'
313
- puts 'No module is available!'
314
+ when 'mod', 'module', 'modules'
315
+ code, modules = get_data(opts[:address], 1314, "/modules")
316
+ if code == '200' and modules =~ /{.+}/
317
+ output = YAML.dump(JSON[modules])
318
+ puts (opts[:color] ? CodeRay.encode(output, :yaml, :terminal) : output)
319
+ elsif code == '404' or modules == '{}'
320
+ puts 'No module is available.'
314
321
  else
315
322
  $stderr.puts "Error: #{code}"
316
323
  end
317
324
 
318
- when 'exec', 'execute'
325
+ when 'x', 'exec', 'execute'
319
326
  if args.length > 0
320
327
  name = args.shift
321
328
  name = '$.' + name if !name.isref
@@ -328,25 +335,44 @@ EOS
328
335
  end
329
336
  data = { 'action' => JSON.generate({ 'name' => name, 'parameters' => parameters }) }
330
337
  code, _ = post_data(opts[:address], 1314, "/execute", data)
331
- puts (code == '200' ? "Executing #{name} [OK]".green : "Executing #{name} [Failed]".red)
338
+ if opts[:color]
339
+ puts (code == '200' ? "Executing #{name} [OK]".green : "Executing #{name} [Failed]".red)
340
+ else
341
+ puts (code == '200' ? "Executing #{name} [OK]" : "Executing #{name} [Failed]")
342
+ end
332
343
  else
333
344
  $stderr.puts 'Invalid parameters (usage: agent exec <action-path> [action-arguments]).'
334
345
  end
335
346
 
336
- when 'log'
347
+ when 'g', 'log', 'logs'
337
348
  puts Net::HTTP.get "#{opts[:address]}", "/log", 1314
338
349
 
339
- when 'list'
350
+ when 'l', 'list'
340
351
  get_agents(opts).each { |name,model| puts "#{name} address=#{model['sfpAddress']} port=#{model['sfpPort']}" }
341
352
 
342
- when 'help'
353
+ when 'reset'
354
+ codes = []
355
+ codes[0], _ = delete_data(opts[:address], 1314, "/bsig")
356
+ codes[1], _ = delete_data(opts[:address], 1314, "/model/cache")
357
+ codes[2], _ = delete_data(opts[:address], 1314, "/model")
358
+ codes[3], _ = delete_data(opts[:address], 1314, "/modules")
359
+ codes[4], _ = delete_data(opts[:address], 1314, "/agents")
360
+
361
+ failed = codes.index { |x| x != '200' }
362
+ if opts[:color]
363
+ puts (failed ? "Reset agent [Failed]".red : "Reset agent [OK]".green)
364
+ else
365
+ puts (failed ? "Reset agent [Failed]" : "Reset agent [OK]")
366
+ end
367
+
368
+ when 'h', 'help'
343
369
  parser.educate(STDOUT)
344
370
 
345
371
  else
346
- $stderr.puts 'Unrecognized command! Use \"-h\" to print available commands.'.red
372
+ $stderr.puts 'Unrecognized command! Use -h to print available commands.'
347
373
  end
348
374
  else
349
- $stderr.puts 'sfpagent gem is not installed!'.red
375
+ $stderr.puts 'sfpagent gem is not installed!'
350
376
  end
351
377
  end
352
378
 
@@ -382,7 +408,8 @@ EOS
382
408
  end
383
409
 
384
410
  def do_console
385
- banner = <<-EOS
411
+ parser = Trollop::Parser.new do
412
+ banner <<-EOS
386
413
  Usage: <command> [options]
387
414
  when <command> is:
388
415
  state get current state
@@ -394,12 +421,13 @@ when <command> is:
394
421
  exit exit this console
395
422
 
396
423
  EOS
424
+ end
397
425
 
398
426
  username = `whoami`.strip
399
427
  puts About
400
428
  loop do
401
429
  begin
402
- print "nuri@#{username}> ".green
430
+ print "nuri@#{username}> ".cyan
403
431
  begin
404
432
  input = STDIN.gets
405
433
  raise Exception if input.nil?
@@ -434,13 +462,13 @@ EOS
434
462
  when 'pwd'
435
463
  puts Dir.pwd
436
464
  when 'ls'
437
- system "ls --color #{args.join(" ")}"
465
+ system "ls #{args.join(" ")}"
438
466
  when 'help'
439
- puts banner
467
+ parser.educate(STDOUT)
440
468
  when 'version', '-v'
441
469
  puts About
442
470
  else
443
- $stderr.puts 'Unrecognized command! Use \"-h\" option to print available commands.'.red
471
+ $stderr.puts 'Unrecognized command! Use -h option to print available commands.'
444
472
  end
445
473
  end
446
474
  rescue Exception => e
@@ -458,7 +486,7 @@ where [options] are:
458
486
  EOS
459
487
  opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
460
488
  opt :json, 'print output in JSON'
461
- opt :plain, 'print output in plain format'
489
+ opt :color, 'enable colorized output'
462
490
  end
463
491
  help, args = check_help(args)
464
492
  opts = process_args args, parser
@@ -475,13 +503,13 @@ EOS
475
503
  model.accept(Sfp::Helper::Sfp2Ruby)
476
504
 
477
505
  if opts[:json]
478
- puts (opts[:plain] ? JSON.generate(model) : CodeRay.encode(JSON.pretty_generate(model), :json, :terminal))
506
+ puts (!opts[:color] ? JSON.generate(model) : CodeRay.encode(JSON.pretty_generate(model), :json, :terminal))
479
507
  else
480
- puts (opts[:plain] ? YAML.dump(model) : CodeRay.encode(YAML.dump(model), :yaml, :terminal))
508
+ puts (!opts[:color] ? YAML.dump(model) : CodeRay.encode(YAML.dump(model), :yaml, :terminal))
481
509
  end
482
510
 
483
511
  else
484
- $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details.".red
512
+ $stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details."
485
513
 
486
514
  end
487
515
 
@@ -503,19 +531,19 @@ EOS
503
531
  if ARGV.length > 0
504
532
  success = false
505
533
  case ARGV.shift
506
- when 'state'
534
+ when 's', 'state'
507
535
  do_state
508
- when 'plan'
536
+ when 'p', 'plan'
509
537
  do_plan
510
- when 'bsig'
538
+ when 'b', 'bsig'
511
539
  do_bsig
512
- when 'agent'
540
+ when 'a', 'agent'
513
541
  do_agent
514
- when 'console'
542
+ when 'c', 'console'
515
543
  do_console
516
- when 'edit'
544
+ when 'e', 'edit'
517
545
  do_edit
518
- when 'model'
546
+ when 'm', 'model'
519
547
  do_model
520
548
  else
521
549
  puts banner