aboutsummaryrefslogtreecommitdiff
path: root/lcitool/formatters.py
blob: ec4b580469a12b60157cddefbed7eee1cd550dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# formatters.py - module containing various recipe formatting backends
#
# Copyright (C) 2017-2020 Red Hat, Inc.
#
# SPDX-License-Identifier: GPL-2.0-or-later

import abc
import json
import logging
import shlex

from lcitool import util, LcitoolError
from lcitool.packages import package_names_by_type


log = logging.getLogger(__name__)


class FormatterError(LcitoolError):
    """
    Global exception type for this module.

    Contains a detailed message coming from one of its subclassed exception
    types.
    """

    pass


class DockerfileError(FormatterError):
    def __init__(self, message):
        super().__init__(message, "Docker formatter")


class VariablesError(FormatterError):
    def __init__(self, message):
        super().__init__(message, "Variables formatter")


class ShellBuildEnvError(FormatterError):
    def __init__(self, message):
        super().__init__(message, "Shell build env formatter")


class Formatter(metaclass=abc.ABCMeta):
    """
    This an abstract base class that each formatter must subclass.
    """

    def __init__(self, projects):
        self._projects = projects

    @abc.abstractmethod
    def format(self):
        """
        Outputs a recipe using format implemented by a Foo(Formatter) subclass

        Given the input, this method will generate and output an instruction
        recipe (or a configuration file in general) using the format of the
        subclassed formatter. Each formatter must implement this method.

        Returns a formatted recipe as string.
        """
        pass

    def _get_meson_cross(self, cross_abi):
        cross_path = util.package_resource(__package__,
                                           f"cross/{cross_abi}.meson")
        with open(cross_path, "r") as c:
            return c.read().rstrip()

    def _generator_build_varmap(self,
                                target,
                                selected_projects):
        projects = self._projects

        # we need the 'base' internal project here, but packages for internal
        # projects are not resolved via the public API, so it requires special
        # handling
        pkgs = {}
        pkgs.update(projects.internal["base"].get_packages(target))

        # we can now load packages for the rest of the projects
        pkgs.update(projects.get_packages(selected_projects, target))
        package_names = package_names_by_type(pkgs)

        varmap = {
            "packaging_command": target.facts["packaging"]["command"],
            "paths_ccache": target.facts["paths"]["ccache"],
            "paths_make": target.facts["paths"]["make"],
            "paths_ninja": target.facts["paths"]["ninja"],
            "paths_python": target.facts["paths"]["python"],
            "paths_pip3": target.facts["paths"]["pip3"],

            "cross_arch": None,
            "cross_abi": None,
            "cross_arch_deb": None,

            "mappings": [pkg.mapping for pkg in pkgs.values()],
            "pkgs": package_names["native"],
            "cross_pkgs": package_names["cross"],
            "pypi_pkgs": package_names["pypi"],
            "cpan_pkgs": package_names["cpan"],
        }

        if target.cross_arch:
            varmap["cross_arch"] = target.cross_arch
            varmap["cross_abi"] = util.native_arch_to_abi(target.cross_arch)

            if target.facts["packaging"]["format"] == "deb":
                cross_arch_deb = util.native_arch_to_deb_arch(target.cross_arch)
                varmap["cross_arch_deb"] = cross_arch_deb

        log.debug(f"Generated varmap: {varmap}")
        return varmap


