From dd3c505ecb3863f11d477b09a662b437b19cff79 Mon Sep 17 00:00:00 2001 From: Mikhail Gordeev Date: Tue, 13 Apr 2021 16:02:58 +0300 Subject: [PATCH] Change hardcoded kick command to after_sync_commands from config --- cloud_build/cloud_build.py | 18 +++++- example-config.yaml | 1 + tests/test_after_sync_commands.py | 60 +++++++++++++++++++ tests/test_run_after_sync_local_commands.yaml | 15 +++++ .../test_run_after_sync_remote_commands.yaml | 15 +++++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tests/test_after_sync_commands.py create mode 100644 tests/test_run_after_sync_local_commands.yaml create mode 100644 tests/test_run_after_sync_remote_commands.yaml diff --git a/cloud_build/cloud_build.py b/cloud_build/cloud_build.py index 61d8b76..eef78b1 100755 --- a/cloud_build/cloud_build.py +++ b/cloud_build/cloud_build.py @@ -184,6 +184,8 @@ class CB: self._services = cfg.get('services', {}) self._scripts = cfg.get('scripts', {}) + self._after_sync_commands = cfg.get('after_sync_commands', []) + self.key = cfg.get('key') if isinstance(self.key, int): self.key = '{:X}'.format(self.key) @@ -702,12 +704,22 @@ Dir::Etc::preferencesparts "/var/empty"; self.call(['gpg2', '--yes', '-basu', self.key, sum_file]) shutil.copyfile(sum_file + '.asc', 'SHA256SUMS.gpg') - def kick(self): + def after_sync_commands(self): remote = self._remote colon = remote.find(':') if colon != -1: host = remote[:colon] - self.call(['ssh', host, 'kick']) + + def cmd(command): + return ['ssh', host, command] + else: + host = remote + + def cmd(command): + return [command] + + for command in self._after_sync_commands: + self.call(cmd(command)) def sync(self, create_remote_dirs: bool = False) -> None: for branch in self.branches: @@ -724,4 +736,4 @@ Dir::Etc::preferencesparts "/var/empty"; cmd.append('--delete') self.call(cmd) - self.kick() + self.after_sync_commands() diff --git a/example-config.yaml b/example-config.yaml index ef0d221..cf64554 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -1,5 +1,6 @@ --- remote: remote:/pub/images/{branch}/cloud +after_sync_commands: ['kick'] key: 0x00000000 try_build_all: False repository_url: http://mirror.yandex.ru/altlinux/{branch}/branch diff --git a/tests/test_after_sync_commands.py b/tests/test_after_sync_commands.py new file mode 100644 index 0000000..8647e18 --- /dev/null +++ b/tests/test_after_sync_commands.py @@ -0,0 +1,60 @@ +from pathlib import Path +from unittest import TestCase +from unittest import mock + +import tempfile +import shutil + +from cloud_build import CB + +import tests.call as call + + +DS = {'rsync': [call.return_d(0), call.nop_d]} + + +class TestAfterSyncCommands(TestCase): + def setUp(self): + self.data_dir = Path(tempfile.mkdtemp(prefix='cloud_build')) + + def tearDown(self): + shutil.rmtree(self.data_dir) + + @mock.patch('subprocess.call', call.Call(decorators=DS)) + def test_run_after_sync_remote_commands(self): + cb = CB( + config='tests/test_run_after_sync_remote_commands.yaml', + data_dir=self.data_dir, + ) + cb.create_images(no_tests=True) + regex = r'ssh.*kick' + self.assertRaisesRegex( + Exception, + regex, + cb.sync, + create_remote_dirs=True + ) + + @mock.patch('subprocess.call', call.Call(decorators=DS)) + def test_run_after_sync_local_commands(self): + cb = CB( + config='tests/test_run_after_sync_local_commands.yaml', + data_dir=self.data_dir, + ) + cb.create_images(no_tests=True) + regex = r'\[\'kick' + self.assertRaisesRegex( + Exception, + regex, + cb.sync, + create_remote_dirs=True + ) + + @mock.patch('subprocess.call', call.Call(decorators=DS)) + def test_dont_run_after_sync_local_commands(self): + cb = CB( + config='tests/minimal_config.yaml', + data_dir=self.data_dir, + ) + cb.create_images(no_tests=True) + cb.sync(create_remote_dirs=False) diff --git a/tests/test_run_after_sync_local_commands.yaml b/tests/test_run_after_sync_local_commands.yaml new file mode 100644 index 0000000..5d7ddca --- /dev/null +++ b/tests/test_run_after_sync_local_commands.yaml @@ -0,0 +1,15 @@ +--- +remote: '/var/empty' +after_sync_commands: ['kick'] + +images: + rootfs-minimal: + target: ve/docker + kinds: + - tar.xz + +branches: + Sisyphus: + arches: + x86_64: +... diff --git a/tests/test_run_after_sync_remote_commands.yaml b/tests/test_run_after_sync_remote_commands.yaml new file mode 100644 index 0000000..9b6c92d --- /dev/null +++ b/tests/test_run_after_sync_remote_commands.yaml @@ -0,0 +1,15 @@ +--- +remote: 'example.com:/var/empty' +after_sync_commands: ['kick'] + +images: + rootfs-minimal: + target: ve/docker + kinds: + - tar.xz + +branches: + Sisyphus: + arches: + x86_64: +...