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

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

src: improve logging

Adds new logging handler redirecting messages to the log file
located in the Samba shared directory (applies to live mode
clients, i.e: ogLive)

Parses log level configuration from ogclient.json. See:

{

"opengnsys": {

...
"log": "INFO",
...

}
...

}

Adds --debug option to set root logger level to DEBUG when starting
ogClient. Overrides log level from config file.

In addition:

  • Replaces any occurence of print with a corresponding logging function.
  • Unsets log level for handlers, use root logger level instead.
  • Default level for root logger is INFO.
  • Replaces level from response log messages to debug (ogRest)
  • 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 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)
Note: See TracBrowser for help on using the repository browser.