forked from mluscon/Deltacloud-libvirt-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
executable file
·67 lines (52 loc) · 1.4 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
require 'rubygems'
require 'amqp'
require 'nokogiri'
require 'parseconfig'
require './web'
require './helper'
#config
config = ParseConfig.new( './driver.conf' )
amqp_server = config.get_value( 'amqp_server' )
workers = config.get_value( 'workers' ).to_i
#workers
workers.times do
fork do
redis = Redis.new
helper = Helper.new
loop do
if uuid = redis.lpop( 'waiting' )
helper.transform( uuid )
helper.copy_and_launch( uuid )
else
sleep 5
end
end
end
end
#web interface
Thread.new do
Web.run!
end
#amqp client
helper = Helper.new
AMQP.start( :host => amqp_server ) do |connection|
channel = AMQP::Channel.new( connection )
queue = channel.queue("libvirt", :auto_delete => true)
queue.subscribe do |metadata, payload|
message = Nokogiri::XML( payload )
puts payload
case message.root.name
when 'query'
uuid = message.xpath( '/query/uuid' ).first.text
state = helper.state( uuid )
channel.default_exchange.publish(state,
:routing_key => metadata.reply_to,
:correlation_id => metadata.message_id,
:immediate => true,
:mandatory => true)
when 'launch'
helper.add( message )
end
end
end