bin/nuri in nuri-0.5.3 vs bin/nuri in nuri-0.5.4
- old
+ new
@@ -4,12 +4,10 @@
require "#{dir}/lib/nuri"
Version = File.read(File.dirname(__FILE__) + "/../VERSION").strip
About = "Nuri #{Version} (c) 2013"
-Nuri.init
-
class Nuri::Console
include Nuri::Helper
ParentEliminator = Sfp::Visitor::ParentEliminator.new
@@ -25,11 +23,11 @@
end
def do_state(args=ARGV, cmd="nuri ")
parser = Trollop::Parser.new do
banner <<-EOS
-Usage: #{cmd}state [options]
+Usage: #{cmd}state [options] [path]
where [options] are:
EOS
opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
opt :json, 'print output in JSON'
opt :color, 'enable colorized output'
@@ -46,16 +44,21 @@
master = Nuri::Master.new
master.set_model opts
state = master.get_state opts
state.accept(Sfp::Helper::Sfp2Ruby)
+ if args.length > 0
+ state = state.at?("$.#{args[0]}")
+ puts (!opts[:color] ? "Path: #{args[0]}" : "Path: " + "#{args[0]}".yellow)
+ end
+
if opts[:json]
puts (!opts[:color] ? JSON.generate(state) : CodeRay.encode(JSON.pretty_generate(state), :json, :terminal))
else
puts (!opts[:color] ? YAML.dump(state) : CodeRay.encode(YAML.dump(state), :yaml, :terminal))
-
end
+
else
$stderr.puts "Model file '#{opts[:model_file]}' is not exist! Use \"-h\" option for more details.".red
end
end
@@ -71,11 +74,11 @@
opt :apply, 'generate and execute a plan', :short => '-a'
opt :json, 'print the plan in JSON'
opt :color, 'enable colorized output'
opt :no_interactive, 'disable interactive input'
opt :no_push_module, 'disable automatic push module'
- opt :image, 'image graph of the generated plan', :default => Dir.home + '/.nuri/plan.png'
+ opt :image, 'image graph of the generated plan', :default => '' #Dir.home + '/.nuri/plan.png'
end
help, args = check_help(args)
opts = process_args args, parser
if help
@@ -90,11 +93,11 @@
if plan.is_a?(Hash) and !plan['workflow'].nil?
if plan['workflow'].length > 0
plan_file = Dir.home + '/.nuri/plan.json'
File.open(plan_file, 'w') { |f| f.write(JSON.generate(plan)) }
- if opts[:image]
+ if opts[:image].strip.length > 0
system "#{File.dirname(__FILE__)}/nuri-sfwgraph #{plan_file} #{opts[:image]}"
end
if opts[:json]
json = JSON.pretty_generate(plan)
@@ -145,29 +148,64 @@
banner <<-EOS
Usage: #{cmd}bsig [options]
where [options] are:
EOS
opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
+ opt :apply, 'generate and then deploy a BSig model'
+ opt :status, 'get BSig status of all agents'
opt :purge, 'purge existing BSig model', :short => '-g'
- opt :deploy, 'generate and then deploy a BSig model'
opt :color, 'enable colorized output'
opt :no_interactive, 'disable interactive input'
+ opt :no_push_module, 'disable automatic push module'
end
help, args = check_help(args)
opts = process_args args, parser
if help
parser.educate(STDOUT)
+
+ elsif opts[:status]
+ opts[:push_modules] = true if !opts[:no_push_module]
+ master = Nuri::Master.new
+ master.set_model opts
+ master.get_state(opts).each do |name,state|
+ print "- #{name}: "
+ if not state.is_a?(Hash) or not state.has_key?('sfpAddress')
+ puts (opts[:color] ? "unknown address".yellow : "unknown address")
+ else
+ address = state['sfpAddress']
+ port = (state['sfpPort'] ? state['sfpPort'] : 1314)
+ if address.is_a?(String) and port.is_a?(Fixnum)
+ status = Net::HTTP.get(URI("http://#{address}:#{port}/bsig/flaws"))
+ if status == '{}'
+ puts (opts[:color] ? "no-flaws".green : "no-flaws")
+ else
+ puts (opts[:color] ? status.yellow : status)
+ end
+ elsif address.is_a?(Sfp::Undefined)
+ puts (opts[:color] ? "not-exist".yellow : "not-exist")
+ elsif address.is_a?(Sfp::Unknown)
+ puts (opts[:color] ? "unknown".yellow : "unknown")
+ end
+ end
+ end
+
elsif File.exist?(opts[:model_file])
opts[:bsig_deploy] = true
opts[:parallel] = true
+ opts[:push_modules] = true if !opts[:no_push_module]
master = Nuri::Master.new
master.set_model(opts)
if opts[:purge]
print "Purging Behavioural Signature model "
- if master.purge_bsig(opts)
+ puts (opts[:color] ? "[Wait]".yellow : "[Wait]")
+
+ status = master.purge_bsig(opts)
+ print "Purging Behavioural Signature model "
+
+ if status
puts (opts[:color] ? "[OK]".green : "[OK]")
else
puts (opts[:color] ? "[Failed]".red : "[Failed]")
end
@@ -177,21 +215,25 @@
empty_local_bsig = bsig.select { |name,local_bsig| local_bsig['operators'].length <= 0 }
if empty_local_bsig.length != bsig.length
json = JSON.pretty_generate(bsig)
puts (opts[:color] ? CodeRay.encode(json, :json, :terminal) : json)
- if not opts[:no_interactive] and not opts[:deploy]
+ if not opts[:no_interactive] and not opts[:apply]
print "Deploy the BSig model [y/N]? "
- opts[:deploy] = true if STDIN.gets.chomp.upcase == 'Y'
+ opts[:apply] = true if STDIN.gets.chomp.upcase == 'Y'
end
- if opts[:deploy]
+ if opts[:apply]
print 'Deploying the BSig model '
+ puts (opts[:color] ? "[Wait]".yellow : "[Wait]")
+
opts[:bsig] = bsig
if master.deploy_bsig(opts)
+ print 'Deploying the BSig model '
puts (opts[:color] ? "[OK]".green : "[OK]")
else
+ print 'Deploying the BSig model '
puts (opts[:color] ? "[Failed]".red : "[Failed]")
end
end
else
puts (opts[:color] ? "Goal state has been achieved.".green : "Goal state has been achieved.")
@@ -398,12 +440,12 @@
master.set_model opts
state = master.get_state opts
agents = {}
state.each do |name,model|
agents[name] = {
- 'sfpAddress' => (model['sfpAddress'].is_a?(String) ? model['sfpAddress'] : ''),
- 'sfpPort' => (model['sfpPort'].is_a?(Fixnum) ? model['sfpPort'] : 0)
+ 'sfpAddress' => (model.is_a?(Hash) and model['sfpAddress'].is_a?(String) ? model['sfpAddress'] : ''),
+ 'sfpPort' => (model.is_a?(Hash) and model['sfpPort'].is_a?(Fixnum) ? model['sfpPort'] : 0)
}
end
agents
end
@@ -552,6 +594,10 @@
puts banner
end
end
end
-Nuri::Console.new.run if $0 == __FILE__
+if $0 == __FILE__
+ Nuri.init
+ Nuri::Console.new.run
+end
+