-
Notifications
You must be signed in to change notification settings - Fork 9
/
render.sh
104 lines (88 loc) · 3.15 KB
/
render.sh
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
#!/bin/bash
# generate Dockerfile from template with build settings
# init stuff
if [ $# -ne 1 ]; then
printf "no directory provided\n\n"
echo "Usage: sh render.sh <DIRECTORY>"
exit 2
fi
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
ENV_DIR="${1-SCRIPT_DIR}"
ENV_FILE="${ENV_DIR}/build.yaml"
BUILD_FILE="${ENV_DIR}/.build"
DOCKERFILE_TEMPLATE_D="${SCRIPT_DIR}/template.d"
DOCKERFILE_TEMPLATE="${ENV_DIR}/Dockerfile.template"
DOCKERFILE="${ENV_DIR}/Dockerfile"
# function
build_template() {
TEMPLATES=$(ls "${DOCKERFILE_TEMPLATE_D}")
for TEMPLATE in ${TEMPLATES}
do
cat "${DOCKERFILE_TEMPLATE_D}/${TEMPLATE}" >> "${DOCKERFILE_TEMPLATE}"
echo >> "${DOCKERFILE_TEMPLATE}"
done
}
parse_yaml() {
define_fs=$(echo @|tr @ '\034')
local file=$1
local s='[[:space:]]*'
local w='[a-zA-Z0-9_.-]*'
local fs=${define_fs}
(
sed -e '/- [^\“]'"[^\']"'.*: /s|\([ ]*\)- \([[:space:]]*\)|\1-\'$'\n'' \1\2|g' |
sed -ne '/^--/s|--||g; s|\"|\\\"|g; s/[[:space:]]*$//g;' \
-e 's/\$/\\\$/g' \
-e "/#.*[\"\']/!s| #.*||g; /^#/s|#.*||g;" \
-e "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)${s}[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" |
awk -F"$fs" '{
indent = length($1)/2;
if (length($2) == 0) { conj[indent]="+";} else {conj[indent]="";}
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
if (vname[1]) {
printf("%s=%s\n", vname[0], vname[1]);
}
printf("%s%s%s=%s\n", vn, $2, conj[indent-1], $3);
}
}' |
sed -e 's/_=/=/g' |
sed -e 's/_\./__/g' |
awk '{ print }'
) <"$file"
}
# main
echo > "${DOCKERFILE}"
build_template
parse_yaml "${ENV_FILE}" > "${BUILD_FILE}"
KEYS=$(cut -d '=' -f1 "${BUILD_FILE}" | awk '!a[$0]++')
## generate ARG=VALUE for replacement
for KEY in ${KEYS}
do
if [ "$(grep -c "^${KEY}=" "${BUILD_FILE}")" -gt 1 ]; then
VALUE=$(grep "^${KEY}=" "${BUILD_FILE}" | cut -d '=' -f2- | awk '!a[$0]++' | tr '\n' ';')
else
VALUE=$(grep "^${KEY}=" "${BUILD_FILE}" | cut -d '=' -f2- | awk '!a[$0]++' | tr -d '\n')
fi
ARG=$(echo "${KEY}" | awk '{print toupper($0)}')
if [ "$(grep -cw "ARG ${ARG}" "${DOCKERFILE_TEMPLATE}")" -eq 0 ]; then
ARG_N=$(echo "${ARG}" | awk -F '_' '{print $1"_N"}')
SED_APPEND_ARGS="${SED_APPEND_ARGS}#${ARG_N}=${ARG}=\"${VALUE}\""
else
SED_REPLACE="${SED_REPLACE} s!ARG \<${ARG}\>.*\$!ARG ${ARG}=\"${VALUE}\"!g;"
fi
done
## replace ARG
sed -r "${SED_REPLACE}" "${DOCKERFILE_TEMPLATE}" > "${DOCKERFILE}"
## append ARG
if [ "${SED_APPEND_ARGS}" != "" ]; then
SED_APPEND_KEYS=$(echo "$SED_APPEND_ARGS" | tr '#' '\n' | cut -d '=' -f1 | awk '!a[$0]++' | xargs)
for SED_APPEND_KEY in ${SED_APPEND_KEYS}; do
SED_APPEND_KEY_ARG=$(echo "${SED_APPEND_ARGS}" | tr '#' '\n' | sed -n "s/${SED_APPEND_KEY}=/ARG /p" | sed 's/$/\\n/g' | tr -d '\n')
sed -i "/ARG ${SED_APPEND_KEY}/a ${SED_APPEND_KEY_ARG}" "${DOCKERFILE}"
done
fi
## remove helper files
rm "${BUILD_FILE}" "${DOCKERFILE_TEMPLATE}"