lib/nuri/master.rb in nuri-0.5.2 vs lib/nuri/master.rb in nuri-0.5.3
- old
+ new
@@ -10,10 +10,12 @@
AgentSchema = '$.Node'
CloudSchema = '$.Cloud'
VMSchema = '$.VM'
+ InstallModule = File.dirname(__FILE__) + '/../../bin/nuri-install-module'
+
attr_reader :model
def initialize(p={})
@mutex_vm_updater = Mutex.new
@cloudfinder = Sfp::Helper::CloudFinder.new
@@ -60,17 +62,21 @@
def get_plan(p={})
# set parameters value to be given to the planner
p[:sfp] = create_plan_task(p)
p[:sas_post_processor] = SASPostProcessor
+ print "Planning "
+
plan = nil
planning_time = Benchmark.measure do
planner = Sfp::Planner.new
plan = planner.solve(p)
end
- puts "Planning time (s): #{planning_time}"
+ print (p[:color] ? "[Finish] ".green : "[Finish] ")
+ puts format_benchmark(planning_time)
+
plan
end
def get_state(p={})
state = {}
@@ -120,17 +126,28 @@
state
end
protected
+ def format_benchmark(benchmark)
+ "cpu-time: user=#{benchmark.cutime.round(2)} sys=#{benchmark.cstime.round(2)} total=#{benchmark.total.round(2)}"
+ end
+
def create_plan_task(p={})
task = get_schemata
- puts "Getting current state [WAIT]".yellow
- b = Benchmark.measure { task['initial'] = to_state('initial', get_state(p)) }
- puts "Getting current state [OK] : #{b}".green
+ print "Getting current state "
+ puts (p[:color] ? "[Wait]".yellow : "[Wait]")
+ b = Benchmark.measure do
+ task['initial'] = to_state('initial', get_state(p))
+ end
+
+ print "Getting current state "
+ print (p[:color] ? "[OK]".green : "[OK]")
+ puts " " + format_benchmark(b)
+
task['initial'].accept(Sfp::Visitor::SfpGenerator.new(task))
f1 = Sfp::Helper::SfpFlatten.new
task['initial'].accept(f1)
# modify condition of procedures of each VM's component
@@ -147,21 +164,28 @@
# find dead-node, remove from the task, print WARNING to the console
dead_nodes = task['initial'].select { |k,v| v.is_a?(Sfp::Unknown) }
dead_nodes.each_key { |name|
task['initial'].delete(name)
task['goal'].keep_if { |k,v| !(k =~ /(\$\.#{name}\.|\$\.#{name}$)/) }
- puts "[WARN] Removing node #{name} from the task.".red
+ print (p[:color] ? "[Warn]".red : "[Warn]")
+ puts " Removing node #{name} from the task."
}
# print the status of goal state
- puts "Goal state:".yellow
+ puts "Goal state:"
goalgen.results.each { |k,v|
next if k[0,1] == '_'
- print " #{k}: " + Sfp::Helper::Sfp2Ruby.val(v['_value']).to_s.green
+
+ print " #{k}: "
+ value = Sfp::Helper::Sfp2Ruby.val(v['_value']).to_s
+ print (p[:color] ? value.green : value) + " "
+
if f1.results.has_key?(k) and f1.results[k] != v['_value']
- print " " + Sfp::Helper::Sfp2Ruby.val(f1.results[k]).to_s.red
+ value = Sfp::Helper::Sfp2Ruby.val(f1.results[k]).to_s
+ print (p[:color] ? value.red : value)
end
+
puts ""
}
# add global constraint (if exist)
task['global'] = @model['global'] if @model.has_key?('global')
@@ -308,10 +332,19 @@
rescue Exception => exp
end
false
end
+ ###############
+ #
+ # Push required modules to agent based on schemata available in agent's model
+ #
+ # @param agent_model agent's model
+ # @param address agent's address (IP or DNS address)
+ # @param port agent's port
+ #
+ ###############
def push_modules(agent_model, address=nil, port=nil)
if address.nil? or port.nil?
return false if !agent_model.is_a?(Hash) or !agent_model['sfpAddress'].is_a?(String)
address = agent_model['sfpAddress'].to_s.strip
port = agent_model['sfpPort'].to_s.strip
@@ -328,18 +361,23 @@
# get modules list
code, body = get_data(address, port, '/modules')
raise Exception, "Unable to get modules list from #{name}" if code.to_i != 200
modules = JSON[body]
- list = ''
- schemata.each { |m|
- list += "#{m} " if File.exist?("#{@modules_dir}/#{m}") and
- (not modules.has_key?(m) or modules[m] != get_local_module_hash(m).to_s)
- }
+ tobe_installed_modules = []
+ schemata.each do |name|
+ module_dir = "#{@modules_dir}/#{name}"
+ if File.exist?(module_dir) and
+ ( not modules.has_key?(name) or modules[name] != get_local_module_hash(name).to_s )
+ tobe_installed_modules << name
+ end
+ end
- return true if list == ''
+ return true if tobe_installed_modules.length <= 0
- output = JSON.parse(`cd #{@modules_dir}; ./install_module #{address} #{port} #{list}`)
+ ### install new modules and replace old ones
+ list = tobe_installed_modules.join(" ")
+ output = JSON.parse(`cd #{@modules_dir}; #{InstallModule} #{address}:#{port} #{list}`)
if output['installed_modules'].length > 0
puts ("Push modules: " + output['installed_modules'].join(" ") + " to agent #{name} [OK]").green
end
if output['missing_modules'].length > 0
puts ("Missing modules: " + output['missing_modules'].join(" ") + ".").red