reconcile-resources-cyclades.py 5.82 KB
Newer Older
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from django.core.management.base import BaseCommand
from optparse import make_option

37 38

from synnefo import quotas
39 40
from synnefo.quotas.util import (get_db_holdings, get_quotaholder_holdings,
                                 transform_quotas)
41
from synnefo.webproject.management.utils import pprint_table
42
from synnefo.settings import CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN
43 44 45


class Command(BaseCommand):
46
    help = """Reconcile resource usage of Astakos with Cyclades DB.
47

48
    Detect unsynchronized usage between Astakos and Cyclades DB resources and
49
    synchronize them if specified so.
50 51

    """
52 53 54
    option_list = BaseCommand.option_list + (
        make_option("--userid", dest="userid",
                    default=None,
55 56 57 58 59 60 61 62 63 64
                    help="Reconcile resources only for this user"),
        make_option("--fix", dest="fix",
                    default=False,
                    action="store_true",
                    help="Synchronize Astakos quotas with Cyclades DB."),
        make_option("--force",
                    default=False,
                    action="store_true",
                    help="Override Astakos quotas. Force Astakos to impose"
                         " the Cyclades quota, independently of their value.")
65 66 67 68 69 70
    )

    def handle(self, *args, **options):
        write = self.stdout.write
        userid = options['userid']

71 72 73
        # Get holdings from Cyclades DB
        db_holdings = get_db_holdings(userid)
        # Get holdings from QuotaHolder
74
        qh_holdings = get_quotaholder_holdings(userid)
75

76 77 78 79
        users = set(db_holdings.keys())
        users.update(qh_holdings.keys())
        # Remove 'None' user
        users.discard(None)
80

81 82 83 84
        if userid and userid not in users:
            write("User '%s' does not exist in Quotaholder!", userid)
            return

85 86
        pending_exists = False
        unknown_user_exists = False
87 88
        unsynced = []
        for user in users:
89
            db = db_holdings.get(user, {})
90 91 92 93 94
            try:
                qh_all = qh_holdings[user]
            except KeyError:
                write("User '%s' does not exist in Quotaholder!\n" %
                      user)
95
                unknown_user_exists = True
96 97
                continue

98
            # Assuming only one source
99
            qh = qh_all.get(quotas.DEFAULT_SOURCE, {})
100
            qh = transform_quotas(qh)
101 102
            for resource in quotas.RESOURCES:
                db_value = db.pop(resource, 0)
103 104 105 106 107 108
                try:
                    qh_value, _, qh_pending = qh[resource]
                except KeyError:
                    write("Resource '%s' does not exist in Quotaholder"
                          " for user '%s'!\n" % (resource, user))
                    continue
109 110 111
                if qh_pending:
                    write("Pending commission. User '%s', resource '%s'.\n" %
                          (user, resource))
112
                    pending_exists = True
113
                    continue
114 115
                if db_value != qh_value:
                    data = (user, resource, db_value, qh_value)
116 117
                    unsynced.append(data)

118
        headers = ("User", "Resource", "Database", "Quotaholder")
119 120
        if unsynced:
            pprint_table(self.stderr, unsynced, headers)
121 122 123 124 125
            if options["fix"]:
                qh = quotas.Quotaholder.get()
                request = {}
                request["force"] = options["force"]
                request["auto_accept"] = True
126
                request["name"] = "RECONCILE"
127
                request["provisions"] = map(create_provision, unsynced)
128 129 130 131 132 133
                try:
                    qh.issue_commission(ASTAKOS_TOKEN, request)
                except quotas.QuotaLimit:
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return
134 135 136 137 138 139
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. Run 'snf-manage"
                  " reconcile-commissions-cyclades'\n")
        elif not (unsynced or unknown_user_exists):
140 141 142 143 144 145 146 147 148
            write("Everything in sync.\n")


def create_provision(provision_info):
    user, resource, db_value, qh_value = provision_info
    return {"holder": user,
            "source": quotas.DEFAULT_SOURCE,
            "resource": resource,
            "quantity": db_value - qh_value}