forked from rancher/image-build-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-build-tools.sh
executable file
·257 lines (233 loc) · 7.01 KB
/
image-build-tools.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
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
#!/bin/sh
#
# image-build-tools is a helper script to ease
# the administrative burden of updating, building,
# and pushing updates for all of the image-build
# repositories.
#
# set debug to print out commands. this is set
# from an environment variable.
if [ "${DEBUG}" = "1" ]; then
set -x
fi
USAGE="usage: $0 command sub-command
description:
helper script to ease the administrative burden of updating, building, and pushing
updates for all of the image-build repositories.
flags:
-h --help print this message
commands:
get
all-repos
local print out all local image-build repos
remote print out all remote image-build repos
update
build-image <image-name:version> update build-image for all repos
ubi-image <image-name:version> update ubi-image for all repos
commit
all commit changes for all image-build repos
push
all commit changes for all image-build repos
help print this message
examples:
$0 get all-repos local
$0 update build-image rancher/hardened-build-base:v1.14.2
"
# return error codes
ERR_GEN=1
ERR_DEP=2
ERR_ARG=3
# cmd_check checks if the command output
# passed in is empty or not. If it is then
# we print an error containing the missing
# command and exit.
cmd_check() {
if [ -z "$1" ]; then
echo "error: $0 requires $1"
exit ${ERR_DEP}
fi
}
# make sure we have at least one argument to
# process or die early.
if [ -z "$1" ]; then
echo "${USAGE}"
exit ${ERR_GEN}
fi
# make sure we have docker installed
GIT="$(command -v git)"
cmd_check "${GIT}"
# make sure we have docker installed
DOCKER="$(command -v docker)"
cmd_check "${DOCKER}"
# make sure we have jq installed
JQ="$(command -v jq)"
cmd_check "${JQ}"
# make sure we have curl installed
CURL="$(command -v curl)"
cmd_check "${CURL}"
COMMAND="$1"
RANCHER_PATH="${GOPATH}/src/github.com/rancher"
INVALID_ARG_ERROR="error: update requires an argument"
IMAGE_BUILD_REPOS=$(find "${RANCHER_PATH}" \
-path "*image-build-*" \
-type f -not -path "*image-build-tools/*" \
-type f -not -path "*image-build-base/*" \
-type f -name "Dockerfile")
# list_local_repos prints out all of the local
# repos to STDOUT.
list_local_repos() {
for i in ${IMAGE_BUILD_REPOS}; do
echo "$i" | sed 's/\/Dockerfile//g'
done
}
# list_remote_repos prints out all of the remote
# repos to STDOUT.
#list_remote_repos() {}
case ${COMMAND} in
"help|-h|--help")
echo "${USAGE}"
exit 0
;;
"get")
if [ -z "$2" ]; then
echo "error: get requires an argument"
exit ${ERR_ARG}
fi
case ${2} in
"all-repos")
if [ -z "${3}" ]; then
echo "error: all-repos requires an argument: {local|remote}"
exit ${ERR_GEN}
fi
case ${3} in
"local")
list_local_repos
;;
"remote")
# list_remote_repos
echo "not implemented"
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
exit 0
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
;;
"update")
if [ -z "$2" ]; then
echo "error: update requires an argument: {build-image|ubi-image}"
exit ${ERR_ARG}
fi
case ${2} in
"build-image")
if [ -z "$3" ]; then
echo "error: build-image requires an argument: {image_name:version}"
exit ${ERR_ARG}
fi
for i in ${IMAGE_BUILD_REPOS}; do
image_name=$(awk -F '=' '/ARG GO_IMAGE/ {print $2}' "$i")
sed -i "s|${image_name}|$3|g" "${i}"
done
exit 0
;;
"ubi-image")
if [ -z "$3" ]; then
echo "error: ubi-image requires an argument: {image_name:version}"
exit ${ERR_ARG}
fi
for i in ${IMAGE_BUILD_REPOS}; do
image_name=$(awk -F '=' '/ARG UBI_IMAGE/ {print $2}' "$i")
sed -i "s|${image_name}|$3|g" "${i}"
done
exit 0
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
;;
"commit")
if [ -z "$2" ]; then
echo "error: commit requires an argument: {all}"
exit ${ERR_ARG}
fi
case ${2} in
"all")
if [ -z "$3" ]; then
echo "error: all requires an argument: <git commit message>"
exit ${ERR_ARG}
fi
for i in ${IMAGE_BUILD_REPOS}; do
target=$(echo "$i" | sed 's/\/Dockerfile//g')
echo "commiting ${target} ..."
cd "${target}" || exit ${ERR_GEN}
${GIT} add .
${GIT} commit -am "${3}"
done
exit 0
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
;;
"push")
if [ -z "$2" ]; then
echo "error: commit requires an argument: {all}"
exit ${ERR_ARG}
fi
case ${2} in
"all")
if [ -z "$3" ]; then
echo "error: all requires an argument: <git commit message>"
exit ${ERR_ARG}
fi
for i in ${IMAGE_BUILD_REPOS}; do
target=$(echo "$i" | sed 's/\/Dockerfile//g')
echo "commiting ${target} ..."
cd "${target}" || exit ${ERR_GEN}
${GIT} push
done
exit 0
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
;;
"revert")
if [ -z "$2" ]; then
echo "error: revert requires an argument: {all}"
exit ${ERR_ARG}
fi
case ${2} in
"all")
for i in ${IMAGE_BUILD_REPOS}; do
target=$(echo "$i" | sed 's/\/Dockerfile//g')
echo "reverting ${target} ..."
cd "${target}" || exit ${ERR_GEN}
${GIT} stash
done
;;
*)
echo "${INVALID_ARG_ERROR}"
exit ${ERR_ARG}
;;
esac
;;
*)
echo "${USAGE}"
exit ${ERR_ARG}
;;
esac
exit 0