Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / reconcile-servers.py @ 75dc539e

History | View | Annotate | Download (5.4 kB)

1
# Copyright 2011-2013 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_backend
43
from synnefo.logic import reconciliation
44
from synnefo.webproject.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-all', action='store_true', dest='fix_all',
75
                    default=False, help='Enable all --fix-* arguments'),
76
    )
77

    
78
    def _process_args(self, options):
79
        keys_fix = [k for k in options.keys() if k.startswith('fix_')]
80
        if options['fix_all']:
81
            for kf in keys_fix:
82
                options[kf] = True
83

    
84
    def handle(self, **options):
85
        backend_id = options['backend-id']
86
        if backend_id:
87
            backends = [get_backend(backend_id)]
88
        else:
89
            backends = reconciliation.get_online_backends()
90

    
91
        parallel = parse_bool(options["parallel"])
92
        if parallel and len(backends) > 1:
93
            cmd = sys.argv
94
            processes = []
95
            for backend in backends:
96
                p = subprocess.Popen(cmd + ["--backend-id=%s" % backend.id])
97
                processes.append(p)
98
            for p in processes:
99
                p.wait()
100
            return
101

    
102
        verbosity = int(options["verbosity"])
103

    
104
        logger = logging.getLogger("reconcile-severs")
105
        logger.propagate = 0
106

    
107
        formatter = logging.Formatter("%(message)s")
108
        log_handler = logging.StreamHandler()
109
        log_handler.setFormatter(formatter)
110
        if verbosity == 2:
111
            formatter = logging.Formatter("%(asctime)s: %(message)s")
112
            log_handler.setFormatter(formatter)
113
            logger.setLevel(logging.DEBUG)
114
        elif verbosity == 1:
115
            logger.setLevel(logging.INFO)
116
        else:
117
            logger.setLevel(logging.WARNING)
118

    
119
        logger.addHandler(log_handler)
120

    
121
        self._process_args(options)
122

    
123
        for backend in backends:
124
            r = reconciliation.BackendReconciler(backend=backend,
125
                                                 logger=logger,
126
                                                 options=options)
127
            r.reconcile()