forked from posuch/zabbix-kubernetes-1
-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.sh
executable file
·153 lines (127 loc) · 3.57 KB
/
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
#!/bin/bash
####################################################################
## Helpers
notice(){
echo -e "\e[1;32m$1\e[0m"
}
# Parameter:
# 1: cmd
# Execute simple shell command, exit if errorcode of shell command != 0
exec_cmd(){
local CMD="$1"
echo "+ $CMD"
eval "$CMD 2>&1"
local RET="$?"
if [ "$RET" != "0" ];then
echo "ERROR: execution failed (returncode $RET)"
exit 2
fi
return 0
}
get_env(){
echo -n "--env ZABBIX_SERVER=localhost "
echo -n "--env ZABBIX_HOST=localhost "
echo -n "--env K8S_CONFIG_TYPE=token "
}
####################################################################
## MAIN
DEFAULT_PHASES="build_image cleanup docu test_container"
VERSION="${VERSION:-$(git describe --abbrev=0 --tags)}"
TIMESTAMP="$(date --date="today" "+%Y%m%d%H%M%S")"
DOCKER_SQUASH="${DOCKER_SQUASH:-true}"
DELAY="35"
BDIR="$(dirname $(readlink -f $0))"
cd $BDIR || exit 1
# PHASES
build_image(){
if [ -z "$VERSION" ];then
echo "ERROR: no git release tag available"
exit 1
fi
if [ "$DOCKER_SQUASH" == "true" ];then
SQUASH_OPT="--squash"
notice "Squashing of image is enabled, you can disable that by 'export DOCKER_SQUASH=false'"
else
SQUASH_OPT=""
fi
exec_cmd "docker build $SQUASH_OPT -t ${IMAGE_BASE} -f Dockerfile ."
SIZE="$(docker inspect $IMAGE_BASE --format='{{.Size}}')"
notice "Image size $(( $SIZE / 1024 / 1024 ))MB"
}
test_container(){
IDENT="${IMAGE_NAME}_test"
docker kill $IDENT &> /dev/null
docker rm $IDENT &> /dev/null
exec_cmd "docker run --rm $(get_env) -d --name $IDENT ${IMAGE_BASE} --disable_colors"
sleep 10
echo "====== DOCKER LOGS"
docker logs --until=50s $IDENT
echo "=================="
exec_cmd "docker ps |grep $IDENT"
exec_cmd "docker kill $IDENT"
}
inspect(){
IDENT="${IMAGE_NAME}_test"
exec_cmd "docker run -ti --rm $(get_env) --name $IDENT ${IMAGE_BASE} /bin/sh"
}
cleanup(){
exec_cmd "rm -rf /tmp/${IMAGE_NAME}*"
exec_cmd "docker rmi ${IMAGE_NAME} --force"
}
docu(){
exec_cmd "template/create_template_documentation"
}
publish_image(){
TIMESTAMP="$(date --date="today" "+%Y-%m-%d_%H-%M-%S")"
exec_cmd "docker tag ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION} ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION}"
exec_cmd "docker push ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION}"
exec_cmd "docker tag ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION} ${IMAGE_REPO}/${IMAGE_NAME}:latest"
exec_cmd "docker push ${IMAGE_REPO}/${IMAGE_NAME}:latest"
}
display_hint(){
notice "CMD:"
echo
echo "$0 <phase>..<phase> <dockerhub repo_name>"
echo
notice "AVAILABLE PHASES:"
echo " - default"
echo " ($DEFAULT_PHASES)"
echo " - inspect"
for PHASE in $DEFAULT_PHASES; do
echo " - $PHASE <repo_name>"
done
echo " - publish_image (optional)"
echo " - inspect (optional)"
}
if [ ${#@} -lt 2 ];then
display_hint
exit 2
fi
IMAGE_REPO="${@: -1}"
if type $IMAGE_REPO &>/dev/null;then
echo "ERROR: last param is not the dockerhub repo"
exit 1
fi
PHASES=""
for arg in "${@:1:$(( ${#@} - 1 ))}"; do
if [ "$arg" = "default" ];then
PHASES="$PHASES $DEFAULT_PHASES"
else
PHASES="$PHASES $arg"
fi
done
IMAGE_NAME="k8s-zabbix"
IMAGE_BASE="${IMAGE_REPO}/${IMAGE_NAME}:${VERSION}"
for PHASE in $PHASES;
do
if ( type "$PHASE" >/dev/null 2>&1 );then
notice "INFO: PHASE >>>$PHASE<<< for $IMAGE_BASE"
$PHASE
else
notice "ERROR: no such phase : $PHASE"
exit 1
fi
done
#SIZE="$(docker inspect $IMAGE_BASE --format='{{.Size}}')"
#notice "Image size $(( $SIZE / 1024 / 1024 ))MB"
notice "SUCESSFULLY COMPLETED"