Revision 600a3d4d

b/snf-cyclades-app/synnefo/app_settings/__init__.py
9 9
    'synnefo.helpdesk',
10 10
    'synnefo.ui.userdata',
11 11
    'synnefo.helpdesk',
12
    'synnefo.quotas',
12 13
]
13 14

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

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

  
37
from synnefo import quotas
38

  
39

  
40
class Command(BaseCommand):
41
    help = "Reconcile quotas with Quotaholder"
42
    output_transaction = True
43
    option_list = BaseCommand.option_list + (
44
        make_option("--fix", dest="fix",
45
                    action='store_true',
46
                    default=False,
47
                    help="Fix pending commissions"
48
                    ),
49
    )
50

  
51
    def handle(self, *args, **options):
52
        fix = options['fix']
53

  
54
        accepted, rejected = quotas.resolve_pending_commissions()
55

  
56
        if accepted:
57
            self.stdout.write("Pending accepted commissions:\n %s\n" \
58
                              % list_to_string(accepted))
59

  
60
        if rejected:
61
            self.stdout.write("Pending rejected commissions:\n %s\n" \
62
                              % list_to_string(rejected))
63

  
64
        if fix and (accepted or rejected):
65
            self.stdout.write("Fixing pending commissions..\n")
66
            quotas.accept_commissions(accepted)
67
            quotas.reject_commissions(rejected)
68

  
69

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

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

  
37
from synnefo.quotas import get_quota_holder
38
from synnefo.quotas.util import get_db_holdings
39

  
40

  
41
class Command(BaseCommand):
42
    help = "Reconcile quotas with Quotaholder"
43
    output_transaction = True
44
    option_list = BaseCommand.option_list + (
45
        make_option("--userid", dest="userid",
46
                    default=None,
47
                    help="Verify quotas only for this user"),
48
        make_option("--fix", dest="fix",
49
                    action='store_true',
50
                    default=False,
51
                    help="Fix pending commissions"
52
                    ),
53
        make_option("--dry-run", dest="dry_run",
54
                    action='store_true',
55
                    default=False),
56
    )
57

  
58
    def handle(self, *args, **options):
59
        userid = options['userid']
60

  
61
        users = [userid] if userid else None
62
        # Get info from DB
63
        db_holdings = get_db_holdings(users)
64

  
65
        # Create commissions
66
        with get_quota_holder() as qh:
67
            for user, resources in db_holdings.items():
68
                if not user:
69
                    continue
70
                reset_holding = []
71
                for res, val in resources.items():
72
                    reset_holding.append((user, "cyclades." + res, "1", val, 0, 0, 0))
73
                if not options['dry_run']:
74
                    try:
75
                        qh.reset_holding(context={}, reset_holding=reset_holding)
76
                    except Exception as e:
77
                        self.stderr.write("Can not set up holding:%s" % e)
78
                else:
79
                    self.stdout.write("Reseting holding: %s\n" % reset_holding)
b/snf-cyclades-app/synnefo/quotas/management/commands/quotas-verify.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

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

  
37
from synnefo.quotas.util import get_db_holdings, get_quotaholder_holdings
38
from synnefo.management.common import pprint_table
39

  
40

  
41
class Command(BaseCommand):
42
    output_transaction = True
43
    option_list = BaseCommand.option_list + (
44
        make_option("--userid", dest="userid",
45
                    default=None,
46
                    help="Verify quotas only for this user"),
47
    )
48

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

  
53
        users = [userid] if userid else None
54
        # Get info from DB
55
        db_holdings = get_db_holdings(users)
56
        users = db_holdings.keys()
57
        qh_holdings = get_quotaholder_holdings(users)
58
        qh_users = qh_holdings.keys()
59

  
60
        if len(qh_users) < len(users):
61
            for u in set(users) - set(qh_users):
62
                write("Unknown entity: %s\n" % u)
63
                users = qh_users
64

  
65
        headers = ("User", "Resource", "Database", "Quotaholder")
66
        unsynced = []
67
        for user in users:
68
            db = db_holdings[user]
69
            qh = qh_holdings[user]
70
            if not self.verify_resources(user, db.keys(), qh.keys()):
71
                continue
72

  
73
            for res in db.keys():
74
                if db[res] != qh[res]:
75
                    unsynced.append((user, res, str(db[res]), str(qh[res])))
76

  
77
        if unsynced:
78
            pprint_table(self.stderr, unsynced, headers)
