Add renaming of images

This commit is contained in:
Mikhail Gordeev 2021-08-08 20:35:21 +03:00
parent 0df33f57cb
commit d00c803b91
6 changed files with 114 additions and 6 deletions

View File

@ -17,6 +17,7 @@ import time
import yaml
import cloud_build.image_tests
import cloud_build.rename
PROG = 'cloud-build'
@ -652,10 +653,11 @@ Dir::Etc::preferencesparts "/var/empty";
arch: str,
kind: str
) -> Path:
path = (
self.images_dir(branch, arch)
/ f'alt-{branch.lower()}-{image}-{arch}.{kind}'
)
name = f'alt-{branch.lower()}-{image}-{arch}.{kind}'
rename_dict = self._images[image].get('rename', {})
if rename_dict:
name = cloud_build.rename.rename(rename_dict, name)
path = self.images_dir(branch, arch) / name
return path
def copy_image(self, src: Path, dst: Path, *, rewrite=False) -> None:
@ -730,6 +732,7 @@ Dir::Etc::preferencesparts "/var/empty";
)
if tarball is None:
continue
image_path = self.image_path(image, branch, arch, kind)
self.copy_image(tarball, image_path)
if not no_tests:

20
cloud_build/rename.py Normal file
View File

@ -0,0 +1,20 @@
from typing import Dict
import re
import subprocess
def rename(rename_dict: Dict[str, str], name: str) -> str:
if regex := rename_dict.get('regex'):
to = rename_dict['to']
name = re.sub(regex, to, name)
elif prog := rename_dict.get('prog'):
name = subprocess.run(
[prog, name],
stdout=subprocess.PIPE,
).stdout.decode().strip()
else:
to = rename_dict['to']
name = to
return name

View File

@ -141,7 +141,10 @@ class TestIntegrationImages(TestCase):
else:
self.assertCountEqual(self._images.keys(), ['branch'])
def test_build_cloud(self):
def test_build_cloud_img(self):
self.assertIn('alt-p9-cloud-x86_64.img', self.images('p9', 'x86_64'))
def test_rename_regex_cloud(self):
self.assertIn('alt-p9-cloud-x86_64.qcow2', self.images('p9', 'x86_64'))
def test_exclude_arches(self):

View File

@ -21,12 +21,16 @@ images:
target: vm/cloud-systemd
kinds:
- img
- qcow2
- qcow2c
exclude_arches:
- armh
- ppc64le
exclude_branches:
- p8
rename:
regex: '(.*)\.qcow2c$'
to: '\1.qcow2'
rootfs-minimal:
target: ve/docker
kinds:

56
tests/test_rename.py Normal file
View File

@ -0,0 +1,56 @@
from contextlib import ExitStack
from pathlib import Path
from unittest import TestCase
from unittest import mock
import os
import shutil
from cloud_build import CB
from tests.call import Call
def renamer(directory):
renamer = directory / 'renamer.py'
renamer.touch()
renamer.write_text('''\
#!/usr/bin/python3
print("lxd.tar.xz")
''')
renamer.chmod(0o700)
class TestRename(TestCase):
def setUp(self):
self.images = self.__class__.images
@classmethod
def setUpClass(cls):
cls.work_dir = Path('/tmp/cloud-build')
os.makedirs(cls.work_dir, exist_ok=True)
renamer(cls.work_dir)
with ExitStack() as stack:
stack.enter_context(mock.patch('subprocess.call', Call()))
cloud_build = CB(
config='tests/test_rename.yaml',
data_dir=(cls.work_dir / 'cloud_build').as_posix(),
)
cloud_build.create_images(no_tests=True)
cloud_build.sync(create_remote_dirs=True)
images_dir = cls.work_dir / 'images'
cls.images = os.listdir(images_dir)
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.work_dir, ignore_errors=True)
def test_simple_rename(self):
self.assertIn('docker.tar.xz', self.images)
def test_prog_rename(self):
self.assertIn('lxd.tar.xz', self.images)

22
tests/test_rename.yaml Normal file
View File

@ -0,0 +1,22 @@
---
remote: '/tmp/cloud-build/images'
images:
rootfs-minimal:
target: ve/docker
kinds:
- tar.xz
rename:
to: docker.tar.xz
rootfs-systemd:
target: ve/systemd-networkd
kinds:
- tar.xz
rename:
prog: /tmp/cloud-build/renamer.py
branches:
p9:
arches:
x86_64:
...