Revision 76a429fb logic/management/commands/reconcile.py
b/logic/management/commands/reconcile.py | ||
---|---|---|
29 | 29 |
# |
30 | 30 |
# Reconcile VM state - Management Script |
31 | 31 |
|
32 |
|
|
33 |
from django.core.management.base import NoArgsCommand |
|
34 | 32 |
from synnefo.db.models import VirtualMachine |
35 | 33 |
from django.conf import settings |
36 | 34 |
from datetime import datetime, timedelta |
35 |
from optparse import make_option |
|
36 |
from django.core.management.base import BaseCommand |
|
37 | 37 |
|
38 | 38 |
from synnefo.logic import amqp_connection |
39 | 39 |
from synnefo.logic.amqp_connection import AMQPError |
... | ... | |
41 | 41 |
import json |
42 | 42 |
import sys |
43 | 43 |
|
44 |
class Command(NoArgsCommand):
|
|
44 |
class Command(BaseCommand):
|
|
45 | 45 |
help = 'Reconcile VM status with the backend' |
46 | 46 |
|
47 |
def handle_noargs(self, **options): |
|
47 |
option_list = BaseCommand.option_list + ( |
|
48 |
make_option('--all', action='store_true', dest='all_vms', default=False, |
|
49 |
help='Run the reconciliation function for all VMs, now'), |
|
50 |
make_option('--interval', action='store', dest='interval', default=1, |
|
51 |
help='Interval in minutes between reconciliations'), |
|
52 |
) |
|
53 |
|
|
54 |
def handle(self, all_vms = False, interval = 1, **options): |
|
55 |
all = VirtualMachine.objects.all() |
|
48 | 56 |
|
49 |
now = datetime.now() |
|
50 |
last_update = timedelta(minutes = settings.RECONCILIATION_MIN) |
|
51 |
not_updated = VirtualMachine.objects \ |
|
57 |
if not all_vms: |
|
58 |
now = datetime.now() |
|
59 |
last_update = timedelta(minutes = settings.RECONCILIATION_MIN) |
|
60 |
not_updated = VirtualMachine.objects \ |
|
52 | 61 |
.filter(deleted = False) \ |
53 | 62 |
.filter(suspended = False) \ |
54 | 63 |
.filter(updated__lte = (now - last_update)) |
55 |
all = VirtualMachine.objects.all() |
|
56 | 64 |
|
57 |
to_update = all.count() / settings.RECONCILIATION_MIN |
|
65 |
to_update = ((all.count() / settings.RECONCILIATION_MIN) * interval) |
|
66 |
else: |
|
67 |
to_update = all.count() |
|
68 |
not_updated = all |
|
69 |
|
|
58 | 70 |
vm_ids = map(lambda x: x.id, not_updated[:to_update]) |
59 | 71 |
|
60 | 72 |
for vmid in vm_ids : |
Also available in: Unified diff