Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.8 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 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-disks', action='store_true',
72
                    dest='fix_unsynced_disks', default=False,
73
                    help='Fix unsynced disks between DB and Ganeti'),
74
        make_option('--fix-unsynced-flavors', action='store_true',
75
                    dest='fix_unsynced_flavors', default=False,
76
                    help='Fix unsynced flavors between DB and Ganeti'),
77
        make_option('--fix-pending-tasks', action='store_true',
78
                    dest='fix_pending_tasks', default=False,
79
                    help='Fix servers with stale pending tasks.'),
80
        make_option('--fix-all', action='store_true', dest='fix_all',
81
                    default=False, help='Enable all --fix-* arguments'),
82
    )
83

    
84
    def _process_args(self, options):
85
        keys_fix = [k for k in options.keys() if k.startswith('fix_')]
86
        if options['fix_all']:
87
            for kf in keys_fix:
88
                options[kf] = True
89

    
90
    def handle(self, **options):
91
        backend_id = options['backend-id']
92
        if backend_id:
93
            backends = [get_backend(backend_id)]
94
        else:
95
            backends = reconciliation.get_online_backends()
96

    
97
        parallel = parse_bool(options["parallel"])
98
        if parallel and len(backends) > 1:
99
            cmd = sys.argv
100
            processes = []
101
            for backend in backends:
102
                p = subprocess.Popen(cmd + ["--backend-id=%s" % backend.id])
103
                processes.append(p)
104
            for p in processes:
105
                p.wait()
106
            return
107

    
108
        verbosity = int(options["verbosity"])
109

    
110
        logger = logging.getLogger("reconcile-servers")
111
        logger.propagate = 0
112

    
113
        formatter = logging.Formatter("%(message)s")
114
        log_handler = logging.StreamHandler()
115
        log_handler.setFormatter(formatter)
116
        if verbosity == 2:
117
            formatter =\
118
                logging.Formatter("%(asctime)s [%(process)d]: %(message)s")
119
            log_handler.setFormatter(formatter)
120
            logger.setLevel(logging.DEBUG)
121
        elif verbosity == 1:
122
            logger.setLevel(logging.INFO)
123
        else:
124
            logger.setLevel(logging.WARNING)
125

    
126
        logger.addHandler(log_handler)
127

    
128
        self._process_args(options)
129

    
130
        for backend in backends:
131
            r = reconciliation.BackendReconciler(backend=backend,
132
                                                 logger=logger,
133
                                                 options=options)
134
            r.reconcile()