summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-05-09 12:40:46 +0200
committerJose M. Guisado <jguisado@soleta.eu>2022-06-01 11:48:30 +0200
commit81ee4b02dd8ac8adde7755aaff965a16edbf3ab9 (patch)
treee04b655f33940b3f318663c5f6974e3fee56e940
parent4e5b17ce6dcafa2818114c49fcff71af5afcdd26 (diff)
live: generate cache.txt file in refresh
Generates a cache.txt file if a cache partition is detected. OpenGnsys stores information about stored images in its 'cache' partition via a text file. The file is stored in a samba shared directory, mounted at '/opt/opengnsys/log/' in a live client. The file name is '{ip}.cache.txt'. Previously, the generation of this file was delegated to external bash scripts.
-rw-r--r--src/live/ogOperations.py2
-rw-r--r--src/utils/cache.py69
2 files changed, 71 insertions, 0 deletions
diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py
index 51f5f2e..853d661 100644
--- a/src/live/ogOperations.py
+++ b/src/live/ogOperations.py
@@ -20,6 +20,7 @@ from src.utils.menu import generate_menu
from src.utils.fs import mount_mkdir, umount, get_usedperc
from src.utils.probe import os_probe, cache_probe
from src.utils.disk import get_disks
+from src.utils.cache import generate_cache_txt
OG_SHELL = '/bin/bash'
@@ -351,6 +352,7 @@ class OgLiveOperations:
parsed['partition_setup'].append(part_setup)
generate_menu(parsed['partition_setup'])
+ generate_cache_txt()
self._restartBrowser(self._url)
return parsed
diff --git a/src/utils/cache.py b/src/utils/cache.py
new file mode 100644
index 0000000..150a5df
--- /dev/null
+++ b/src/utils/cache.py
@@ -0,0 +1,69 @@
+#
+# 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 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:
+ print("About to write")
+ 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)