-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-builder
182 lines (166 loc) · 6.32 KB
/
docker-builder
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
#!/bin/bash
# Check if script run under bash
if [ -z "$BASH_VERSION" ]; then
# If not / switchovere to bash
echo "*** Run under shell - switchovere to bash ***"
exec /bin/bash "$0" "$@"
fi
DB_VERSION="0.23.0"
DB_VERSION_DATE="2024-07-20"
############################################################
# ENV #
############################################################
env_file="build.env"
build_env="build/$env_file"
# Function to check if variables are defined
check_variables() {
if [ -z "${BUILD_NAME}" ] || [ -z "${BUILD_VERSION}" ]; then
echo "*** ERROR: BUILD_NAME or BUILD_VERSION not defined in $1"
exit 1
fi
}
if [ -f "$env_file" ]; then
export $(cat "$env_file" | xargs)
check_variables "$env_file"
echo "***"
echo "Builder: ${BUILD_NAME}:${BUILD_VERSION}"
elif [ -f "$build_env" ]; then
export $(cat "$build_env" | xargs)
check_variables "$build_env"
echo "***"
echo "Builder: ${BUILD_NAME}:${BUILD_VERSION}"
else
echo "*** ERROR: Missing $env_file file where we need BUILD_NAME and BUILD_VERSION"
echo "*** use commands to create basic file:"
echo "echo 'BUILD_NAME=\"easyrsa\"' > $env_file"
echo "echo 'BUILD_VERSION=\"1.0\"' >> $env_file"
exit 1
fi
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "***"
echo "*** Built docker container based on Docerfile and predefined variables"
echo "***"
echo "*** Syntax: $0 [-f] [-s] [-d] [-l] [-h|-v] "
echo " options:"
echo " -f Force build without prompting, even if an identical image already exists in the repository."
echo " -s Save the image to tar.gz file."
echo " -d If the file './docker-compose.yml' exists - try to restart service '${BUILD_NAME}'"
echo " -v Print software version and exit."
echo " -l Disable cache and log all output to the docker-builder.log file"
echo " -h Print this Help."
echo
}
############################################################
# Functions #
############################################################
Version()
{
echo "$0 - $DB_VERSION [$DB_VERSION_DATE]"
}
SaveDockerFile()
{
echo "Writing local tar.gz file - please wait..."
echo ""
docker save ${BUILD_NAME}:${BUILD_VERSION/\~/-} | gzip > ${BUILD_NAME}_${BUILD_VERSION/\~/-}.tar.gz
echo "Files:"
ls -lah | grep ${BUILD_NAME} | grep tar.gz
if [ -f "/usr/bin/md5sum" ]; then
/usr/bin/md5sum ${BUILD_NAME}_${BUILD_VERSION/\~/-}.tar.gz > ${BUILD_NAME}_${BUILD_VERSION/\~/-}.tar.gz.md5
MD5=$(md5sum ${BUILD_NAME}_${BUILD_VERSION/\~/-}.tar.gz)
echo "MD5: ${MD5}"
echo "*** Check MD5 sum by command: md5sum -c ${BUILD_NAME}_${BUILD_VERSION/\~/-}.tar.gz.md5"
echo ""
fi
echo "*** You can load this file to repo by 'docker load' command"
echo "*** ie.# docker load -i <path to image file>"
echo "*** docker load options:"
echo "*** -i, --input string Read from the tar archive file, instead of STDIN"
echo "*** (even if compressed with gzip, bzip2, or xz)"
echo "*** -q, --quiet Suppress the load output"
echo ""
}
# Specifying the optstring
# https://www.computerhope.com/unix/bash/getopts.htm
while getopts ":fhvdsSl" option; do
case $option in
h)
Help
exit;;
v)
Version
exit;;
f)
FORCE_BUILD="y"
;;
s)
FORCE_SAVE_TAR_GZ="y"
;;
d)
FORCE_DOCKER_COMPOSE_RESTART="y"
;;
l)
CACHE_OFF="--no-cache --progress=plain"
LOGGER='2>&1 | tee docker-builder.log'
;;
\?)
echo "Error: Invalid option"
exit;;
esac
done
#shift $((OPTIND-1)) #This tells getopts to move on to the next argument.
############################################################
############################################################
# Main program #
############################################################
############################################################
if [ -d "./app" ]; then
echo "*** Found Python APP folder - clean Python Cache folders."
find ./app | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
fi
if [[ "$(docker images -q ${BUILD_NAME}:${BUILD_VERSION/\~/-} 2> /dev/null)" != "" ]]; then
if [ "$FORCE_BUILD" = "y" ]; then
echo "*** FORCE_BUILD==YES: ${BUILD_NAME}:${BUILD_VERSION/\~/-} exist in docker repository - will be overwritten"
else
while true; do
read -p "${BUILD_NAME}:${BUILD_VERSION/\~/-} exist in the docker repository - do you want to remove it? [y/n]: " answer
case ${answer:0:1} in
y|Y)
docker rmi ${BUILD_NAME}:${BUILD_VERSION/\~/-}
echo ""
echo "DONE: Docker Remove Image - ${BUILD_NAME}:${BUILD_VERSION/\~/-}"
echo ""
break;;
n|N)
echo "ERROR: You can't build a new image when an image with the same tag exists in the repository !!!"
echo "All Done."
exit;;
*) echo "Please answer yes or no [y/n].";;
esac
done
fi
fi
echo "Docker build: ${BUILD_NAME}:${BUILD_VERSION/\~/-}"
echo "build ${CACHE_OFF} --build-arg VERSION=${BUILD_VERSION} -t ${BUILD_NAME}:${BUILD_VERSION/\~/-} . ${LOGGER}"
eval "docker build ${CACHE_OFF} --build-arg VERSION=${BUILD_VERSION} -t ${BUILD_NAME}:${BUILD_VERSION/\~/-} . ${LOGGER}"
echo ""
echo "Build Completed."
docker images | grep -e ${BUILD_NAME} -e REPOSITORY
echo ""
# Option -s
if [ "$FORCE_SAVE_TAR_GZ" = "y" ] ; then
SaveDockerFile
fi
# Option -d
if [ "$FORCE_DOCKER_COMPOSE_RESTART" = "y" ] && [ -f "./docker-compose.yml" ]; then
echo "*** File ./docker-compose.yml exist - will try to replace service by the new image."
docker-compose stop ${BUILD_NAME}
docker-compose rm -f ${BUILD_NAME}
docker-compose up -d ${BUILD_NAME}
fi
echo ""
echo "All done!"