annotate srxproxy.py @ 41:386a6117d08e default tip

Routing instance for nat on srx
author zynzel <zynzel+bitbucket@spof.pl>
date Tue, 19 Feb 2013 09:29:47 +0100
parents 8062c5f64c33
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
1 #!/usr/bin/env python
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2
15
db3457d2e456 Move eventlet.monkey_patch to top
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 14
diff changeset
3 import eventlet
db3457d2e456 Move eventlet.monkey_patch to top
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 14
diff changeset
4 eventlet.monkey_patch()
db3457d2e456 Move eventlet.monkey_patch to top
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 14
diff changeset
5
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
6 import datetime
12
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 11
diff changeset
7 import sys
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
8
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
9 from nova import rpc
6
ad8cca40add9 Added logging
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 5
diff changeset
10 from nova.rpc import amqp
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
11 from nova import log
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
12 from nova import flags
11
3936b3fdee1a Read config
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 10
diff changeset
13 from nova import utils
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
14 from nova import exception
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
15 from nova.network.manager import NetworkManager
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
16
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
17 from ncclient import manager, operations
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
18
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
19
38
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
20 DELAY = 10
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
21 FLAGS = flags.FLAGS
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
22 LOG = log
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
23 RULESET_XML = """\
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
24 <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
25 <configuration><security><nat><static><rule-set>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
26 <name>{name}</name>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
27 {rules}
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
28 </rule-set></static></nat></security></configuration>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
29 </config>"""
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
30 RULE_XML = """\
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
31 <rule>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
32 <name>{name}</name>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
33 <static-nat-rule-match>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
34 <destination-address>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
35 <dst-addr>{destination}/32</dst-addr>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
36 </destination-address>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
37 </static-nat-rule-match>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
38 <then>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
39 <static-nat>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
40 <prefix>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
41 <addr-prefix>{prefix}/32</addr-prefix>
41
386a6117d08e Routing instance for nat on srx
zynzel <zynzel+bitbucket@spof.pl>
parents: 40
diff changeset
42 <routing-instance>cloud</routing-instance>
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
43 </prefix>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
44 </static-nat>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
45 </then>
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
46 </rule>"""
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
47 DELETE_XML = """\
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
48 <rule operation="delete">
16
99842df807e9 fix the delete template
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 15
diff changeset
49 <name>{name}</name>
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
50 </rule>"""
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
51
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
52
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
53 def ensure_floating_forwards(ips):
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
54 rules_xml = [
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
55 RULE_XML.format(
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
56 name=floating_ip.replace('.', '_'),
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
57 destination=floating_ip,
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
58 prefix=fixed_ip,
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
59 )
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
60 for context, floating_ip, fixed_ip, host, instance_id in ips
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
61 ]
10
8c94e2bf19a9 Different default_operation for delete
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
62 apply_ruleset(rules_xml, default_operation="merge")
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
63
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
64
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
65 def remove_floating_forwards(ips):
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
66 rules_xml = [
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
67 DELETE_XML.format(name=address.replace('.', '_'))
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
68 for context, address, instance_id in ips
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
69 ]
10
8c94e2bf19a9 Different default_operation for delete
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
70 apply_ruleset(rules_xml, default_operation="none")
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
71
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
72
10
8c94e2bf19a9 Different default_operation for delete
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
73 def apply_ruleset(rules_xml, default_operation):
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
74 netconf_retries = FLAGS.netconf_retries
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
75 netconf_ip = FLAGS.netconf_server_ip
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
76 netconf_port = FLAGS.netconf_server_port
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
77 netconf_user = FLAGS.netconf_server_user
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
78 netconf_password = FLAGS.netconf_server_password
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
79 netconf_ruleset = FLAGS.netconf_rule_set
14
08bc320abed1 even more logs
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 13
diff changeset
80 LOG.debug('Connecting to %s:%s', netconf_ip, netconf_port)
36
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
81 for retry in xrange(netconf_retries):
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
82 try:
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
83 m = manager.connect_ssh(
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
84 netconf_ip,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
85 port=netconf_port,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
86 unknown_host_cb=lambda host, fingerprint: True,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
87 username=netconf_user,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
88 password=netconf_password,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
89 allow_agent=False,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
90 look_for_keys=False,
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
91 )
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
92 except Exception as e:
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
93 LOG.warning("Failed to connect %s:%s (%s)", netconf_ip, netconf_port, e)
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
94 else:
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
95 break
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
96 eventlet.sleep(2**retry + 1)
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
97 else:
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
98 raise e
727fd98b7b78 Retry connecting to the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 35
diff changeset
99 with m as m:
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
100 assert ':url' in m.server_capabilities
14
08bc320abed1 even more logs
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 13
diff changeset
101 LOG.debug('Starting private edit')
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
102 for retry in xrange(netconf_retries):
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
103 try:
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
104 m.private()
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
105 except operations.rpc.RPCError as e:
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
106 if (e._message ==
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
107 '\nuncommitted changes will be discarded on exit\n'):
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
108 break
14
08bc320abed1 even more logs
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 13
diff changeset
109 LOG.debug('Failed to start private edit, try %d', retry)
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
110 m.privateend()
8
4666b92cf626 Sleep at least 1s
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 7
diff changeset
111 eventlet.sleep(2**retry + 1)
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
112 else:
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
113 LOG.debug('Failed to start private edit %r' % e)
28
f96559a7e9b6 nicer messages
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 27
diff changeset
114 raise Exception("Failed to start private edit")
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
115 ruleset_xml = RULESET_XML.format(
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
116 name=netconf_ruleset,
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
117 rules=''.join(rules_xml),
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
118 )
14
08bc320abed1 even more logs
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 13
diff changeset
119 LOG.debug('Editing config')
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
120 m.edit_config(
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
121 target="candidate",
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
122 config=ruleset_xml,
10
8c94e2bf19a9 Different default_operation for delete
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
123 default_operation=default_operation,
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
124 )
14
08bc320abed1 even more logs
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 13
diff changeset
125 LOG.debug('Commiting')
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
126 for retry in xrange(netconf_retries):
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
127 try:
29
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
128 reply = m.commit(confirmed=False)
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
129 except operations.rpc.RPCError as e:
29
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
130 pass
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
131 else:
29
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
132 for child in reply._root.getchildren():
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
133 error = child.find(
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
134 '{urn:ietf:params:xml:ns:netconf:base:1.0}rpc-error',
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
135 )
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
136 if error:
30
3261a356ce18 Show better error message on commit error
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 29
diff changeset
137 text = error.find(
3261a356ce18 Show better error message on commit error
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 29
diff changeset
138 '{urn:ietf:params:xml:ns:netconf:base:1.0}error-message',
3261a356ce18 Show better error message on commit error
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 29
diff changeset
139 )
3261a356ce18 Show better error message on commit error
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 29
diff changeset
140 LOG.warning('Commit error (%s)', text)
29
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
141 break
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
142 else:
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
143 LOG.debug('Commited')
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
144 break
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
145 LOG.warning('Failed to commit, try %d', retry)
321d12efed3d Check for errors in the commit reply
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 28
diff changeset
146 eventlet.sleep(2**retry + 1)
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
147 else:
20
a92b4657c517 better error on failure with netconf
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 19
diff changeset
148 LOG.error('Failed configuring network device %r.', netconf_ip)
28
f96559a7e9b6 nicer messages
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 27
diff changeset
149 raise Exception("Failed configuring network device")
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
150
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
151
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
152 def associate_floating_ips(ips):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
153 if not ips:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
154 return
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
155 ips = [
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
156 (context, floating_ip, fixed_ip, host, instance_id)
24
dedb1841cd15 make requests unique by fixed_ip, not by floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 23
diff changeset
157 for fixed_ip, (context, floating_ip, host, instance_id) in {
dedb1841cd15 make requests unique by fixed_ip, not by floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 23
diff changeset
158 fixed_ip: (context, floating_ip, host, instance_id)
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
159 for context, floating_ip, fixed_ip, host, instance_id in ips
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
160 }.iteritems()
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
161 ]
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
162 try:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
163 # Change the device configuration
6
ad8cca40add9 Added logging
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 5
diff changeset
164 LOG.info("Changing device configuration for %r", ips)
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
165 ensure_floating_forwards(ips)
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
166 except Exception as e:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
167 LOG.exception(e)
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
168 return
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
169 nm = NetworkManager()
18
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
170 for context, floating_ip, fixed_ip, host, instance_id in ips:
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
171 # Write the changes to the database
6
ad8cca40add9 Added logging
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 5
diff changeset
172 LOG.info("Updating database for %s", fixed_ip)
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
173 try:
32
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
174 instance_ids = {instance_id}
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
175 floating_ip_ref = nm.db.floating_ip_get_by_address(
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
176 context,
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
177 floating_ip,
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
178 )
33
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 32
diff changeset
179 fixed_ip_id = floating_ip_ref['fixed_ip_id']
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 32
diff changeset
180 if fixed_ip_id:
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 32
diff changeset
181 old_fixed_ip_ref = nm.db.fixed_ip_get(context, fixed_ip_id)
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 32
diff changeset
182 if old_fixed_ip_ref and old_fixed_ip_ref['instance_id']:
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 32
diff changeset
183 instance_ids.add(old_fixed_ip_ref['instance_id'])
34
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
184 nm.db.floating_ip_fixed_ip_associate(
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
185 context,
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
186 floating_ip,
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
187 fixed_ip,
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
188 host,
29b0aa997665 check old instance before db update
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 33
diff changeset
189 )
32
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
190 for id_ in instance_ids:
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
191 instance = nm.db.instance_get(context, id_)
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
192 nm.get_instance_nw_info(
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
193 context.elevated(),
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
194 instance['id'],
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
195 instance['uuid'],
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
196 instance['instance_type_id'],
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
197 instance['host'],
0d38307952e7 Update the cache for the old owner of that ip too
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 31
diff changeset
198 )
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
199 except Exception as e:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
200 LOG.exception(e)
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
201
1
c608c09c7edf Use even lower level interface for registering a consumer
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
202
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
203 def disassociate_floating_ips(ips):
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
204 if not ips:
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
205 return
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
206 ips = [
25
513c5993bc76 Wrong order
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 24
diff changeset
207 (context, address, instance_id)
23
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
208 for address, (context, instance_id) in {
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
209 address: (context, instance_id)
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
210 for context, address, instance_id in ips
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
211 }.iteritems()
50bbb36cfbec make the ips unqiue earlier
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 22
diff changeset
212 ]
38
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
213 nm = NetworkManager()
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
214 good_ips = []
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
215 for context, address, instance_id in ips:
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
216 floating_ip_ref = nm.db.floating_ip_get_by_address(context, address)
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
217 if floating_ip_ref['fixed_ip_id']:
39
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 38
diff changeset
218 good_ips.append((context, address, instance_id))
38
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
219 else:
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
220 LOG.warning("Address %s already removed!" % address)
40
8062c5f64c33 Don't update empty ip list
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 39
diff changeset
221 if not good_ips:
8062c5f64c33 Don't update empty ip list
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 39
diff changeset
222 return
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
223 try:
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
224 # Change the device configuration
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
225 LOG.info("Changing device configuration for %r", ips)
38
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
226 remove_floating_forwards(good_ips)
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
227 except Exception as e:
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
228 LOG.exception(e)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
229 return
38
a640206c0444 Check if an ip is already disassociated
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 37
diff changeset
230 for context, address, instance_id in good_ips:
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
231 # Write the changes to the database
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
232 LOG.info("Updating database for %s", address)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
233 try:
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
234 nm.db.floating_ip_disassociate(context, address)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
235 try:
18
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
236 instance = nm.db.instance_get(context, instance_id)
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
237 nm.get_instance_nw_info(
19
5d3f1bc25257 Use elevated context for get_instance_nw_info
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 18
diff changeset
238 context.elevated(),
18
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
239 instance['id'],
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
240 instance['uuid'],
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
241 instance['instance_type_id'],
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
242 instance['host'],
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
243 )
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
244 except exception.InstanceNotFound:
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
245 pass
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
246 except Exception as e:
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
247 LOG.exception(e)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
248
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
249
3
cecad00e9d9f Remove self
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 2
diff changeset
250 def on_message(data):
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
251 method = data.get('method')
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
252 kwargs = data.get('args', {})
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
253 context = amqp.unpack_context(data)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
254 if method == 'srx_associate_floating_ip':
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
255 assoc_acc.add((
6
ad8cca40add9 Added logging
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 5
diff changeset
256 context,
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
257 kwargs.get('floating_ip'),
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
258 kwargs.get('fixed_ip'),
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
259 kwargs.get('host'),
18
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
260 kwargs.get('instance_id'),
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
261 ))
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
262 elif method == 'srx_disassociate_floating_ip':
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
263 dis_acc.add((
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
264 context,
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
265 kwargs.get('address'),
18
155897278c68 Explicitly pass instance_id
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 17
diff changeset
266 kwargs.get('instance_id'),
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
267 ))
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
268
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
269
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
270 class Accumulator(object):
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
271 """Accumulates entries until the oldest one is older than timeout."""
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
272
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
273 def __init__(self, timeout):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
274 self.cache = []
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
275 self.oldest = None
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
276 self.timeout = datetime.timedelta(seconds=timeout)
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
277
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
278 def add(self, item):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
279 if not self.cache:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
280 self.oldest = datetime.datetime.now()
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
281 self.cache.append(item)
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
282
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
283 def purge(self):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
284 content = self.cache
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
285 self.cache = []
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
286 self.oldest = None
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
287 return content
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
288
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
289 def check(self):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
290 if self.oldest and self.oldest + self.timeout < datetime.datetime.now():
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
291 return self.purge()
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
292 return []
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
293
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
294 def delay(self):
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
295 if not self.oldest:
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
296 return 0
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
297 return (datetime.datetime.now() - self.oldest).seconds
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
298
1
c608c09c7edf Use even lower level interface for registering a consumer
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
299
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
300
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
301 if __name__ == '__main__':
12
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 11
diff changeset
302 utils.default_flagfile()
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 11
diff changeset
303 FLAGS(sys.argv)
0
927227da56d8 Initial version
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
304 log.setup()
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
305 assoc_acc = Accumulator(DELAY)
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
306 dis_acc = Accumulator(DELAY)
35
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
307 for retry in xrange(FLAGS.netconf_retries):
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
308 try:
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
309 connection = rpc.create_connection()
37
3edbc32c07f5 Raise on failure
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 36
diff changeset
310 except Exception as e:
35
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
311 log.warning("Can't connect, retry %d", retry + 1)
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
312 eventlet.sleep(2**retry + 1)
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
313 else:
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
314 break
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
315 else:
2bbc58bc81e7 Add retries for connecting to queue
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 34
diff changeset
316 log.error("Failed to connect.")
37
3edbc32c07f5 Raise on failure
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 36
diff changeset
317 raise e
4
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
318 connection.declare_topic_consumer('srxproxy', on_message)
85a2bef9574e Add ensure_floating_formwards for actually communicationg with the switch
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 3
diff changeset
319 connection.declare_topic_consumer('srxproxy.%s' % flags.FLAGS.host, on_message)
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
320 connection.consume_in_thread()
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
321 while True:
9
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
322 associate_floating_ips(assoc_acc.check())
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
323 disassociate_floating_ips(dis_acc.check())
09811474d92e Add disassociate_floating_ip
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
324 delay = min(DELAY - assoc_acc.delay(), DELAY - dis_acc.delay())
5
286632cf1e03 Use eventlet to accumulate more requests at a time
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 4
diff changeset
325 eventlet.sleep(delay)