-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-tools.sh
More file actions
executable file
·243 lines (228 loc) · 7.15 KB
/
docker-tools.sh
File metadata and controls
executable file
·243 lines (228 loc) · 7.15 KB
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
#!/bin/bash
function usage_and_exit {
echo "Error: Illegal number of parameters"
echo "$0 (new|build|init|deinit|clean|start|stop|shell)"
echo ""
echo " new <template-dir> <dst> - creates new docker by copying template directory to a new one"
echo " build - builds new image (docker build)"
echo " init - starts the newly created image (docker run)"
echo " deinit - stops and removes container, so it could be initialized again (docker stop; docker rm)"
echo " clean - stop, remove container and image (docker stop; docker rm; docker rmi)"
echo " start - starts a stopped container (docker start)"
echo " stop - stop a container"
echo " shell - run a terminal inside container ( docker exec -ti bash )"
echo " snapshot - create snapshot"
echo " snapshot-all - create snapshot for all(!) running docker images"
echo " save - save current changes to the image"
echo " save-all - save current changes for all(!) running dokcer images"
echo " export - export <name>-<datetime>.tar.gz archive of container fs"
echo
exit;
}
function container_exec {
container="$1"
shift
cmdParams=("$@")
output=$(docker exec -t $container ${cmdParams[@]})
if [ $? -ne 0 ]
then
echo -e "[$container]: ${cmdParams[@]}\n$output\n[$container] Error\n"
else
echo -e "[$container]: ${cmdParams[@]}\n$output\n[$container] Success\n"
fi
}
if [[ "$1" != "pexec" ]]
then
if [[ "$#" -ne 1 && "$#" -ne 3 ]]; then
usage_and_exit
fi
if [[ "$#" -eq 1 && $1 == "new" ]]; then
usage_and_exit
fi
if [[ "$#" -eq 3 && $1 != "new" ]]; then
usage_and_exit
fi
fi
if [[ "$1" != "new" && "$1" != "snapshot-all" && "$1" != "save-all" && "$1" != "pexec" ]]; then
if [[ !(-a config && -f Dockerfile) ]]; then
echo "Error: Not a docker-tools directory"
echo
exit;
fi
source ./config
fi
FILENAME=`realpath $0`
DIRNAME=`pwd`
DATADIR="${DIRNAME}/data/";
DATE=`date +%Y%m%d`
DATETIME=`date +%Y%m%d%H%M%S`
if [ -z ${IMAGE+x} ];
then
IMAGE="${NAME}-image"
fi
for i in `set`
do
if [[ $i == "PORT"* ]]; then
P1=`echo ${i:4} | cut -d '=' -f 1`
P2=`echo ${i:4} | cut -d '=' -f 2`
PORTFW="$PORTFW -p $P2:$P1"
fi
if [[ $i == "UPORT"* ]]; then
P1=`echo ${i:5} | cut -d '=' -f 1`
P2=`echo ${i:5} | cut -d '=' -f 2`
PORTFW="$PORTFW -p $P2:$P1/udp"
fi
done
for i in "${DIRMAP[@]}"
do
P1=`echo $i | cut -d ':' -f 1`
P2=`echo $i | cut -d ':' -f 2`
DIRMAP_OPT="$DIRMAP_OPT -v ${DIRNAME}/data$P1:$P2"
done
for i in "${VOLMAP[@]}"
do
P1=`echo $i | cut -d ':' -f 1`
P2=`echo $i | cut -d ':' -f 2`
if [[ ${P1:0:1} == '/' ]]; then
DIRMAP_OPT="$DIRMAP_OPT -v $P1:$P2"
else
DIRMAP_OPT="$DIRMAP_OPT -v ${DATADIR}$P1:$P2"
fi
done
for i in "${NETLINK[@]}"
do
NETLINK_OPT="$NETLINK_OPT --link $i"
done
for i in "${DOCKERENV[@]}"
do
P1=`echo $i | cut -d '=' -f 1`
P2=`echo $i | cut -d '=' -f 2`
DOCKERENV_OPT="$DOCKERENV_OPT --env $P1=$P2"
done
case $1 in
new)
if [[ !(-d $2) ]]; then
echo "Error: Invalid template directory specified"
echo
exit;
fi
if [[ -a $3 ]]; then
echo "Error: Destination exists"
echo
exit;
fi
echo -n "Copying template to $3 ..."
cp -a $2 $3
echo done
echo "You probably want to do now: cd $3"
echo
;;
build)
docker build --build-arg NAME=${NAME} -t ${IMAGE} .
;;
init)
echo "Running ${NAME}"
docker run -d \
-h ${NAME} \
--name ${NAME} \
${PORTFW} \
${DIRMAP_OPT} \
${NETLINK_OPT} \
${DOCKERENV_OPT} \
--restart="unless-stopped" \
${IMAGE} #/root/start.sh
;;
start)
echo "Running ${NAME}"
docker start ${NAME}
;;
stop)
echo "Stopping ${NAME}"
docker stop ${NAME}
;;
deinit)
echo -e "\nYou will loose all unsaved data unless you used 'docker-tools.sh save' first!!!\n"
echo "Are you sure you want to deinit container?"
read -p "Type the name of the container (${NAME}): " -r
echo # (optional) move to a new line
if [[ $REPLY == $NAME ]]
then
echo "docker stop ${NAME}"
docker stop ${NAME}
echo "docker rm ${NAME}"
docker rm ${NAME}
fi
;;
shell)
docker exec -ti ${NAME} /bin/bash || docker exec -ti ${NAME} /bin/sh
;;
clean)
echo -e "\nYou will loose all container data!!!\n"
echo "Are you sure you want to delete container and image?"
read -p "Type the name of the container (${NAME}): " -r
echo # (optional) move to a new line
if [[ $REPLY == $NAME ]]
then
echo "docker stop ${NAME}"
docker stop ${NAME}
echo "docker rm ${NAME}"
docker rm ${NAME}
echo "docker rmi ${IMAGE}"
docker rmi ${IMAGE}
fi
;;
snapshot)
echo -n "Saving ${IMAGE}:${DATETIME}... "
docker commit ${NAME} ${IMAGE}:${DATETIME}
echo "done"
;;
snapshot-all)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in `docker ps --format "{{.Names}} {{.Image}}"`
do
echo $i
CNAME=`echo $i | cut -f 1 -d ' '`
INAME=`echo $i | cut -f 2 -d ' '`
echo docker commit $CNAME ${INAME}:${DATETIME}
echo -n "Saving ${INAME}:${DATETIME}... "
docker commit $CNAME ${INAME}:${DATETIME}
done
IFS=$SAVEIFS
# echo -n "Saving ${IMAGE}:${DATETIME}... "
# docker commit ${NAME} ${IMAGE}:${DATETIME}
# echo "done"
;;
save)
docker commit $NAME ${IMAGE}:latest
;;
save-all)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in `docker ps --format "{{.Names}} {{.Image}}"`
do
echo $i
CNAME=`echo $i | cut -f 1 -d ' '`
INAME=`echo $i | cut -f 2 -d ' '`
echo -n "Saving ${INAME}:${DATETIME}... "
docker commit $CNAME ${INAME}:latest
done
IFS=$SAVEIFS
# echo -n "Saving ${IMAGE}:${DATETIME}... "
# docker commit ${NAME} ${IMAGE}:${DATETIME}
# echo "done"
;;
export)
docker export ${NAME} | gzip -c > ${NAME}-${DATETIME}.tar.gz
;;
pexec)
cmdParams=${@:2}
for i in `docker ps --format "table {{.Names}}" | tail -n +2`
do
container_exec $i ${cmdParams[@]} &
done
;;
*)
echo "Unknown option"
;;
esac