Revision 1da50fe3 snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py

b/snf-cyclades-app/synnefo/logic/management/commands/backend-update-status.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or without
4 4
# modification, are permitted provided that the following conditions
......
27 27
# those of the authors and should not be interpreted as representing official
28 28
# policies, either expressed or implied, of GRNET S.A.
29 29
#
30

  
31
from optparse import make_option
32 30
from django.core.management.base import BaseCommand
33
from synnefo.management.common import get_backend
34

  
35
from django.conf import settings
36
import datetime
37 31

  
38 32
from synnefo.db.models import Backend
39
from synnefo.logic.backend import update_resources
40

  
41

  
42
class Command(BaseCommand):
43
    can_import_settings = True
33
from synnefo.logic import backend as backend_mod
44 34

  
45
    help = "Update backend statistics, which are used for instance allocation."
46
    output_transaction = True  # The management command runs inside
47
                               # an SQL transaction
48
    option_list = BaseCommand.option_list + (
49
        make_option('--backend-id', dest='backend_id',
50
                    help="Update statistics of only this backend"),
51
        make_option('--older-than', dest='older_than', metavar="MINUTES",
52
                    help="Update only backends that have not been updated for\
53
                    MINUTES. Set to 0 to force update."),
54
        make_option('--include-drained', dest='drained',
55
                    default=False,
56
                    action='store_true',
57
                    help="Also update statistics of drained backends")
58
    )
59 35

  
60
    def handle(self, **options):
36
HELP_MSG = """Query Ganeti backends and update the status of backend in DB.
61 37

  
62
        if options['backend_id']:
63
            backends = [get_backend(options['backend_id'])]
64
        else:
65
            backends = Backend.objects.filter(offline=False)
66
            if not options['drained']:
67
                backends = backends.filter(drained=False)
38
This command updates:
39
    * the list of the enabled disk-templates
40
    * the available resources (disk, memory, CPUs)
41
"""
68 42

  
69
        now = datetime.datetime.now()
70
        if options['older_than'] is not None:
71
            minutes = int(options['older_than'])
72
        else:
73
            minutes = settings.BACKEND_REFRESH_MIN
74 43

  
75
        delta = datetime.timedelta(minutes=minutes)
44
class Command(BaseCommand):
45
    help = HELP_MSG
76 46

  
77
        for b in backends:
78
            if now > b.updated + delta:
79
                update_resources(b)
80
                print 'Successfully updated backend with id: %d' % b.id
81
            else:
82
                print 'Backend %d does not need update' % b.id
47
    def handle(self, **options):
48
        for backend in Backend.objects.filter(offline=False):
49
            backend_mod.update_backend_disk_templates(backend)
50
            backend_mod.update_backend_resources(backend)
51
            self.stdout.write("Successfully updated backend '%s'\n" % backend)

Also available in: Unified diff