blob: a97cc416421bd6455536c005684f503b06599993 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#
# Copyright (C) 2022 Soleta Networks <info@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
import logging
import os
from src.utils.fs import mount_mkdir, umount
from src.utils.net import getifaddr
from src.utils.probe import cache_probe
OGIMG='/opt/opengnsys/images/'
OGCACHE_MOUNTPOINT='/opt/opengnsys/cache'
OGCLIENT_LOG_CACHE='/opt/opengnsys/log/{ip}.cache.txt'
def mount_cache():
"""
Probes for cache and mounts if succesful.
Returns the mountpoint or an empty string.
"""
cache_dev = cache_probe()
if cache_dev:
# cache_target = cache_dev.replace('dev', 'mnt')
mount_mkdir(cache_dev, OGCACHE_MOUNTPOINT)
return OGCACHE_MOUNTPOINT
return ''
def umount_cache():
"""
If OGCACHE_MOUNTPOINT is a mountpoint, umounts.
If not, does nothing.
"""
if os.path.ismount(OGCACHE_MOUNTPOINT):
umount(OGCACHE_MOUNTPOINT)
def write_cache_txt(content):
"""
Dumps content to /opt/opengnsys/log/{ip}.cache.txt
"""
client_ip = getifaddr(os.getenv('DEVICE'))
with open(OGCLIENT_LOG_CACHE.format(ip=client_ip), 'w') as f:
logging.debug('Writing cache contents to %s.cache.txt', client_ip)
f.write(content)
def generate_cache_txt():
"""
If no OpenGnsys cache partition is detected this function will
do nothing.
Generates a *.cache.txt file from a given path.
A .cache.txt file is just a comma separated list of
the files contained in the images folder in the OpenGnsys cache.
"""
cache_path = mount_cache()
if cache_path:
try:
files = os.listdir(f'{cache_path}{OGIMG}')
except FileNotFoundError:
return
content = ','.join(files)
write_cache_txt(content)
def init_cache():
"""
If a cache partition is present, creates the following directories
/opt/opengnsys/images. This is the default folder in which images
are stored when using tiptorrent-cache.
"""
mountpoint = mount_cache()
if mountpoint:
logging.info(f'Creating cache directory at {mountpoint}')
os.makedirs('/opt/opengnsys/cache/opt/opengnsys/images')
|