Statistics
| Branch: | Tag: | Revision:

root / ganeti / snf-ganeti-hook.py @ c0f6fb49

History | View | Annotate | Download (2.7 kB)

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

7
This is the generic Synnefo Ganeti hook.
8

9
It uses the full path of the hook, as passed through sys.argv[0]
10
to discover the root of the Synnefo project, then passes
11
control to the function which implements this specific hook,
12
based on the GANETI_HOOKS_PATH and GANETI_HOOKS_PHASE env variables,
13
set by Ganeti.
14

15
"""
16
import logging
17

    
18
import sys
19
import os
20

    
21
# IMPORTANT: PYTHONPATH must contain the parent of the Synnefo project root.
22
try:
23
    import synnefo.settings as settings
24
except ImportError:
25
    raise Exception("Cannot import settings, make sure PYTHONPATH contains "
26
                    "the parent directory of the Synnefo Django project.")
27

    
28
# A hook runs either in the "pre" or "post" phase of a Ganeti operation.
29
# Possible values for the Ganeti operation are "instance-start",
30
# "instance-stop", "instance-reboot", "instance-modify". See the Ganeti
31
# documentation for a full list.
32

    
33
# The operation and phase for which the hook run are determined from the 
34
# values of the GANETI_HOOK_PATH and GANETI_HOOK_PHASE environment variables.
35
# For each valid (operation, phase) pair control passes to the corresponding
36
# Python function, based on the following dictionary.
37

    
38
from synnefo.ganeti.hooks import \
39
    PostStartHook, PostStopHook
40

    
41
hooks = {
42
    ("instance-add", "post"): PostStartHook,
43
    ("instance-start", "post"): PostStartHook,
44
    ("instance-reboot", "post"): PostStartHook,
45
    ("instance-stop", "post"): PostStopHook,
46
    ("instance-modify", "post"): PostStartHook
47
}
48

    
49
def main():
50
    logging.basicConfig(level=logging.DEBUG)
51
    logger = logging.getLogger("synnefo.ganeti")
52

    
53
    try:
54
        instance = os.environ['GANETI_INSTANCE_NAME']
55
        op = os.environ['GANETI_HOOKS_PATH']
56
        phase = os.environ['GANETI_HOOKS_PHASE']
57
    except KeyError:
58
        raise Exception("Environment missing one of: " \
59
            "GANETI_INSTANCE_NAME, GANETI_HOOKS_PATH, GANETI_HOOKS_PHASE")
60
        
61
    prefix = instance.split('-')[0]
62
  
63
    # FIXME: The hooks should only run for Synnefo instances.
64
    # Uncomment the following lines for a shared Ganeti deployment.
65
    # Currently, the following code is commented out because multiple
66
    # backend prefixes are used in the same Ganeti installation during development.
67
    #if not instance.startswith(settings.BACKEND_PREFIX_ID):
68
    #    logger.warning("Ignoring non-Synnefo instance %s", instance)
69
    #    return 0
70

    
71
    try:
72
        hook = hooks[(op, phase)](logger, os.environ, instance, prefix)
73
    except KeyError:
74
        raise Exception("No hook found for operation = '%s', phase = '%s'" % (op, phase))
75
    return hook.run()
76

    
77

    
78
if __name__ == "__main__":
79
    sys.exit(main())
80

    
81
# vim: set ts=4 sts=4 sw=4 et ai :