class BuildEnvFormatter(Formatter):

    def __init__(self, inventory, indent=0, pkgcleanup=False, nosync=False):
        super().__init__(inventory)
        self._indent = indent
        self._pkgcleanup = pkgcleanup
        self._nosync = nosync

    def _align(self, command, strings):
        if len(strings) == 1:
            return strings[0]

        align = " \\\n" + (" " * (self._indent + len(command + " ")))
        strings = [shlex.quote(x) for x in strings]
        return align[1:] + align.join(strings)

    def _generator_build_varmap(self,
                                target,
                                selected_projects):
        varmap = super()._generator_build_varmap(target,
                                                 selected_projects)

        varmap["nosync"] = ""
        if self._nosync:
            if target.facts["packaging"]["format"] == "deb":
                varmap["nosync"] = "eatmydata "
            elif target.facts["packaging"]["format"] == "rpm" and target.facts["os"]["name"] == "Fedora":
                varmap["nosync"] = "nosync "
            elif target.facts["packaging"]["format"] == "apk":
                # TODO: 'libeatmydata' package is present in 'testing' repo
                # for Alpine Edge. Once it graduates to 'main' repo we
                # should use it here, and see later comment about adding
                # the package too
                # varmap["nosync"] = "eatmydata "
                pass

        nosync = varmap["nosync"]
        varmap["pkgs"] = self._align(nosync + target.facts["packaging"]["command"],
                                     varmap["pkgs"])

        if varmap["cross_pkgs"]:
            varmap["cross_pkgs"] = self._align(nosync + target.facts["packaging"]["command"],
                                               varmap["cross_pkgs"])
        if varmap["pypi_pkgs"]:
            varmap["pypi_pkgs"] = self._align(nosync + target.facts["paths"]["pip3"],
                                              varmap["pypi_pkgs"])
        if varmap["cpan_pkgs"]:
            varmap["cpan_pkgs"] = self._align(nosync + "cpanm",
                                              varmap["cpan_pkgs"])

        return varmap

    def _format_commands_ccache(self, target, varmap):
        commands = []
        compilers = set()

        if "ccache" not in varmap["mappings"]:
            return []

        for compiler in ["gcc", "clang"]:
            if compiler in varmap["mappings"]:
                compilers.add(compiler)
                compilers.add("cc")
        for compiler in ["g++"]:
            if compiler in varmap["mappings"]:
                compilers.add(compiler)
                compilers.add("c++")

        if compilers:
            commands.extend([
                "mkdir -p /usr/libexec/ccache-wrappers",
            ])

            for compiler in sorted(compilers):
                if target.cross_arch:
                    compiler = "{cross_abi}-" + compiler
                commands.extend([
                    "ln -s {paths_ccache} /usr/libexec/ccache-wrappers/" + compiler,
                ])
        return commands

    def _format_commands_pkglist(self, target):
        facts = target.facts
        commands = []
        if facts["packaging"]["format"] == "apk":
            commands.extend(["apk list | sort > /packages.txt"])
        elif facts["packaging"]["format"] == "deb":
            commands.extend([
                "dpkg-query --showformat '${{Package}}_${{Version}}_${{Architecture}}\\n' --show > /packages.txt"
            ])
        elif facts["packaging"]["format"] == "rpm":
            commands.extend(["rpm -qa | sort > /packages.txt"])
        return commands

    def _format_commands_native(self, target, varmap):
        facts = target.facts
        commands = []
        osname = facts["os"]["name"]
        osversion = facts["os"]["version"]

        if facts["packaging"]["format"] == "apk":
            # See earlier comment about adding this later
            # "{packaging_command} add libeatmydata",
            commands.extend([
                "{packaging_command} update",
                "{packaging_command} upgrade"])

            commands.extend([
                "{nosync}{packaging_command} add {pkgs}",
            ])
        elif facts["packaging"]["format"] == "deb":
            commands.extend([
                "export DEBIAN_FRONTEND=noninteractive",
                "{packaging_command} update"])
            if varmap["nosync"] != "":
                commands.extend(["{packaging_command} install -y eatmydata"])
            commands.extend([
                "{nosync}{packaging_command} dist-upgrade -y",
                "{nosync}{packaging_command} install --no-install-recommends -y {pkgs}"])
            if self._pkgcleanup:
                commands.extend([
                    "{nosync}{packaging_command} autoremove -y",
                    "{nosync}{packaging_command} autoclean -y",
                ])
            commands.extend([
                "sed -Ei 's,^# (en_US\\.UTF-8 .*)$,\\1,' /etc/locale.gen",
                "dpkg-reconfigure locales",
            ])
        elif facts["packaging"]["format"] == "rpm":
            # Rawhide needs this because the keys used to sign packages are
            # cycled from time to time
            if osname == "Fedora" and osversion == "Rawhide":
                commands.extend([
                    "{packaging_command} update -y --nogpgcheck fedora-gpg-keys",
                ])

            if osname == "Fedora" and varmap["nosync"] != "":
                nosyncsh = [
                    "#!/bin/sh",
                    "if test -d /usr/lib64",
                    "then",
                    "    export LD_PRELOAD=/usr/lib64/nosync/nosync.so",
                    "else",
                    "    export LD_PRELOAD=/usr/lib/nosync/nosync.so",
                    "fi",
                    "exec \"$@\""
                ]
                varmap["nosyncsh"] = "\\n\\\n".join(nosyncsh)

                commands.extend([
                    "{packaging_command} install -y nosync",
                    "printf '{nosyncsh}\\n' > /usr/bin/nosync",
                    "chmod +x /usr/bin/nosync"])

            # First we need to run update, then config and install.
            # For rolling distros, it's preferable to do a distro syncing type
            # of update rather than a regular package update
            if (osname == "Fedora" and osversion == "Rawhide" or
                osname == "CentOS" and (osversion == "Stream8" or
                                        osversion == "Stream9")):
                commands.extend(["{nosync}{packaging_command} distro-sync -y"])
            elif osname == "OpenSUSE" and osversion == "Tumbleweed":
                commands.extend(["{nosync}{packaging_command} dist-upgrade -y"])
            else:
                commands.extend(["{nosync}{packaging_command} update -y"])

            if osname in ["AlmaLinux", "CentOS"]:
                # NOTE: AlmaLinux is one of the replacement community distros
                # for the original CentOS distro and so the checks below apply
                # there as well
                #
                # Starting with CentOS 8, most -devel packages are shipped in
                # a separate repository which is not enabled by default. The
                # name of this repository has changed over time
                commands.extend([
                    "{nosync}{packaging_command} install 'dnf-command(config-manager)' -y",
                ])
                if osversion in ["9", "Stream9"]:
                    commands.extend([
                        "{nosync}{packaging_command} config-manager --set-enabled -y crb",
                    ])
                if osversion in ["8", "Stream8"]:
                    commands.extend([
                        "{nosync}{packaging_command} config-manager --set-enabled -y powertools",
                    ])

                # Not all of the virt related -devel packages are provided by
                # virt:rhel module so we have to enable AV repository as well.
                # CentOS Stream 9 no longer uses modules for virt
                if osversion in ["8", "Stream8"]:
                    commands.extend([
                        "{nosync}{packaging_command} install -y centos-release-advanced-virtualization",
                    ])

                # Some of the packages we need are not part of CentOS proper
                # and are only available through EPEL
                commands.extend([
                    "{nosync}{packaging_command} install -y epel-release",
                ])

                # For CentOS Stream, we want EPEL Next as well
                if osversion in ["Stream8", "Stream9"]:
                    commands.extend([
                        "{nosync}{packaging_command} install -y epel-next-release",
                    ])

            commands.extend(["{nosync}{packaging_command} install -y {pkgs}"])

            if self._pkgcleanup:
                # openSUSE doesn't seem to have a convenient way to remove all
                # unnecessary packages, but CentOS and Fedora do
                if osname == "OpenSUSE":
                    commands.extend([
                        "{nosync}{packaging_command} clean --all",
                    ])
                else:
                    commands.extend([
                        "{nosync}{packaging_command} autoremove -y",
                        "{nosync}{packaging_command} clean all -y",
                    ])

        # If distro forces "pip" to use a venv by default,
        # then undo that, because our CI env is expected to
        # have a mix of distro & pip installed pieces
        if "python3" in varmap["mappings"]:
            commands.extend(["rm -f /usr/lib*/python3*/EXTERNALLY-MANAGED"])

        if not target.cross_arch:
            commands.extend(self._format_commands_pkglist(target))
            commands.extend(self._format_commands_ccache(target, varmap))

        commands = [c.format(**varmap) for c in commands]

        groups = [commands]
        if varmap["pypi_pkgs"]:
            groups.append(["{paths_pip3} install {pypi_pkgs}".format(**varmap)])

        if varmap["cpan_pkgs"]:
            groups.append(["cpanm --notest {cpan_pkgs}".format(**varmap)])

        return groups

    def _format_env_native(self, varmap):
        env = {}

        env["LANG"] = "en_US.UTF-8"
        if "make" in varmap["mappings"]:
            env["MAKE"] = varmap["paths_make"]
        if "meson" in varmap["mappings"]:
            env["NINJA"] = varmap["paths_ninja"]
        if "python3" in varmap["mappings"]:
            env["PYTHON"] = varmap["paths_python"]
        if "ccache" in varmap["mappings"]:
            env["CCACHE_WRAPPERSDIR"] = "/usr/libexec/ccache-wrappers"

        return env

    def _format_commands_foreign(self, target, varmap):
        facts = target.facts
        cross_commands = []

        if facts["packaging"]["format"] == "deb":
            cross_commands.extend([
                "export DEBIAN_FRONTEND=noninteractive",
                "dpkg --add-architecture {cross_arch_deb}",
            ])
            if target.cross_arch == "riscv64":
                cross_commands.extend([
                    "{nosync}{packaging_command} install debian-ports-archive-keyring",
                    "{nosync}echo 'deb http://ftp.ports.debian.org/debian-ports/ sid main' > /etc/apt/sources.list.d/ports.list",
                    "{nosync}echo 'deb http://ftp.ports.debian.org/debian-ports/ unreleased main' >> /etc/apt/sources.list.d/ports.list",
                ])
            cross_commands.extend([
                "{nosync}{packaging_command} update",
                "{nosync}{packaging_command} dist-upgrade -y",
                "{nosync}{packaging_command} install --no-install-recommends -y dpkg-dev",
            ])
            if varmap["cross_pkgs"]:
                cross_commands.extend([
                    "{nosync}{packaging_command} install --no-install-recommends -y {cross_pkgs}",
                ])
            if self._pkgcleanup:
                cross_commands.extend([
                    "{nosync}{packaging_command} autoremove -y",
                    "{nosync}{packaging_command} autoclean -y",
                ])
        elif facts["packaging"]["format"] == "rpm":
            if varmap["cross_pkgs"]:
                cross_commands.extend([
                    "{nosync}{packaging_command} install -y {cross_pkgs}",
                ])
            if self._pkgcleanup:
                cross_commands.extend([
                    "{nosync}{packaging_command} clean all -y",
                ])

        if not target.cross_arch.startswith("mingw"):
            cross_commands.extend([
                "mkdir -p /usr/local/share/meson/cross",
                "printf \"{cross_meson}\\n\" > /usr/local/share/meson/cross/{cross_abi}",
            ])

            cross_meson = self._get_meson_cross(varmap["cross_abi"])
            varmap["cross_meson"] = cross_meson.replace("\n", "\\n\\\n")

        cross_commands.extend(self._format_commands_pkglist(target))
        cross_commands.extend(self._format_commands_ccache(target, varmap))

        cross_commands = [c.format(**varmap) for c in cross_commands]

        return cross_commands

    def _format_env_foreign(self, target, varmap):
        env = {}
        env["ABI"] = varmap["cross_abi"]

        if "autoconf" in varmap["mappings"]:
            env["CONFIGURE_OPTS"] = "--host=" + varmap["cross_abi"]

        if "meson" in varmap["mappings"]:
            if target.cross_arch.startswith("mingw"):
                env["MESON_OPTS"] = "--cross-file=/usr/share/mingw/toolchain-" + varmap["cross_arch"] + ".meson"
            else:
                env["MESON_OPTS"] = "--cross-file=" + varmap["cross_abi"]

        return env


