-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.funcs
320 lines (259 loc) · 7.59 KB
/
framework.funcs
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
#!/bin/bash
function bb_is_debug_build {
if [ ! -z ${BB_BUILD_TYPE} ] && [ "${BB_BUILD_TYPE}" = "DEBUG" ]; then
return 0
fi
return 1
}
function bb_get_root_tag {
local PKG_ROOT_IT=${1}
local OLDIFS=${IFS}
local IFS=:
set -- $PKG_ROOT_IT
IFS=${OLDIFS}
local PKG_ROOT_IMAGE=$1
local PKG_ROOT_TAG=$2
if bb_is_debug_build; then
PKG_ROOT_TAG=${PKG_ROOT_TAG}_dbg
fi
if [ -z $PKG_ROOT_TAG ]; then
PKG_ROOT_TAG=latest
fi
echo ${PKG_ROOT_IMAGE}_${PKG_ROOT_TAG}
}
function bb_sanitize_pkg_identifier_notag {
PKG_NAME=$1
local PKG_ROOT_IT=$2
PKG_VERS=$3
if [ -z $PKG_NAME ]; then
echo "Must specify a Name for the container structure."
return 1
fi
if [ -z $PKG_ROOT_IT ]; then
echo "Must specify a root image name for the container structure."
return 2
fi
local OLDIFS=${IFS}
local IFS=:
set -- $PKG_ROOT_IT
IFS=${OLDIFS}
PKG_ROOT_IMAGE=$1
PKG_ROOT_TAG=$2
if [ -z $PKG_ROOT_TAG ]; then
PKG_ROOT_TAG=latest
fi
}
function bb_sanitize_pkg_identifier {
PKG_NAME=$1
local PKG_ROOT_IT=$2
PKG_VERS=$3
PKG_TAG=$4
if [ -z $PKG_TAG ]; then
PKG_TAG=$(bb_get_root_tag ${PKG_ROOT_IT})
fi
bb_sanitize_pkg_identifier_notag ${PKG_NAME} ${PKG_ROOT_IT} ${PKG_VERS}
}
function bb_say_packages {
if [ ! -d ${BB_ROOT} ]; then
echo "Possible package directories: "
for d in ${BB_ROOT}/*/ ; do
echo "\t ${d}"
done
fi
}
function bb_say_versions {
if [ -d ${BB_ROOT}/${1} ]; then
echo "Possible versions for package ${1}: "
for d in ${BB_ROOT}/${1}/*/ ; do
echo -e "\t $(echo $d | sed "s|${BB_ROOT}/${1}/||g" | sed 's|/||g')"
done
fi
}
function bb_say_root_names {
if [ -d ${BB_ROOT}/${1}/${2} ]; then
echo "Possible root image names for package ${1}, version: ${2}: "
for d in ${BB_ROOT}/${1}/${2}/*/ ; do
echo -e "\t $(echo $d | sed "s|${BB_ROOT}/${1}/${2}/||g" | sed 's|/||g')"
done
fi
}
function bb_say_root_tags {
if [ -d ${BB_ROOT}/${1}/${2}/${3} ]; then
echo "Possible root tag names for package ${1}, version: ${2}, based on ${3}: "
for d in ${BB_ROOT}/${1}/${2}/${3}/*/ ; do
echo -e "\t $(echo $d | sed "s|${BB_ROOT}/${1}/${2}/${3}/||g" | sed 's|/||g')"
done
fi
}
function bb_check_dir_structure {
local PKG_NAME=$1
local PKG_ROOT_IT=$2
local PKG_VERS=$3
local PKG_ROOT_IMAGE
local PKG_ROOT_TAG
if ! bb_sanitize_pkg_identifier_notag ${PKG_NAME} \
${PKG_ROOT_IT} ${PKG_VERS}; then
return ${?}
fi
if [ ! -d $BB_ROOT ]; then
echo "BB_ROOT directory \"$BB_ROOT/\" doesn't exist."
return 3
fi
if [ ! -d $BB_ROOT/$PKG_NAME ]; then
echo "Package directory \"$BB_ROOT/$PKG_NAME\" doesn't exist."
bb_say_packages
return 3
fi
if [ ! -d $BB_ROOT/$PKG_NAME/$PKG_VERS ]; then
echo "Version directory \"$BB_ROOT/$PKG_NAME/$PKG_VERS\" doesn't exist."
bb_say_versions ${PKG_NAME}
return 3
fi
if [ ! -d $BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE ]; then
echo "Package root container directory \"$BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE\" doesn't exist."
bb_say_root_names ${PKG_NAME} ${PKG_VERS}
return 3
fi
if [ ! -d $BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG ]; then
echo "Package root container tag directory \"$BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG\" doesn't exist."
bb_say_root_names ${PKG_NAME} ${PKG_VERS} ${PKG_ROOT_IMAGE}
return 3
fi
if [ ! -e $BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG/build.ah ]; then
echo "Script \"$BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG/build.ah\" doesn't exist."
return 4
fi
if [ ! -x $BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG/build.ah ]; then
echo "Script \"$BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG/build.ah\" isn't executable."
return 5
fi
return 0
}
function bb_dir_structure_exists {
local PKG_NAME=$1
local PKG_ROOT_IT=$2
local PKG_VERS=$3
local PKG_ROOT_IMAGE
local PKG_ROOT_TAG
if ! bb_sanitize_pkg_identifier_notag ${PKG_NAME} \
${PKG_ROOT_IT} ${PKG_VERS}; then
return ${?}
fi
if [ ! -d $BB_ROOT/$PKG_NAME/$PKG_VERS/$PKG_ROOT_IMAGE/$PKG_ROOT_TAG ]; then
return 1
fi
return 0
}
function bb_resolve_vers {
local PKG_NAME=$1
local PKG_VERS=$2
if [ ! -z "${PKG_VERS}" ] && [ ! "${PKG_VERS}" = "default" ]; then
echo "${PKG_VERS}"
else
if [ ! -d ${BB_ROOT}/${PKG_NAME}/default ]; then
echo "[ERROR]: Tried to resolve default version for package: ${PKG_NAME}, but ${BB_ROOT}/${PKG_NAME}/default doesn't exist."
exit 1
fi
echo $(echo $(readlink -f ${BB_ROOT}/${PKG_NAME}/default) | sed "s|${BB_ROOT}/${PKG_NAME}/||g")
fi
}
function bb_get_image_FQName {
local PKG_NAME=$1
local PKG_ROOT_IT=$2
local PKG_VERS=$3
local PKG_TAG=$4
PKG_VERS=$(bb_resolve_vers ${PKG_NAME} ${PKG_VERS})
if [ -z ${PKG_TAG} ]; then
PKG_TAG=$(bb_get_root_tag ${PKG_ROOT_IT})
fi
# If default is really the name of the version, then we don't add it to the image name
if [ "${PKG_VERS}" = "default" ]; then
echo "${BB_REPO_SL}${PKG_NAME}:${PKG_TAG}"
else
echo "${BB_REPO_SL}${PKG_NAME}_${PKG_VERS}:${PKG_TAG}"
fi
}
function bb_depends_on_strip_base {
local SANIT_LINE=$1
echo $SANIT_LINE | sed "s/^!//g"
}
function bb_sanitize_depends_on_line {
local RAW_LINE=$1
echo $RAW_LINE | sed "s/^[[:space:]]\+//g" | sed "s/[[:space:]]\{2,\}/ /g" | grep -v "^#"
}
function bb_parse_depends_on_line {
local SANIT_LINE=$1
local IFS=" "
set -- $SANIT_LINE
local DEP_IDENTIFIER=$1
local DEP_ROOT_IT=$2
if [ "$DEP_ROOT_IT" == "\$ENV\$" ]; then
DEP_ROOT_IT=$BB_ROOT_IT
fi
IFS="@"
set -- $DEP_IDENTIFIER
DEP_NAME=$1
local DEP_QUALS=$2
DEP_VERS="default"
DEP_TAG=""
if [ ! -z ${DEP_QUALS} ]; then
IFS=":"
set -- ${DEP_QUALS}
DEP_VERS=$1
DEP_TAG=$2
else
IFS=":"
set -- ${DEP_NAME}
DEP_NAME=$1
DEP_TAG=$2
fi
if [ -z $DEP_VERS ]; then
DEP_VERS=default
fi
set -- $DEP_ROOT_IT
DEP_ROOT_IMAGE=$1
DEP_ROOT_TAG=$2
}
function bb_depends_on_line_is_base {
local RAW_LINE=$1
local SANIT_LINE=$(bb_sanitize_depends_on_line $RAW_LINE)
if [ -z ${SANIT_LINE} ]; then
return 1
fi
if [[ "${SANIT_LINE}" =~ ^! ]]; then
return 0
fi
return 1
}
function bb_get_base_image_name {
local PKG_NAME=$1
local PKG_VERS=$2
if [ -z $PKG_ROOT_IT ]; then
echo "Error: Must be run from within a bashbuildah environment (PKG_ROOT_IT is not defined)."
return 1
fi
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
PKG_VERS=$(bb_resolve_vers ${PKG_NAME} ${PKG_VERS})
local PKG_DIR="${BB_ROOT}/${PKG_NAME}/${PKG_VERS}/${PKG_ROOT_IMAGE}/${PKG_ROOT_TAG}"
if [ ! -e ${PKG_DIR}/depends.on ]; then
return
fi
local IFS=$'\n'
{
for line in $(cat ${PKG_DIR}/depends.on); do
if bb_depends_on_line_is_base $line; then
local DEP_NAME
local DEP_VERS
local DEP_TAG
local DEP_ROOT_IMAGE
local DEP_ROOT_TAG
bb_parse_depends_on_line $(bb_sanitize_depends_on_line $(bb_depends_on_strip_base $line))
echo $(bb_get_image_FQName ${DEP_NAME} ${DEP_ROOT_IMAGE}:${DEP_ROOT_TAG} ${DEP_VERS} ${DEP_TAG})
return
fi
done
}
}