From ad87ea2d282b1ccfcfea28bcfc5381e534d698a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 5 Apr 2016 08:40:54 +0200 Subject: [PATCH] [ostree] Move cloning config repo to chroot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lubomír Sedlář --- pungi/atomic.py | 46 ++++++++++++++++++++++++++++++++++++- pungi/phases/ostree.py | 29 ++++------------------- tests/helpers.py | 7 +++++- tests/test_atomic_script.py | 23 ++++++++++++++++--- tests/test_ostree_phase.py | 15 ++++-------- 5 files changed, 79 insertions(+), 41 deletions(-) diff --git a/pungi/atomic.py b/pungi/atomic.py index 7c0075be..627ce34d 100644 --- a/pungi/atomic.py +++ b/pungi/atomic.py @@ -8,6 +8,11 @@ It is expected to be runnable in Koji runroot. import argparse import os from kobo import shortcuts +import tempfile +import shutil +import re + +from .wrappers import scm def make_log_file(log_dir, filename): @@ -31,9 +36,42 @@ def make_ostree_repo(repo, config, log_dir=None): logfile=log_file) +def clone_repo(repodir, url, branch): + scm.get_dir_from_scm( + {'scm': 'git', 'repo': url, 'branch': branch, 'dir': '.'}, repodir) + + +def tweak_mirrorlist(repodir, source_repo): + for file in os.listdir(repodir): + if file.endswith('.repo'): + tweak_file(os.path.join(repodir, file), source_repo) + + +def tweak_file(path, source_repo): + """Replace mirrorlist line in repo file with baseurl pointing to source_repo.""" + with open(path, 'r') as f: + contents = f.read() + replacement = 'baseurl={}'.format(source_repo) + contents = re.sub(r'^mirrorlist=.*$', replacement, contents) + with open(path, 'w') as f: + f.write(contents) + + +def prepare_config(workdir, config_url, config_branch, source_repo): + repodir = os.path.join(workdir, 'config_repo') + clone_repo(repodir, config_url, config_branch) + tweak_mirrorlist(repodir, source_repo) + return repodir + + def run(opts): + workdir = tempfile.mkdtemp() + repodir = prepare_config(workdir, opts.config_url, opts.config_branch, + opts.source_repo) init_atomic_repo(opts.atomic_repo, log_dir=opts.log_dir) - make_ostree_repo(opts.atomic_repo, opts.treefile, log_dir=opts.log_dir) + treefile = os.path.join(repodir, opts.treefile) + make_ostree_repo(opts.atomic_repo, treefile, log_dir=opts.log_dir) + shutil.rmtree(workdir) def main(args=None): @@ -45,6 +83,12 @@ def main(args=None): help='where to put the atomic repo') parser.add_argument('--treefile', required=True, help='treefile for rpm-ostree') + parser.add_argument('--config-url', required=True, + help='git repository with the treefile') + parser.add_argument('--config-branch', default='master', + help='git branch to be used') + parser.add_argument('--source-repo', required=True, + help='yum repo used as source for') opts = parser.parse_args(args) diff --git a/pungi/phases/ostree.py b/pungi/phases/ostree.py index 36cbc972..79e58d4a 100644 --- a/pungi/phases/ostree.py +++ b/pungi/phases/ostree.py @@ -2,12 +2,11 @@ import os from kobo.threads import ThreadPool, WorkerThread -import re from .base import ConfigGuardedPhase from .. import util from ..paths import translate_path -from ..wrappers import scm, kojiwrapper +from ..wrappers import kojiwrapper class OSTreePhase(ConfigGuardedPhase): @@ -42,15 +41,11 @@ class OSTreeThread(WorkerThread): msg = 'OSTree phase for variant %s, arch %s' % (variant.uid, arch) self.pool.log_info('[BEGIN] %s' % msg) - workdir = compose.paths.work.topdir('atomic') self.logdir = compose.paths.log.topdir('{}/atomic'.format(arch)) - repodir = os.path.join(workdir, 'config_repo') source_variant = compose.variants[config['source_repo_from']] source_repo = translate_path(compose, compose.paths.compose.repository(arch, source_variant)) - self._clone_repo(repodir, config['config_url'], config.get('config_branch', 'master')) - self._tweak_mirrorlist(repodir, source_repo) self._run_atomic_cmd(compose, variant, arch, config, source_repo) self.pool.log_info('[DONE ] %s' % msg) @@ -60,6 +55,9 @@ class OSTreeThread(WorkerThread): 'pungi-make-ostree', '--log-dir={}'.format(self.logdir), '--treefile={}'.format(config['treefile']), + '--config-url={}'.format(config['config_url']), + '--config-branch={}'.format(config.get('config_branch', 'master')), + '--source-repo={}'.format(source_repo), config['atomic_repo'] ] @@ -77,22 +75,3 @@ class OSTreeThread(WorkerThread): if output["retcode"] != 0: raise RuntimeError("Runroot task failed: %s. See %s for more details." % (output["task_id"], log_file)) - - def _clone_repo(self, repodir, url, branch): - scm.get_dir_from_scm({'scm': 'git', 'repo': url, 'branch': branch, 'dir': '.'}, - repodir, logger=self.pool._logger) - - def _tweak_mirrorlist(self, repodir, source_repo): - for file in os.listdir(repodir): - if file.endswith('.repo'): - tweak_file(os.path.join(repodir, file), source_repo) - - -def tweak_file(path, source_repo): - """Replace mirrorlist line in repo file with baseurl pointing to source_repo.""" - with open(path, 'r') as f: - contents = f.read() - replacement = 'baseurl={}'.format(source_repo) - contents = re.sub(r'^mirrorlist=.*$', replacement, contents) - with open(path, 'w') as f: - f.write(contents) diff --git a/tests/helpers.py b/tests/helpers.py index 11c07a40..d93e0955 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -5,6 +5,7 @@ import os import unittest import tempfile import shutil +import errno from pungi.util import get_arch_variant_data from pungi import paths @@ -15,7 +16,11 @@ class PungiTestCase(unittest.TestCase): self.topdir = tempfile.mkdtemp() def tearDown(self): - shutil.rmtree(self.topdir) + try: + shutil.rmtree(self.topdir) + except OSError as err: + if err.errno != errno.ENOENT: + raise class DummyCompose(object): diff --git a/tests/test_atomic_script.py b/tests/test_atomic_script.py index a871659e..4b0b3966 100755 --- a/tests/test_atomic_script.py +++ b/tests/test_atomic_script.py @@ -17,20 +17,37 @@ from pungi import atomic class OstreeScriptTest(helpers.PungiTestCase): + def _dummy_config_repo(self, scm_dict, target, logger=None): + helpers.touch(os.path.join(target, 'fedora-atomic-docker-host.json')) + helpers.touch(os.path.join(target, 'fedora-rawhide.repo')) + + @mock.patch('tempfile.mkdtemp') @mock.patch('kobo.shortcuts.run') - def test_full_run(self, run): + @mock.patch('pungi.wrappers.scm.get_dir_from_scm') + def test_full_run(self, get_dir_from_scm, run, tempfile): + tempfile.return_value = self.topdir + get_dir_from_scm.side_effect = self._dummy_config_repo + atomic.main([ '--log-dir={}'.format(os.path.join(self.topdir, 'logs', 'Atomic')), - '--treefile={}'.format(os.path.join(self.topdir, 'work', 'fedora-atomic-docker-host.json')), + '--treefile={}'.format('fedora-atomic-docker-host.json'), + '--config-url=https://git.fedorahosted.org/git/fedora-atomic.git', + '--config-branch=f24', + '--source-repo=https://kojipkgs.fedoraproject.org/repo', os.path.join(self.topdir, 'atomic'), ]) + self.maxDiff = None + self.assertEqual(get_dir_from_scm.call_args_list, + [mock.call({'scm': 'git', 'repo': 'https://git.fedorahosted.org/git/fedora-atomic.git', + 'branch': 'f24', 'dir': '.'}, + self.topdir + '/config_repo')]) self.assertItemsEqual( run.call_args_list, [mock.call(['ostree', 'init', '--repo={}/atomic'.format(self.topdir), '--mode=archive-z2'], logfile=self.topdir + '/logs/Atomic/init-atomic-repo.log'), mock.call(['rpm-ostree', 'compose', 'tree', '--repo={}/atomic'.format(self.topdir), - self.topdir + '/work/fedora-atomic-docker-host.json'], + self.topdir + '/config_repo/fedora-atomic-docker-host.json'], logfile=self.topdir + '/logs/Atomic/create-atomic-repo.log')]) diff --git a/tests/test_ostree_phase.py b/tests/test_ostree_phase.py index e4664e14..fe0036c8 100755 --- a/tests/test_ostree_phase.py +++ b/tests/test_ostree_phase.py @@ -74,13 +74,8 @@ class OSTreePhaseTest(helpers.PungiTestCase): class OSTreeThreadTest(helpers.PungiTestCase): - def _dummy_config_repo(self, scm_dict, target, logger=None): - helpers.touch(os.path.join(target, 'fedora-atomic-docker-host.json')) - helpers.touch(os.path.join(target, 'fedora-rawhide.repo')) - - @mock.patch('pungi.wrappers.scm.get_dir_from_scm') @mock.patch('pungi.wrappers.kojiwrapper.KojiWrapper') - def test_run(self, KojiWrapper, get_dir_from_scm): + def test_run(self, KojiWrapper): compose = helpers.DummyCompose(self.topdir, { 'koji_profile': 'koji', 'runroot_tag': 'rrt', @@ -93,7 +88,6 @@ class OSTreeThreadTest(helpers.PungiTestCase): 'treefile': 'fedora-atomic-docker-host.json', 'atomic_repo': '/other/place/for/atomic' } - get_dir_from_scm.side_effect = self._dummy_config_repo koji = KojiWrapper.return_value koji.run_runroot_cmd.return_value = { 'task_id': 1234, @@ -105,15 +99,14 @@ class OSTreeThreadTest(helpers.PungiTestCase): t.process((compose, compose.variants['Everything'], 'x86_64', cfg), 1) - self.assertEqual(get_dir_from_scm.call_args_list, - [mock.call({'scm': 'git', 'repo': 'https://git.fedorahosted.org/git/fedora-atomic.git', - 'branch': 'f24', 'dir': '.'}, - self.topdir + '/work/atomic/config_repo', logger=pool._logger)]) self.assertEqual(koji.get_runroot_cmd.call_args_list, [mock.call('rrt', 'x86_64', ['pungi-make-ostree', '--log-dir={}/logs/x86_64/atomic'.format(self.topdir), '--treefile=fedora-atomic-docker-host.json', + '--config-url=https://git.fedorahosted.org/git/fedora-atomic.git', + '--config-branch=f24', + '--source-repo={}/compose/Everything/x86_64/os'.format(self.topdir), '/other/place/for/atomic'], channel=None, mounts=[self.topdir], packages=['pungi', 'ostree', 'rpm-ostree'],