class DockerfileFormatter(BuildEnvFormatter):

    def __init__(self, inventory, base=None, layers="all"):
        super().__init__(inventory,
                         indent=len("RUN "),
                         pkgcleanup=True,
                         nosync=True)
        self._base = base
        self._layers = layers

    @staticmethod
    def _format_env(env):
        lines = []
        for key in sorted(env.keys()):
            val = env[key]
            lines.append(f"\nENV {key} \"{val}\"")
        return "".join(lines)

    def _format_section_base(self, target):
        strings = []
        if self._base:
            base = self._base
        else:
            base = target.facts["containers"]["base"]
        strings.append(f"FROM {base}")
        return strings

    def _format_section_native(self, target, varmap):
        groups = self._format_commands_native(target, varmap)

        strings = []
        for commands in groups:
            strings.append("\nRUN " + " && \\\n    ".join(commands))

        env = self._format_env_native(varmap)
        strings.append(self._format_env(env))
        return strings

    def _format_section_foreign(self, target, varmap):
        commands = self._format_commands_foreign(target, varmap)

        strings = ["\nRUN " + " && \\\n    ".join(commands)]

        env = self._format_env_foreign(target, varmap)
        strings.append(self._format_env(env))
        return strings

    def _format_dockerfile(self, target, project, varmap):
        strings = []
        strings.extend(self._format_section_base(target))
        if self._layers in ["all", "native"]:
            strings.extend(self._format_section_native(target, varmap))
        if target.cross_arch and self._layers in ["all", "foreign"]:
            strings.extend(self._format_section_foreign(target, varmap))
        return strings

    def format(self, target, selected_projects):
        """
        Generates and formats a Dockerfile.

        Given the application commandline arguments, this function will take
        the projects and inventory attributes and generate a Dockerfile
        describing an environment for doing a project build on a given
        inventory platform.

        :param args: Application class' command line arguments
        :returns: String represented Dockerfile
        """

        log.debug(f"Generating Dockerfile for projects '{selected_projects}' "
                  f"on target {target}")

        # We can only generate Dockerfiles for Linux
        if (target.facts["packaging"]["format"] not in ["apk", "deb", "rpm"]):
            raise DockerfileError(f"Target {target} doesn't support this generator")

        try:
            varmap = self._generator_build_varmap(target, selected_projects)
        except FormatterError as ex:
            raise DockerfileError(str(ex))

        return '\n'.join(self._format_dockerfile(target, selected_projects, varmap))


