Debian Codegraph @

spark/runners/image_build.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2017-2021 Matthias Klumpp <[email protected]>
4 #
5 # Licensed under the GNU Lesser General Public License Version 3
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the license, or
10 # (at your option) any later version.
11 #
12 # This software is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this software. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import glob
22 import shlex
23
24 from spark.utils import RunnerError, RunnerResult
25 from spark.utils.command import safe_run, run_logged
26 from spark.utils.workspace import make_commandfile, debspawn_run_commandfile
27
28
29 def get_version():
30 return ('imagebuild', '0.1')
31
32
33 def build_image(
34 jlog, host_arch: str, job, jdata
35 ) -> tuple[RunnerResult, list[os.PathLike | str] | None, os.PathLike | None]:
36 '''
37 Build an image using the provided recipe (usually utilizing debos)
38 '''
39 distro_name = jdata.get('distribution')
40 suite_name = jdata.get('suite')
41 build_arch = jdata.get('architecture')
42 image_format = jdata.get('image_format', 'iso')
43 env_name = jdata.get('environment')
44 image_style = jdata.get('style')
45
46 # clone the image build recipe repository
47 safe_run(['git', 'clone', '--depth=1', jdata.get('git_url'), 'ib'])
48 run_logged(jlog, ['git', 'log', '--pretty=oneline', '-1'], cwd=os.path.abspath('ib'))
49
50 # test if we have a prepare script and something to cache
51 init_script = os.path.join('ib', 'prepare.sh')
52 init_commands = []
53 cache_key = None
54 if os.path.isfile(init_script):
55 cache_key = 'mkimage-{}-{}-{}'.format(
56 image_format, env_name if env_name else 'any', image_style if image_style else 'any'
57 )
58 init_commands.append('export DEBIAN_FRONTEND=noninteractive')
59 init_commands.append('cd /srv/build')
60 init_commands.append('exec ./prepare.sh')
61
62 # construct build recipe
63 if not os.path.isfile(os.path.join('ib', 'build.sh')):
64 raise RunnerError('No "build.sh" script found to build the image')
65 commands = []
66 commands.append('export DEBIAN_FRONTEND=noninteractive')
67 commands.append('export IB_SUITE="{}"'.format(shlex.quote(suite_name)))
68 commands.append('export IB_ENVIRONMENT="{}"'.format(shlex.quote(env_name)))
69 commands.append('export IB_IMAGE_STYLE="{}"'.format(shlex.quote(image_style)))
70 commands.append('export IB_TARGET_ARCH="{}"'.format(shlex.quote(build_arch)))
71 commands.append('systemd-machine-id-setup')
72 commands.append('cd /srv/build')
73 commands.append('exec ./build.sh')
74
75 with make_commandfile(jlog.job_id, init_commands) as shi_fname:
76 with make_commandfile(jlog.job_id, commands) as shc_fname:
77 ret, _ = debspawn_run_commandfile(
78 jlog,
79 suite_name,
80 host_arch,
81 build_dir=os.path.abspath('ib'),
82 artifacts_dir=os.path.abspath('artifacts'),
83 init_script=shi_fname if init_commands else None,
84 command_script=shc_fname,
85 header='{} {} image build for {} {} [{}]'.format(
86 distro_name, image_format.upper(), suite_name, env_name, image_style
87 ),
88 allow_kvm=True,
89 cache_key=cache_key,
90 )
91 if ret != 0:
92 return RunnerResult.FAILURE, None, None
93
94 # collect list of files to upload
95 files: list[str | os.PathLike] = []
96 for f in glob.glob('artifacts/*'):
97 files.append(os.path.abspath(f))
98
99 return RunnerResult.SUCCESS, files, None
100
101
102 def run(
103 jlog, job, jdata
104 ) -> tuple[RunnerResult, list[os.PathLike | str] | None, os.PathLike | None]:
105 suite_name = jdata.get('suite')
106 arch = job.get('architecture')
107 if not suite_name:
108 return RunnerResult.FAILURE, None, None
109 if not arch:
110 return RunnerResult.FAILURE, None, None
111
112 return build_image(jlog, arch, job, jdata)