source: ogClient-Git/src/utils/cache.py

Last change on this file was 7801d48, checked in by Jose M. Guisado <jguisado@…>, 2 years ago

utils: add init_cache function

init_cache() creates the default directory in which OpenGnsys stores
images when using any cache enabled transfer method.

As of this commit this folder must exist for tiptorrent.py to
work properly.

Subprocess Popen object inside tiptorrent.py use
'cwd' optional parameter like:

cwd='/opt/opengnsys/cache/opt/opengnsys/images/'

This folder convention might change in the future.

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