class VariablesFormatter(Formatter):
    @staticmethod
    def _normalize_variables(varmap):
        normalized_vars = {}
        for key in varmap:
            if varmap[key] is None:
                continue

            if key == "mappings":
                # For internal use only
                continue

            if key.startswith("paths_"):
                name = key[len("paths_"):]
            else:
                name = key
            normalized_vars[name] = varmap[key]

        return normalized_vars

    @staticmethod
    @abc.abstractmethod
    def _format_variables(varmap):
        pass

    def format(self, target, selected_projects):
        """
        Generates and formats environment variables as KEY=VAL pairs.

        Given the commandline arguments, this function will take take the
        projects and inventory attributes and generate a KEY=VAL encoded list
        of environment variables that can be consumed by various CI backends.

        :param args: Application class' command line arguments
        :returns: String represented list of environment variables
        """

        log.debug(f"Generating variables for projects '{selected_projects} on "
                  f"target {target}")

        try:
            varmap = self._generator_build_varmap(target, selected_projects)
        except FormatterError as ex:
            raise VariablesError(str(ex))

        varmap = self._normalize_variables(varmap)
        return self._format_variables(varmap)


class ShellVariablesFormatter(VariablesFormatter):
    @staticmethod
    def _format_variables(varmap):
        strings = []

        for key in sorted(varmap.keys()):
            value = varmap[key]
            if key == "pkgs" or key.endswith("_pkgs"):
                value = " ".join(varmap[key])

            uppername = key.upper()
            strings.append(f"{uppername}='{value}'")
        return "\n".join(strings)


