root / snf-cyclades-app / synnefo / logic / management / commands / reconcile-servers.py @ a6e6fe48
History | View | Annotate | Download (5.6 kB)
1 |
# Copyright 2011-2014 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
#
|
30 |
"""Reconciliation management command
|
31 |
|
32 |
Management command to reconcile the contents of the Synnefo DB with
|
33 |
the state of the Ganeti backend. See docstring on top of
|
34 |
logic/reconciliation.py for a description of reconciliation rules.
|
35 |
|
36 |
"""
|
37 |
import sys |
38 |
import logging |
39 |
import subprocess |
40 |
from optparse import make_option |
41 |
from django.core.management.base import BaseCommand |
42 |
from synnefo.management.common import get_resource |
43 |
from synnefo.logic import reconciliation |
44 |
from snf_django.management.utils import parse_bool |
45 |
|
46 |
|
47 |
class Command(BaseCommand): |
48 |
can_import_settings = True
|
49 |
|
50 |
help = 'Reconcile contents of Synnefo DB with state of Ganeti backend'
|
51 |
option_list = BaseCommand.option_list + ( |
52 |
make_option('--backend-id', default=None, dest='backend-id', |
53 |
help='Reconcilie VMs only for this backend'),
|
54 |
make_option("--parallel",
|
55 |
dest="parallel",
|
56 |
default="True",
|
57 |
choices=["True", "False"], |
58 |
metavar="True|False",
|
59 |
help="Perform server reconciliation for each backend"
|
60 |
" parallel."),
|
61 |
make_option('--fix-stale', action='store_true', dest='fix_stale', |
62 |
default=False, help='Fix (remove) stale DB entries in DB'), |
63 |
make_option('--fix-orphans', action='store_true', dest='fix_orphans', |
64 |
default=False, help='Fix (remove) orphan Ganeti VMs'), |
65 |
make_option('--fix-unsynced', action='store_true', dest='fix_unsynced', |
66 |
default=False, help='Fix server operstate in DB, set ' + |
67 |
'from Ganeti'),
|
68 |
make_option('--fix-unsynced-nics', action='store_true', |
69 |
dest='fix_unsynced_nics', default=False, |
70 |
help='Fix unsynced nics between DB and Ganeti'),
|
71 |
make_option('--fix-unsynced-flavors', action='store_true', |
72 |
dest='fix_unsynced_flavors', default=False, |
73 |
help='Fix unsynced flavors between DB and Ganeti'),
|
74 |
make_option('--fix-pending-tasks', action='store_true', |
75 |
dest='fix_pending_tasks', default=False, |
76 |
help='Fix servers with stale pending tasks.'),
|
77 |
make_option('--fix-all', action='store_true', dest='fix_all', |
78 |
default=False, help='Enable all --fix-* arguments'), |
79 |
) |
80 |
|
81 |
def _process_args(self, options): |
82 |
keys_fix = [k for k in options.keys() if k.startswith('fix_')] |
83 |
if options['fix_all']: |
84 |
for kf in keys_fix: |
85 |
options[kf] = True
|
86 |
|
87 |
def handle(self, **options): |
88 |
backend_id = options['backend-id']
|
89 |
if backend_id:
|
90 |
backends = [get_resource("backend", backend_id)]
|
91 |
else:
|
92 |
backends = reconciliation.get_online_backends() |
93 |
|
94 |
parallel = parse_bool(options["parallel"])
|
95 |
if parallel and len(backends) > 1: |
96 |
cmd = sys.argv |
97 |
processes = [] |
98 |
for backend in backends: |
99 |
p = subprocess.Popen(cmd + ["--backend-id=%s" % backend.id])
|
100 |
processes.append(p) |
101 |
for p in processes: |
102 |
p.wait() |
103 |
return
|
104 |
|
105 |
verbosity = int(options["verbosity"]) |
106 |
|
107 |
logger = logging.getLogger("reconcile-servers")
|
108 |
logger.propagate = 0
|
109 |
|
110 |
formatter = logging.Formatter("%(message)s")
|
111 |
log_handler = logging.StreamHandler() |
112 |
log_handler.setFormatter(formatter) |
113 |
if verbosity == 2: |
114 |
formatter =\ |
115 |
logging.Formatter("%(asctime)s [%(process)d]: %(message)s")
|
116 |
log_handler.setFormatter(formatter) |
117 |
logger.setLevel(logging.DEBUG) |
118 |
elif verbosity == 1: |
119 |
logger.setLevel(logging.INFO) |
120 |
else:
|
121 |
logger.setLevel(logging.WARNING) |
122 |
|
123 |
logger.addHandler(log_handler) |
124 |
|
125 |
self._process_args(options)
|
126 |
|
127 |
for backend in backends: |
128 |
r = reconciliation.BackendReconciler(backend=backend, |
129 |
logger=logger, |
130 |
options=options) |
131 |
r.reconcile() |