bin/nuri in nuri-0.5.1 vs bin/nuri in nuri-0.5.2
- old
+ new
@@ -4,21 +4,20 @@
require "#{dir}/lib/nuri"
Version = File.read(File.dirname(__FILE__) + "/../VERSION").strip
About = "Nuri #{Version} (c) 2013"
-HTTP = Object.new.extend(Nuri::Net::Helper)
-
Nuri.init
def console(*args)
print Time.now.strftime('%Y-%m-%d %H:%M:%S '.yellow)
$stdout.puts(args)
end
-class Sfp::Console
- PrettyStateGenerator = Sfp::Visitor::PrettyStateGenerator.new
+class Nuri::Console
+ include Nuri::Helper
+
ParentEliminator = Sfp::Visitor::ParentEliminator.new
def process_args(args, parser)
Trollop::with_standard_exception_handling parser do
parser.parse args
@@ -75,10 +74,11 @@
opt :parallel, 'generate a parallel plan', :short => '-l'
opt :apply, 'generate and execute a plan', :short => '-a'
opt :plain, 'print output in plain JSON'
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'
end
help, args = check_help(args)
opts = process_args args, parser
if help
@@ -89,10 +89,17 @@
master = Nuri::Master.new
master.set_model(opts)
plan = master.get_plan(opts)
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]
+ system "#{File.dirname(__FILE__)}/sfw2graph #{plan_file} #{opts[:image]}"
+ end
+
if opts[:plain]
puts JSON.generate(plan)
else
actions = plan['workflow']
if plan['type'] == 'sequential'
@@ -118,10 +125,11 @@
puts "Execution success!".green
else
puts "Execution failed!".red
end
end
+
elsif plan['workflow'].length == 0
puts (opts[:plain] ? 'Goal state has been achieved.' : 'Goal state has been achieved.').green
end
else
$stderr.puts (opts[:plain] ? "No solution!".yellow : "No solution!".red)
@@ -210,11 +218,11 @@
status get status (use SSH for remote node)
state get current state
model get current model
bsig get current Behavioural Signature model
exec <action> execute given action description
- <action> = <path> [param1=value1 param2=value2 ...]
+ <action> := <path> [param1=value1 param2=value2 ...]
module get modules list
log get last 100 lines of logs
list list of available agents from model file
where [options] are:
@@ -222,10 +230,11 @@
opt :model_file, 'file contains a model of desired state', :default => Nuri.main, :short => '-m'
opt :address, "address", :default => 'localhost'
opt :port, "port", :default => 1314
opt :ssh_user, "SSH username", :short => '-u', :default => 'root'
opt :ssh_port, "SSH port", :short => '-p', :default => '22'
+ opt :raw, "print a RAW output"
end
help, args = check_help(args)
subcommand = (args.length > 0 ? args.shift : '')
opts = process_args args, parser
ssh_opt = ''
@@ -235,11 +244,11 @@
ssh_opt += (opts[:ssh_port] ? " -p #{opts[:ssh_port]}" : '')
end
if help
parser.educate(STDOUT)
- elsif `which sfpagent`.strip.length > 0
+ elsif `which sfpagent`.strip.length > 0 or ssh_opt.length > 0
case subcommand
when 'install'
system("#{ssh_opt} sudo gem install sfpagent --no-ri --no-rdoc")
when 'start'
@@ -257,36 +266,56 @@
when 'upgrade'
system("#{ssh_opt} sudo gem update sfpagent --no-ri --no-rdoc")
when 'state'
- code, state = HTTP.get_data(opts[:address], 1314, "/state")
+ code, state = get_data(opts[:address], 1314, "/state")
if code == '200' and state =~ /{.*}/
state = JSON[state]['state']
- state.keys.each { |key| state.delete(key) if state[key]['_context'] != 'object' }
- puts CodeRay.encode(JSON.pretty_generate(state), :json, :terminal)
+ if not opts[:raw]
+ state.keys.each { |key| state.delete(key) if state[key]['_context'] != 'object' }
+ state.accept(Sfp::Helper::Sfp2Ruby)
+ end
+ puts CodeRay.encode(YAML.dump(state), :yaml, :terminal)
else
puts state
end
when 'model'
- code, model = HTTP.get_data(opts[:address], 1314, "/model")
+ code, model = get_data(opts[:address], 1314, "/model")
if code == '200' and model =~ /{.*}/
model = JSON[model]
- model.keys.each { |key| model.delete(key) if model[key]['_context'] != 'object' }
- puts CodeRay.encode(JSON.pretty_generate(model), :json, :terminal)
+ if not opts[:raw]
+ model.select! { |k,v| k[0,1] != '_' and (not v.is_a?(Hash) or v['_context'] == 'object') }
+ model.accept(Sfp::Helper::Sfp2Ruby)
+ end
+ puts CodeRay.encode(YAML.dump(model), :yaml, :terminal)
+ elsif code == '404'
+ puts "The model of desired state is not exist!"
else
- puts model
+ $stderr.puts "Error: #{code}"
end
when 'bsig'
- code, json = HTTP.get_data(opts[:address], 1314, "/bsig")
- puts (code == '200' and json.length >= 2 ? CodeRay.encode(JSON.pretty_generate(JSON[json]), :json, :terminal) : json)
+ code, json = get_data(opts[:address], 1314, "/bsig")
+ if code == '200' and json.length > 0
+ puts CodeRay.encode(JSON.pretty_generate(JSON[json]), :json, :terminal)
+ elsif code == '404'
+ puts 'Behavioural Signature model is not exist!'
+ else
+ $stderr.puts "Error: #{code}"
+ end
when 'module'
- code, json = HTTP.get_data(opts[:address], 1314, "/bsig")
- puts (code == '200' and json.length >= 2 ? CodeRay.encode(JSON.pretty_generate(JSON[json]), :json, :terminal) : json)
+ code, json = get_data(opts[:address], 1314, "/modules")
+ if code == '200' and json.length >= 2
+ puts CodeRay.encode(YAML.dump(JSON[json]), :yaml, :terminal)
+ elsif code == '404'
+ puts 'No module is available!'
+ else
+ $stderr.puts "Error: #{code}"
+ end
when 'exec', 'execute'
if args.length > 0
name = args.shift
name = '$.' + name if !name.isref
@@ -296,11 +325,11 @@
arg = arg.split('=', 2)
parameters[arg[0]] = arg[1]
end
end
data = { 'action' => JSON.generate({ 'name' => name, 'parameters' => parameters }) }
- code, _ = HTTP.post_data(opts[:address], 1314, "/execute", data)
+ code, _ = post_data(opts[:address], 1314, "/execute", data)
puts (code == '200' ? "Executing #{name} [OK]".green : "Executing #{name} [Failed]".red)
else
$stderr.puts 'Invalid parameters (usage: agent exec <action-path> [action-arguments]).'
end
@@ -495,25 +524,6 @@
puts banner
end
end
end
-module Sfp::Helper
- Sfp2Ruby = Object.new
- def Sfp2Ruby.visit(name, value, parent)
- if name[0] == '_'
- parent.delete(name)
- elsif value.is_a?(Hash)
- case value['_context']
- when 'null'
- parent[name] = nil
- when 'any_value', 'constraint', 'procedure'
- parent.delete(name)
- when 'set'
- parent[name] = value['_values']
- end
- end
- true
- end
-end
-
-Sfp::Console.new.run if $0 == __FILE__
+Nuri::Console.new.run if $0 == __FILE__