-
Notifications
You must be signed in to change notification settings - Fork 1
/
build
executable file
·181 lines (140 loc) · 3.08 KB
/
build
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
#!/bin/sh -e
#
# Do a DOSB build with a container from GHCR.
#
#BEGIN-HELP
# Usage: build [ OPTIONS ] BUILD-DIR CONTAINER-OS
#
# Arguments (see README.md for more information):
# BUILD-DIR is the build directory
# CONTAINER-OS is one of el{7,8,9}, d10, u{18,20}
#
# Options:
# --absolute Take CONTAINER-OS as the full name of a container
# --command Show the docker run command to run the container
# --name N Name the container 'N'
# --no-halt Don't halt the container when the build finishes
# --run S Copy script S into the container and run it from
# the build area instead of the default 'make'.
#END-HELP
#
set -e
WHOAMI=$(basename "$0")
die()
{
echo "$@" 1>&2
exit 1
}
self_help()
{
sed -e '1,/^#BEGIN-HELP/d ; /^#END-HELP/,$d ; s/^#\s\{0,1\}//' "$0"
exit ${1:-0}
}
# Gargle the arguments
ABSOLUTE=false
while echo "$1" | egrep -q -e '^--'
do
OPTION=$1
shift
case "${OPTION}" in
--help)
self_help
;;
--absolute)
ABSOLUTE=true
;;
--command)
ECHO=echo
;;
--name)
NAME_ARG="--name $1"
shift
;;
--no-halt)
NO_HALT="--env BUILD_NO_HALT=1"
;;
--run)
RUN_ARG=$1
shift
;;
--*)
die "Unknown option ${OPTION}."
;;
esac
done
[ "$#" -eq 2 ] \
|| self_help 1
[ -z "${RUN_ARG}" -o -f "${RUN_ARG}" ] \
|| die "Can't find script '${RUN_ARG}'"
# Build directory
BUILD_DIR="$1"
[ -d "$1" -a -w "$1" ] \
|| die "${BUILD_DIR} is not a writable directory."
echo "${BUILD_DIR}" | fgrep -q ':' \
&& die "Build directory cannot contain colons."
BUILD_DIR=$(cd "${BUILD_DIR}" && pwd)
# Run script, stored in the DOSB-specific area
PRODUCTS="${BUILD_DIR}/DOSB"
rm -rf "${PRODUCTS}"
if [ -s "${RUN_ARG}" ]
then
mkdir -p "${PRODUCTS}"
BUILD_RUN="${PRODUCTS}/build-script"
cp "${RUN_ARG}" "${BUILD_RUN}"
chmod +x "${BUILD_RUN}"
fi
# Container name
if ${ABSOLUTE}
then
CONTAINER="$2"
else
CONTAINER="ghcr.io/perfsonar/docker-oneshot-builder/$2:latest"
PULL_ARG="--pull always"
fi
# Permission args
OS=$(uname -s)
case "${OS}" in
Linux)
if [ -d '/sys/fs/cgroup/cgroup.controllers' ]
then
# Cgroups v2
PERM_ARGS="--volume /sys/fs/cgroup:/sys/fs/cgroup:ro"
else
PERM_ARGS="--privileged"
fi
;;
Darwin)
PERM_ARGS="--privileged"
;;
*)
die "No support for ${OS}" 1>&2
exit 1
esac
# Make it happen
if [ "$(id -u)" -ne 0 ]
then
SUDO=sudo
fi
STATUS=0
${ECHO} ${SUDO} docker run \
${NAME_ARG} \
${PULL_ARG} \
--tty \
--volume "${BUILD_DIR}:/build" \
${NO_HALT} \
--rm \
${PERM_ARGS} \
"${CONTAINER}" \
|| STATUS=$?
# Docker exits with a 130 when systemd is stopped with SIGINT (halt).
# Anything else was a container problem, not a result of the build.
[ $STATUS -eq 0 -o $STATUS -eq 130 ] || false
# Check on the status of the build
STATUS_FILE="${BUILD_DIR}/DOSB/status"
[ -e "${STATUS_FILE}" ] || die "Build produced no status."
BUILD_STATUS=$(cat "${STATUS_FILE}")
if [ "${BUILD_STATUS}" -ne 0 ]
then
echo "Build failed" 1>&2
exit "${BUILD_STATUS}"
fi