Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / callbacks.py @ 0827883e

History | View | Annotate | Download (8 kB)

1 cb409cfd Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
2 ad2d6807 Vangelis Koukis
#
3 cb409cfd Georgios Gousios
# Redistribution and use in source and binary forms, with or without
4 cb409cfd Georgios Gousios
# modification, are permitted provided that the following conditions
5 cb409cfd Georgios Gousios
# are met:
6 ad2d6807 Vangelis Koukis
#
7 cb409cfd Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
8 cb409cfd Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
9 ad2d6807 Vangelis Koukis
#
10 cb409cfd Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
11 cb409cfd Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
12 cb409cfd Georgios Gousios
#     documentation and/or other materials provided with the distribution.
13 cb409cfd Georgios Gousios
#
14 cb409cfd Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 cb409cfd Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 cb409cfd Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 cb409cfd Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 cb409cfd Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 cb409cfd Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 cb409cfd Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 cb409cfd Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 cb409cfd Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 cb409cfd Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 cb409cfd Georgios Gousios
# SUCH DAMAGE.
25 cb409cfd Georgios Gousios
#
26 cb409cfd Georgios Gousios
# The views and conclusions contained in the software and documentation are
27 cb409cfd Georgios Gousios
# those of the authors and should not be interpreted as representing official
28 cb409cfd Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
29 cb409cfd Georgios Gousios
30 cb409cfd Georgios Gousios
# Callback functions used by the dispatcher to process incoming notifications
31 cb409cfd Georgios Gousios
# from AMQP queues.
32 cb409cfd Georgios Gousios
33 86f046a8 Giorgos Verigakis
import logging
34 23c84263 Georgios Gousios
import json
35 c4e55622 Christos Stavrakakis
from functools import wraps
36 23c84263 Georgios Gousios
37 22ee6892 Christos Stavrakakis
from synnefo.db.models import Backend, VirtualMachine, Network, BackendNetwork
38 1ed37c1d Giorgos Verigakis
from synnefo.logic import utils, backend
39 23c84263 Georgios Gousios
40 c4e55622 Christos Stavrakakis
from synnefo.lib.utils import merge_time
41 c4e55622 Christos Stavrakakis
42 86f046a8 Giorgos Verigakis
43 86f046a8 Giorgos Verigakis
log = logging.getLogger()
44 23c84263 Georgios Gousios
45 95aee02c Vangelis Koukis
46 a17a8e98 Christos Stavrakakis
def handle_message_delivery(func):
47 a17a8e98 Christos Stavrakakis
    """ Generic decorator for handling messages.
48 c4e55622 Christos Stavrakakis

49 a17a8e98 Christos Stavrakakis
    This decorator is responsible for converting the message into json format,
50 a17a8e98 Christos Stavrakakis
    handling of common exceptions and acknowledment of message if needed.
51 c4e55622 Christos Stavrakakis

52 c4e55622 Christos Stavrakakis
    """
53 c4e55622 Christos Stavrakakis
    @wraps(func)
54 c4e55622 Christos Stavrakakis
    def wrapper(client, message, *args, **kwargs):
55 c4e55622 Christos Stavrakakis
        try:
56 c4e55622 Christos Stavrakakis
            msg = json.loads(message['body'])
57 a17a8e98 Christos Stavrakakis
            func(msg)
58 a17a8e98 Christos Stavrakakis
            client.basic_ack(message)
59 22ee6892 Christos Stavrakakis
        except ValueError as e:
60 22ee6892 Christos Stavrakakis
            log.error("Incoming message not in JSON format %s: %s", e, message)
61 c4e55622 Christos Stavrakakis
            client.basic_ack(message)
62 22ee6892 Christos Stavrakakis
        except KeyError as e:
63 22ee6892 Christos Stavrakakis
            log.error("Malformed incoming JSON, missing attribute %s: %s",
64 22ee6892 Christos Stavrakakis
                      e, message)
65 c4e55622 Christos Stavrakakis
            client.basic_ack(message)
66 a17a8e98 Christos Stavrakakis
        except Exception as e:
67 a17a8e98 Christos Stavrakakis
            log.exception("Unexpected error: %s, msg: %s", e, msg)
