root / snf-cyclades-app / synnefo / logic / management / commands / reconcile-servers.py @ b6426ead
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 |
|
42 |
from snf_django.management.commands import SynnefoCommand |
43 |
from synnefo.management.common import get_resource |
44 |
from synnefo.logic import reconciliation |
45 |
from snf_django.management.utils import parse_bool |
46 |
|
47 |
|
48 |
class Command(SynnefoCommand): |
49 |
can_import_settings = True
|
50 |
|
51 |
help = 'Reconcile contents of Synnefo DB with state of Ganeti backend'
|
52 |
option_list = SynnefoCommand.option_list + ( |
53 |
make_option('--backend-id', default=None, dest='backend-id', |
54 |
help='Reconcilie VMs only for this backend'),
|
55 |
make_option("--parallel",
|
56 |
dest="parallel",
|
57 |
default="True",
|
58 |
choices=["True", "False"], |
59 |
metavar="True|False",
|
60 |
help="Perform server reconciliation for each backend"
|
61 |
" parallel."),
|
62 |
make_option('--fix-stale', action='store_true', dest='fix_stale', |
63 |
default=False, help='Fix (remove) stale DB entries in DB'), |
64 |
make_option('--fix-orphans', action='store_true', dest='fix_orphans', |
65 |
default=False, help='Fix (remove) orphan Ganeti VMs'), |
66 |
make_option('--fix-unsynced', action='store_true', dest='fix_unsynced', |
67 |
default=False, help='Fix server operstate in DB, set ' + |
68 |
'from Ganeti'),
|
69 |
make_option('--fix-unsynced-nics', action='store_true', |
70 |
dest='fix_unsynced_nics', default=False, |
71 |
help='Fix unsynced nics between DB and Ganeti'),
|
72 |
make_option('--fix-unsynced-flavors', action='store_true', |
73 |
dest='fix_unsynced_flavors', default=False, |
74 |
help='Fix unsynced flavors between DB and Ganeti'),
|
75 |
make_option('--fix-pending-tasks', action='store_true', |
76 |
dest='fix_pending_tasks', default=False, |
77 |
help='Fix servers with stale pending tasks.'),
|
78 |
make_option('--fix-all', action='store_true', dest='fix_all', |
79 |
default=False, help='Enable all --fix-* arguments'), |
80 |
) |
81 |
|
82 |
def _process_args(self, options): |
83 |
keys_fix = [k for k in options.keys() if k.startswith('fix_')] |
84 |
if options['fix_all']: |
85 |
for kf in keys_fix: |
86 |
options[kf] = True
|
87 |
|
88 |
def handle(self, **options): |
89 |
backend_id = options['backend-id']
|
90 |
if backend_id:
|
91 |
backends = [get_resource("backend", backend_id)]
|
92 |
else:
|
93 |
backends = reconciliation.get_online_backends() |
94 |
|
95 |
parallel = parse_bool(options["parallel"])
|
96 |
if parallel and len(backends) > 1: |
97 |
cmd = sys.argv |
98 |
processes = [] |
99 |
for backend in backends: |
100 |
p = subprocess.Popen(cmd + ["--backend-id=%s" % backend.id])
|
101 |
processes.append(p) |
102 |
for p in processes: |
103 |
p.wait() |
104 |
return
|
105 |
|
106 |
verbosity = int(options["verbosity"]) |
107 |
|
108 |
logger = logging.getLogger("reconcile-servers")
|
109 |
logger.propagate = 0
|
110 |
|
111 |
formatter = logging.Formatter("%(message)s")
|
112 |
log_handler = logging.StreamHandler() |
113 |
log_handler.setFormatter(formatter) |
114 |
if verbosity == 2: |
115 |
formatter =\ |
116 |
logging.Formatter("%(asctime)s [%(process)d]: %(message)s")
|
117 |
log_handler.setFormatter(formatter) |
118 |
logger.setLevel(logging.DEBUG) |
119 |
elif verbosity == 1: |
120 |
logger.setLevel(logging.INFO) |
121 |
else:
|
122 |
logger.setLevel(logging.WARNING) |
123 |
|
124 |
logger.addHandler(log_handler) |
125 |
|
126 |
self._process_args(options)
|
127 |
|
128 |
for backend in backends: |
129 |
r = reconciliation.BackendReconciler(backend=backend, |
130 |
logger=logger, |
131 |
options=options) |
132 |
r.reconcile() |