summaryrefslogtreecommitdiffstats
path: root/src/utils/cache.py
blob: d1ae3d9dde6734a55ae82bb7cfa90d38e19187ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#
# 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 logging
import shutil
import os

from src.utils.fs import mount_mkdir, umount
from src.utils.net import *
from src.utils.probe import get_cache_dev_path

OG_IMAGE_PATH = '/opt/opengnsys/images/'
OG_CACHE_PATH = '/opt/opengnsys/cache'
OG_CACHE_IMAGE_PATH =  OG_CACHE_PATH + OG_IMAGE_PATH
OGCLIENT_LOG_CACHE='/opt/opengnsys/log/{ip}.cache.txt'

def mount_cache():
    cache_dev = get_cache_dev_path()

    if cache_dev:
        cache_mounted = mount_mkdir(cache_dev, OG_CACHE_PATH)

        if cache_mounted and not os.path.exists(OG_CACHE_IMAGE_PATH):
            os.makedirs(OG_CACHE_IMAGE_PATH)
        return OG_CACHE_PATH

    return ''

def umount_cache():
    """
    If OG_CACHE_PATH is a mountpoint, umounts.
    If not, does nothing.
    """
    if os.path.ismount(OG_CACHE_PATH):
        umount(OG_CACHE_PATH)

def write_cache_txt(content):
    """
    Dumps content to /opt/opengnsys/log/{ip}.cache.txt
    """
    client_ip = getifaddr(get_ethernet_interface())
    with open(OGCLIENT_LOG_CACHE.format(ip=client_ip), 'w') as f:
        logging.debug('Writing cache contents to %s.cache.txt', client_ip)
        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}{OG_IMAGE_PATH}')
        except FileNotFoundError:
            return
        content = ','.join(files)
        write_cache_txt(content)

def _read_checksum(file_path):
    res = ''
    try:
        with open(file_path, 'r') as f:
            res = f.read()
    except (FileNotFoundError, PermissionError, OSError) as e:
        logging.info(f'Cannot read checksum file {file_path}: {e}')
    return res

def update_live_cache():
    live_dir = os.getenv('oglivedir')

    if not live_dir:
        logging.warning('No oglivedir environment variable defined')
        return

    cache_mnt = mount_cache()
    if not cache_mnt:
        logging.info(f'No cache partition available, skipping live cache generation')
        return

    src_dir = f"/opt/oglive/tftpboot/{live_dir}"
    dst_dir = f"{cache_mnt}/boot/{live_dir}"

    if not os.path.exists(src_dir):
        logging.info(f'{src_dir} not found')
        return

    os.makedirs(dst_dir, exist_ok=True)

    sum_extension = '.sum'

    files = ['ogvmlinuz', 'oginitrd.img']

    for file_name in files:
        server_file = f'{src_dir}/{file_name}'
        client_file = f'{dst_dir}/{file_name}'
        server_checksum = _read_checksum(server_file + sum_extension)
        client_checksum = _read_checksum(client_file + sum_extension)

        if server_checksum and server_checksum != client_checksum:
            logging.info(f'Updating live cache {client_file}')
            shutil.copyfile(server_file, client_file)
            shutil.copyfile(server_file + sum_extension, client_file + sum_extension)
        else:
            logging.info(f'{client_file} is already up to date')