68 a17a8e98 Christos Stavrakakis
69 a17a8e98 Christos Stavrakakis
    return wrapper
70 a17a8e98 Christos Stavrakakis
71 22ee6892 Christos Stavrakakis
72 a17a8e98 Christos Stavrakakis
def instance_from_msg(func):
73 a17a8e98 Christos Stavrakakis
    """ Decorator for getting the VirtualMachine object of the msg.
74 a17a8e98 Christos Stavrakakis

75 a17a8e98 Christos Stavrakakis
    """
76 a17a8e98 Christos Stavrakakis
    @handle_message_delivery
77 a17a8e98 Christos Stavrakakis
    @wraps(func)
78 a17a8e98 Christos Stavrakakis
    def wrapper(msg):
79 a17a8e98 Christos Stavrakakis
        try:
80 a17a8e98 Christos Stavrakakis
            vm_id = utils.id_from_instance_name(msg["instance"])
81 a17a8e98 Christos Stavrakakis
            vm = VirtualMachine.objects.get(id=vm_id)
82 a17a8e98 Christos Stavrakakis
            func(vm, msg)
83 c4e55622 Christos Stavrakakis
        except VirtualMachine.InvalidBackendIdError:
84 c4e55622 Christos Stavrakakis
            log.debug("Ignoring msg for unknown instance %s.", msg['instance'])
85 c4e55622 Christos Stavrakakis
        except VirtualMachine.DoesNotExist:
86 c4e55622 Christos Stavrakakis
            log.error("VM for instance %s with id %d not found in DB.",
87 c4e55622 Christos Stavrakakis
                      msg['instance'], vm_id)
88 15cb13b5 Christos Stavrakakis
        except Network.InvalidBackendIdError, Network.DoesNotExist:
89 15cb13b5 Christos Stavrakakis
            log.error("Invalid message", msg)
90 a17a8e98 Christos Stavrakakis
    return wrapper
91 a17a8e98 Christos Stavrakakis
92 22ee6892 Christos Stavrakakis
93 a17a8e98 Christos Stavrakakis
def network_from_msg(func):
94 22ee6892 Christos Stavrakakis
    """ Decorator for getting the BackendNetwork object of the msg.
95 a17a8e98 Christos Stavrakakis

96 a17a8e98 Christos Stavrakakis
    """
97 a17a8e98 Christos Stavrakakis
    @handle_message_delivery
98 a17a8e98 Christos Stavrakakis
    @wraps(func)
99 a17a8e98 Christos Stavrakakis
    def wrapper(msg):
100 a17a8e98 Christos Stavrakakis
        try:
101 a17a8e98 Christos Stavrakakis
            network_id = utils.id_from_network_name(msg["network"])
102 a17a8e98 Christos Stavrakakis
            network = Network.objects.get(id=network_id)
103 22ee6892 Christos Stavrakakis
            backend = Backend.objects.get(clustername=msg['cluster'])
104 22ee6892 Christos Stavrakakis
            backend_network = BackendNetwork.objects.get(network=network,
105 22ee6892 Christos Stavrakakis
                                                         backend=backend)
106 22ee6892 Christos Stavrakakis
            func(backend_network, msg)
107 a17a8e98 Christos Stavrakakis
        except Network.InvalidBackendIdError:
108 a17a8e98 Christos Stavrakakis
            log.debug("Ignoring msg for unknown network %s.", msg['network'])
109 a17a8e98 Christos Stavrakakis
        except Network.DoesNotExist:
110 22ee6892 Christos Stavrakakis
            log.error("Network %s not found in DB.", msg['network'])
111 22ee6892 Christos Stavrakakis
        except Backend.DoesNotExist:
112 22ee6892 Christos Stavrakakis
            log.error("Backend %s not found in DB.", msg['cluster'])
113 22ee6892 Christos Stavrakakis
        except BackendNetwork.DoesNotExist:
114 22ee6892 Christos Stavrakakis
            log.error("Network %s on backend %s not found in DB.",
115 22ee6892 Christos Stavrakakis
                      msg['network'], msg['cluster'])
116 a17a8e98 Christos Stavrakakis
    return wrapper
