source: ogClient-Git/src/utils/cache.py @ 81ee4b0

Last change on this file since 81ee4b0 was 81ee4b0, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

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.

  • Property mode set to 100644
File size: 1.9 KB
Line 
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
9import os
10
11from src.utils.fs import mount_mkdir, umount
12from src.utils.net import getifaddr
13from src.utils.probe import cache_probe
14
15OGIMG='/opt/opengnsys/images/'
16OGCACHE_MOUNTPOINT='/opt/opengnsys/cache'
17OGCLIENT_LOG_CACHE='/opt/opengnsys/log/{ip}.cache.txt'
18
19def 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
34def 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
42def 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
51def 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)
Note: See TracBrowser for help on using the repository browser.