root / ganeti / snf-ganeti-hook.py @ f533f224
History | View | Annotate | Download (3.6 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 |
from django.core.management import setup_environ |
18 |
|
19 |
import sys |
20 |
import os |
21 |
|
22 |
# Discover the path for the Synnefo project.
|
23 |
#
|
24 |
# IMPORTANT:
|
25 |
# This assumes this script has been *linked* into the /etc/ganeti/hooks
|
26 |
# directory on the master and nodes, and that it lives in a ganeti/ directory
|
27 |
# under the Synnefo project root.
|
28 |
|
29 |
try:
|
30 |
target = os.readlink(sys.argv[0])
|
31 |
except OSError: |
32 |
target = sys.argv[0]
|
33 |
target_script = os.path.abspath(target) |
34 |
target_dirname = os.path.dirname(target_script) |
35 |
|
36 |
if os.path.split(target_dirname)[1] != "ganeti": |
37 |
raise Exception, "Unexpected location for the Synnefo Ganeti hook, " \ |
38 |
"cannot determine Synnefo project root.\n" \
|
39 |
"This script run as: %s\nLocation determined to be at: %s\n" \
|
40 |
"Script in %s, not under ganeti/ directory." % \
|
41 |
(sys.argv[0], target_script, target_dirname)
|
42 |
|
43 |
# Add the parent of the project root to sys.path (for Python imports),
|
44 |
# then load Django settings.
|
45 |
# FIXME: Why do import references start at synnefo.* ?
|
46 |
synnefo_project_root = os.path.split(target_dirname)[0]
|
47 |
sys.path.append(os.path.join(synnefo_project_root, '..'))
|
48 |
|
49 |
import synnefo.settings as settings |
50 |
setup_environ(settings) |
51 |
|
52 |
# A hook runs either in the "pre" or "post" phase of a Ganeti operation.
|
53 |
# Possible values for the Ganeti operation are "instance-start",
|
54 |
# "instance-stop", "instance-reboot", "instance-modify". See the Ganeti
|
55 |
# documentation for a full list.
|
56 |
|
57 |
# The operation and phase for which the hook run are determined from the
|
58 |
# values of the GANETI_HOOK_PATH and GANETI_HOOK_PHASE environment variables.
|
59 |
# For each valid (operation, phase) pair control passes to the corresponding
|
60 |
# Python function, based on the following dictionary.
|
61 |
|
62 |
from synnefo.ganeti.hooks import \ |
63 |
PostStartHook, PostStopHook |
64 |
|
65 |
hooks = { |
66 |
("instance-add", "post"): PostStartHook, |
67 |
("instance-start", "post"): PostStartHook, |
68 |
("instance-reboot", "post"): PostStartHook, |
69 |
("instance-stop", "post"): PostStopHook, |
70 |
("instance-modify", "post"): PostStartHook |
71 |
} |
72 |
|
73 |
def main(): |
74 |
logging.basicConfig(level=logging.DEBUG) |
75 |
logger = logging.getLogger("synnefo.ganeti")
|
76 |
|
77 |
try:
|
78 |
instance = os.environ['GANETI_INSTANCE_NAME']
|
79 |
op = os.environ['GANETI_HOOKS_PATH']
|
80 |
phase = os.environ['GANETI_HOOKS_PHASE']
|
81 |
except KeyError: |
82 |
raise Exception("Environment missing one of: " \ |
83 |
"GANETI_INSTANCE_NAME, GANETI_HOOKS_PATH, GANETI_HOOKS_PHASE")
|
84 |
|
85 |
prefix = instance.split('-')[0] |
86 |
|
87 |
# FIXME: The hooks should only run for Synnefo instances.
|
88 |
# Uncomment the following lines for a shared Ganeti deployment.
|
89 |
# Currently, the following code is commented out because multiple
|
90 |
# backend prefixes are used in the same Ganeti installation during development.
|
91 |
#if not instance.startswith(settings.BACKEND_PREFIX_ID):
|
92 |
# logger.warning("Ignoring non-Synnefo instance %s", instance)
|
93 |
# return 0
|
94 |
|
95 |
try:
|
96 |
hook = hooks[(op, phase)](logger, os.environ, instance, prefix) |
97 |
except KeyError: |
98 |
raise Exception("No hook found for operation = '%s', phase = '%s'" % (op, phase)) |
99 |
return hook.run()
|
100 |
|
101 |
|
102 |
if __name__ == "__main__": |
103 |
sys.exit(main()) |
104 |
|
105 |
# vim: set ts=4 sts=4 sw=4 et ai :
|