-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnovaqueue.py
More file actions
70 lines (62 loc) · 2.92 KB
/
Copy pathnovaqueue.py
File metadata and controls
70 lines (62 loc) · 2.92 KB
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
67
68
69
import sys
import os
import pika
import json
import logging
import cPickle
# attach to our global logger
logger = logging.getLogger("novadns")
def load_cache(file):
if os.path.exists(file):
return cPickle.load(open(file, "r"))
else:
return {}
def write_cache(file, cache):
cPickle.dump(cache, open(file, "w"), -1)
class novaQueue:
def __init__(self, config, pdns):
self.config = config
self.pdns = pdns
self.broker = "amqp://" + self.config.get('AMQP', 'amqpuser')
self.broker += ":" + self.config.get("AMQP", "amqppass")
self.broker += "@" + self.config.get('AMQP', 'amqphost')
self.broker += ':' + self.config.get('AMQP', 'amqpport') + "/%2F"
self.broker = pika.URLParameters(self.broker)
self.connection = pika.BlockingConnection(self.broker)
self.channel = self.connection.channel()
self.channel.queue_declare("notifications.info")
self.channel.basic_consume(self.get, queue="notifications.info", no_ack=True)
self.channel.start_consuming()
def get(self, ch, method, properties, body):
message = json.loads(body)
try:
if 'oslo.message' in message:
message = json.loads(message['oslo.message'])
# pull out the messages that we care about
if message['event_type']:
if message['event_type'] == 'compute.instance.create.end':
instance = message['payload']
name = instance['hostname']
# tenant = message['_context_project_name']
# name = name + "-" + tenant
if not "fixed_ips" in instance:
logger.warning("VM %s does not have an IP address, skipping" % name)
return
logger.info("Queue Add Message: %s %s %s" % (instance['instance_id'], name.lower(), instance['fixed_ips'][0]['address']))
self.pdns.update(name.lower(), instance['fixed_ips'][0]['address'], instance['instance_id'])
cache = load_cache(self.config.get('CACHE', 'cache_file'))
cache[instance['instance_id']] = (name.lower(), instance['fixed_ips'][0]['address'])
write_cache(self.config.get('CACHE', 'cache_file'), cache)
elif message['event_type'] == 'compute.instance.delete.end':
instance = message['payload']
logger.info("Queue Remove Message: %s" % (instance['instance_id']))
cache = load_cache(self.config.get('CACHE', 'cache_file'))
if instance['instance_id'] in cache:
name = cache[instance['instance_id']][0]
ip = cache[instance['instance_id']][1]
self.pdns.remove(name.lower(), ip, instance['instance_id'])
del cache[instance['instance_id']]
write_cache(self.config.get('CACHE', 'cache_file'), cache)
except:
logger.exception("Exception handling message")
logger.debug(message)