Statistics
| Branch: | Tag: | Revision:

root / ganeti / hooks.py @ ff55193e

History | View | Annotate | Download (2.6 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright (c) 2010 Greek Research and Technology Network
4
#
5
"""Ganeti hooks for Synnefo
6

7
These are the individual Ganeti hooks for Synnefo.
8

9
"""
10

    
11
import sys
12
import os
13

    
14
import time
15
import json
16
import socket
17
import logging
18

    
19
import synnefo.settings as settings
20

    
21
def on_master(environ):
22
    """Return True if running on the Ganeti master"""
23
    return socket.getfqdn() == environ['GANETI_MASTER']
24

    
25
def ganeti_net_status(logger, environ):
26
    """Produce notifications of type 'Ganeti-net-status'
27
    
28
    Process all GANETI_INSTANCE_NICx_y environment variables,
29
    where x is the NIC index, starting at 0,
30
    and y is one of "MAC", "IP".
31

32
    The result is returned as a single notification message
33
    of type 'Ganeti-net-status', detailing the NIC configuration
34
    of a Ganeti instance.
35

36
    """
37
    nics = {}
38

    
39
    for env in environ.keys():
40
        if env.startswith("GANETI_INSTANCE_NIC"):
41
            s = env.replace("GANETI_INSTANCE_NIC", "").split('_', 1)
42
            if len(s) == 2 and s[0].isdigit() and s[1] in ('MAC', 'IP'):
43
                index = int(s[0])
44
                key = s[1].lower()
45

    
46
                if nics.has_key(index):
47
                    nics[index][key] = environ[env]
48
                else:
49
                    nics[index] = { key: environ[env] }
50

    
51
    # Verify our findings are consistent with the Ganeti environment
52
    indexes = list(nics.keys())
53
    ganeti_nic_count = int(environ['GANETI_INSTANCE_NIC_COUNT'])
54
    if len(indexes) != ganeti_nic_count:
55
        logger.error("I have %d NICs, Ganeti says number of NICs is %d",
56
            len(indexes), ganeti_nic_count)
57
        raise Exception("Inconsistent number of NICs in Ganeti environment")
58

    
59
    if indexes != range(0, len(indexes)):
60
        logger.error("Ganeti NIC indexes are not consecutive starting at zero.");
61
        logger.error("NIC indexes are: %s. Environment is: %s", indexes, environ)
62
        raise Exception("Unexpected inconsistency in the Ganeti environment")
63

    
64
    # Construct the notification
65
    instance = environ['GANETI_INSTANCE_NAME']
66

    
67
    nics_list = []
68
    for i in indexes:
69
        nics_list.append(nics[i])
70

    
71
    msg = {
72
        "type": "ganeti-net-status",
73
        "instance": instance,
74
        "nics": nics_list
75
    }
76

    
77
    return msg
78

    
79
def post_start_hook(logger, environ):
80
    """Construct notifications to the rest of Synnefo on instance startup.
81
    
82
    Currently, this list only contains a single message,
83
    detailing the net configuration of an instance.
84

85
    """
86
    notifs = []
87
    notifs.append(ganeti_net_status(logger, environ))
88

    
89
    print "post_start_hook: ", notifs
90
    return 0
91

    
92
def post_stop_hook(logger, environ):
93
    return 0
94