class JSONVariablesFormatter(VariablesFormatter):
    @staticmethod
    def _format_variables(varmap):
        return json.dumps(varmap, indent="  ", sort_keys=True)


class ShellBuildEnvFormatter(BuildEnvFormatter):

    def __init__(self, inventory, base=None, layers="all"):
        super().__init__(inventory,
                         indent=len("    "),
                         pkgcleanup=False,
                         nosync=False)

    @staticmethod
    def _format_env(env):
        exp = []
        for key in sorted(env.keys()):
            val = env[key]
            exp.append(f"export {key}=\"{val}\"")
        return "\n" + "\n".join(exp)

    def _format_buildenv(self, target, project, varmap):
        strings = [
            "function install_buildenv() {",
        ]
        groups = self._format_commands_native(target, varmap)
        for commands in groups:
            strings.extend(["    " + c for c in commands])
        if target.cross_arch:
            for command in self._format_commands_foreign(target, varmap):
                strings.append("    " + command)
        strings.append("}")

        strings.append(self._format_env(self._format_env_native(varmap)))
        if target.cross_arch:
            strings.append(self._format_env(
                self._format_env_foreign(target, varmap)))
        return strings

    def format(self, target, selected_projects):
        """
        Generates and formats a Shell script for preparing a build env.

        Given the application commandline arguments, this function will take
        the projects and inventory attributes and generate a shell script
        that prepares a environment for doing a project build on a given
        inventory platform.

        :param args: Application class' command line arguments
        :returns: String represented shell script
        """

        log.debug(f"Generating Shell Build Env for projects '{selected_projects}' "
                  f"on target {target}")

        try:
            varmap = self._generator_build_varmap(target, selected_projects)
        except FormatterError as ex:
            raise ShellBuildEnvError(str(ex))

        return '\n'.join(self._format_buildenv(target, selected_projects, varmap))