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