-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilbo
More file actions
executable file
·352 lines (243 loc) · 8.52 KB
/
builbo
File metadata and controls
executable file
·352 lines (243 loc) · 8.52 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
function detect_container_command() {
if [ -z "${CONTAINER_CMD}" ]; then
CONTAINER_CMD="$(podman version > /dev/null 2>&1 && echo podman)"
fi
if [ -z "${CONTAINER_CMD}" ]; then
CONTAINER_CMD="$(docker version > /dev/null 2>&1 && echo docker)"
fi
}
# default values for command line options:
LANG=c
REGISTRY=quay.io
CONTAINER_CMD=""
OS=fedora
NAMESPACE=buildbox
BUILD_CMD=""
DEPS=""
DEFAULT_ACTION="help"
ACTION="$DEFAULT_ACTION"
# action that was explicitly set from the command line:
EXPLICIT_ACTION=""
# shell for interactive use:
I_SHELL="bash"
detect_container_command
if [ -z "$CONTAINER_CMD" ]; then
echo "error: failed to detect container command (podman or docker)."
exit 1
fi
# Detect whether GNU getopt is available.
# GNU getopt returns exit code 4 when called with --test; BSD getopt does not.
HAS_GNU_GETOPT=false
getopt --test > /dev/null 2>&1
if [ $? -eq 4 ]; then
HAS_GNU_GETOPT=true
fi
function usage() {
>&2 cat <<EOF
builbo: build software projects in buildbox containers.
USAGE: $0 options
These are the supported options:
[ -l | --lang (c|latex) ] - compilation language (default: $LANG)
[ -r | --registry (quay.io|... ] - container registry (default: $REGISTRY)
[ -c | --container-cmd (podman|docker) ] - default: $CONTAINER_CMD (auto-detected)
[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: $OS)
[ -n | --registry-namespace (...) ] - default: $NAMESPACE
[ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
[ -d | --deps (pkg,pkg,pkg,...) ] - additional packages to install (comma-separated)
[ -h | --help ] - action help: print this usage information
[ -t | --test ] - action test: print some diagnostic info for testing this program
[ -b | --build ] - action build: perform a build in the CWD
[ -e | --enter ] - action enter: enter container in interactive mode (shell)
Note: Long options (e.g. --build, --lang) require GNU getopt.
If GNU getopt is not available (e.g. on macOS without Homebrew's gnu-getopt),
only short options are supported and a warning will be shown for long options.
Install GNU getopt with: brew install gnu-getopt (macOS) or via your Linux package manager.
The Options -l, -r, -o, and -n in combination determine the buildbox image (by tag) to be used.
The options -h, -t, -e, and -b are the available actions (help, test, enter, and build) to be performed.
Exactly one of the actions must be selected.
The build action requires the additional option -s.
The enter action is triggered by the -e flag.
EOF
}
function set_action() {
local action="$1"
if [ -n "$EXPLICIT_ACTION" ]; then
echo "Error: multiple actions specified on cmdline but only one is allowed."
usage
exit 1
fi
ACTION="$action"
EXPLICIT_ACTION="$action"
}
# parsing command line arguments:
optstring="l:r:c:o:n:hbtes:d:"
optstring_long="lang:,registry:,container-cmd:,os:,registry-namespace:,help,test,build,enter,build-script:,deps:"
if $HAS_GNU_GETOPT; then
# GNU getopt is available: support both short and long options.
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")
args_valid=$?
if [ "$args_valid" != "0" ]; then
echo "error: invalid args."
usage
exit 1
fi
# processing parsed args
eval set -- "$parsed_args"
while :
do
case "$1" in
-l | --lang) LANG="$2" ; shift 2 ;;
-r | --registry) REGISTRY="$2" ; shift 2 ;;
-c | --container-cmd) CONTAINER_CMD="$2"; shift 2 ;;
-o | --os) OS="$2" ; shift 2 ;;
-n | --registry-namespace) NAMESPACE="$2" ; shift 2 ;;
-s | --build-script) BUILD_CMD="$2" ; shift 2 ;;
-d | --deps) DEPS="$2" ; shift 2 ;;
-h | --help) set_action "help" ; shift ;;
-b | --build) set_action "build"; shift ;;
-t | --test) set_action "test" ; shift ;;
-e | --enter) set_action "enter"; shift ;;
# -- means end of args; stop processing.
--) shift ; break ;;
*) echo "Unexpected option $1 - this should not happen."
usage
exit 1
;;
esac
done
else
# GNU getopt is not available; fall back to Bash built-in getopts (short options only).
echo "warning: GNU getopt is not available on this system. Only short options are supported." >&2
echo " Install GNU getopt for long option support (e.g. brew install gnu-getopt on macOS)." >&2
# Error on any long options passed by the user.
# Track value-consuming short options so their values are not mistaken for long options.
skip_next=false
for arg in "$@"; do
if $skip_next; then
skip_next=false
continue
fi
if [[ "$arg" == --?* ]]; then
echo "error: long option '$arg' is not supported without GNU getopt." >&2
echo " Please use the equivalent short option, or install GNU getopt." >&2
usage
exit 1
fi
# Short options that consume the next argument (from optstring: l:r:c:o:n:s:d:)
if [[ "$arg" =~ ^-[lrconsd]$ ]]; then
skip_next=true
fi
done
while getopts "$optstring" opt; do
case "$opt" in
l) LANG="$OPTARG" ;;
r) REGISTRY="$OPTARG" ;;
c) CONTAINER_CMD="$OPTARG";;
o) OS="$OPTARG" ;;
n) NAMESPACE="$OPTARG" ;;
s) BUILD_CMD="$OPTARG" ;;
d) DEPS="$OPTARG" ;;
h) set_action "help" ;;
b) set_action "build" ;;
t) set_action "test" ;;
e) set_action "enter" ;;
*)
echo "error: invalid args."
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
fi
# done processing args
#post-processing args
if [ -z "$CONTAINER_CMD" ]; then
echo "error: container command not set. Please use '-c command'."
usage
exit 1
fi
DEPS_PKGS="${DEPS//,/ }"
IMAGE="$REGISTRY/$NAMESPACE/buildbox/$OS-$LANG:latest"
case "$OS" in
fedora)
PKG_CMD="dnf install -y"
;;
debian|ubuntu)
PKG_CMD="apt-get install --yes"
;;
suse)
PKG_CMD="zypper install -y"
;;
esac
# setting CONTAINER_RUN_CMD assumes CONTAINER_RUN_CMD is set.
if [ -z "$CONTAINER_CMD" ]; then
echo "error: container command not set. this should not happen."
exit 1
fi
if [ "$CONTAINER_CMD" = "podman" ]; then
CONTAINER_RUN_CMD="$CONTAINER_CMD run -it --workdir /work --mount type=bind,source=$(pwd),target=/work,relabel=private --platform linux/amd64 $IMAGE"
elif [ "$CONTAINER_CMD" = "docker" ]; then
CONTAINER_RUN_CMD="$CONTAINER_CMD run --workdir /work -v $(pwd):/work --platform linux/amd64 $IMAGE"
fi
if [ -z "$ACTION" ]; then
echo "error: no action selected. Please use exactly one of -t, -h, and -b."
usage
exit 1
fi
if [ "$ACTION" = "build" ]; then
if [ -z "$BUILD_CMD" ]; then
echo "error: action 'build' requires --build-script (-s)"
usage
exit 1
fi
fi
function print_settings() {
echo "settings:"
echo
echo "LANG: $LANG"
echo "OS: $OS"
echo "REGISTRY: $REGISTRY"
echo "NAMESPACE: $NAMESPACE"
echo "BUILD_CMD: $BUILD_CMD"
echo "DEPS: $DEPS"
echo "DEPS_PKGS: $DEPS_PKGS"
echo "PKG_CMD: $PKG_CMD"
echo "CONTAINER_CMD: $CONTAINER_CMD"
echo "CONTAINER_RUN_CMD: '$CONTAINER_RUN_CMD'"
echo "interactive shell: '$I_SHELL'"
echo "IMAGE: $IMAGE"
}
if [ "$ACTION" = "test" ]; then
echo "performing test action."
print_settings
exit 0
fi
if [ "$ACTION" = "help" ]; then
echo "performing help action."
usage
exit 0
fi
function install_deps() {
if [ -n "$DEPS" ]; then
$CONTAINER_RUN_CMD bash -c "$PKG_CMD $DEPS_PKGS"
fi
}
function install_deps_and_run_build() {
local CMD="$BUILD_CMD"
if [ -n "$DEPS" ]; then
CMD="$PKG_CMD $DEPS_PKGS && $BUILD_CMD"
fi
$CONTAINER_RUN_CMD bash -c "$CMD"
}
function enter_container() {
$CONTAINER_RUN_CMD "$I_SHELL"
}
if [ "$ACTION" = "build" ]; then
echo "performing build action."
install_deps_and_run_build
elif [ "$ACTION" = "enter" ]; then
echo "performing enter action."
enter_container
fi