Revision d05e5324 snf-cyclades-app/synnefo/quotas/__init__.py
b/snf-cyclades-app/synnefo/quotas/__init__.py | ||
---|---|---|
29 | 29 |
|
30 | 30 |
from django.utils import simplejson as json |
31 | 31 |
from django.db import transaction |
32 |
from django.db.models import Sum |
|
32 | 33 |
|
33 | 34 |
from snf_django.lib.api import faults |
34 | 35 |
from synnefo.db.models import (QuotaHolderSerial, VirtualMachine, Network, |
35 |
IPAddress) |
|
36 |
IPAddress, Volume)
|
|
36 | 37 |
|
37 | 38 |
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN, |
38 | 39 |
ASTAKOS_AUTH_URL) |
39 | 40 |
from astakosclient import AstakosClient |
40 | 41 |
from astakosclient import errors |
42 |
from synnefo.logic.utils import id_from_disk_name |
|
41 | 43 |
|
42 | 44 |
import logging |
43 | 45 |
log = logging.getLogger(__name__) |
... | ... | |
300 | 302 |
flavor = resource.flavor |
301 | 303 |
resources = {"cyclades.vm": 1, |
302 | 304 |
"cyclades.total_cpu": flavor.cpu, |
303 |
"cyclades.disk": 1073741824 * flavor.disk, |
|
304 |
"cyclades.total_ram": 1048576 * flavor.ram} |
|
305 |
"cyclades.total_ram": flavor.ram << 20} |
|
305 | 306 |
online_resources = {"cyclades.cpu": flavor.cpu, |
306 |
"cyclades.ram": 1048576 * flavor.ram}
|
|
307 |
"cyclades.ram": flavor.ram << 20}
|
|
307 | 308 |
if action == "BUILD": |
309 |
new_volumes = resource.volumes.filter(status="CREATING") |
|
310 |
new_volumes_size = new_volumes.aggregate(Sum("size"))["size__sum"] |
|
311 |
resources["cyclades.disk"] = new_volumes_size << 30 |
|
308 | 312 |
resources.update(online_resources) |
309 | 313 |
return resources |
310 | 314 |
if action == "START": |
... | ... | |
323 | 327 |
else: |
324 | 328 |
return None |
325 | 329 |
elif action == "DESTROY": |
330 |
volumes = resource.volumes.filter(deleted=False) |
|
331 |
volumes_size = volumes.aggregate(Sum("size"))["size__sum"] |
|
332 |
resources["cyclades.disk"] = volumes_size << 30 |
|
333 |
resources.update(online_resources) |
|
326 | 334 |
if resource.operstate in ["STARTED", "BUILD", "ERROR"]: |
327 | 335 |
resources.update(online_resources) |
328 | 336 |
return reverse_quantities(resources) |
... | ... | |
331 | 339 |
cpu = beparams.get("vcpus", flavor.cpu) |
332 | 340 |
ram = beparams.get("maxmem", flavor.ram) |
333 | 341 |
return {"cyclades.total_cpu": cpu - flavor.cpu, |
334 |
"cyclades.total_ram": 1048576 * (ram - flavor.ram)} |
|
342 |
"cyclades.total_ram": (ram - flavor.ram) << 20} |
|
343 |
elif action in ["ATTACH_VOLUME", "DETACH_VOLUME"]: |
|
344 |
if action_fields is not None: |
|
345 |
volumes_changes = action_fields.get("disks") |
|
346 |
if volumes_changes is not None: |
|
347 |
size_delta = get_volumes_size_delta(volumes_changes) |
|
348 |
if size_delta: |
|
349 |
return {"cyclades.disk": size_delta << 30} |
|
335 | 350 |
else: |
336 | 351 |
#["CONNECT", "DISCONNECT", "SET_FIREWALL_PROFILE"]: |
337 | 352 |
return None |
... | ... | |
350 | 365 |
return reverse_quantities(resources) |
351 | 366 |
else: |
352 | 367 |
return None |
368 |
elif isinstance(resource, Volume): |
|
369 |
size = resource.size |
|
370 |
resources = {"cyclades.disk": size << 30} |
|
371 |
if resource.status == "CREATING" and action == "BUILD": |
|
372 |
return resources |
|
373 |
elif action == "DESTROY": |
|
374 |
reverse_quantities(resources) |
|
375 |
else: |
|
376 |
return None |
|
377 |
|
|
378 |
|
|
379 |
def get_volumes_size_delta(volumes_changes): |
|
380 |
"""Compute the total change in the size of volumes""" |
|
381 |
size_delta = 0 |
|
382 |
for vchange in volumes_changes: |
|
383 |
action, db_volume, info = vchange |
|
384 |
if action == "add": |
|
385 |
size_delta += int(db_volume.size) |
|
386 |
elif action == "remove": |
|
387 |
size_delta -= int(db_volume.size) |
|
388 |
elif action == "modify": |
|
389 |
size_delta += info.get("size_delta", 0) |
|
390 |
else: |
|
391 |
raise ValueError("Unknwon volume action '%s'" % action) |
|
392 |
return size_delta |
|
353 | 393 |
|
354 | 394 |
|
355 | 395 |
def reverse_quantities(resources): |
Also available in: Unified diff