[ostree] Move cloning config repo to chroot

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-04-05 08:40:54 +02:00
parent 5ff2d9f957
commit ad87ea2d28
5 changed files with 79 additions and 41 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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')])

View File

@ -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'],