-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathdocker-build.sh
executable file
·156 lines (138 loc) · 3.69 KB
/
docker-build.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
#!/bin/bash
# define paths
SCRIPT=$(readlink -f "$0")
DIR=$(dirname "$SCRIPT")
BUILD_FBG="$DIR/fbg-server"
BUILD_BACKUPER="$DIR/fbg-server"
EXPORT_FILE="freeboardgames.tar"
## read docker build paths and image names from env file
if [ ! -f .env ]; then
echo -e "No .env file found"
exit 1
else
source .env
fi
# ensure a docker-compose.yml is present
if [ ! -f docker-compose.yml ]; then
cp docker-compose.yml.example docker-compose.yml
fi
# ensure yarn and npm are installed
yarn -v > /dev/null 2>&1 || (echo "ERROR: yarn is not installed." && exit 1)
npm -v > /dev/null 2>&1 || (echo "ERROR: npm is not installed." && exit 1)
# define functions
confirm() {
# call with a prompt string or use a default
read -r -p "$@"" [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
return 0
;;
*)
return 1
;;
esac
}
run_gameserver() {
cd "$DIR"
echo -e "Now the game index will be generated."
yarn run codegen || (echo -e "ERROR. (ensure node is up-to-date)" && exit 1)
cd web && yarn run i18n:copy && cd -
}
install_dependencies() {
cd "$DIR"
yarn install || (echo -e "ERROR. (ensure node is up-to-date)" && exit 1)
}
compile_dependencies() {
cd "$DIR"
install_dependencies && run_gameserver || exit 1
}
build_docker() {
cd "$DIR"
docker build -t "$BUILD_IMAGE_COMMON" "$BUILD_DIR_COMMON" || exit 1
docker build -t "fbg-web" -t "$BUILD_IMAGE_WEB" "$BUILD_DIR_WEB" || exit 1
docker build -t "fbg-server" -t "$BUILD_IMAGE_FBG" "$BUILD_DIR_FBG" || exit 1
docker build -t "fbg-backuper" -t "$BUILD_IMAGE_BACKUPER" "$BUILD_DIR_BACKUPER" || exit 1
}
push_docker() {
cd "$DIR"
docker push "$BUILD_IMAGE_WEB" || exit 1
docker push "$BUILD_IMAGE_FBG" || exit 1
docker push "$BUILD_IMAGE_BACKUPER" || exit 1
}
prune_docker_images() {
PRUNE_IMAGES=$(docker images --format '{{.Repository}}:{{.Tag}}' | grep -e freeboardgames -e fbg)
echo -e "$PRUNE_IMAGES"
if $(confirm "Delete listed docker images?") ; then
echo "$PRUNE_IMAGES" | xargs docker rmi
fi
}
minikube_cache() {
echo -e "Uploading $1..."
minikube cache add $1 || exit 1
}
upload_minikube() {
minikube_cache fbg-web
minikube_cache fbg-server
}
exportdocker() {
cd "$DIR"
EXP_IMAGES=$(docker images --format '{{.Repository}}:{{.Tag}}' | grep freeboardgames/)
echo -e "Exporting :\n\n$EXP_IMAGES \n\ninto $EXPORT_FILE"
docker save ${EXP_IMAGES} -o $EXPORT_FILE
}
importdocker() {
cd "$DIR"
echo -e "Importing $EXPORT_FILE"
docker load -i $EXPORT_FILE
}
# ###### Parsing arguments
#Usage print
usage() {
echo "Usage: $0 -[d|b|p|e|i|r|h|m]" >&2
echo "
-d, Installs dependencies and generates game index.
-b, Build docker image
-p, Push docker images
-e, Export build docker images
-i, Import docker images
-r, Prune docker images
-h, Print this help text
-m, Upload images to minikube
If the script will be called without parameters, it will run:
$0 -d -b
">&2
exit 1
}
while getopts ':dbpeirm' opt
do
case "$opt" in
'd')compile_dependencies;
;;
'b')build_docker;
;;
'p')push_docker;
;;
'e')exportdocker;
;;
'i')importdocker;
;;
'r')prune_docker_images;
;;
'm')upload_minikube;
;;
*) usage;
;;
esac
done
# Executed when no arguments passed
if [ $OPTIND -eq 1 ]; then
if $(confirm "Install dependencies and generate game index?") ; then
compile_dependencies
fi
if $(confirm "Build docker image?") ; then
build_docker
fi
if $(confirm "Upload images to minikube?") ; then
upload_minikube
fi
fi