Add list command

master
Dustin 2023-02-24 12:00:16 -06:00
parent 6a31ac392a
commit 184d270472
2 changed files with 35 additions and 11 deletions

View File

@ -5,7 +5,6 @@ from pathlib import Path
import rich.console import rich.console
import rich.logging import rich.logging
import xdg
from ocivm import image from ocivm import image
@ -17,24 +16,20 @@ class CLI:
def __init__(self, console: rich.console.Console) -> None: def __init__(self, console: rich.console.Console) -> None:
self.console = console 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: 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 tempfile.NamedTemporaryFile(suffix='.tar') as tmp_tar:
with self.console.status('Exporting OCI image as tarball ...'): with self.console.status('Exporting OCI image as tarball ...'):
tar = Path(tmp_tar.name) tar = Path(tmp_tar.name)
image.make_tar(name, tar) image.make_tar(name, tar)
self.console.print('Created tarball:', tar) self.console.print('Created tarball:', tar)
with self.console.status('Converting tarball to QCOW2 image ...'): 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) 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: def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -56,6 +51,8 @@ def parse_args() -> argparse.Namespace:
p_import = sp.add_parser('import', help='Import container image') p_import = sp.add_parser('import', help='Import container image')
p_import.add_argument('name', help='Container image name') p_import.add_argument('name', help='Container image name')
sp.add_parser('list', help='List VM images')
return parser.parse_args() return parser.parse_args()
@ -72,3 +69,5 @@ def main() -> None:
match args.command: match args.command:
case 'import': case 'import':
cli.import_image(args.name) cli.import_image(args.name)
case 'list':
cli.list_images()

View File

@ -1,9 +1,13 @@
import importlib.resources import importlib.resources
import logging import logging
import os
import string import string
import subprocess import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Iterable
import xdg
from .util import list2cmdline 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: def make_tar(name: str, dest: Path) -> None:
res = importlib.resources.files(__package__).joinpath('linuxrc') res = importlib.resources.files(__package__).joinpath('linuxrc')
with tempfile.TemporaryDirectory() as t: 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') raw = dest.with_suffix('.img')
cmd = [ cmd = [
'virt-make-fs', 'virt-make-fs',
@ -85,3 +98,15 @@ def tar2qcow2(src: Path, dest: Path, size: str = '+1G') -> None:
check=True, check=True,
) )
raw.unlink() 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'),
)