forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose-image-yml.sh
More file actions
executable file
·133 lines (114 loc) · 3.62 KB
/
compose-image-yml.sh
File metadata and controls
executable file
·133 lines (114 loc) · 3.62 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
#!/bin/bash
set -e
yq() {
docker run -i --rm -v "${PWD}/":/workdir -w /workdir mikefarah/yq:4.40.5 "$@"
}
process-image-template() {
local out_templ_path="$1"
local eve_version="$2"
local flags
local -a bits
# Drop everything before the git hashcode (including the hash)
flags="$(sed -r 's/.*[0-9a-fA-F]{8}(.*)/\1/p' <<< "${eve_version}")"
# Drop dirty flag
flags="$(sed -r 's/-dirty[0-9.\-]{18}//g' <<< "${flags}")"
IFS='-' read -r -a bits <<< "${flags}"
local dev=0
local k=0
for bit in "${bits[@]}"; do
case "${bit}" in
dev)
dev=1
;;
k)
k=1
;;
esac
done
local pillar_tag="PILLAR_TAG"
if [[ $dev -eq 1 && $k -eq 1 ]]; then
pillar_tag="PILLAR_K_DEV_TAG"
elif [[ $dev -eq 1 ]]; then
pillar_tag="PILLAR_DEV_TAG"
elif [[ $k -eq 1 ]]; then
pillar_tag="PILLAR_K_TAG"
fi
# shellcheck disable=SC2094
yq '(.onboot[] | select(.image == "PILLAR_TAG").image) |= "'"$pillar_tag"'"' < "${out_templ_path}" | spongefile "${out_templ_path}"
# shellcheck disable=SC2094
yq '(.services[] | select(.image == "PILLAR_TAG").image) |= "'"$pillar_tag"'"' < "${out_templ_path}" | spongefile "${out_templ_path}"
}
patch_hv() {
# note that we have to do that careful shell substitution, because yq 4
# doesn't support passing in a variable as the value to --arg, requiring
# setting it as an env var; which is more difficult to do when passed to docker
# shellcheck disable=SC2016,SC2094
yq '(.files[] | select(.contents == "EVE_HV")).contents |= "'"$1"'"' < "$2" | spongefile "$2"
}
patch_platform() {
# shellcheck disable=SC2016,SC2094
yq '(.files[] | select(.contents == "EVE_PLATFORM")).contents |= "'"$1"'"' < "$2" | spongefile "$2"
}
# because sponge doesn't exist everywhere, and this one uses a tmpfile
spongefile() {
local tmp=""
tmp=$(mktemp)
cat > "$tmp"
cat "$tmp" > "$1"
rm "$tmp"
}
main() {
local base_templ_path=""
local out_templ_path=""
local eve_version=""
local eve_hv=""
local eve_platform=""
while getopts "b:o:v:h:p:" opt; do
case ${opt} in
b )
base_templ_path=$OPTARG
;;
o )
out_templ_path=$OPTARG
;;
v )
eve_version=$OPTARG
;;
h )
eve_hv=$OPTARG
;;
p )
eve_platform=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$out_templ_path" ] || [ -z "$eve_version" ] || [ -z "$eve_hv" ] || [ -z "$base_templ_path" ] || [ -z "$eve_platform" ]; then
usage
fi
local modifiers="$*"
cp "${base_templ_path}" "${out_templ_path}"
for modifier in ${modifiers}; do
if [ ! -f "${modifier}" ]; then
continue
fi
# shellcheck disable=SC2094
yq --from-file "${modifier}" < "${out_templ_path}" | spongefile "${out_templ_path}"|| exit 1
done
patch_hv "${eve_hv}" "${out_templ_path}"
patch_platform "${eve_platform}" "${out_templ_path}"
process-image-template "${out_templ_path}" "${eve_version}"
}
usage() {
echo "Usage: $0 -b <base template> -o <output template> -v <eve version> -h <eve hv> -p <platform> <modifiers>"
exit 1
}
main "$@"