1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

im-collectd: Synchronous 'onehost sync'.

This commit is contained in:
Jaime Melis 2013-10-14 20:25:08 +02:00
parent 4dedcda455
commit 0efc81f0ef
2 changed files with 60 additions and 8 deletions

View File

@ -29,7 +29,7 @@ class OneHostHelper < OpenNebulaHelper::OneHelper
def self.state_to_str(id)
id = id.to_i
state_str = Host::HOST_STATES[id]
return Host::SHORT_HOST_STATES[state_str]
end
@ -114,7 +114,7 @@ class OneHostHelper < OpenNebulaHelper::OneHelper
"#{cpu_usage} / #{max_cpu} (#{ratio}%)"
else
"#{cpu_usage} / -"
end
end
end
end
@ -156,6 +156,61 @@ class OneHostHelper < OpenNebulaHelper::OneHelper
table
end
NUM_THREADS = 15
def sync
# Get remote_dir (implies oneadmin group)
rc = OpenNebula::System.new(@client).get_configuration
return -1, rc.message if OpenNebula.is_error?(rc)
conf = rc
remote_dir = conf['SCRIPTS_REMOTE_DIR']
# Verify the existence of REMOTES_LOCATION
if !File.directory? REMOTES_LOCATION
error_msg = "'#{REMOTES_LOCATION}' does not exist. " <<
"This command must be run in the frontend."
return -1,error_msg
end
filter_flag ||= OpenNebula::Pool::INFO_ALL
pool = factory_pool(filter_flag)
rc = pool.info
return -1, rc.message if OpenNebula.is_error?(rc)
i = 0
hs_threads = Array.new
pool.each do |host|
hs_threads[i % NUM_THREADS] ||= []
hs_threads[i % NUM_THREADS] << host['NAME']
i+=1
end
host_errors = Array.new
ts = hs_threads.map do |t|
Thread.new {
t.each do |host|
sync_cmd = "scp -rp #{REMOTES_LOCATION}/. #{host}:#{remote_dir} 2> /dev/null"
`#{sync_cmd} 2>/dev/null`
host_errors << host if !$?.success?
end
}
end
ts.each{|t| t.join}
if host_errors.empty?
puts "All hosts updated successfully."
0
else
STDERR.puts "Failed to update the following hosts:"
host_errors.each{|h| STDERR.puts "* #{h}"}
-1
end
end
private
def factory(id=nil)

View File

@ -20,8 +20,10 @@ ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
REMOTES_LOCATION="/var/lib/one/remotes"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
REMOTES_LOCATION=ONE_LOCATION+"/var/remotes/"
end
$: << RUBY_LIB_LOCATION
@ -168,12 +170,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
EOT
command :sync, sync_desc do
if ONE_LOCATION
FileUtils.touch "#{ONE_LOCATION}/var/remotes"
else
FileUtils.touch "/var/lib/one/remotes"
end
0
helper.sync
end
list_desc = <<-EOT.unindent