reconcile-resources-cyclades.py 5.18 KB
Newer Older
1
# Copyright 2012-2014 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
#
# 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.

34
from datetime import datetime
35 36 37
from django.core.management.base import BaseCommand
from optparse import make_option

38
from synnefo import quotas
39
from synnefo.quotas import util
40
from snf_django.management.utils import pprint_table
41
from snf_django.utils import reconcile
42 43 44


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

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

    """
51 52 53
    option_list = BaseCommand.option_list + (
        make_option("--userid", dest="userid",
                    default=None,
54
                    help="Reconcile resources only for this user"),
55 56
        make_option("--project",
                    help="Reconcile resources only for this project"),
57 58 59 60 61 62 63 64 65
        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.")
66 67 68
    )

    def handle(self, *args, **options):
69
        write = self.stderr.write
70
        userid = options['userid']
71
        project = options["project"]
72

73
        # Get holdings from Cyclades DB
74 75
        db_holdings = util.get_db_holdings(userid, project)
        db_project_holdings = util.get_db_project_holdings(project)
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        # Get holdings from QuotaHolder
        qh_holdings = util.get_qh_users_holdings(
            [userid] if userid is not None else None)
        qh_project_holdings = util.get_qh_project_holdings(
            [project] if project is not None else None)

        unsynced_users, users_pending, users_unknown =\
            reconcile.check_users(self.stderr, quotas.RESOURCES,
                                  db_holdings, qh_holdings)

        unsynced_projects, projects_pending, projects_unknown =\
            reconcile.check_projects(self.stderr, quotas.RESOURCES,
                                     db_project_holdings, qh_project_holdings)
        pending_exists = users_pending or projects_pending
        unknown_exists = users_unknown or projects_unknown

        headers = ("Type", "Holder", "Source", "Resource",
                   "Database", "Quotaholder")
        unsynced = unsynced_users + unsynced_projects
96
        if unsynced:
97
            pprint_table(self.stdout, unsynced, headers)
98 99
            if options["fix"]:
                qh = quotas.Quotaholder.get()
100 101 102 103 104 105 106
                force = options["force"]
                name = ("client: reconcile-resources-cyclades, time: %s"
                        % datetime.now())
                user_provisions = reconcile.create_user_provisions(
                    unsynced_users)
                project_provisions = reconcile.create_project_provisions(
                    unsynced_projects)
107
                try:
108 109 110 111
                    qh.issue_commission_generic(
                        user_provisions, project_provisions,
                        name=name, force=force,
                        auto_accept=True)
112
                except quotas.errors.QuotaLimit:
113 114 115
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return
116 117 118 119 120
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. Run 'snf-manage"
                  " reconcile-commissions-cyclades'\n")
121
        elif not (unsynced or unknown_exists):
122
            write("Everything in sync.\n")