117 a17a8e98 Christos Stavrakakis
118 22ee6892 Christos Stavrakakis
119 a17a8e98 Christos Stavrakakis
def if_update_required(func):
120 a17a8e98 Christos Stavrakakis
    """
121 a17a8e98 Christos Stavrakakis
    Decorator for checking if an incoming message needs to update the db.
122 a17a8e98 Christos Stavrakakis

123 a17a8e98 Christos Stavrakakis
    The database will not be updated in the following cases:
124 a17a8e98 Christos Stavrakakis
    - The message has been redelivered and the action has already been
125 a17a8e98 Christos Stavrakakis
      completed. In this case the event_time will be equal with the one
126 a17a8e98 Christos Stavrakakis
      in the database.
127 22ee6892 Christos Stavrakakis
    - The message describes a previous state in the ganeti, from the one that
128 22ee6892 Christos Stavrakakis
      is described in the db. In this case the event_time will be smaller from
129 22ee6892 Christos Stavrakakis
      the one in the database.
130 a17a8e98 Christos Stavrakakis

131 a17a8e98 Christos Stavrakakis
    """
132 a17a8e98 Christos Stavrakakis
    @wraps(func)
133 a17a8e98 Christos Stavrakakis
    def wrapper(target, msg):
134 a17a8e98 Christos Stavrakakis
        event_time = merge_time(msg['event_time'])
135 a17a8e98 Christos Stavrakakis
        db_time = target.backendtime
136 a17a8e98 Christos Stavrakakis
137 22ee6892 Christos Stavrakakis
        if db_time and event_time <= db_time:
138 a17a8e98 Christos Stavrakakis
            format_ = "%d/%m/%y %H:%M:%S:%f"
139 a17a8e98 Christos Stavrakakis
            log.debug("Ignoring message %s.\nevent_timestamp: %s db_timestamp: %s",
140 a17a8e98 Christos Stavrakakis
                      msg,
141 a17a8e98 Christos Stavrakakis
                      event_time.strftime(format_),
142 a17a8e98 Christos Stavrakakis
                      db_time.strftime(format_))
143 a17a8e98 Christos Stavrakakis
            return
144 a17a8e98 Christos Stavrakakis
        # New message. Update the database!
145 a17a8e98 Christos Stavrakakis
        func(target, msg, event_time)
146 c4e55622 Christos Stavrakakis
147 c4e55622 Christos Stavrakakis
    return wrapper
148 c4e55622 Christos Stavrakakis
149 c4e55622 Christos Stavrakakis
150 a17a8e98 Christos Stavrakakis
@instance_from_msg
151 a17a8e98 Christos Stavrakakis
@if_update_required
152 a17a8e98 Christos Stavrakakis
def update_db(vm, msg, event_time):
153 c25cc9ec Vangelis Koukis
    """Process a notification of type 'ganeti-op-status'"""
154 33b93f81 Christos Stavrakakis
    log.debug("Processing ganeti-op-status msg: %s", msg)
155 23c84263 Georgios Gousios
156 c4e55622 Christos Stavrakakis
    if msg['type'] != "ganeti-op-status":
157 c4e55622 Christos Stavrakakis
        log.error("Message is of unknown type %s.", msg['type'])
158 c4e55622 Christos Stavrakakis
        return
159 7ca9e930 Vangelis Koukis
160 c4e55622 Christos Stavrakakis
    backend.process_op_status(vm, event_time, msg['jobId'], msg['operation'],
161 c4e55622 Christos Stavrakakis
                              msg['status'], msg['logmsg'])
162 c4e55622 Christos Stavrakakis
163 c4e55622 Christos Stavrakakis
    log.debug("Done processing ganeti-op-status msg for vm %s.",
164 c4e55622 Christos Stavrakakis
              msg['instance'])
165 c4e55622 Christos Stavrakakis
166 c4e55622 Christos Stavrakakis
167 a17a8e98 Christos Stavrakakis
@instance_from_msg
168 a17a8e98 Christos Stavrakakis
@if_update_required
169 a17a8e98 Christos Stavrakakis
def update_net(vm, msg, event_time):
170 c25cc9ec Vangelis Koukis
    """Process a notification of type 'ganeti-net-status'"""
