storagedriver.py 2.92 KB
Newer Older
1
from disk import Disk
Guba Sándor committed
2
from storagecelery import celery
3 4
import os
from os import path, unlink, statvfs, listdir
5
from celery.contrib.abortable import AbortableTask
6 7 8 9 10
import logging

logger = logging.getLogger(__name__)

trash_directory = "trash"
11

Bach Dániel committed
12

13
@celery.task()
14
def list(dir):
Dudás Ádám committed
15
    return [d.get_desc() for d in Disk.list(dir)]
Bach Dániel committed
16 17


18
@celery.task()
19 20 21 22 23 24
def list_files(datastore):
    return [l for l in listdir(datastore) if
            path.isfile(path.join(datastore, l))]


@celery.task()
25
def create(disk_desc):
Dudás Ádám committed
26
    disk = Disk.deserialize(disk_desc)
27
    disk.create()
Bach Dániel committed
28 29


30 31 32 33 34 35
class download(AbortableTask):
    time_limit = 18000  # TODO: calculate proper value it's 5h now

    def run(self, **kwargs):
        disk_desc = kwargs['disk']
        url = kwargs['url']
36
        parent_id = kwargs.get("parent_id", None)
37
        disk = Disk.deserialize(disk_desc)
38
        disk.download(self, url, parent_id)
39
        return {'size': disk.size,
Guba Sándor committed
40 41
                'type': disk.format,
                'checksum': disk.checksum, }
42 43


44
@celery.task()
45 46
def delete(json_data):
    disk = Disk.deserialize(json_data)
47
    disk.delete()
Bach Dániel committed
48 49


50
@celery.task()
51 52
def delete_dump(disk_path):
    if disk_path.endswith(".dump") and path.isfile(disk_path):
53
        unlink(disk_path)
54 55 56


@celery.task()
Guba Sándor committed
57
def snapshot(json_data):
58
    disk = Disk.deserialize(json_data)
Guba Sándor committed
59 60 61
    disk.snapshot()


62 63 64 65 66 67
class merge(AbortableTask):
    time_limit = 18000

    def run(self, **kwargs):
        old_json = kwargs['old_json']
        new_json = kwargs['new_json']
Guba Sándor committed
68
        parent_id = kwargs.get("parent_id", None)
69 70
        disk = Disk.deserialize(old_json)
        new_disk = Disk.deserialize(new_json)
Guba Sándor committed
71
        disk.merge(self, new_disk, parent_id=parent_id)
Guba Sándor committed
72 73 74


@celery.task()
75
def get(json_data):
Guba Sándor committed
76 77
    disk = Disk.get(dir=json_data['dir'], name=json_data['name'])
    return disk.get_desc()
78 79 80


@celery.task()
81 82
def get_storage_stat(path):
    ''' Return free disk space avaliable at path in bytes and percent.'''
83
    s = statvfs(path)
84 85 86 87 88
    all_space = s.f_bsize * s.f_blocks
    free_space = s.f_bavail * s.f_frsize
    free_space_percent = 100.0 * free_space / all_space
    return {'free_space': free_space,
            'free_percent': free_space_percent}
89 90 91


@celery.task
92 93
def exists(path, disk_name):
    return os.path.exists(os.path.join(path, disk_name))
94 95 96


@celery.task
97
def make_free_space(path, deletable_disks, percent=10):
98 99 100 101 102
    ''' Check for free space on datastore.
        If free space is less than the given percent
        removes oldest files to satisfy the given requirement.
    '''
    logger.info("Free space on datastore: %s" %
103 104 105
                get_storage_stat(path).get('free_percent'))
    while get_storage_stat(path).get('free_percent') < percent:
        logger.debug(get_storage_stat(path))
106
        try:
107 108
            f = deletable_disks.pop(0)
            unlink(os.path.join(path, f))
109 110
            logger.info('Image: %s removed.' % f)
        except IndexError:
111
            raise Exception("There is not deletable disk.")
112
    return True