diff --git a/src/ocivm/cli.py b/src/ocivm/cli.py index 9c8a726..e53c64a 100644 --- a/src/ocivm/cli.py +++ b/src/ocivm/cli.py @@ -5,7 +5,6 @@ from pathlib import Path import rich.console import rich.logging -import xdg from ocivm import image @@ -17,24 +16,20 @@ class CLI: def __init__(self, console: rich.console.Console) -> None: self.console = console - def image_dir(self) -> Path: - image_dir = xdg.xdg_data_home() / 'ocivm' / 'images' - log.debug('Using image storage directory %s', image_dir) - return image_dir - def import_image(self, name: str) -> None: - img = self.image_dir() / name - img.parent.mkdir(parents=True, exist_ok=True) - img = img.with_suffix('.qcow2') with tempfile.NamedTemporaryFile(suffix='.tar') as tmp_tar: with self.console.status('Exporting OCI image as tarball ...'): tar = Path(tmp_tar.name) image.make_tar(name, tar) self.console.print('Created tarball:', tar) with self.console.status('Converting tarball to QCOW2 image ...'): - image.tar2qcow2(tar, img) + img = image.import_image(name, tar) self.console.print('Created QCOW2 image:', img) + def list_images(self) -> None: + for name in image.list_images(): + print(name) + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() @@ -56,6 +51,8 @@ def parse_args() -> argparse.Namespace: p_import = sp.add_parser('import', help='Import container image') p_import.add_argument('name', help='Container image name') + sp.add_parser('list', help='List VM images') + return parser.parse_args() @@ -72,3 +69,5 @@ def main() -> None: match args.command: case 'import': cli.import_image(args.name) + case 'list': + cli.list_images() diff --git a/src/ocivm/image.py b/src/ocivm/image.py index 70787c7..2a074e0 100644 --- a/src/ocivm/image.py +++ b/src/ocivm/image.py @@ -1,9 +1,13 @@ import importlib.resources import logging +import os import string import subprocess import tempfile from pathlib import Path +from typing import Iterable + +import xdg from .util import list2cmdline @@ -19,6 +23,12 @@ ADD linuxrc / ) +def get_image_dir() -> Path: + image_dir = xdg.xdg_data_home() / 'ocivm' / 'images' + log.debug('Using image storage directory %s', image_dir) + return image_dir + + def make_tar(name: str, dest: Path) -> None: res = importlib.resources.files(__package__).joinpath('linuxrc') with tempfile.TemporaryDirectory() as t: @@ -53,7 +63,10 @@ def make_tar(name: str, dest: Path) -> None: ) -def tar2qcow2(src: Path, dest: Path, size: str = '+1G') -> None: +def import_image(name: str, src: Path, size: str = '+1G') -> Path: + dest = get_image_dir() / name + dest = dest.with_suffix('.qcow2') + dest.parent.mkdir(parents=True, exist_ok=True) raw = dest.with_suffix('.img') cmd = [ 'virt-make-fs', @@ -85,3 +98,15 @@ def tar2qcow2(src: Path, dest: Path, size: str = '+1G') -> None: check=True, ) raw.unlink() + return dest + + +def list_images() -> Iterable[str]: + base = get_image_dir() + for dirpath, _dirnames, filenames in os.walk(base): + for filename in filenames: + if filename.endswith('.qcow2'): + yield os.path.join( + os.path.relpath(dirpath, base), + filename.removesuffix('.qcow2'), + )