-
Notifications
You must be signed in to change notification settings - Fork 344
/
pkg
executable file
·314 lines (284 loc) · 8.71 KB
/
pkg
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
#!/usr/bin/env bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# macOS's version of realpath does not resolve symlinks, so we add a function
# for it.
get_realpath() {
local bin
local found=''
first_realpath="$(type -P realpath)"
for bin in $(type -aP grealpath realpath | uniq); do
if "$bin" -e . >/dev/null 2>&1; then
found=y
break
fi
done
if [[ -n "$found" ]]; then
if [[ "$first_realpath" == "$bin" ]]; then
# Default realpath works.
return
fi
realpath_path="$bin"
# by default, macOS does not have realpath
eval "$(<<FUNCTION cat
realpath() {
"$realpath_path" "\$@"
}
FUNCTION
)"
export -f realpath
else
cat <<'MESSAGE'
GNU realpath is required to build Apache Traffic Control if your
realpath binary does not support the -e flag, as is the case on BSD-like
operating systems like macOS. Install it by running the following
command:
brew install coreutils
MESSAGE
exit 1
fi
}
get_realpath
# Files are relative to this script directory.
SELF="${BASH_SOURCE[0]}"
cd "$( dirname "${BASH_SOURCE[0]}" )"
COMPOSE_FILE=./infrastructure/docker/build/docker-compose.yml
COMPOSE_FILE_OPT=./infrastructure/docker/build/docker-compose-opt.yml
# Check for dependencies
if ! which docker >/dev/null 2>&1; then
echo "Error: docker is required for a docker build." >&2
exit 1
fi
# If the user defined COMPOSE, use that as the path for docker-compose.
if [ ! -z "$COMPOSE" ]; then
COMPOSECMD=( "$COMPOSE" )
else
COMPOSECMD=()
fi
# Check to see if docker compose is already installed and use it directly, if possible.
if [ ${#COMPOSECMD[@]} -eq 0 ]; then
if docker compose >/dev/null 2>&1; then
COMPOSECMD=( docker compose )
fi
fi
# If it's unavailable, download the image and run docker compose inside a container.
# This is considerably slower, but allows for building on hosts without docker-compose.
if [ ${#COMPOSECMD[@]} -eq 0 ]; then
# Pin the version of docker-compose.
IMAGE="docker:latest"
# We need to either mount the docker socket or export the docker host into the container.
# This allows the container to manage "sibling" containers via docker.
if [ -z "$DOCKER_HOST" ]; then
DOCKER_HOST="/var/run/docker.sock"
fi
if [ -S "$DOCKER_HOST" ]; then
DOCKER_ADDR=(-v "$DOCKER_HOST:$DOCKER_HOST" -e DOCKER_HOST)
else
DOCKER_ADDR=(-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH)
fi
# We mount the current directory (the base of the repository) into the same location
# inside the container. There are many places for which this won't work, but "/" is
# a major one.
#
# You're going to want to avoid keeping your repository in a folder named "/usr/bin",
# "/home", "/var" or any other standard paths that will be needed by the docker container.
#
# This is very unlikely to cause trouble for anyone in practice.
if [ "$(pwd)" == "/" ]; then
echo "Error: Cannot compile directly at filesystem root." >&2
exit 1
fi
# Mount the working directory, and the home directory. Mounting $HOME provides container
# access to config files that are kept there.
VOLUMES=(-v "$(pwd):$(pwd)" -v "$HOME:$HOME" -v "$HOME:/root")
# Prepull the image, to avoid spitting out pull progress during other commands.
if ! docker inspect $IMAGE >/dev/null 2>&1; then
docker pull $IMAGE >/dev/null 2>&1
fi
# COMPOSECMD is kept as an array to significantly simplify handling paths that contain
# spaces and other special characters.
COMPOSECMD=(docker run --rm "${DOCKER_ADDR[@]}" $COMPOSE_OPTIONS "${VOLUMES[@]}" -w "$(pwd)" $IMAGE docker compose)
fi
# Parse command line arguments
verbose=0
debug=0
quiet=0
all=0
build_images=0
NO_LOG_FILES=0
NO_SOURCE=0
SIMPLE=0
list=0
failure=0
print_help=0
while getopts h78abdf:lopqvsSL opt; do
case $opt in
\?)
failure=1;
;;
7)
BASE_IMAGE=centos
RHEL_VERSION=7
;;
8)
BASE_IMAGE=rockylinux
RHEL_VERSION=8
;;
a)
all=1
;;
b)
build_images=1
;;
d)
echo '-d is set! Disabling all compiler optimizations for debugging...';
# If DEBUG_BUILD is true, then Golang binaries will remain unoptimized and
# RPM packaging will not strip binaries.
RUN_OPTIONS+=(-e 'DEBUG_BUILD=true');
debug=1
;;
f)
COMPOSE_FILE="$OPTARG"
;;
h)
print_help=1;
;;
L)
NO_LOG_FILES=1
;;
l)
list=1;
;;
o)
COMPOSE_FILE="$COMPOSE_FILE_OPT";
;;
p)
build_images=0
;;
q)
exec >/dev/null 2>&1
quiet=1
;;
v)
verbose=1
;;
s)
SIMPLE=1
;;
S)
NO_SOURCE=1
;;
esac
done
CMD=$("${COMPOSECMD[@]}" -f $COMPOSE_FILE config --services)
# Weasel must be built first otherwise it will fail from build artifacts/cache in an environment that doesn't have git
# e.g. the environment produced by `./pkg source`.
PROJECTS=$(grep -E "^weasel$" <<<"$CMD"; grep -Ev "^weasel$"<<<"$CMD")
HELP_TEXT="$(cat <<HELP_TEXT
Usage: $SELF [options] [projects]
-7 Build RPMs targeting CentOS 7
-8 Build RPMs targeting Rocky Linux 8 (default)
-a Build all projects, including optional ones.
-b Build builder Docker images before building projects
-d Disable compiler optimizations for debugging.
-f <file> Use <file> as the docker-compose. Default: $COMPOSE_FILE
-h Print help message and exit
-L Don$(echo \')t write logs to files - respects output levels on stderr/stdout as set by -q/-v
-l List available projects.
-o Build from the optional list. Same as -f "$COMPOSE_FILE_OPT"
-p Pull builder Docker images, do not build them (default)
-q Quiet mode. Supresses output. (default)
-s Simple output filenames - e.g. traffic_ops.rpm instead of traffic_ops-6.1.0-11637.ec9ff6a6.el8.x86_64.rpm
-S Skip building "source rpms"
-v Verbose mode. Lists all build output.
If no projects are listed, all projects will be packaged.
Valid projects:
$(echo "$PROJECTS" | sed "s/^/ - /")
HELP_TEXT
)";
if [[ "$failure" -ne 0 ]]; then
echo "$HELP_TEXT" >&2;
exit "$failure";
fi
if [[ "$print_help" -eq 1 ]]; then
echo "$HELP_TEXT";
exit 0;
fi
if [[ "$list" -eq 1 ]]; then
echo "$PROJECTS";
exit 0;
fi
shift $((OPTIND-1))
# Mark BASE_IMAGE for export
export BASE_IMAGE;
if [[ -n "$BASE_IMAGE" ]]; then
RUN_OPTIONS+=(-e BASE_IMAGE);
fi
# Mark RHEL_VERSION for export
export RHEL_VERSION;
if [[ -n "$RHEL_VERSION" ]]; then
RUN_OPTIONS+=(-e RHEL_VERSION);
fi
export SIMPLE;
export NO_SOURCE;
export NO_LOG_FILES;
RUN_OPTIONS+=(-e SIMPLE);
RUN_OPTIONS+=(-e NO_SOURCE);
RUN_OPTIONS+=(-e NO_LOG_FILES);
# If no specific packages are listed, or -a is used, run them all.
if (( ! "$#" || "$all" == 1 )); then
set -- `$SELF -f "$COMPOSE_FILE" -l`
fi
# Create the dist directory if it does not exist.
mkdir -p dist
# Build each project in turn.
badproj=""
while (( "$#" )); do
echo Building $1.
(
if (( "$verbose" == 0 )); then
exec >/dev/null 2>&1
fi
# Build the project
if [[ $build_images -eq 1 ]]; then
"${COMPOSECMD[@]}" -f $COMPOSE_FILE build $1 || exit 1
else
"${COMPOSECMD[@]}" -f $COMPOSE_FILE pull $1 || exit 1
fi
"${COMPOSECMD[@]}" -f $COMPOSE_FILE run "${RUN_OPTIONS[@]}" --rm $1 || exit 1
# Check for a chained compose file for this particular project.
# A chained compose file will be named exactly the same as main docker compose, with .service added,
# where <service> is the name of the specific service to be chained. The file may be a symlink to another
# compose file, in which case the symlink will be followed before it is processed.
if [ -e "$COMPOSE_FILE.$1" ] ; then
$SELF -f $(realpath -e "$COMPOSE_FILE.$1") $([ "$verbose" == 0 ] || echo "-v") $([ "$quiet" == 0 ] || echo "-q") $([ "$debug" == 0 ] || echo "-d") $([ "$build" == 0 ] || echo "-b") $("${COMPOSECMD[@]}" -f $(realpath -e "$COMPOSE_FILE.$1") config --services)
chained_exit=$?
[ $chained_exit == 0 ] || exit $chained_exit
fi
) || {
# Don't totally bail out, but make note of the failures.
failure=1
badproj="$badproj $1"
}
shift
done
[ "$all" == 0 ] || $SELF -o $([ "$verbose" == 0 ] || echo "-v") $([ "$quiet" == 0 ] || echo "-q") $([ "$debug" == 0 ] || echo "-d") || failure=1
if [[ ! -z $badproj ]]; then
echo "Failed to build$badproj."
fi
if [[ -d dist ]]; then
echo "Results in 'dist':"
ls -lt dist
else
echo "dist artifact directory not found."
fi
exit $failure