|
1
|
+#!/usr/bin/env bash
|
|
2
|
+#
|
|
3
|
+# Copyright (C) 2018 Codethink Limited
|
|
4
|
+#
|
|
5
|
+# This program is free software; you can redistribute it and/or
|
|
6
|
+# modify it under the terms of the GNU Lesser General Public
|
|
7
|
+# License as published by the Free Software Foundation; either
|
|
8
|
+# version 2 of the License, or (at your option) any later version.
|
|
9
|
+#
|
|
10
|
+# This library is distributed in the hope that it will be useful,
|
|
11
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+# Lesser General Public License for more details.
|
|
14
|
+#
|
|
15
|
+# You should have received a copy of the GNU Lesser General Public
|
|
16
|
+# License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+#
|
|
18
|
+# Authors:
|
|
19
|
+# Tristan Maat <tristan maat codethink co uk>
|
|
20
|
+
|
|
21
|
+set -eu
|
|
22
|
+
|
|
23
|
+usage () {
|
|
24
|
+ cat <<EOF
|
|
25
|
+Usage:
|
|
26
|
+ test-bst [options] [<bst-version>]
|
|
27
|
+
|
|
28
|
+Run a test buildstream container with your cwd as /src. Also allows
|
|
29
|
+using an editable local copy of the buildstream repository, or a
|
|
30
|
+different branch from the current repository.
|
|
31
|
+
|
|
32
|
+The bst-version argument is presumed to be a branch of the main repo
|
|
33
|
+by default, and will default to "master" if not specified.
|
|
34
|
+
|
|
35
|
+Options:
|
|
36
|
+ -b|--branch Treat bst-version as a branch name
|
|
37
|
+ -c|--commit Treat bst-version as a commit sha
|
|
38
|
+ -d|--directory Treat bst-version as a local directory
|
|
39
|
+ containing buildstream's repository
|
|
40
|
+ -i|--image Use the given image as base
|
|
41
|
+ -v|--volume Specify additional volumes to mount; should be in format
|
|
42
|
+ 'host-src:container-dest' same as 'docker run -v'
|
|
43
|
+ -e|--env Specify additional environment variables; same as
|
|
44
|
+ 'docker run --env'
|
|
45
|
+ --keep-artifacts Enable local artifact cache
|
|
46
|
+ --no-source-cache Disable local source cache
|
|
47
|
+ --help Display this help message and exit
|
|
48
|
+EOF
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+RED='\033[0;31m'
|
|
52
|
+RESET='\033[0m'
|
|
53
|
+
|
|
54
|
+main () {
|
|
55
|
+ local env
|
|
56
|
+ local image
|
|
57
|
+ local branch
|
|
58
|
+ local commit
|
|
59
|
+ local volumes
|
|
60
|
+ local buildstream_dir
|
|
61
|
+ local keep_sources=true
|
|
62
|
+ local keep_artifacts=false
|
|
63
|
+
|
|
64
|
+ while : ; do
|
|
65
|
+ case "${1:-}" in
|
|
66
|
+ "")
|
|
67
|
+ break ;;
|
|
68
|
+ -v|--volume)
|
|
69
|
+ volumes="$volumes $2"
|
|
70
|
+ shift 2 ;;
|
|
71
|
+ -e|--env)
|
|
72
|
+ env="$env $2"
|
|
73
|
+ shift 2 ;;
|
|
74
|
+ -d|--directory)
|
|
75
|
+ if [ ! -z "$branch" ] || [ ! -z "$commit" ]; then
|
|
76
|
+ echo -e "${RED}Error: Cannot specify '$1' and '-c' or '-b'"
|
|
77
|
+ usage
|
|
78
|
+ exit 1
|
|
79
|
+ fi
|
|
80
|
+
|
|
81
|
+ buildstream_dir=$(abspath "$2")
|
|
82
|
+ shift 2 ;;
|
|
83
|
+ -b|--branch)
|
|
84
|
+ if [ ! -z "$buildstream_dir" ] || [ ! -z "$commit" ]; then
|
|
85
|
+ echo -e "${RED}Error: Cannot specify '$1' and '-d' or '-c'"
|
|
86
|
+ usage
|
|
87
|
+ exit 1
|
|
88
|
+ fi
|
|
89
|
+
|
|
90
|
+ branch="$2"
|
|
91
|
+ shift 2 ;;
|
|
92
|
+ -c|--commit)
|
|
93
|
+ if [ ! -z "$buildstream_dir" ] || [ ! -z "$branch" ]; then
|
|
94
|
+ echo -e "${RED}Error: Cannot specify '$1' and '-d' or '-b'"
|
|
95
|
+ usage
|
|
96
|
+ exit 1
|
|
97
|
+ fi
|
|
98
|
+
|
|
99
|
+ commit="$2"
|
|
100
|
+ shift 2 ;;
|
|
101
|
+ -i|--image)
|
|
102
|
+ image="$2"
|
|
103
|
+ shift 2 ;;
|
|
104
|
+ --no-source-cache)
|
|
105
|
+ keep_sources=false
|
|
106
|
+ shift ;;
|
|
107
|
+ --keep-artifacts)
|
|
108
|
+ keep_artifacts=true
|
|
109
|
+ shift ;;
|
|
110
|
+ *)
|
|
111
|
+ echo -e "${RED}Error: Unrecognized argument '${1:-}'${RESET}" 1>&2
|
|
112
|
+ usage
|
|
113
|
+ exit 1 ;;
|
|
114
|
+ esac
|
|
115
|
+ done
|
|
116
|
+
|
|
117
|
+ run_bst "$branch" "$keep_sources" "$keep_artifacts" "$buildstream_dir" "$commit" "$image"
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+abspath() {
|
|
121
|
+ local path="$1"
|
|
122
|
+
|
|
123
|
+ cd "$(dirname "$path")"
|
|
124
|
+ path="$(basename "$path")"
|
|
125
|
+
|
|
126
|
+ while [ -L "$path" ]; do
|
|
127
|
+ path="$(readlink "$path")"
|
|
128
|
+ cd "$(dirname "$path")"
|
|
129
|
+ path="$(basename "$path")"
|
|
130
|
+ done
|
|
131
|
+
|
|
132
|
+ echo "$(pwd -P)/$path"
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+run_bst() {
|
|
136
|
+ local branch="$1"
|
|
137
|
+ local keep_sources="$2"
|
|
138
|
+ local keep_artifacts="$3"
|
|
139
|
+ local buildstream_dir="$4"
|
|
140
|
+ local commit="$5"
|
|
141
|
+
|
|
142
|
+ local opt_args=""
|
|
143
|
+ local install_bst
|
|
144
|
+
|
|
145
|
+ if [ -z "$branch" ]; then
|
|
146
|
+ branch="master"
|
|
147
|
+ fi
|
|
148
|
+
|
|
149
|
+ if [ -z "$image" ]; then
|
|
150
|
+ image="${BUILDSTREAM_DOCKER_IMAGE:-buildstream/buildstream-fedora:latest}"
|
|
151
|
+ fi
|
|
152
|
+
|
|
153
|
+ # Install the version of buildstream that the user specified
|
|
154
|
+ if [ ! -z "$commit" ]; then
|
|
155
|
+ install_bst="(set -e;
|
|
156
|
+ git clone https://gitlab.com/BuildStream/buildstream.git /buildstream;
|
|
157
|
+ git -C /buildstream checkout $commit;
|
|
158
|
+ pip3 install --no-index /buildstream)"
|
|
159
|
+ elif [ ! -z "$buildstream_dir" ]; then
|
|
160
|
+ install_bst="(set -e; pip3 install --no-index -e /buildstream)"
|
|
161
|
+ opt_args="$opt_args --volume $buildstream_dir:/buildstream"
|
|
162
|
+ else
|
|
163
|
+ install_bst="(set -e;
|
|
164
|
+ git clone -b $branch https://gitlab.com/BuildStream/buildstream.git /buildstream;
|
|
165
|
+ pip3 install --no-index /buildstream)"
|
|
166
|
+ fi
|
|
167
|
+
|
|
168
|
+ # Create a volume to cache sources
|
|
169
|
+ if [ "$keep_sources" == true ]; then
|
|
170
|
+ opt_args="$opt_args --env BST_SOURCE_CACHE=/temp/bst_cache/buildstream/sources"
|
|
171
|
+ opt_args="$opt_args --env XDG_CACHE_HOME=/temp/bst_cache/"
|
|
172
|
+ opt_args="$opt_args --volume bst-test-sources:/temp/bst_cache/buildstream/sources"
|
|
173
|
+ docker volume create "bst-test-sources"
|
|
174
|
+ fi
|
|
175
|
+
|
|
176
|
+ # Create a volume to cache artifacts
|
|
177
|
+ if [ "$keep_artifacts" == true ]; then
|
|
178
|
+ opt_args="$opt_args --env XDG_CACHE_HOME=/temp/bst_cache"
|
|
179
|
+ opt_args="$opt_args --volume bst-test-artifacts:/temp/bst_cache/buildstream/artifacts"
|
|
180
|
+ docker volume create "bst-test-artifacts"
|
|
181
|
+ fi
|
|
182
|
+
|
|
183
|
+ # Run a container with a buildstream setup as specified.
|
|
184
|
+ #
|
|
185
|
+ # --privileged is required to run bst in docker
|
|
186
|
+ # --net="host" is used for local artifact cache server testing
|
|
187
|
+ # --device /dev/fuse is required to run bst in docker
|
|
188
|
+ # --security-opt seccomp=unconfined is required to run bst in docker
|
|
189
|
+ # --env PS1 is set to distinguish the shell from a normal shell
|
|
190
|
+ #
|
|
191
|
+ # Any un-escaped variables are argument lists - unfortunately we
|
|
192
|
+ # can't disable shellcheck warnings for these here.
|
|
193
|
+ #
|
|
194
|
+ exec docker run --rm -it \
|
|
195
|
+ --privileged \
|
|
196
|
+ --net="host" \
|
|
197
|
+ --device /dev/fuse \
|
|
198
|
+ --security-opt seccomp=unconfined \
|
|
199
|
+ --env PS1="${PS1:-bst-test:\u@\h \$ }" \
|
|
200
|
+ --volume "$PWD":/src \
|
|
201
|
+ ${volumes// / --volume } \
|
|
202
|
+ ${env// / --env } \
|
|
203
|
+ --workdir /src \
|
|
204
|
+ $opt_args \
|
|
205
|
+ "$image" \
|
|
206
|
+ /bin/bash -c "$install_bst; bash -i"
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+main "$@"
|