make-packaging-target.py 10.2 KB
Newer Older
aviau's avatar
aviau committed
1 2
#!/usr/bin/env python3
#
3
# Copyright (C) 2016-2017 Savoir-faire Linux Inc.
aviau's avatar
aviau committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# Author: Alexandre Viau <alexandre.viau@savoirfairelinux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Creates packaging targets for a distribution and architecture.
Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
21
# This helps reduce the length of the top Makefile.
aviau's avatar
aviau committed
22 23 24 25 26 27 28 29 30
#

import argparse

target_template = """\
##
## Distro: %(distribution)s
##

31 32 33 34 35 36 37 38 39
# We don't simply use ring-packaging-distro as the docker image name because
# we want to be able to build multiple versions of the same distro at the
# same time and it could result in race conditions on the machine as we would
# overwrite the docker image of other builds.
#
# This does not impact caching as the docker daemon does not care about the image
# names, just about the contents of the Dockerfile.
PACKAGE_%(distribution)s_DOCKER_IMAGE_NAME:=ring-packaging-%(distribution)s$(RING_PACKAGING_IMAGE_SUFFIX)
PACKAGE_%(distribution)s_DOCKER_IMAGE_FILE:=.docker-image-$(PACKAGE_%(distribution)s_DOCKER_IMAGE_NAME)
40
DOCKER_EXTRA_ARGS =
41

42
PACKAGE_%(distribution)s_DOCKER_RUN_COMMAND = docker run \\
aviau's avatar
aviau committed
43 44 45
    --rm \\
    -e RELEASE_VERSION=$(RELEASE_VERSION) \\
    -e RELEASE_TARBALL_FILENAME=$(RELEASE_TARBALL_FILENAME) \\
46
    -e DEBIAN_VERSION=%(version)s \\
aviau's avatar
aviau committed
47 48
    -e DEBIAN_PACKAGING_OVERRIDE=%(debian_packaging_override)s \\
    -e CURRENT_UID=$(CURRENT_UID) \\
aviau's avatar
aviau committed
49
    -e DISTRIBUTION=%(distribution)s \\
aviau's avatar
aviau committed
50 51
    -v $(CURDIR):/opt/ring-project-ro:ro \\
    -v $(CURDIR)/packages/%(distribution)s:/opt/output \\
52
    -t $(DOCKER_EXTRA_ARGS) %(options)s \\
53
    $(PACKAGE_%(distribution)s_DOCKER_IMAGE_NAME)
aviau's avatar
aviau committed
54

55
$(PACKAGE_%(distribution)s_DOCKER_IMAGE_FILE): docker/Dockerfile_%(docker_image)s
aviau's avatar
aviau committed
56
	docker build \\
57
        -t $(PACKAGE_%(distribution)s_DOCKER_IMAGE_NAME) \\
58
        -f docker/Dockerfile_%(docker_image)s \\
aviau's avatar
aviau committed
59
        $(CURDIR)
60
	touch $(PACKAGE_%(distribution)s_DOCKER_IMAGE_FILE)
aviau's avatar
aviau committed
61 62 63 64

packages/%(distribution)s:
	mkdir -p packages/%(distribution)s

aviau's avatar
aviau committed
65
packages/%(distribution)s/%(output_file)s: $(RELEASE_TARBALL_FILENAME) packages/%(distribution)s $(PACKAGE_%(distribution)s_DOCKER_IMAGE_FILE)
aviau's avatar
aviau committed
66 67 68 69
	$(PACKAGE_%(distribution)s_DOCKER_RUN_COMMAND)
	touch packages/%(distribution)s/*

.PHONY: package-%(distribution)s
aviau's avatar
aviau committed
70
package-%(distribution)s: packages/%(distribution)s/%(output_file)s
aviau's avatar
aviau committed
71 72

.PHONY: package-%(distribution)s-interactive
73
package-%(distribution)s-interactive: DOCKER_EXTRA_ARGS = -i
74
package-%(distribution)s-interactive: $(RELEASE_TARBALL_FILENAME) packages/%(distribution)s $(PACKAGE_%(distribution)s_DOCKER_IMAGE_FILE)
aviau's avatar
aviau committed
75 76 77 78
	$(PACKAGE_%(distribution)s_DOCKER_RUN_COMMAND) bash
"""


79 80 81 82 83
def generate_target(distribution, debian_packaging_override, output_file, options='', docker_image='', version=''):
    if (docker_image == ''):
        docker_image = distribution
    if (version == ''):
        version = "$(DEBIAN_VERSION)"
aviau's avatar
aviau committed
84 85
    return target_template % {
        "distribution": distribution,
86
        "docker_image": docker_image,
aviau's avatar
aviau committed
87
        "debian_packaging_override": debian_packaging_override,
aviau's avatar
aviau committed
88
        "output_file": output_file,
89
        "options": options,
90
        "version": version,
aviau's avatar
aviau committed
91 92 93 94 95 96 97 98 99 100 101 102
    }


def run_generate(parsed_args):
    print(generate_target(parsed_args.distribution,
                          parsed_args.debian_packaging_override))


def run_generate_all(parsed_args):
    targets = [
        # Debian
        {
aviau's avatar
aviau committed
103
            "distribution": "debian_9",
aviau's avatar
aviau committed
104
            "debian_packaging_override": "",
aviau's avatar
aviau committed
105
            "output_file": "$(DEBIAN_DSC_FILENAME)",
aviau's avatar
aviau committed
106 107
        },
        {
aviau's avatar
aviau committed
108
            "distribution": "debian_9_i386",
aviau's avatar
aviau committed
109
            "debian_packaging_override": "",
aviau's avatar
aviau committed
110
            "output_file": "$(DEBIAN_DSC_FILENAME)",
aviau's avatar
aviau committed
111
        },
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        {
            "distribution": "debian_9_oci",
            "docker_image": "debian_9",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
        {
            "distribution": "debian_9_i386_oci",
            "docker_image": "debian_9_i386",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
aviau's avatar
aviau committed
128 129
        # Ubuntu
        {
aviau's avatar
aviau committed
130
            "distribution": "ubuntu_16.04",
aviau's avatar
aviau committed
131
            "debian_packaging_override": "",
aviau's avatar
aviau committed
132
            "output_file": "$(DEBIAN_DSC_FILENAME)",
aviau's avatar
aviau committed
133 134
        },
        {
aviau's avatar
aviau committed
135
            "distribution": "ubuntu_16.04_i386",
aviau's avatar
aviau committed
136
            "debian_packaging_override": "",
aviau's avatar
aviau committed
137 138
            "output_file": "$(DEBIAN_DSC_FILENAME)",
        },
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        {
            "distribution": "ubuntu_16.04_oci",
            "docker_image": "ubuntu_16.04",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
        {
            "distribution": "ubuntu_16.04_i386_oci",
            "docker_image": "ubuntu_16.04_i386",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
155
        {
156
            "distribution": "ubuntu_18.04",
157 158 159 160
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_DSC_FILENAME)",
        },
        {
161
            "distribution": "ubuntu_18.04_i386",
162 163 164
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_DSC_FILENAME)",
        },
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        {
            "distribution": "ubuntu_18.04_oci",
            "docker_image": "ubuntu_18.04",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
        {
            "distribution": "ubuntu_18.04_i386_oci",
            "docker_image": "ubuntu_18.04_i386",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
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
        {
            "distribution": "ubuntu_18.10",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_DSC_FILENAME)",
        },
        {
            "distribution": "ubuntu_18.10_i386",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_DSC_FILENAME)",
        },
        {
            "distribution": "ubuntu_18.10_oci",
            "docker_image": "ubuntu_18.10",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
        {
            "distribution": "ubuntu_18.10_i386_oci",
            "docker_image": "ubuntu_18.10_i386",
            "debian_packaging_override": "",
            "output_file": "$(DEBIAN_OCI_DSC_FILENAME)",
            "options": "-e OVERRIDE_PACKAGING_DIR=$(DEBIAN_OCI_PKG_DIR)",
            "version": "$(DEBIAN_OCI_VERSION)",
        },
aviau's avatar
aviau committed
207
        # Fedora
208
        {
209
            "distribution": "fedora_27",
210 211 212 213
            "debian_packaging_override": "",
            "output_file": ".packages-built",
        },
        {
214
            "distribution": "fedora_27_i386",
215 216 217
            "debian_packaging_override": "",
            "output_file": ".packages-built",
        },
Guillaume Roguez's avatar
Guillaume Roguez committed
218
        {
219
            "distribution": "fedora_28",
Guillaume Roguez's avatar
Guillaume Roguez committed
220 221
            "debian_packaging_override": "",
            "output_file": ".packages-built",
222
            "options": "--security-opt seccomp=./docker/profile-seccomp-fedora_28.json --privileged",
Guillaume Roguez's avatar
Guillaume Roguez committed
223 224
        },
        {
225
            "distribution": "fedora_28_i386",
Guillaume Roguez's avatar
Guillaume Roguez committed
226 227
            "debian_packaging_override": "",
            "output_file": ".packages-built",
228
            "options": "--security-opt seccomp=./docker/profile-seccomp-fedora_28.json --privileged",
Guillaume Roguez's avatar
Guillaume Roguez committed
229
        },
Sébastien Blin's avatar
Sébastien Blin committed
230
        {
231
            "distribution": "fedora_29",
Sébastien Blin's avatar
Sébastien Blin committed
232 233
            "debian_packaging_override": "",
            "output_file": ".packages-built",
234
            "options": "--security-opt seccomp=./docker/profile-seccomp-fedora_28.json --privileged",
Sébastien Blin's avatar
Sébastien Blin committed
235 236
        },
        {
237
            "distribution": "fedora_29_i386",
Sébastien Blin's avatar
Sébastien Blin committed
238 239
            "debian_packaging_override": "",
            "output_file": ".packages-built",
240
            "options": "--security-opt seccomp=./docker/profile-seccomp-fedora_28.json --privileged",
Sébastien Blin's avatar
Sébastien Blin committed
241
        },
242 243 244 245 246 247
        # Gentoo
        {
            "distribution": "gentoo",
            "debian_packaging_override": "",
            "output_file": ".packages-built",
        },
aviau's avatar
aviau committed
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

    ]

    for target in targets:
        print(generate_target(**target))


def parse_args():
    ap = argparse.ArgumentParser(
        description="Packaging targets generation tool"
    )

    ga = ap.add_mutually_exclusive_group(required=True)

    # Action arguments
    ga.add_argument('--generate',
                    action='store_true',
                    help='Generate a single packaging target')
    ga.add_argument('--generate-all',
                    action='store_true',
                    help='Generates all packaging targets')

    # Parameters
    ap.add_argument('--distribution')
    ap.add_argument('--architecture')
    ap.add_argument('--debian_packaging_override', default='')
aviau's avatar
aviau committed
274
    ap.add_argument('--output_file')
aviau's avatar
aviau committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

    parsed_args = ap.parse_args()

    return parsed_args


def main():
    parsed_args = parse_args()

    if parsed_args.generate:
        run_generate(parsed_args)
    elif parsed_args.generate_all:
        run_generate_all(parsed_args)

if __name__ == "__main__":
    main()