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 os |
---|
10 | |
---|
11 | from src.utils.fs import mount_mkdir, umount |
---|
12 | from src.utils.net import getifaddr |
---|
13 | from src.utils.probe import cache_probe |
---|
14 | |
---|
15 | OGIMG='/opt/opengnsys/images/' |
---|
16 | OGCACHE_MOUNTPOINT='/opt/opengnsys/cache' |
---|
17 | OGCLIENT_LOG_CACHE='/opt/opengnsys/log/{ip}.cache.txt' |
---|
18 | |
---|
19 | def mount_cache(): |
---|
20 | """ |
---|
21 | Probes for cache and mounts if succesful. |
---|
22 | |
---|
23 | Returns the mountpoint or an empty string. |
---|
24 | """ |
---|
25 | cache_dev = cache_probe() |
---|
26 | |
---|
27 | if cache_dev: |
---|
28 | # cache_target = cache_dev.replace('dev', 'mnt') |
---|
29 | mount_mkdir(cache_dev, OGCACHE_MOUNTPOINT) |
---|
30 | return OGCACHE_MOUNTPOINT |
---|
31 | |
---|
32 | return '' |
---|
33 | |
---|
34 | def umount_cache(): |
---|
35 | """ |
---|
36 | If OGCACHE_MOUNTPOINT is a mountpoint, umounts. |
---|
37 | If not, does nothing. |
---|
38 | """ |
---|
39 | if os.path.ismount(OGCACHE_MOUNTPOINT): |
---|
40 | umount(OGCACHE_MOUNTPOINT) |
---|
41 | |
---|
42 | def write_cache_txt(content): |
---|
43 | """ |
---|
44 | Dumps content to /opt/opengnsys/log/{ip}.cache.txt |
---|
45 | """ |
---|
46 | client_ip = getifaddr(os.getenv('DEVICE')) |
---|
47 | with open(OGCLIENT_LOG_CACHE.format(ip=client_ip), 'w') as f: |
---|
48 | print("About to write") |
---|
49 | f.write(content) |
---|
50 | |
---|
51 | def generate_cache_txt(): |
---|
52 | """ |
---|
53 | If no OpenGnsys cache partition is detected this function will |
---|
54 | do nothing. |
---|
55 | |
---|
56 | Generates a *.cache.txt file from a given path. |
---|
57 | |
---|
58 | A .cache.txt file is just a comma separated list of |
---|
59 | the files contained in the images folder in the OpenGnsys cache. |
---|
60 | """ |
---|
61 | cache_path = mount_cache() |
---|
62 | |
---|
63 | if cache_path: |
---|
64 | try: |
---|
65 | files = os.listdir(f'{cache_path}{OGIMG}') |
---|
66 | except FileNotFoundError: |
---|
67 | return |
---|
68 | content = ','.join(files) |
---|
69 | write_cache_txt(content) |
---|