Statistics
| Branch: | Tag: | Revision:

root / logic / management / commands / reconcile.py @ 76a429fb

History | View | Annotate | Download (3.5 kB)

1 cb409cfd Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
2 8007ba7b Georgios Gousios
#
3 cb409cfd Georgios Gousios
# Redistribution and use in source and binary forms, with or without
4 cb409cfd Georgios Gousios
# modification, are permitted provided that the following conditions
5 cb409cfd Georgios Gousios
# are met:
6 8007ba7b Georgios Gousios
#
7 cb409cfd Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
8 cb409cfd Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
9 8007ba7b Georgios Gousios
#
10 cb409cfd Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
11 cb409cfd Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
12 cb409cfd Georgios Gousios
#     documentation and/or other materials provided with the distribution.
13 cb409cfd Georgios Gousios
#
14 cb409cfd Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 cb409cfd Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 cb409cfd Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 cb409cfd Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 cb409cfd Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 cb409cfd Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 cb409cfd Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 cb409cfd Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 cb409cfd Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 cb409cfd Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 cb409cfd Georgios Gousios
# SUCH DAMAGE.
25 cb409cfd Georgios Gousios
#
26 cb409cfd Georgios Gousios
# The views and conclusions contained in the software and documentation are
27 cb409cfd Georgios Gousios
# those of the authors and should not be interpreted as representing official
28 cb409cfd Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
29 cb409cfd Georgios Gousios
#
30 cb409cfd Georgios Gousios
# Reconcile VM state - Management Script
31 cb409cfd Georgios Gousios
32 8007ba7b Georgios Gousios
from synnefo.db.models import VirtualMachine
33 8007ba7b Georgios Gousios
from django.conf import settings
34 8007ba7b Georgios Gousios
from datetime import datetime, timedelta
35 76a429fb Georgios Gousios
from optparse import make_option
36 76a429fb Georgios Gousios
from django.core.management.base import BaseCommand
37 8007ba7b Georgios Gousios
38 4331aa1f Georgios Gousios
from synnefo.logic import amqp_connection
39 4331aa1f Georgios Gousios
from synnefo.logic.amqp_connection import AMQPError
40 8007ba7b Georgios Gousios
41 8007ba7b Georgios Gousios
import json
42 4331aa1f Georgios Gousios
import sys
43 8007ba7b Georgios Gousios
44 76a429fb Georgios Gousios
class Command(BaseCommand):
45 cb409cfd Georgios Gousios
    help = 'Reconcile VM status with the backend'
46 8007ba7b Georgios Gousios
47 76a429fb Georgios Gousios
    option_list = BaseCommand.option_list +  (
48 76a429fb Georgios Gousios
         make_option('--all', action='store_true', dest='all_vms', default=False,
49 76a429fb Georgios Gousios
                     help='Run the reconciliation function for all VMs, now'),
50 76a429fb Georgios Gousios
         make_option('--interval', action='store', dest='interval', default=1,
51 76a429fb Georgios Gousios
                     help='Interval in minutes between reconciliations'),
52 76a429fb Georgios Gousios
    )
53 76a429fb Georgios Gousios
54 76a429fb Georgios Gousios
    def handle(self, all_vms = False, interval = 1, **options):
55 76a429fb Georgios Gousios
        all =  VirtualMachine.objects.all()
56 8007ba7b Georgios Gousios
57 76a429fb Georgios Gousios
        if not all_vms:
58 76a429fb Georgios Gousios
            now = datetime.now()
59 76a429fb Georgios Gousios
            last_update = timedelta(minutes = settings.RECONCILIATION_MIN)
60 76a429fb Georgios Gousios
            not_updated = VirtualMachine.objects \
61 65bf328b Georgios Gousios
                                    .filter(deleted = False) \
62 65bf328b Georgios Gousios
                                    .filter(suspended = False) \
63 65bf328b Georgios Gousios
                                    .filter(updated__lte = (now - last_update))
64 8007ba7b Georgios Gousios
65 76a429fb Georgios Gousios
            to_update = ((all.count() / settings.RECONCILIATION_MIN) * interval)
66 76a429fb Georgios Gousios
        else:
67 76a429fb Georgios Gousios
            to_update = all.count()
68 76a429fb Georgios Gousios
            not_updated = all
69 76a429fb Georgios Gousios
70 f4e15f24 Georgios Gousios
        vm_ids = map(lambda x: x.id, not_updated[:to_update])
71 4331aa1f Georgios Gousios
72 8007ba7b Georgios Gousios
        for vmid in vm_ids :
73 4331aa1f Georgios Gousios
            msg = dict(type = "reconcile", vmid = vmid)
74 4331aa1f Georgios Gousios
            try:
75 4331aa1f Georgios Gousios
                amqp_connection.send(json.dumps(msg), settings.EXCHANGE_CRON,
76 4331aa1f Georgios Gousios
                                 "reconciliation.%s"%vmid)
77 4331aa1f Georgios Gousios
            except AMQPError as e:
78 4331aa1f Georgios Gousios
                print >> sys.stderr, 'Error sending reconciliation request: %s' % e
79 4331aa1f Georgios Gousios
                raise
80 8007ba7b Georgios Gousios
81 65bf328b Georgios Gousios
        print "All: %d, To update: %d, Triggered update for: %s" % \
82 65bf328b Georgios Gousios
              (all.count(), not_updated.count(), vm_ids)