Statistics
| Branch: | Tag: | Revision:

root / logic / management / commands / reconcile.py @ cbadad34

History | View | Annotate | Download (3.3 kB)

1
# Copyright 2011 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
# Reconcile VM state - Management Script
31

    
32

    
33
from django.core.management.base import NoArgsCommand
34
from synnefo.db.models import VirtualMachine
35
from django.conf import settings
36
from datetime import datetime, timedelta
37

    
38
from amqplib import client_0_8 as amqp
39

    
40
import time
41
import socket
42
import json
43

    
44
class Command(NoArgsCommand):
45
    help = 'Reconcile VM status with the backend'
46
    chan = None
47
    def open_channel(self):
48
        conn = None
49
        while conn == None:
50
            try:
51
                conn = amqp.Connection( host=settings.RABBIT_HOST,
52
                     userid=settings.RABBIT_USERNAME,
53
                     password=settings.RABBIT_PASSWORD,
54
                     virtual_host=settings.RABBIT_VHOST)
55
            except socket.error:
56
                time.sleep(1)
57
                pass
58

    
59
        self.chan = conn.channel()
60

    
61
    def handle_noargs(self, **options):
62

    
63
        now = datetime.now()
64
        last_update = timedelta(minutes = 30)
65
        not_updated = VirtualMachine.objects.filter(updated__lte = (now - last_update))
66
        all =  VirtualMachine.objects.all()
67

    
68
        to_update = all.count() / settings.RECONCILIATION_MIN
69

    
70
        vm_ids = map(lambda x: x.name,  VirtualMachine.objects.all()[:to_update])
71
        sent = False
72

    
73
        for vmid in vm_ids :
74
            while sent is False:
75
                try:
76
                    msg = dict(type = "reconciliate", vmid = vmid)
77
                    self.chan.basic_publish(json.dumps(msg),
78
                            exchange=settings.EXCHANGE_CRON,
79
                            routing_key="reconciliation.%s"%vmid)
80
                    sent = True
81
                except socket.error:
82
                    self.chan = self.open_channel()
83
                except Exception:
84
                    raise
85

    
86

    
87
        print "All: %d, To update: %d, Triggered update for: %s" % (all.count(), not_updated.count(), vm_ids)