Statistics
| Branch: | Tag: | Revision:

root / logic / management / commands / reconciliate.py @ 8007ba7b

History | View | Annotate | Download (1.9 kB)

1
#
2
# Reconciliate VM state - Management Script
3
#
4
# Copyright 2010 Greek Research and Technology Network
5
#
6

    
7
from django.core.management.base import NoArgsCommand
8
from synnefo.db.models import VirtualMachine
9
from django.conf import settings
10
from datetime import datetime, timedelta
11

    
12
from amqplib import client_0_8 as amqp
13

    
14
import time
15
import socket
16
import json
17

    
18
class Command(NoArgsCommand):
19
    help = 'Reconciliate VM status with the backend'
20

    
21
    def open_channel(self):
22
        conn = None
23
        while conn == None:
24
            try:
25
                conn = amqp.Connection( host=settings.RABBIT_HOST,
26
                     userid=settings.RABBIT_USERNAME,
27
                     password=settings.RABBIT_PASSWORD,
28
                     virtual_host=settings.RABBIT_VHOST)
29
            except socket.error:
30
                time.sleep(1)
31
                pass
32

    
33
        return conn.channel()
34

    
35
    def handle_noargs(self, **options):
36

    
37
        now = datetime.now()
38
        last_update = timedelta(minutes = 30)
39
        not_updated = VirtualMachine.objects.filter(updated__lte = (now - last_update))
40
        all =  VirtualMachine.objects.all()
41

    
42
        to_update = all.count() / settings.RECONCILIATION_MIN
43

    
44
        vm_ids = map(lambda x: x.vm_id,  all.filter()) #TODO: Fix filtering
45
        sent = False
46

    
47
        for vmid in vm_ids :
48
            while sent is False:
49
                try:
50
                    msg = dict(type = "reconciliate", vmid = vmid)
51
                    self.chan.basic_publish(json.dumps(msg),
52
                            exchange=settings.EXCHANGE_CRON,
53
                            routing_key="reconciliation.%s", vmid)
54
                    sent = True
55
                except socket.error:
56
                    self.chan = self.open_channel()
57
                except Exception:
58
                    raise
59

    
60

    
61
        print "All:%d, Not Updated:%d, Triggered update for:%d" % (all.count(), not_updated.count(), vm_ids)