-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatingip_handler.py
More file actions
131 lines (102 loc) · 4.33 KB
/
floatingip_handler.py
File metadata and controls
131 lines (102 loc) · 4.33 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
"""Openstack Floating IP events handler script"""
import argparse
import ast
import ConfigParser
import json
import os.path
import signal
import pika
# Callback functions (change as needed)
def callback_delete_floating_ip(data):
"""Callback function for floating ip disassociation"""
print " * Floating IP %s disassociated" % data['floating_ip_address']
def callback_create_floating_ip(data):
"""Callback function for floating ip association"""
print " * Floating IP {0} associated to {1}".format(
data['floating_ip_address'], data['fixed_ip_address'])
# Arguments
parser = argparse.ArgumentParser(
description='Monitoring and handler neutron floating IP updates')
parser.add_argument('-c', '--configfile',
default="./floatingip_handler.config",
help='file to read the config from')
args = vars(parser.parse_args())
# Check if config file exists
if not os.path.isfile(args['configfile']):
print " * ERROR: config file doesn't exist"
exit(1)
# Read config
config_parser = ConfigParser.RawConfigParser()
config_parser.read(args['configfile'])
DEBUG = ast.literal_eval(config_parser.get('Default', 'debug'))
if DEBUG:
print " [*] Debug mode enabled"
MONITORING_QUEUE = config_parser.get('RabbitMQ', 'monitoring_queue_name')
RABBITMQ_HOSTS = config_parser.get('RabbitMQ', 'rabbitmq_hosts').split(',')
RABBITMQ_PORT = int(config_parser.get('RabbitMQ', 'rabbitmq_port'))
RABBITMQ_USER = config_parser.get('RabbitMQ', 'user')
RABBITMQ_PASS = config_parser.get('RabbitMQ', 'pass')
# Functions
def _signal_handler(_signal, _frame):
# pylint: disable=W0612,W0613
"""Signal handler func for CTRL+C"""
print '[*] Stopping handling!'
exit(0)
def _process_msg(_channel, _method, _properties, body):
# pylint: disable=W0612,W0613
payload = json.loads(body)
if 'event_type' in payload:
event_type = payload['event_type']
payload_data = payload['payload']
if DEBUG:
print " Received: %r" % event_type
if DEBUG:
print json.dumps(payload, sort_keys=True,
indent=4, separators=(',', ': '))
if event_type == 'floatingip.update.start':
print " * Starting updating floating IP"
elif event_type == 'floatingip.update.end':
if payload_data['floatingip']['status'] == 'ACTIVE':
callback_create_floating_ip(payload_data['floatingip'])
elif payload_data['floatingip']['status'] == 'DOWN':
callback_delete_floating_ip(payload_data['floatingip'])
else:
print " * Floating IP unknown operation"
if DEBUG:
print json.dumps(payload, sort_keys=True, indent=4,
separators=(',', ': '))
else:
if DEBUG:
print " * Other event"
print json.dumps(payload, sort_keys=True, indent=4,
separators=(',', ': '))
# Main loop
while True:
for mq_host in RABBITMQ_HOSTS:
try:
print " [*] Connecting %s ..." % mq_host
credentials = pika.PlainCredentials(RABBITMQ_USER,
RABBITMQ_PASS)
parameters = pika.ConnectionParameters(mq_host, RABBITMQ_PORT,
'/', credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue=MONITORING_QUEUE,
auto_delete=True)
channel.queue_bind(queue=MONITORING_QUEUE,
exchange='neutron',
routing_key='notifications.info')
channel.basic_consume(_process_msg,
queue=MONITORING_QUEUE,
no_ack=True)
print ' [*] Waiting for floating IP changes. To exit press CTRL+C'
signal.signal(signal.SIGINT, _signal_handler)
channel.start_consuming()
# Do not recover on channel errors
except pika.exceptions.AMQPChannelError:
break
# Recover on all other connection errors
except pika.exceptions.AMQPConnectionError:
print " * ERROR Connection"
continue