Commit dca66ad0 authored by Christos Stavrakakis's avatar Christos Stavrakakis

Add backend-id option to reconcile-servers

Make reconcile-servers perform reconciliation only for the specified
backend.
parent 56893aa2
......@@ -642,6 +642,8 @@ def get_ganeti_jobs(backend=None, bulk=False):
def get_backends(backend=None):
if backend:
if backend.offline:
return []
return [backend]
return Backend.objects.filter(offline=False)
......
......@@ -43,7 +43,9 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import VirtualMachine, Network, pooled_rapi_client
from synnefo.logic import reconciliation, backend, utils
from synnefo.logic import reconciliation, utils
from synnefo.logic import backend as backend_mod
from synnefo.management.common import get_backend
class Command(BaseCommand):
......@@ -85,7 +87,10 @@ class Command(BaseCommand):
dest='fix_unsynced_nics', default=False,
help='Fix unsynced nics between DB and Ganeti'),
make_option('--fix-all', action='store_true', dest='fix_all',
default=False, help='Enable all --fix-* arguments'))
default=False, help='Enable all --fix-* arguments'),
make_option('--backend-id', default=None, dest='backend-id',
help='Reconcilie VMs only for this backend'),
)
def _process_args(self, options):
keys_detect = [k for k in options.keys() if k.startswith('detect_')]
......@@ -111,11 +116,13 @@ class Command(BaseCommand):
def handle(self, **options):
verbosity = int(options['verbosity'])
self._process_args(options)
backend_id = options['backend-id']
backend = get_backend(backend_id) if backend_id else None
D = reconciliation.get_servers_from_db()
G, GNics = reconciliation.get_instances_from_ganeti()
D = reconciliation.get_servers_from_db(backend)
G, GNics = reconciliation.get_instances_from_ganeti(backend)
DBNics = reconciliation.get_nics_from_db()
DBNics = reconciliation.get_nics_from_db(backend)
#
# Detect problems
......@@ -190,7 +197,7 @@ class Command(BaseCommand):
"servers in the DB:" % len(stale)
for vm in VirtualMachine.objects.filter(pk__in=stale):
event_time = datetime.datetime.now()
backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
opcode='OP_INSTANCE_REMOVE', status='success',
logmsg='Reconciliation: simulated Ganeti event')
print >> sys.stderr, " ...done"
......@@ -216,7 +223,7 @@ class Command(BaseCommand):
opcode = "OP_INSTANCE_REBOOT" if ganeti_up \
else "OP_INSTANCE_SHUTDOWN"
event_time = datetime.datetime.now()
backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
opcode=opcode, status='success',
logmsg='Reconciliation: simulated Ganeti event')
print >> sys.stderr, " ...done"
......@@ -227,7 +234,7 @@ class Command(BaseCommand):
for id in build_errors:
vm = VirtualMachine.objects.get(pk=id)
event_time = datetime.datetime.now()
backend.process_op_status(vm=vm, etime=event_time, jobid=-0,
backend_mod.process_op_status(vm=vm, etime=event_time, jobid=-0,
opcode="OP_INSTANCE_CREATE", status='error',
logmsg='Reconciliation: simulated Ganeti event')
print >> sys.stderr, " ...done"
......@@ -260,7 +267,7 @@ class Command(BaseCommand):
print 'Network of nic %d of vm %s is None. ' \
'Can not reconcile' % (i, vm.backend_vm_id)
event_time = datetime.datetime.now()
backend.process_net_status(vm=vm, etime=event_time, nics=final_nics)
backend_mod.process_net_status(vm=vm, etime=event_time, nics=final_nics)
print >> sys.stderr, " ...done"
......
......@@ -73,7 +73,7 @@ from datetime import datetime, timedelta
from synnefo.db.models import (VirtualMachine, pooled_rapi_client)
from synnefo.logic.rapi import GanetiApiError
from synnefo.logic.backend import get_ganeti_instances
from synnefo.logic.backend import get_ganeti_instances, get_backends
from synnefo.logic import utils
......@@ -160,13 +160,14 @@ def instances_with_build_errors(D, G):
return failed
def get_servers_from_db():
vms = VirtualMachine.objects.filter(deleted=False, backend__offline=False)
def get_servers_from_db(backend=None):
backends = get_backends(backend)
vms = VirtualMachine.objects.filter(deleted=False, backend__in=backends)
return dict(map(lambda x: (x.id, x.operstate), vms))
def get_instances_from_ganeti():
ganeti_instances = get_ganeti_instances(bulk=True)
def get_instances_from_ganeti(backend=None):
ganeti_instances = get_ganeti_instances(backend=backend, bulk=True)
snf_instances = {}
snf_nics = {}
......@@ -193,11 +194,11 @@ def get_instances_from_ganeti():
#
# Nics
#
def get_nics_from_ganeti():
def get_nics_from_ganeti(backend=None):
"""Get network interfaces for each ganeti instance.
"""
instances = get_ganeti_instances(bulk=True)
instances = get_ganeti_instances(backend=backend, bulk=True)
prefix = settings.BACKEND_PREFIX_ID
snf_instances_nics = {}
......@@ -232,11 +233,13 @@ def get_nics_from_instance(i):
return nics
def get_nics_from_db():
def get_nics_from_db(backend=None):
"""Get network interfaces for each vm in DB.
"""
instances = VirtualMachine.objects.filter(deleted=False)
backends = get_backends(backend)
instances = VirtualMachine.objects.filter(deleted=False,
backend__in=backends)
instances_nics = {}
for instance in instances:
nics = {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment