File: docker-utils

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (152 lines) | stat: -rwxr-xr-x 4,739 bytes parent folder | download
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
#!/usr/bin/env python

"""
 This source file is part of the Swift.org open source project

 Copyright (c) 2018 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See http://swift.org/LICENSE.txt for license information
 See http://swift.org/CONTRIBUTORS.txt for Swift project authors

 -------------------------------------------------------------------------
 This script contains helper operations to use Docker for llbuild development
 and testing.
"""

import argparse
import urllib2
import os
import shutil
import subprocess
import tempfile
import yaml


defaultTarget = "16.04"

def call(args):
    """Prints and executes a command."""
    print(" ".join(args))
    return subprocess.call(args)

def build(args):
    """Builds a docker image with the latest snapshot."""
    target = args.target
    targetNoDot = target.replace(".", "")

    # Create a temporary directory
    tmpdir = tempfile.mkdtemp()

    try:
        # Get latest snapshot info.
        latest_build_url = "https://swift.org/builds/development/ubuntu{}/latest-build.yml".format(targetNoDot)
        latest_build_data = yaml.safe_load(urllib2.urlopen(latest_build_url).read())
        snapshot_filename = latest_build_data["download"]
        # FIXME: We shouldn"t need to do this, it should be available in the API.
        snapshot_name = snapshot_filename.replace("-ubuntu{}.tar.gz".format(target), "")
        base_url = latest_build_url.rsplit("/", 1)[0]
        latest_snapshot_url = base_url + "/" + snapshot_name + "/" + snapshot_filename
        docker_dir = os.path.dirname(os.path.realpath(__file__))

        # Check if we already have the desired snapshot image
        curImages = subprocess.check_output([
            "docker",
            "images",
            "--filter", "reference=llbuild-docker-{}:latest".format(targetNoDot),
            "--filter", "label=swift-snapshot={}".format(snapshot_name),
            "--format", "{{.ID}}"
        ])

        # Download latest snapshot (if necessary).
        if len(curImages.split()) == 1:
            print("Image exists for {} with {}".format(target, snapshot_name))
            exit(0)
        else:
            result = call([
                "docker",
                "rmi", "-f",
                "llbuild-docker-{}".format(target)
            ])

            if result != 0:
                print("Unable to clean prior images")
                exit(1)

            result = call([
                "curl",
                "-o",
                os.path.join(tmpdir, snapshot_filename),
                latest_snapshot_url
            ])

            if result != 0:
                print("Unable to fetch snapshot")
                exit(1)

        # Create docker image.
        call([
            "docker",
            "build",
            "-t", "llbuild-docker-{}".format(targetNoDot),
            "--label", "swift-snapshot={}".format(snapshot_name),
            "--build-arg", "SNAPSHOT=%s" % snapshot_filename,
            "--file", os.path.join(docker_dir, "Dockerfile-{}".format(target)),
            tmpdir
        ])
    finally:
        shutil.rmtree(tmpdir)

def run(args):
    """Runs an executable in the container."""
    target = args.target
    targetNoDot = target.replace(".", "")

    call([
        "docker",
        "run",
        "-it",
        "--cap-add=SYS_PTRACE",
        "--security-opt", "seccomp=unconfined",
        "-v", "%s:/llbuild" % os.getcwd(),
        "-w", "/llbuild",
        "--rm",
        "llbuild-docker-{}".format(targetNoDot)
    ] + args.command)

def main():
    """Main script entry-point."""

    parser = argparse.ArgumentParser(
        usage="%(prog)s [build|run]",
        description="This script simplifies all the docker operations to build "
                    "and run a container for LLBuild development and testing "
                    "on Linux.")
    subparsers = parser.add_subparsers(dest='command')

    # build
    parser_build = subparsers.add_parser(
        "build",
        help="builds a docker image from the latest snapshot.")
    parser_build.add_argument(
        "-t", "--target",
        help="The target platform",
        default=defaultTarget)
    parser_build.set_defaults(func=build)

    # run
    parser_run = subparsers.add_parser(
        "run",
        help="runs a command in a container.")
    parser_run.add_argument(
        "-t", "--target",
        help="The target platform",
        default=defaultTarget)
    parser_run.add_argument("command", help="the command to run with its arguments", nargs="*")
    parser_run.set_defaults(func=run)

    args = parser.parse_args()
    args.func(args)

if __name__ == "__main__":
    main()