Revision 0e1f3323 snf-cyclades-gtools/synnefo/ganeti/eventd.py
b/snf-cyclades-gtools/synnefo/ganeti/eventd.py | ||
---|---|---|
56 | 56 |
from signal import signal, SIGINT, SIGTERM |
57 | 57 |
import setproctitle |
58 | 58 |
|
59 |
from ganeti import utils |
|
60 |
from ganeti import jqueue |
|
61 |
from ganeti import constants |
|
62 |
from ganeti import serializer |
|
59 |
from ganeti import utils, jqueue, constants, serializer, cli |
|
63 | 60 |
from ganeti.ssconf import SimpleConfigReader |
64 | 61 |
|
65 | 62 |
|
... | ... | |
67 | 64 |
from synnefo.lib.amqp import AMQPClient |
68 | 65 |
|
69 | 66 |
|
70 |
|
|
71 | 67 |
def get_time_from_status(op, job): |
72 | 68 |
"""Generate a unique message identifier for a ganeti job. |
73 | 69 |
|
... | ... | |
97 | 93 |
raise InvalidBackendStatus(status, job) |
98 | 94 |
|
99 | 95 |
|
96 |
def get_instance_nics(instance, logger): |
|
97 |
"""Query Ganeti to a get the instance's NICs. |
|
98 |
|
|
99 |
@type instance: string |
|
100 |
@param instance: the name of the instance |
|
101 |
@rtype: List of dicts |
|
102 |
@retrun: Dictionary containing the instance's NICs. Each dictionary |
|
103 |
contains the following keys: 'network', 'ip', 'mac', 'mode', |
|
104 |
'link' and 'firewall' |
|
105 |
|
|
106 |
""" |
|
107 |
fields = ["nic.networks", "nic.ips", "nic.macs", "nic.modes", "nic.links", |
|
108 |
"tags"] |
|
109 |
# Get Ganeti client |
|
110 |
client = cli.GetClient() |
|
111 |
info = client.QueryInstances([instance], fields, use_locking=False) |
|
112 |
networks, ips, macs, modes, links, tags = info[0] |
|
113 |
nic_keys = ["network", "ip", "mac", "mode", "link"] |
|
114 |
nics = zip(networks, ips, macs, modes, links) |
|
115 |
nics = map(lambda x: dict(zip(nic_keys, x)), nics) |
|
116 |
# Get firewall from instance Tags |
|
117 |
# Tags are of the form synnefo:network:N:firewall_mode |
|
118 |
for tag in tags: |
|
119 |
t = tag.split(":") |
|
120 |
if t[0:2] == ["synnefo", "network"]: |
|
121 |
if len(t) != 4: |
|
122 |
logger.error("Malformed synefo tag %s", tag) |
|
123 |
continue |
|
124 |
try: |
|
125 |
index = int(t[2]) |
|
126 |
nics[index]['firewall'] = t[3] |
|
127 |
except ValueError: |
|
128 |
logger.error("Malformed synnefo tag %s", tag) |
|
129 |
except IndexError: |
|
130 |
logger.error("Found tag %s for non-existent NIC %d", |
|
131 |
tag, index) |
|
132 |
return nics |
|
133 |
|
|
134 |
|
|
100 | 135 |
class InvalidBackendStatus(Exception): |
101 | 136 |
def __init__(self, status, job): |
102 | 137 |
self.status = status |
... | ... | |
192 | 227 |
"logmsg": logmsg, |
193 | 228 |
"jobId": job_id}) |
194 | 229 |
|
230 |
if op_id in ["OP_INSTANCE_CREATE", "OP_INSTANCE_SET_PARAMS", |
|
231 |
"OP_INSTANCE_STARTUP"]: |
|
232 |
if op.status == "success": |
|
233 |
nics = get_instance_nics(msg["instance"], self.logger) |
|
234 |
msg["nics"] = nics |
|
235 |
|
|
195 | 236 |
msg = json.dumps(msg) |
196 | 237 |
self.logger.debug("Delivering msg: %s (key=%s)", msg, routekey) |
197 | 238 |
|
Also available in: Unified diff