root / ganeti / snf-ganeti-hook.py @ 737b0e28
History | View | Annotate | Download (4.2 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
#
|
3 |
# -*- coding: utf-8 -*-
|
4 |
#
|
5 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
6 |
#
|
7 |
# Redistribution and use in source and binary forms, with or
|
8 |
# without modification, are permitted provided that the following
|
9 |
# conditions are met:
|
10 |
#
|
11 |
# 1. Redistributions of source code must retain the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer.
|
14 |
#
|
15 |
# 2. Redistributions in binary form must reproduce the above
|
16 |
# copyright notice, this list of conditions and the following
|
17 |
# disclaimer in the documentation and/or other materials
|
18 |
# provided with the distribution.
|
19 |
#
|
20 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
21 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
22 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
23 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
24 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
27 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
28 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
29 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
30 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31 |
# POSSIBILITY OF SUCH DAMAGE.
|
32 |
#
|
33 |
# The views and conclusions contained in the software and
|
34 |
# documentation are those of the authors and should not be
|
35 |
# interpreted as representing official policies, either expressed
|
36 |
# or implied, of GRNET S.A.
|
37 |
#
|
38 |
"""Ganeti hook for Synnefo
|
39 |
|
40 |
This is the generic Synnefo Ganeti hook.
|
41 |
|
42 |
It uses the full path of the hook, as passed through sys.argv[0]
|
43 |
to discover the root of the Synnefo project, then passes
|
44 |
control to the function which implements this specific hook,
|
45 |
based on the GANETI_HOOKS_PATH and GANETI_HOOKS_PHASE env variables,
|
46 |
set by Ganeti.
|
47 |
|
48 |
"""
|
49 |
import logging |
50 |
|
51 |
import sys |
52 |
import os |
53 |
|
54 |
# IMPORTANT: PYTHONPATH must contain the parent of the Synnefo project root.
|
55 |
try:
|
56 |
import synnefo.settings as settings |
57 |
except ImportError: |
58 |
raise Exception("Cannot import settings, make sure PYTHONPATH contains " |
59 |
"the parent directory of the Synnefo Django project.")
|
60 |
|
61 |
# A hook runs either in the "pre" or "post" phase of a Ganeti operation.
|
62 |
# Possible values for the Ganeti operation are "instance-start",
|
63 |
# "instance-stop", "instance-reboot", "instance-modify". See the Ganeti
|
64 |
# documentation for a full list.
|
65 |
|
66 |
# The operation and phase for which the hook run are determined from the
|
67 |
# values of the GANETI_HOOK_PATH and GANETI_HOOK_PHASE environment variables.
|
68 |
# For each valid (operation, phase) pair control passes to the corresponding
|
69 |
# Python function, based on the following dictionary.
|
70 |
|
71 |
from synnefo.ganeti.hooks import \ |
72 |
PostStartHook, PostStopHook |
73 |
|
74 |
hooks = { |
75 |
("instance-add", "post"): PostStartHook, |
76 |
("instance-start", "post"): PostStartHook, |
77 |
("instance-reboot", "post"): PostStartHook, |
78 |
("instance-stop", "post"): PostStopHook, |
79 |
("instance-modify", "post"): PostStartHook |
80 |
} |
81 |
|
82 |
def main(): |
83 |
logging.basicConfig(level=logging.DEBUG) |
84 |
logger = logging.getLogger("synnefo.ganeti")
|
85 |
|
86 |
try:
|
87 |
instance = os.environ['GANETI_INSTANCE_NAME']
|
88 |
op = os.environ['GANETI_HOOKS_PATH']
|
89 |
phase = os.environ['GANETI_HOOKS_PHASE']
|
90 |
except KeyError: |
91 |
raise Exception("Environment missing one of: " \ |
92 |
"GANETI_INSTANCE_NAME, GANETI_HOOKS_PATH, GANETI_HOOKS_PHASE")
|
93 |
|
94 |
prefix = instance.split('-')[0] |
95 |
|
96 |
# FIXME: The hooks should only run for Synnefo instances.
|
97 |
# Uncomment the following lines for a shared Ganeti deployment.
|
98 |
# Currently, the following code is commented out because multiple
|
99 |
# backend prefixes are used in the same Ganeti installation during development.
|
100 |
#if not instance.startswith(settings.BACKEND_PREFIX_ID):
|
101 |
# logger.warning("Ignoring non-Synnefo instance %s", instance)
|
102 |
# return 0
|
103 |
|
104 |
try:
|
105 |
hook = hooks[(op, phase)](logger, os.environ, instance, prefix) |
106 |
except KeyError: |
107 |
raise Exception("No hook found for operation = '%s', phase = '%s'" % (op, phase)) |
108 |
return hook.run()
|
109 |
|
110 |
|
111 |
if __name__ == "__main__": |
112 |
sys.exit(main()) |
113 |
|
114 |
# vim: set ts=4 sts=4 sw=4 et ai :
|