-
Notifications
You must be signed in to change notification settings - Fork 30
/
image-make
executable file
·272 lines (253 loc) · 8.95 KB
/
image-make
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
#!/bin/bash
### NAME
### image-make - create, tag and push docker image
###
### SYNOPSIS
### image-make [Options] <DockerName>
###
### DESCRIPTION
### This program builds, tag, and push docker images to docker registry as:
### zanata/<DockerName>:<tag>
###
### The <DockerName> can be any of sub-directories of this project that
### contains a Dockerfile.
###
### Specify <tag> with -t <tag>.
### If -t <tag> is not specified, default <tag> is 'latest'
### Note that for DockerName "centos-repo-builder", beside just tag as <tag>,
### you also get <DistroVersion>-<tag>. For example, specified 'by default, you get
### following tags for centos-repo-builder:
###
### latest, 7-latest, 6-latest
###
### Note that centos-repo-builder:7 means centos-repo-builder:7-latest, and
### centos-repo-builder:6 is also similar.
###
### Also note that this script assumes that you already have an docker registry
### account and have logged in from the command line at your workstation.
### Refer README.md for instruction.
###
### ARGUMENTS
### <DockerName>: Name of the docker to be build
###
### ENVIRONMENT
### ZANATA_DOCKER_PUSH_REGISTRY: Docker registry for pushing
### Note that you should configure /etc/sysconfig/docker to add
### the registry.
### Default: docker.io
: ${ZANATA_DOCKER_PUSH_REGISTRY:=docker.io}
set -eu
###
shopt -s globstar
ScriptDir=$(dirname $(readlink -q -f $0))
ProgramName=$(basename $0)
##=== function definition Start ===
## Get EXIT_STATUS and other environment variables from zanata-scripts
source <(curl -q https://raw.githubusercontent.com/zanata/zanata-scripts/master/zanata-env.sh)
echo_stderr(){
echo "$@" > /dev/stderr
}
zanata_help_raw(){
local script="$1"
[ -z "$script" ] && script=$0
sed -r -n -e '/^### ?/ {s|^### ?||p}' $script
}
## run_command <command> [args ... ]
## On Dry-run mode, just show the command,
## otherwise run the command.
run_command(){
if [[ ${DryRunMode:-} -eq 1 ]];then
ExitCode=$EXIT_OK
else
set -x
"$@"
ExitCode=$?
set +x
fi
}
docker_tag_image(){
local image=$1
local dockerName=$2
local tag=$3
if [[ $image != $RepoName/$dockerName:$tag ]] ;then
run_command docker tag $image $RepoName/$dockerName:$tag
fi
run_command docker tag $image $ZANATA_DOCKER_PUSH_REGISTRY/$RepoName/$dockerName:$tag
}
CENTOS_VERSION_ARRAY=( 7 6)
CENTOS_VERSION_DEFAULT=7
##=== parsing Start ===
declare -a TagArray=()
declare -a ExtraOptionArray=()
DockerfileSource=
RepoName=zanata
ReleaseMode=0
BuildOptions=
IsPush=0
ImageTag=
IgnoreError=
## BaseVersion is the version without qualifier. e.g. BaseVersion of 4.4.0-alpha-1 is 4.4.0
BaseVersion=
## ArtifactVersion is the actual version. e.g. 4.4.0-alpha-1
ArtifactVersion=
### OPTIONS
while getopts "hb:it:pr:" opt;do
case $opt in
### -h: Show detail help.
h )
zanata_help_raw $0
exit ${EXIT_OK}
;;
### -b: docker build options
### e.g. -b '--build-arg CENTOS_VERSION=6'
b )
BuildOptions=${OPTARG}
;;
### -i: Ignore error
### Proceed the task.
i )
IgnoreError=1
;;
### -t: docker image tags. Can specify multiple times for multiple tags.
### E.g. -t 'latest' -t '4.0.0' -t '4.0.0-1'
### If no other tags are specified, the image is tagged as 'latest'
t )
TagArray+=( ${OPTARG} )
;;
### -p: Also push the image
p )
IsPush=1
;;
###
### -r <tag>: Release docker image as zanata/<DockerName>:<tag>
### For normal release, just put version number. e.g. '4.3.0'
### If you change Dockerfile after a released version, put the
### sequence number after version, e.g. '4.3.0-1'
###
### You can also put pre-release tag like 4.4.0-0.alpha-2,
### This will download corresponding WAR file (e.g. zanata-war-4.4.0-alpha-2.war)
### and make the docker image (e.g. zanata/server:4.4.0-0.alpha-2)
###
### This does following:
### 1. Update ZANATA_VERSION in Dockerfile.
### (Sequence number will NOT be included)
### 2. Make a commit to zanata-docker-file
### with message: '[release] Image zanata/<DockerName>:<version> is released'
### Note that this commit will NOT be pushed to zanata-docker-file automatically.
### 3. Build and tag the image as zanata/<DockerRepoName>:<tag>
### 4. Push the image to DockerHub
###
### You can also put pre-release tag like 4.4.0-alpha-2,
### This will download corresponding WAR file (e.g. zanata-war-4.4.0-alpha-2.war)
### and make the docker image (e.g. zanata/server:4.4.0-alpha-2)
### However, 'latest' won't be tag to pre-release images.
r )
ImageTag=${OPTARG}
TagArray+=( ${ImageTag} )
BaseVersion=${ImageTag%%-*}
Qualifier=$(sed -r -n -e 's/^[^-]*-(.*)$/\1/p'<<<$ImageTag)
if [[ $Qualifier == [abrmABRM]* ]]; then
## Cover the pre-release qualifiers such as alpha, beta, rc, milestone
ArtifactVersion="$BaseVersion-$Qualifier"
else
TagArray+=( latest )
ArtifactVersion=$BaseVersion
fi
IsPush=1
ReleaseMode=1
echo "ArtifactVersion: $ArtifactVersion" > /dev/stderr
echo "TagArray: ${TagArray[@]}" > /dev/stderr
;;
* )
failed ${EXIT_FATAL_INVALID_OPTIONS} "$opt"
;;
esac
done
shift $((OPTIND-1))
## Check DockerName
if [[ -z ${1-} ]];then
echo_stderr "[FATAL] <DockerName> not specified"
echo_stderr "Use '$0 -h' for help"
exit $EXIT_FATAL_INVALID_OPTIONS
fi
DockerName=$1
case $DockerName in
server )
DockerfileSource=zanata-server
;;
centos-repo-builder )
;;
* )
if [[ ! -r $DockerName/Dockerfile ]]; then
echo_stderr "[FATAL] DockerName $DockerName is invalid"
exit $EXIT_FATAL_INVALID_OPTIONS
fi
DockerfileSource=$DockerName
;;
esac
if [ -z "${TagArray-}" ];then
TagArray=( latest )
fi
## Remove duplicate tags
TagArray=($(tr ' ' '\n' <<<${TagArray[@]} | sort -u))
##=== Update Version when releasing
if [[ $ReleaseMode -eq 1 ]]; then
cd $ScriptDir
## Does Docker hub already has the tag?
tagStr=$(curl -s -S "https://registry.hub.docker.com/v2/repositories/$RepoName/$DockerName/tags/" | jq '.results[] | select(.name == "'$ImageTag'" )')
if [[ -n $tagStr ]];then
if [[ $IgnoreError -eq 0 ]];then
echo_stderr "Docker hub already has $RepoName/$DockerName:$ImageTag"
exit $EXIT_RETURN_FALSE
fi
fi
sed -i -e 's/^ARG ZANATA_VERSION=.*$/ARG ZANATA_VERSION='$ArtifactVersion'/' $DockerfileSource/Dockerfile
if ! git diff --exit-code; then
git commit -a -m "[release] Image zanata/$DockerName:$ImageTag is released"
elif [[ $IgnoreError -eq 0 ]];then
echo_stderr "Docker file is already updated"
exit $EXIT_RETURN_FALSE
fi
cd -
fi
##=== make Start ===
for tag in "${TagArray[@]}";do
case $DockerName in
centos-repo-builder )
for CENTOS_VERSION in "${CENTOS_VERSION_ARRAY[@]}" ;do
Image="$RepoName/$DockerName:$CENTOS_VERSION-$tag"
run_command docker build $BuildOptions --build-arg "CENTOS_VERSION=$CENTOS_VERSION" -t $Image - \
<<< "$(sed -e "s/@CENTOS_VERSION@/$CENTOS_VERSION/g" $ScriptDir/$DockerName/Dockerfile)"
## Stage Tag
docker_tag_image $Image $DockerName "$CENTOS_VERSION-$tag"
if [[ $CENTOS_VERSION = $CENTOS_VERSION_DEFAULT ]];then
docker_tag_image $Image $DockerName $tag
fi
if [[ $tag = 'latest' ]];then
docker_tag_image $Image $DockerName $CENTOS_VERSION
fi
## Stage push
if [ $IsPush -eq 1 ];then
run_command docker push $ZANATA_DOCKER_PUSH_REGISTRY/$Image
if [[ $CENTOS_VERSION = $CENTOS_VERSION_DEFAULT ]];then
run_command docker push $ZANATA_DOCKER_PUSH_REGISTRY/$DockerName:$tag
fi
if [[ $tag = 'latest' ]];then
run_command docker push $ZANATA_DOCKER_PUSH_REGISTRY/$DockerName:$CENTOS_VERSION
fi
fi
done
;;
* )
Image=$RepoName/$DockerName:$tag
run_command docker build $BuildOptions -t $Image $DockerfileSource
run_command docker tag $Image $ZANATA_DOCKER_PUSH_REGISTRY/$Image
if [ $IsPush -eq 1 ];then
run_command docker push $ZANATA_DOCKER_PUSH_REGISTRY/$Image
fi
;;
esac
done
###
### EXIT_STATUS
### See https://github.com/zanata/zanata-scripts/blob/master/zanata-env.sh