object_storage.py 2.06 KB
Newer Older
1 2
import os
import logging
3

4 5
from zoe_scheduler.state.application import ApplicationState
from zoe_scheduler.state.execution import ExecutionState
6
from common.configuration import zoeconf
7

8
log = logging.getLogger(__name__)
9 10


Daniele Venzano's avatar
Daniele Venzano committed
11
def init_history_paths() -> bool:
12
    if not os.path.exists(zoeconf().history_path):
Daniele Venzano's avatar
Daniele Venzano committed
13
        try:
14
            os.makedirs(zoeconf().history_path)
Daniele Venzano's avatar
Daniele Venzano committed
15
        except OSError:
16
            log.error("Cannot create history directory in {}".format(zoeconf().history_path))
Daniele Venzano's avatar
Daniele Venzano committed
17
            return False
18 19
        os.makedirs(os.path.join(zoeconf().history_path, 'apps'))
        os.makedirs(os.path.join(zoeconf().history_path, 'logs'))
Daniele Venzano's avatar
Daniele Venzano committed
20 21 22
    return True


23
def application_data_upload(application: ApplicationState, data: bytes) -> bool:
24
    fpath = os.path.join(zoeconf().history_path, 'apps', 'app-{}.zip'.format(application.id))
25
    open(fpath, "wb").write(data)
26 27


28
def application_data_download(application: ApplicationState) -> bytes:
29
    fpath = os.path.join(zoeconf().history_path, 'apps', 'app-{}.zip'.format(application.id))
30 31
    data = open(fpath, "rb").read()
    return data
32 33


34
def application_data_delete(application: ApplicationState):
35
    fpath = os.path.join(zoeconf().history_path, 'apps', 'app-{}.zip'.format(application.id))
36 37 38 39
    try:
        os.unlink(fpath)
    except OSError:
        log.warning("Binary data for application {} not found, cannot delete".format(application.id))
40 41


42
def logs_archive_upload(execution: ExecutionState, data: bytes) -> bool:
43
    fpath = os.path.join(zoeconf().history_path, 'logs', 'log-{}.zip'.format(execution.id))
44
    open(fpath, "wb").write(data)
45 46


47
def logs_archive_download(execution: ExecutionState) -> bytes:
48
    fpath = os.path.join(zoeconf().history_path, 'logs', 'log-{}.zip'.format(execution.id))
49 50
    data = open(fpath, "rb").read()
    return data
51 52


53
def logs_archive_delete(execution: ExecutionState):
54
    fpath = os.path.join(zoeconf().history_path, 'logs', 'log-{}.zip'.format(execution.id))
55 56 57 58
    try:
        os.unlink(fpath)
    except OSError:
        log.warning("Logs archive for execution {} not found, cannot delete".format(execution.id))