Revision c414bc87

b/snf-cyclades-app/synnefo/logic/backend.py
642 642

  
643 643
def get_backends(backend=None):
644 644
    if backend:
645
        if backend.offline:
646
            return []
645 647
        return [backend]
646 648
    return Backend.objects.filter(offline=False)
647 649

  
b/snf-cyclades-app/synnefo/logic/management/commands/reconcile-servers.py
43 43
from django.core.management.base import BaseCommand, CommandError
44 44

  
45 45
from synnefo.db.models import VirtualMachine, Network, pooled_rapi_client
46
from synnefo.logic import reconciliation, backend, utils
46
from synnefo.logic import reconciliation, utils
47
from synnefo.logic import backend as backend_mod
48
from synnefo.management.common import get_backend
47 49

  
48 50

  
49 51
class Command(BaseCommand):
......
85 87
                    dest='fix_unsynced_nics', default=False,
86 88
                    help='Fix unsynced nics between DB and Ganeti'),
87 89
        make_option('--fix-all', action='store_true', dest='fix_all',
88
                    default=False, help='Enable all --fix-* arguments'))
90
                    default=False, help='Enable all --fix-* arguments'),
91
        make_option('--backend-id', default=None, dest='backend-id',
92
                    help='Reconcilie VMs only for this backend'),
93
        )
89 94

  
90 95
    def _process_args(self, options):
91 96
        keys_detect = [k for k in options.keys() if k.startswith('detect_')]
......
111 116
    def handle(self, **options):
112 117
        verbosity = int(options['verbosity'])
113 118
        self._process_args(options)
119
        backend_id = options['backend-id']
120
        backend = get_backend(backend_id) if backend_id else None
114 121

  
115
        D = reconciliation.get_servers_from_db()
116
        G, GNics = reconciliation.get_instances_from_ganeti()
122
        D = reconciliation.get_servers_from_db(backend)
123
        G, GNics = reconciliation.get_instances_from_ganeti(backend)
117 124

  
118
        DBNics = reconciliation.get_nics_from_db()
125
        DBNics = reconciliation.get_nics_from_db(backend)
119 126

  
120 127
        #
121 128
        # Detect problems
......
190 197
                "servers in the DB:" % len(stale)
191 198
            for vm in VirtualMachine.objects.filter(pk__in=stale):
192 199
                event_time = datetime.datetime.now()
193
                backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
200
                backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
194 201
                    opcode='OP_INSTANCE_REMOVE', status='success',
195 202
                    logmsg='Reconciliation: simulated Ganeti event')
196 203
            print >> sys.stderr, "    ...done"
......
216 223
                opcode = "OP_INSTANCE_REBOOT" if ganeti_up \
217 224
                         else "OP_INSTANCE_SHUTDOWN"
218 225
                event_time = datetime.datetime.now()
219
                backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
226
                backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
220 227
                    opcode=opcode, status='success',
221 228
                    logmsg='Reconciliation: simulated Ganeti event')
222 229
            print >> sys.stderr, "    ...done"
......
227 234
            for id in build_errors:
228 235
                vm = VirtualMachine.objects.get(pk=id)
229 236
                event_time = datetime.datetime.now()
230
                backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
237
                backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
231 238
                    opcode="OP_INSTANCE_CREATE", status='error',
232 239
                    logmsg='Reconciliation: simulated Ganeti event')
233 240
            print >> sys.stderr, "    ...done"
......
260 267
                        print 'Network of nic %d of vm %s is None. ' \
261 268
                              'Can not reconcile' % (i, vm.backend_vm_id)
262 269
                event_time = datetime.datetime.now()
263
                backend.process_net_status(vm=vm, etime=event_time, nics=final_nics)
270
                backend_mod.process_net_status(vm=vm, etime=event_time, nics=final_nics)
264 271
            print >> sys.stderr, "    ...done"
265 272

  
266 273

  
b/snf-cyclades-app/synnefo/logic/reconciliation.py
73 73

  
74 74
from synnefo.db.models import (VirtualMachine, pooled_rapi_client)
75 75
from synnefo.logic.rapi import GanetiApiError
76
from synnefo.logic.backend import get_ganeti_instances
76
from synnefo.logic.backend import get_ganeti_instances, get_backends
77 77
from synnefo.logic import utils
78 78

  
79 79

  
......
160 160
    return failed
161 161

  
162 162

  
163
def get_servers_from_db():
164
    vms = VirtualMachine.objects.filter(deleted=False, backend__offline=False)
163
def get_servers_from_db(backend=None):
164
    backends = get_backends(backend)
165
    vms = VirtualMachine.objects.filter(deleted=False, backend__in=backends)
165 166
    return dict(map(lambda x: (x.id, x.operstate), vms))
166 167

  
167 168

  
168
def get_instances_from_ganeti():
169
    ganeti_instances = get_ganeti_instances(bulk=True)
169
def get_instances_from_ganeti(backend=None):
170
    ganeti_instances = get_ganeti_instances(backend=backend, bulk=True)
170 171
    snf_instances = {}
171 172
    snf_nics = {}
172 173

  
......
193 194
#
194 195
# Nics
195 196
#
196
def get_nics_from_ganeti():
197
def get_nics_from_ganeti(backend=None):
197 198
    """Get network interfaces for each ganeti instance.
198 199

  
199 200
    """
200
    instances = get_ganeti_instances(bulk=True)
201
    instances = get_ganeti_instances(backend=backend, bulk=True)
201 202
    prefix = settings.BACKEND_PREFIX_ID
202 203

  
203 204
    snf_instances_nics = {}
......
232 233
    return nics
233 234

  
234 235

  
235
def get_nics_from_db():
236
def get_nics_from_db(backend=None):
236 237
    """Get network interfaces for each vm in DB.
237 238

  
238 239
    """
239
    instances = VirtualMachine.objects.filter(deleted=False)
240
    backends = get_backends(backend)
241
    instances = VirtualMachine.objects.filter(deleted=False,
242
                                              backend__in=backends)
240 243
    instances_nics = {}
241 244
    for instance in instances:
242 245
        nics = {}

Also available in: Unified diff