-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashbuild.ah
executable file
·344 lines (279 loc) · 9.79 KB
/
bashbuild.ah
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
#!/bin/bash
set +x
DRY_RUN=0
PKG_NAME=""
PKG_ROOT_IT=""
PKG_VERS="default"
PKG_TAG=""
if ! echo "${BASH_SOURCE}" | grep "/" --silent; then
BB_SCRIPT_DIR=$(readlink -f $PWD)
else
BB_SCRIPT_DIR=$(readlink -f ${BASH_SOURCE%/*})
fi
while [[ ${#} -gt 0 ]]; do
key="$1"
case $key in
-n)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
PKG_NAME="$2"
echo "[OPT]: Attempting to build package named: \"${PKG_NAME}\""
shift # past argument
;;
-r)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
PKG_ROOT_IT="$2"
echo "[OPT]: Building for root image: \"${PKG_ROOT_IT}\""
shift # past argument
;;
-t)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
PKG_TAG="$2"
echo "[OPT]: Tagging image as: \"${PKG_TAG}\""
shift # past argument
;;
-v)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
PKG_VERS="$2"
echo "[OPT]: Building version: \"${PKG_VERS}\""
shift # past argument
;;
-D|--root-dir)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
BB_ROOT=$(readlink -f $2)
echo "[OPT]: BB_ROOT=${BB_ROOT}"
shift # past argument
;;
-R|--repo)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
BB_REPO="$2/"
echo "[OPT]: BB_REPO=${2}"
shift # past argument
;;
-f|--rebuild-deps)
BB_REBUILD="ON"
echo "[OPT]: Rebuilding dependencies."
;;
-j)
if [[ ${#} -lt 2 ]]; then
echo "[ERROR]: ${1} expected a value."
exit 1
fi
N_CORES="$2"
echo "[OPT]: Building with ${2} cores where supported."
shift # past argument
;;
-?|--help)
echo "[RUNLIKE] ${SCRIPTNAME} -n <pkg> -r <base os:tag> [opts]"
echo -e "\t-n <package name> : The name of the container "
echo -e "\t package to build."
echo -e "\t-r <package name> : The name of the root image "
echo -e "\t to use, this describes the "
echo -e "\t base OS: e.g centos:7"
echo -e "\t-v <version> : Specify a version of the "
echo -e "\t software to build."
echo -e "\t defaults to \"default\"."
echo -e "\t-t <tag> : Tag to use for requested"
echo -e "\t product. Will default to a "
echo -e "\t sanitization of the root"
echo -e "\t container name, e.g."
echo -e "\t debian:stretch-slim => "
echo -e "\t debian_stretch-slim. "
echo -e "\t-D|--root-dir <bb manifest root dir> : The directory to use as the"
echo -e "\t root of the container build"
echo -e "\t script manifest. This option"
echo -e "\t overrides the BB_ROOT env"
echo -e "\t var. If BB_ROOT is not set"
echo -e "\t then the parent directory of "
echo -e "\t this script will be used."
echo -e "\t-R|--repo <reponame> : The registry repo name to"
echo -e "\t used."
echo -e "\t this option overrides the"
echo -e "\t BB_REPO env var."
echo -e "\t-f|--rebuild-deps : This flag will trigger the"
echo -e "\t rebuild of all dependent are"
echo -e "\t containers. N.B. As "
echo -e "\t containers built by custom "
echo -e "\t scripts, user defined caching"
echo -e "\t stages may not be rebuilt. "
echo -e "\t If you mean business, "
echo -e "\t \'buildah rmi --all\'."
echo -e "\t-j <ncores> : Use <ncores> for builds that"
echo -e "\t support it."
echo -e "\t-?|--help : Print this message."
exit 0
;;
*)
# unknown option
echo "Unknown option $1"
exit 1
;;
esac
shift # past argument or value
done
if [ -z ${BB_ROOT} ]; then
BB_ROOT=$(pwd)
fi
export BB_ROOT
export BB_SCRIPT_DIR
export BUILDAH_FORMAT=docker
export BB_REPO_SL=
if [ ! -z $BB_REPO ]; then
BB_REPO_SL="${BB_REPO}/"
fi
if [ -z ${N_CORES} ]; then
N_CORES=1
fi
export N_CORES
if [ -z ${PKG_ROOT_IT} ] && [ ! -z ${BB_ROOT_IT} ]; then
PKG_ROOT_IT=${BB_ROOT_IT}
fi
export BB_REBUILD=OFF
source $BB_SCRIPT_DIR/framework.funcs
function bb_resolve_deps {
local INDENT="${INDENT}+"
local DEPFILE=$1
echo "${INDENT}Resolving dependencies found in: \"${DEPFILE}\""
local IFS=$'\n'
{
for line in $(cat ${DEPFILE}); do
local SANIT_LINE=$(echo $line | sed "s/^[[:space:]]\+//g" | sed "s/[[:space:]]\{2,\}/ /g" | grep -v "^#")
# Skip comments and empty lines
if [ -z "${SANIT_LINE}" ]; then
continue
fi
local DEP_NAME
local DEP_VERS
local DEP_TAG
local DEP_ROOT_IMAGE
local DEP_ROOT_TAG
bb_parse_depends_on_line $SANIT_LINE
DEP_NAME=$(bb_depends_on_strip_base ${DEP_NAME})
local DEP_ROOT_IT=${DEP_ROOT_IMAGE}:${DEP_ROOT_TAG}
local DEP_VERS_STR="\"${DEP_VERS}\""
if [ "${DEP_VERS}" == "default" ] && [ "$(bb_resolve_vers ${DEP_NAME} ${DEP_VERS})" != "default" ]; then
DEP_VERS_STR="${DEP_VERS}: \"($(bb_resolve_vers ${DEP_NAME} ${DEP_VERS}))\""
fi
local DEP_IMAGE_FQNAME=$(bb_get_image_FQName $DEP_NAME $DEP_ROOT_IT $DEP_VERS $DEP_TAG)
echo "${INDENT}Attempt to build dependency:"
echo -e "${INDENT}\timage : \"${DEP_IMAGE_FQNAME}\""
echo -e "${INDENT}\troot-image: \"${DEP_ROOT_IT}\""
echo -e "${INDENT}\tversion : ${DEP_VERS_STR}"
local DID_BUILD=0
if ! bb_build $DEP_NAME $DEP_ROOT_IT $DEP_VERS $DEP_TAG; then
return 1
fi
if [ ${DID_BUILD} -eq 1 ]; then
DEP_NEEDED_REBUILD=1
fi
# This check includes the repo name, to ensure that the dependency is fulfilled
if [ ${DRY_RUN} -eq 0 ] &&
! podman image exists ${DEP_IMAGE_FQNAME}; then
echo "[ERROR]: After building: \"${DEP_IMAGE_FQNAME}\" does not exist. Something went wrong."
return 1
fi
done
}
}
function bb_build {
local INDENT="${INDENT}"
local FORCEBUILD=${BB_REBUILD}
# Used to force build of the specified container
if [ "$1" = "FORCE" ]; then
FORCEBUILD="ON"
shift
fi
local PKG_NAME=$1
local PKG_ROOT_IT=$2
local PKG_ROOT_IMAGE
local PKG_ROOT_TAG
local PKG_VERS=$3
local PKG_TAG=$4
if ! bb_check_dir_structure $PKG_NAME $PKG_ROOT_IT $PKG_VERS; then
return 1
fi
bb_sanitize_pkg_identifier $PKG_NAME $PKG_ROOT_IT $PKG_VERS $PKG_TAG
local PKG_IMAGE_FQNAME=$(bb_get_image_FQName $PKG_NAME $PKG_ROOT_IT $PKG_VERS $PKG_TAG)
PKG_VERS=$(bb_resolve_vers ${PKG_NAME} ${PKG_VERS})
local PKG_DIR="${BB_ROOT}/${PKG_NAME}/${PKG_VERS}/${PKG_ROOT_IMAGE}/${PKG_ROOT_TAG}"
local DEP_NEEDED_REBUILD=0
if [ -e ${PKG_DIR}/depends.on ]; then
if ! bb_resolve_deps ${PKG_DIR}/depends.on; then
echo "[ERROR]: Failed to resolve dependencies of \"$PKG_NAME\" (v: $PKG_VERS, t: ${PKG_TAG}) rooted on: \"$PKG_ROOT_IMAGE:$PKG_ROOT_TAG\""
return 1
fi
fi
if [ ${DEP_NEEDED_REBUILD} -eq 1 ]; then
FORCEBUILD="ON"
fi
if [ ! "${FORCEBUILD}" = "ON" ] && \
podman image exists ${PKG_IMAGE_FQNAME}; then
echo "${INDENT}Not rebuilding: \"${PKG_IMAGE_FQNAME}\""
return 0
else
echo "${INDENT}Image \"${PKG_IMAGE_FQNAME}\" needs building..."
fi
export BB_PKG_DIR=${PKG_DIR}
export BB_PKG_NAME=${PKG_NAME}
export BB_PKG_ROOT_IT=${PKG_ROOT_IT}
export BB_PKG_VERS=${PKG_VERS}
export BB_PKG_TAG=${PKG_TAG}
export BB_PKG_IMAGE_FQNAME=${PKG_IMAGE_FQNAME}
if [ ${DRY_RUN} -eq 0 ] &&
! ${BB_PKG_DIR}/build.ah; then
echo "[ERROR]: Failed building ${PKG_IMAGE_FQNAME}"
return 1
fi
if [ ${DRY_RUN} -eq 0 ]; then
echo "${INDENT}Built: ${PKG_IMAGE_FQNAME}"
DID_BUILD=1
else
echo "BUILD ENV:"
echo -e "\tBB_ROOT=${BB_ROOT}"
echo -e "\tBB_SCRIPT_DIR=${BB_SCRIPT_DIR}"
echo -e "\tBB_PKG_DIR=${BB_PKG_DIR}"
echo -e "\tBB_PKG_NAME=${BB_PKG_NAME}"
echo -e "\tBB_PKG_ROOT_IT=${BB_PKG_ROOT_IT}"
echo -e "\tBB_PKG_VERS=${BB_PKG_VERS}"
echo -e "\tBB_PKG_TAG=${BB_PKG_TAG}"
echo -e "\tBB_PKG_IMAGE_FQNAME=${BB_PKG_IMAGE_FQNAME}"
echo "DRY RUN: Didn't attempt to build ${PKG_IMAGE_FQNAME}"
fi
export -n BB_PKG_DIR
export -n BB_PKG_NAME
export -n BB_PKG_ROOT_IT
export -n BB_PKG_VERS
export -n BB_PKG_TAG
export -n BB_PKG_IMAGE_FQNAME
}
if [ -z $PKG_NAME ]; then
echo "[ERROR]: Must specify a package name for the container to build (-n)."
bb_say_packages
exit 1
fi
if [ -z $PKG_ROOT_IT ]; then
echo "[ERROR]: Must specify a root image name for the container structure."
bb_say_root_names $PKG_NAME default
exit 2
fi
if [ -z $PKG_TAG ]; then
PKG_TAG=$(bb_get_root_tag $PKG_ROOT_IT)
fi
bb_build FORCE $PKG_NAME $PKG_ROOT_IT $PKG_VERS $PKG_TAG