summaryrefslogtreecommitdiffstats
path: root/src/live/ogOperations.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-11-02 14:09:39 +0100
committerJose M. Guisado <jguisado@soleta.eu>2022-11-02 14:09:39 +0100
commit3703fd60634fe41f3ae131bd2b4c4ca9f0ef8b3e (patch)
tree22708ed2c565ee8da6c14fe6817543127cd62cee /src/live/ogOperations.py
parent55c76c9d701b8a328dfd2bfb91f90d0a91c50d8f (diff)
live: support native unicast cache image restore
UNICAST-CACHE consist of: 1. Checking if the target image is already present at the opengnsys cache partition. If so, check for integrity (local and remote checksum). If the image is not present in the cache partition, download the target image into it. 2. Restore the image from cache partition. This commit add support for this operation natively from ogClient Python's code.
Diffstat (limited to 'src/live/ogOperations.py')
-rw-r--r--src/live/ogOperations.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py
index 3e9216d..870ca12 100644
--- a/src/live/ogOperations.py
+++ b/src/live/ogOperations.py
@@ -119,11 +119,34 @@ class OgLiveOperations:
with open(filename, 'w') as f:
f.write(dig)
- def _restore_image_unicast(self, repo, name, devpath):
- image_path = f'/opt/opengnsys/images/{name}.img'
+ def _copy_image_to_cache(self, image_name):
+ """
+ Copies /opt/opengnsys/image/{image_name} into
+ /opt/opengnsys/cache/opt/opengnsys/images/
+
+ Implies a unicast transfer. Does not use tiptorrent.
+ """
+ src = f'/opt/opengnsys/images/{image_name}.img'
+ dst = f'/opt/opengnsys/cache/opt/opengnsys/images/{image_name}.img'
+ try:
+ r = shutil.copy(src, dst)
+ tip_write_csum(image_name)
+ except:
+ logging.error('Error copying image to cache', repo)
+ raise ValueError(f'Error: Cannot copy image {image_name} to cache')
+
+ def _restore_image_unicast(self, repo, name, devpath, cache=False):
if ogChangeRepo(repo).returncode != 0:
logging.error('ogChangeRepo could not change repository to %s', repo)
raise ValueError(f'Error: Cannot change repository to {repo}')
+ logging.debug(f'restore_image_unicast: name => {name}')
+ if cache:
+ image_path = f'/opt/opengnsys/cache/opt/opengnsys/images/{name}.img'
+ if (not os.path.exists(image_path) or
+ not tip_check_csum(repo, name)):
+ self._copy_image_to_cache(name)
+ else:
+ image_path = f'/opt/opengnsys/images/{name}.img'
self._restore_image(image_path, devpath)
def _restore_image_tiptorrent(self, repo, name, devpath):
@@ -134,10 +157,15 @@ class OgLiveOperations:
self._restore_image(image_path, devpath)
def _restore_image(self, image_path, devpath):
+ logging.debug(f'Restoring image at {image_path} into {devpath}')
cmd_lzop = shlex.split(f'lzop -dc {image_path}')
cmd_pc = shlex.split(f'partclone.restore -d0 -C -I -o {devpath}')
cmd_mbuffer = shlex.split('mbuffer -q -m 40M') if shutil.which('mbuffer') else None
+ if not os.path.exists(image_path):
+ logging.error('f{image_path} does not exist, exiting.')
+ raise ValueError(f'Error: Image not found at {image_path}')
+
with open('/tmp/command.log', 'wb', 0) as logfile:
proc_lzop = subprocess.Popen(cmd_lzop,
stdout=subprocess.PIPE)
@@ -309,7 +337,8 @@ class OgLiveOperations:
if shutil.which('restoreImageCustom'):
restoreImageCustom(repo, name, disk, partition, ctype)
elif 'UNICAST' in ctype:
- self._restore_image_unicast(repo, name, partdev)
+ cache = 'DIRECT' not in ctype
+ self._restore_image_unicast(repo, name, partdev, cache)
elif ctype == 'TIPTORRENT':
self._restore_image_tiptorrent(repo, name, partdev)