171 33b93f81 Christos Stavrakakis
    log.debug("Processing ganeti-net-status msg: %s", msg)
172 7ca9e930 Vangelis Koukis
173 c4e55622 Christos Stavrakakis
    if msg['type'] != "ganeti-net-status":
174 c4e55622 Christos Stavrakakis
        log.error("Message is of unknown type %s", msg['type'])
175 c4e55622 Christos Stavrakakis
        return
176 9068cd85 Georgios Gousios
177 c4e55622 Christos Stavrakakis
    backend.process_net_status(vm, event_time, msg['nics'])
178 604b2bf8 Georgios Gousios
179 c4e55622 Christos Stavrakakis
    log.debug("Done processing ganeti-net-status msg for vm %s.",
180 c4e55622 Christos Stavrakakis
              msg["instance"])
181 604b2bf8 Georgios Gousios
182 604b2bf8 Georgios Gousios
183 a17a8e98 Christos Stavrakakis
@network_from_msg
184 a17a8e98 Christos Stavrakakis
@if_update_required
185 a17a8e98 Christos Stavrakakis
def update_network(network, msg, event_time):
186 a17a8e98 Christos Stavrakakis
    """Process a notification of type 'ganeti-network-status'"""
187 a17a8e98 Christos Stavrakakis
    log.debug("Processing ganeti-network-status msg: %s", msg)
188 a17a8e98 Christos Stavrakakis
189 a17a8e98 Christos Stavrakakis
    if msg['type'] != "ganeti-network-status":
190 a17a8e98 Christos Stavrakakis
        log.error("Message is of unknown type %s.", msg['type'])
191 a17a8e98 Christos Stavrakakis
        return
192 a17a8e98 Christos Stavrakakis
193 22ee6892 Christos Stavrakakis
    backend.process_network_status(network, event_time,
194 22ee6892 Christos Stavrakakis
                                   msg['jobId'], msg['operation'],
195 22ee6892 Christos Stavrakakis
                                   msg['status'], msg['logmsg'])
196 a17a8e98 Christos Stavrakakis
197 22ee6892 Christos Stavrakakis
    log.debug("Done processing ganeti-network-status msg for network %s.",
198 22ee6892 Christos Stavrakakis
              msg['network'])
199 a17a8e98 Christos Stavrakakis
200 a17a8e98 Christos Stavrakakis
201 a17a8e98 Christos Stavrakakis
@instance_from_msg
202 a17a8e98 Christos Stavrakakis
@if_update_required
203 a17a8e98 Christos Stavrakakis
def update_build_progress(vm, msg, event_time):
204 c4e55622 Christos Stavrakakis
    """Process a create progress message"""
205 33b93f81 Christos Stavrakakis
    log.debug("Processing ganeti-create-progress msg: %s", msg)
206 604b2bf8 Georgios Gousios
207 0827883e Nikos Skalkotos
    if msg['type'] not in ('image-copy-progress', 'image-error',
208 0827883e Nikos Skalkotos
                           'image-helper'):
209 c4e55622 Christos Stavrakakis
        log.error("Message is of unknown type %s", msg['type'])
210 c4e55622 Christos Stavrakakis
        return
211 c25cc9ec Vangelis Koukis
212 0827883e Nikos Skalkotos
    if msg['type'] == 'image-copy-progress':
213 0827883e Nikos Skalkotos
        backend.process_create_progress(vm, event_time, msg['progress'])
214 604b2bf8 Georgios Gousios
215 c4e55622 Christos Stavrakakis
    log.debug("Done processing ganeti-create-progress msg for vm %s.",
216 c4e55622 Christos Stavrakakis
              msg['instance'])
217 604b2bf8 Georgios Gousios
218 604b2bf8 Georgios Gousios
219 a17a8e98 Christos Stavrakakis
def dummy_proc(client, message, *args, **kwargs):
220 23c84263 Georgios Gousios
    try:
221 c4e55622 Christos Stavrakakis
        log.debug("Msg: %s", message['body'])
222 33b93f81 Christos Stavrakakis
        client.basic_ack(message)
223 23c84263 Georgios Gousios
    except Exception as e:
224 22ee6892 Christos Stavrakakis
        log.exception("Could not receive message %s" % e)