Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.7 kB)

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

7 ff55193e Vangelis Koukis
This is the generic Synnefo Ganeti hook.
8 ff55193e Vangelis Koukis

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

15 ff55193e Vangelis Koukis
"""
16 ff55193e Vangelis Koukis
import logging
17 ff55193e Vangelis Koukis
18 ff55193e Vangelis Koukis
import sys
19 ff55193e Vangelis Koukis
import os
20 ff55193e Vangelis Koukis
21 0bb376ce Vangelis Koukis
# IMPORTANT: PYTHONPATH must contain the parent of the Synnefo project root.
22 ff55193e Vangelis Koukis
try:
23 0bb376ce Vangelis Koukis
    import synnefo.settings as settings
24 0bb376ce Vangelis Koukis
except ImportError:
25 0bb376ce Vangelis Koukis
    raise Exception("Cannot import settings, make sure PYTHONPATH contains "
26 0bb376ce Vangelis Koukis
                    "the parent directory of the Synnefo Django project.")
27 ff55193e Vangelis Koukis
28 ff55193e Vangelis Koukis
# A hook runs either in the "pre" or "post" phase of a Ganeti operation.
29 ff55193e Vangelis Koukis
# Possible values for the Ganeti operation are "instance-start",
30 ff55193e Vangelis Koukis
# "instance-stop", "instance-reboot", "instance-modify". See the Ganeti
31 ff55193e Vangelis Koukis
# documentation for a full list.
32 ff55193e Vangelis Koukis
33 ff55193e Vangelis Koukis
# The operation and phase for which the hook run are determined from the 
34 ff55193e Vangelis Koukis
# values of the GANETI_HOOK_PATH and GANETI_HOOK_PHASE environment variables.
35 ff55193e Vangelis Koukis
# For each valid (operation, phase) pair control passes to the corresponding
36 ff55193e Vangelis Koukis
# Python function, based on the following dictionary.
37 ff55193e Vangelis Koukis
38 ff55193e Vangelis Koukis
from synnefo.ganeti.hooks import \
39 b9eef123 Vangelis Koukis
    PostStartHook, PostStopHook
40 ff55193e Vangelis Koukis
41 ff55193e Vangelis Koukis
hooks = {
42 f533f224 Vangelis Koukis
    ("instance-add", "post"): PostStartHook,
43 b9eef123 Vangelis Koukis
    ("instance-start", "post"): PostStartHook,
44 f533f224 Vangelis Koukis
    ("instance-reboot", "post"): PostStartHook,
45 f533f224 Vangelis Koukis
    ("instance-stop", "post"): PostStopHook,
46 f533f224 Vangelis Koukis
    ("instance-modify", "post"): PostStartHook
47 ff55193e Vangelis Koukis
}
48 ff55193e Vangelis Koukis
49 ff55193e Vangelis Koukis
def main():
50 b9eef123 Vangelis Koukis
    logging.basicConfig(level=logging.DEBUG)
51 ff55193e Vangelis Koukis
    logger = logging.getLogger("synnefo.ganeti")
52 ff55193e Vangelis Koukis
53 ff55193e Vangelis Koukis
    try:
54 ff55193e Vangelis Koukis
        instance = os.environ['GANETI_INSTANCE_NAME']
55 ff55193e Vangelis Koukis
        op = os.environ['GANETI_HOOKS_PATH']
56 ff55193e Vangelis Koukis
        phase = os.environ['GANETI_HOOKS_PHASE']
57 ff55193e Vangelis Koukis
    except KeyError:
58 ff55193e Vangelis Koukis
        raise Exception("Environment missing one of: " \
59 ff55193e Vangelis Koukis
            "GANETI_INSTANCE_NAME, GANETI_HOOKS_PATH, GANETI_HOOKS_PHASE")
60 b9eef123 Vangelis Koukis
        
61 b9eef123 Vangelis Koukis
    prefix = instance.split('-')[0]
62 ff55193e Vangelis Koukis
  
63 ff55193e Vangelis Koukis
    # FIXME: The hooks should only run for Synnefo instances.
64 ff55193e Vangelis Koukis
    # Uncomment the following lines for a shared Ganeti deployment.
65 ff55193e Vangelis Koukis
    # Currently, the following code is commented out because multiple
66 ff55193e Vangelis Koukis
    # backend prefixes are used in the same Ganeti installation during development.
67 b9eef123 Vangelis Koukis
    #if not instance.startswith(settings.BACKEND_PREFIX_ID):
68 b9eef123 Vangelis Koukis
    #    logger.warning("Ignoring non-Synnefo instance %s", instance)
69 ff55193e Vangelis Koukis
    #    return 0
70 ff55193e Vangelis Koukis
71 ff55193e Vangelis Koukis
    try:
72 b9eef123 Vangelis Koukis
        hook = hooks[(op, phase)](logger, os.environ, instance, prefix)
73 ff55193e Vangelis Koukis
    except KeyError:
74 ff55193e Vangelis Koukis
        raise Exception("No hook found for operation = '%s', phase = '%s'" % (op, phase))
75 b9eef123 Vangelis Koukis
    return hook.run()
76 ff55193e Vangelis Koukis
77 ff55193e Vangelis Koukis
78 ff55193e Vangelis Koukis
if __name__ == "__main__":
79 ff55193e Vangelis Koukis
    sys.exit(main())
80 ff55193e Vangelis Koukis
81 ff55193e Vangelis Koukis
# vim: set ts=4 sts=4 sw=4 et ai :