root / snf-cyclades-app / synnefo / quotas / util.py @ d05e5324
History | View | Annotate | Download (4.9 kB)
1 |
# Copyright 2012, 2013 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, Q |
35 |
|
36 |
from synnefo.db.models import VirtualMachine, Network, IPAddress, Volume |
37 |
from synnefo.quotas import Quotaholder |
38 |
|
39 |
|
40 |
def get_db_holdings(user=None): |
41 |
"""Get holdings from Cyclades DB."""
|
42 |
holdings = {} |
43 |
|
44 |
vms = VirtualMachine.objects.filter(deleted=False)
|
45 |
networks = Network.objects.filter(deleted=False)
|
46 |
floating_ips = IPAddress.objects.filter(deleted=False, floating_ip=True) |
47 |
volumes = Volume.objects.filter(deleted=False)
|
48 |
|
49 |
if user is not None: |
50 |
vms = vms.filter(userid=user) |
51 |
networks = networks.filter(userid=user) |
52 |
floating_ips = floating_ips.filter(userid=user) |
53 |
|
54 |
# Get resources related with VMs
|
55 |
vm_resources = vms.values("userid")\
|
56 |
.annotate(num=Count("id"),
|
57 |
total_ram=Sum("flavor__ram"),
|
58 |
total_cpu=Sum("flavor__cpu"))
|
59 |
vm_active_resources = \ |
60 |
vms.values("userid")\
|
61 |
.filter(Q(operstate="STARTED") | Q(operstate="BUILD") | |
62 |
Q(operstate="ERROR"))\
|
63 |
.annotate(ram=Sum("flavor__ram"),
|
64 |
cpu=Sum("flavor__cpu"))
|
65 |
|
66 |
for vm_res in vm_resources.iterator(): |
67 |
user = vm_res['userid']
|
68 |
res = {"cyclades.vm": vm_res["num"], |
69 |
"cyclades.total_cpu": vm_res["total_cpu"], |
70 |
"cyclades.total_ram": vm_res["total_ram"] << 20} |
71 |
holdings[user] = res |
72 |
|
73 |
for vm_res in vm_active_resources.iterator(): |
74 |
user = vm_res['userid']
|
75 |
holdings[user]["cyclades.cpu"] = vm_res["cpu"] |
76 |
holdings[user]["cyclades.ram"] = vm_res["ram"] << 20 |
77 |
|
78 |
# Get disk resource
|
79 |
disk_resources = volumes.values("userid").annotate(Sum("size")) |
80 |
for disk_res in disk_resources.iterator(): |
81 |
user = disk_res["userid"]
|
82 |
holdings.setdefault(user, {}) |
83 |
holdings[user]["cyclades.disk"] = disk_res["size__sum"] << 30 |
84 |
|
85 |
# Get resources related with networks
|
86 |
net_resources = networks.values("userid")\
|
87 |
.annotate(num=Count("id"))
|
88 |
for net_res in net_resources.iterator(): |
89 |
user = net_res['userid']
|
90 |
holdings.setdefault(user, {}) |
91 |
holdings[user]["cyclades.network.private"] = net_res["num"] |
92 |
|
93 |
floating_ips_resources = floating_ips.values("userid")\
|
94 |
.annotate(num=Count("id"))
|
95 |
for floating_ip_res in floating_ips_resources.iterator(): |
96 |
user = floating_ip_res["userid"]
|
97 |
holdings.setdefault(user, {}) |
98 |
holdings[user]["cyclades.floating_ip"] = floating_ip_res["num"] |
99 |
|
100 |
return holdings
|
101 |
|
102 |
|
103 |
def get_quotaholder_holdings(user=None): |
104 |
"""Get quotas from Quotaholder for all Cyclades resources.
|
105 |
|
106 |
Returns quotas for all users, unless a single user is specified.
|
107 |
"""
|
108 |
qh = Quotaholder.get() |
109 |
return qh.service_get_quotas(user)
|
110 |
|
111 |
|
112 |
def get_qh_users_holdings(users=None): |
113 |
qh = Quotaholder.get() |
114 |
if users is None or len(users) != 1: |
115 |
req = None
|
116 |
else:
|
117 |
req = users[0]
|
118 |
quotas = qh.service_get_quotas(req) |
119 |
|
120 |
if users is None: |
121 |
return quotas
|
122 |
|
123 |
qs = {} |
124 |
for user in users: |
125 |
try:
|
126 |
qs[user] = quotas[user] |
127 |
except KeyError: |
128 |
pass
|
129 |
return qs
|
130 |
|
131 |
|
132 |
def transform_quotas(quotas): |
133 |
d = {} |
134 |
for resource, counters in quotas.iteritems(): |
135 |
used = counters['usage']
|
136 |
limit = counters['limit']
|
137 |
pending = counters['pending']
|
138 |
d[resource] = (used, limit, pending) |
139 |
return d
|