79

  
80
    def verify_resources(self, user, db_resources, qh_resources):
81
        write = self.stderr.write
82
        db_res = set(db_resources)
83
        qh_res = set(qh_resources)
84
        if qh_res == db_res:
85
            return True
86
        db_extra = db_res - qh_res
87
        if db_extra:
88
            for res in db_extra:
89
                write("Resource %s exists in DB for %s but not in QH\n"\
90
                      % (res, user))
91
        qh_extra = qh_res - db_res
92
        if qh_extra:
93
            for res in qh_extra:
94
                write("Resource %s exists in QH for %s but not in DB\n"\
95
                      % (res, user))
96
        return False
b/snf-cyclades-app/synnefo/quotas/management/commands/reconcile-quotas.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

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

  
37
from synnefo import quotas
38

  
39

  
40
class Command(BaseCommand):
41
    help = "Reconcile quotas with Quotaholder"
42
    output_transaction = True
43
    option_list = BaseCommand.option_list + (
44
        make_option("--fix", dest="fix",
45
                    action='store_true',
46
                    default=False,
47
                    help="Fix pending commissions"
48
                    ),
49
    )
50

  
51
    def handle(self, *args, **options):
52
        fix = options['fix']
53

  
54
        accepted, rejected = quotas.resolve_pending_commissions()
55

  
56
        if accepted:
57
            self.stdout.write("Pending accepted commissions:\n %s\n" \
58
                              % list_to_string(accepted))
59

  
60
        if rejected:
61
            self.stdout.write("Pending rejected commissions:\n %s\n" \
62
                              % list_to_string(rejected))
63

  
64
        if fix and (accepted or rejected):
65
            self.stdout.write("Fixing pending commissions..\n")
66
            quotas.accept_commissions(accepted)
67
            quotas.reject_commissions(rejected)
68

  
69

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

  
34
from django.db.models import Sum, Count
35

  
36
from synnefo.db.models import VirtualMachine, Network
37
from synnefo.quotas import get_quota_holder
38
from synnefo.lib.quotaholder.api.exception import NoEntityError
39

  
40

  
41
def get_db_holdings(users=None):
42
    """Get holdings from Cyclades DB."""
43
    holdings = {}
44

  
45
    vms = VirtualMachine.objects
46
    networks = Network.objects
47

  
48
    if users:
49
        assert(type(users) is list)
50
        vms = vms.filter(userid__in=users)
51
        networks = networks.filter(userid__in=users)
52

  
53
    # Get resources related with VMs
54
    vm_resources = vms.values("userid").annotate(num=Count("id"),
55
                                                 ram=Sum("flavor__ram"),
56
                                                 cpu=Sum("flavor__cpu"),
57
                                                 disk=Sum("flavor__disk"))
58
    for vm_res in vm_resources:
59
        user = vm_res['userid']
60
        res = {"vm": vm_res["num"],
61
               "cpu": vm_res["cpu"],
62
               "disk": 1073741824 * vm_res["disk"],
63
               "ram": 1048576 * vm_res["ram"]}
64
        holdings[user] = res
65

  
66
    # Get resources related with networks
67
    net_resources = networks.values("userid")\
68
                                    .annotate(num=Count("id"))
69
    for net_res in net_resources:
70
        user = net_res['userid']
71
        if user not in holdings:
72
            holdings[user] = {}
73
        holdings[user]["network.private"] = net_res["num"]
74

  
75
    return holdings
76

  
77

  
78
def get_quotaholder_holdings(users=[]):
79
    """Get holdings from Quotaholder.
80

  
81
    If the entity for the user does not exist in quotaholder, no holding
82
    is returned.
83
    """
84
    holdings = {}
85
    with get_quota_holder() as qh:
86
        for user in users:
87
            try:
88
                (qh_holdings, _) = \
89
                    qh.list_holdings(context={}, list_holdings=[(user, "1")])
90
                if not qh_holdings:
91
                    continue
92
                qh_holdings = qh_holdings[0]
93
                qh_holdings = filter(lambda x: x[1].startswith("cyclades."),
94
                                     qh_holdings)
95
                holdings[user] = dict(map(decode_holding, qh_holdings))
96
            except NoEntityError:
97
                pass
98
    return holdings
99

  
100

  
101
def decode_holding(holding):
102
    entity, resource, imported, exported, returned, released = \
103
            holding
104
    res = resource.replace("cyclades.", "")
105
    return (res, imported - exported + returned - released)

Also available in: Unified diff