From defcfed708ee99e2f503b2058409037c67a52fe5 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Thu, 4 Jun 2020 12:32:29 +0200 Subject: [PATCH] Stop jails at the beginning of focker compose. --- focker/compose.py | 13 +++++++++++++ tests/test_compose.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/focker/compose.py b/focker/compose.py index d39821a..e589a69 100644 --- a/focker/compose.py +++ b/focker/compose.py @@ -17,6 +17,7 @@ from .zfs import AmbiguousValueError, \ from .jail import jail_fs_create, \ jail_create, \ jail_remove, \ + jail_stop, \ backup_file, \ quote from .misc import random_sha256_hexdigest, \ @@ -154,6 +155,16 @@ def build_jails(spec): setup_dependencies(spec, generated_names) +def stop_jails(spec): + for jailname, _ in spec.items(): + try: + name, _ = zfs_find(jailname, focker_type='jail') + except ValueError: + continue + print('Stopping:', jailname) + jail_stop(zfs_mountpoint(name)) + + def command_compose_build(args): if not os.path.exists(args.filename): raise ValueError('File not found: ' + args.filename) @@ -161,6 +172,8 @@ def command_compose_build(args): print('path:', path) with open(args.filename, 'r') as f: spec = yaml.safe_load(f) + if 'jails' in spec: + stop_jails(spec['jails']) if 'exec.prebuild' in spec: exec_prebuild(spec['exec.prebuild'], path) if 'volumes' in spec: diff --git a/tests/test_compose.py b/tests/test_compose.py index ea5b3d1..84fb1a8 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -5,6 +5,7 @@ from focker.compose import exec_hook, \ build_images, \ setup_dependencies, \ build_jails, \ + stop_jails, \ command_compose_build import focker.compose from tempfile import TemporaryDirectory @@ -262,6 +263,47 @@ def test_build_jails(): conf.write('/etc/jail.conf') +def test_stop_jails_01(monkeypatch): + spec = { + 'foobar': {} + } + def mock_zfs_find(name, focker_type): + assert name == 'foobar' + assert focker_type == 'jail' + return 'baf', None + def mock_zfs_mountpoint(name): + assert name == 'baf' + return '/beef' + def mock_jail_stop(path): + assert path == '/beef' + monkeypatch.setattr(focker.compose, 'zfs_find', mock_zfs_find) + monkeypatch.setattr(focker.compose, 'zfs_mountpoint', mock_zfs_mountpoint) + monkeypatch.setattr(focker.compose, 'jail_stop', mock_jail_stop) + stop_jails(spec) + + +def test_stop_jails_02(monkeypatch): + spec = { + 'foobar': {} + } + def mock_zfs_find(name, focker_type): + assert name == 'foobar' + assert focker_type == 'jail' + raise ValueError('Not found') + def mock_zfs_mountpoint(name): + mock_zfs_mountpoint.called = True + mock_zfs_mountpoint.called = False + def mock_jail_stop(path): + mock_jail_stop.called = True + mock_jail_stop.called = False + monkeypatch.setattr(focker.compose, 'zfs_find', mock_zfs_find) + monkeypatch.setattr(focker.compose, 'zfs_mountpoint', mock_zfs_mountpoint) + monkeypatch.setattr(focker.compose, 'jail_stop', mock_jail_stop) + stop_jails(spec) + assert not mock_zfs_mountpoint.called + assert not mock_jail_stop.called + + def test_command_compose_build(monkeypatch): with TemporaryDirectory() as d: with open(os.path.join(d, 'focker-compose.yml'), 'w') as f: