Skip to content

Commit f79c00b

Browse files
committed
Add script and documentation for using docker
1 parent ee875c8 commit f79c00b

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ xcuserdata
88
*.xcscmblueprint
99
/default.profraw
1010
*.xcodeproj
11+
Utilities/Docker/*.tar.gz

Documentation/Development.md

+14
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,17 @@ $ Utilities/bootstrap --generate-xcodeproj --enable-perf-tests
121121
```
122122

123123
Then, open the generated project and run the `PerformanceTest` scheme.
124+
125+
## Testing on Linux with Docker
126+
127+
For contributors on macOS who need to test on Linux, install Docker and use the
128+
following commands:
129+
130+
```sh
131+
$ Utilities/docker-utils build # will build an image with the latest swift snapshot
132+
$ Utilities/docker-utils boostrap # will bootstrap SwiftPM on the linux container
133+
$ Utilities/docker-utils run bash # to run an interactive bash sheel in the container
134+
$ Utilities/docker-utils swift-build # to run swift-build in the container
135+
$ Utilities/docker-utils swift-test # to run swift-test in the container
136+
$ Utilities/docker-utils swift-run # to run swift-run in the container
137+
```

Utilities/Docker/Dockerfile

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
FROM ubuntu:16.04
10+
# Install related packages.
11+
RUN apt-get -q update && \
12+
apt-get -q install -y \
13+
build-essential \
14+
make \
15+
libc6-dev \
16+
clang-3.6 \
17+
curl \
18+
libedit-dev \
19+
python2.7 \
20+
python2.7-dev \
21+
libicu-dev \
22+
rsync \
23+
libxml2 \
24+
git \
25+
libcurl4-openssl-dev \
26+
vim \
27+
libblocksruntime-dev \
28+
&& update-alternatives --quiet --install /usr/bin/clang clang /usr/bin/clang-3.6 100 \
29+
&& update-alternatives --quiet --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100 \
30+
&& rm -r /var/lib/apt/lists/*
31+
ARG SNAPSHOT
32+
33+
COPY $SNAPSHOT /
34+
RUN tar -xvzf $SNAPSHOT --directory / --strip-components=1 && \
35+
rm -rf swift-DEVELOPMENT-SNAPSHOT*
36+
# Set Swift Path
37+
ENV PATH /usr/bin:$PATH
38+
# Print Installed Swift Version
39+
RUN swift --version

Utilities/docker-utils

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
This source file is part of the Swift.org open source project
5+
6+
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
7+
Licensed under Apache License v2.0 with Runtime Library Exception
8+
9+
See http://swift.org/LICENSE.txt for license information
10+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
11+
12+
-------------------------------------------------------------------------
13+
This script contains helper operations to use Docker for SwitPM development
14+
and testing.
15+
"""
16+
17+
import argparse
18+
import urllib2
19+
import os
20+
import subprocess
21+
import yaml
22+
23+
def call(args):
24+
"""Prints and executes a command."""
25+
print " ".join(args)
26+
return subprocess.call(args)
27+
28+
def docker_run(command_args):
29+
"""Runs a command in the container."""
30+
call([
31+
"docker",
32+
"run",
33+
"-it",
34+
"--security-opt", "seccomp=unconfined",
35+
"-v", os.getcwd() + ":/swiftpm",
36+
"-w", "/swiftpm",
37+
"--rm",
38+
"swiftpm-docker-1604"
39+
] + command_args)
40+
41+
def build(_):
42+
"""Builds a docker image with the latest snapshot."""
43+
44+
# Get latest snapshot info.
45+
latest_build_url = "https://swift.org/builds/development/ubuntu1604/latest-build.yml"
46+
latest_build_data = yaml.load(urllib2.urlopen(latest_build_url).read())
47+
snapshot_filename = latest_build_data["download"]
48+
# FIXME: We shouldn"t need to do this, it should be available in the API.
49+
snapshot_name = snapshot_filename.replace("-ubuntu16.04.tar.gz", "")
50+
base_url = latest_build_url.rsplit("/", 1)[0]
51+
latest_snapshot_url = base_url + "/" + snapshot_name + "/" + snapshot_filename
52+
docker_dir = os.path.dirname(os.path.realpath(__file__)) + "/Docker"
53+
54+
# Download latest snapshot (if necessary).
55+
if snapshot_filename in os.listdir(docker_dir):
56+
print "Not downloading: " + latest_snapshot_url
57+
else:
58+
# FIXME: We should remove old tarballs if we have newer ones.
59+
result = call([
60+
"curl",
61+
"-o",
62+
docker_dir + "/" + snapshot_filename,
63+
latest_snapshot_url
64+
])
65+
66+
if result != 0:
67+
print "Unable to install package: " + latest_snapshot_url
68+
exit(1)
69+
70+
# Create docker image.
71+
call([
72+
"docker",
73+
"build",
74+
"-t", "swiftpm-docker-1604",
75+
"--build-arg", "SNAPSHOT=%s" % snapshot_filename,
76+
docker_dir
77+
])
78+
79+
def run(args):
80+
"""Runs an executable in the container."""
81+
docker_run([args.executable] + args.arguments)
82+
83+
def bootstrap(args):
84+
"""Runs the bootstrap script in the container."""
85+
docker_run(["Utilities/bootstrap"] + args.arguments)
86+
87+
def build_run(args):
88+
"""Runs a built swift executable in the container."""
89+
docker_run([".build/x86_64-unknown-linux/debug/" + args.command] + args.arguments)
90+
91+
def main():
92+
"""Main script entry-point."""
93+
94+
parser = argparse.ArgumentParser(
95+
usage="%(prog)s [build|run|bootstrap|swift-build|swift-test|swift-run]",
96+
description="This script simplifies all the docker operations to build "
97+
"and run a container for SwiftPM development and testing "
98+
"on Linux.")
99+
subparsers = parser.add_subparsers(dest='command')
100+
101+
# build
102+
parser_build = subparsers.add_parser(
103+
"build",
104+
help="builds a docker image from the latest snapshot.")
105+
parser_build.set_defaults(func=build)
106+
107+
# run
108+
parser_run = subparsers.add_parser(
109+
"run",
110+
help="runs an executable in a container.")
111+
parser_run.add_argument("executable", help="the executable to run")
112+
parser_run.add_argument("arguments", nargs="*")
113+
parser_run.set_defaults(func=run)
114+
115+
# bootstrap
116+
parser_bootstrap = subparsers.add_parser(
117+
"bootstrap",
118+
help="runs the bootstrap script in a container.")
119+
parser_bootstrap.add_argument("arguments", nargs="*")
120+
parser_bootstrap.set_defaults(func=bootstrap)
121+
122+
# swift-build
123+
parser_swift_build = subparsers.add_parser(
124+
"swift-build",
125+
help="runs the swift-build executable in a container.")
126+
parser_swift_build.add_argument("arguments", nargs="*")
127+
parser_swift_build.set_defaults(func=build_run)
128+
129+
# swift-test
130+
parser_swift_test = subparsers.add_parser(
131+
"swift-test",
132+
help="runs the swift-test executable in a container.")
133+
parser_swift_test.add_argument("arguments", nargs="*")
134+
parser_swift_test.set_defaults(func=build_run)
135+
136+
# swift-run
137+
parser_swift_run = subparsers.add_parser(
138+
"swift-run",
139+
help="runs the swift-run executable in a container.")
140+
parser_swift_run.add_argument("arguments", nargs="*")
141+
parser_swift_run.set_defaults(func=build_run)
142+
143+
args = parser.parse_args()
144+
args.func(args)
145+
146+
if __name__ == "__main__":
147+
main()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyYAML==3.12

0 commit comments

Comments
 (0)