-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.sh
executable file
·183 lines (160 loc) · 4.72 KB
/
manage.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
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
#!/bin/bash
# Simple script for easy management for pilulka.cz docker based project
# which I touched. It's mostly wrapper for docker-composer and docker command.
MAIN_DIR=$(dirname "$0")
PROJECT=pilulka
CONTAINERS=( dbm ipilulka.cz edi db pharmdata ipilulka.sk )
SCRIPT_NAME=${0##*/}
usage() {
cat << EOF
Usage: $SCRIPT_NAME [OPTIONS] [COMMAND]
Simple manager for Pilulka.cz docker projects.
Works as simple wrapper and extender for docker-compose.
Options:
--help Display this help and exit.
Available command:
list List of available containers.
buildImage Rebuild necessary images, see "buildImage" for
more informations about possible Docker images.
exec Exec command in running container.
go Attach container shell.
compose Use docker-compose for other commands.
rmStopped Remove stopped containers
EOF
}
callCompose() {
docker-compose -p $PROJECT -f $MAIN_DIR/docker-compose.yml "$@"
}
list() {
echo "Available containers: ${CONTAINERS[@]}"
}
buildImage() {
local images=( apache-php dbm ipilulka.cz edi pharmdata ipilulka.sk )
local buildOptions=(
"-t pilulka:php5.6-apache $MAIN_DIR/php5.6-apache"
"-t pilulka:dbm $MAIN_DIR/dbm"
"-t pilulka:ipilulka.cz $MAIN_DIR/ipilulka.cz"
"-t pilulka:edi $MAIN_DIR/edi"
"-t pilulka:pharmdata $MAIN_DIR/pharmdata"
"-t pilulka:ipilulka.sk $MAIN_DIR/ipilulka.sk"
)
if [ "$#" -eq 0 ]; then
echo "Builds the image according appropriate Dockerfile"
echo "Usage $SCRIPT_NAME buildImage IMAGE_NAME|all"
echo "Available Docker images: ${images[@]}"
echo "If \"all\" is used as IMAGE_NAME, all images are built."
return 0
fi
if [ "$1" == "all" ]; then
for buildCommand in "${buildOptions[@]}"; do
docker build $buildCommand
done
return 0
fi
local i
for ((i=0; i<${#images[@]}; i++)); do
if [ "$1" == "${images[i]}" ]; then
docker build ${buildOptions[i]}
return $?
fi
done
echo "Cannot build docker image \"$1\", image is unknown."
echo "Available Docker images: ${images[@]}"
return 1
}
execCommand() {
local containerID=$(getContainerID $1)
if [[ "$containerID" == ----* ]]; then
# no runnig container found, so we try to run command
# via new container
callCompose run -T --rm "$@"
return $?
fi
shift
docker exec $containerID "$@"
}
# remove all stopped containers
removeStopped() {
local containerIDs=()
local containerNames=()
while IFS= read -r line; do
if [[ "$line" == *Exited* ]]; then
# separator for columns in output from docker ps -a are spaces
# so we can use strin as array
local columns=(${line});
containerIDs+=(${columns[0]})
containerNames+=(${columns[1]})
fi
done < <(docker ps -a --format "{{.ID}} {{.Names}} {{.Status}}")
if [ ${#containerIDs[@]} -eq 0 ]; then
echo "no stopped containers found"
return 0
fi
echo "removing these stopped containers: ${containerNames[@]}"
docker rm ${containerIDs[@]}
}
# get contaner ID by it's name from defined by docker-compose
getContainerID() {
echo $(callCompose ps $1 | tail -n1 | cut -d' ' -f1)
}
# exec interarractive bash session with particular container
attachContainer() {
local containerID=$(getContainerID $1)
if [[ "$containerID" == ----* ]]; then
echo "container $1 doesn't run, cannot attach into it"
echo "start the container first (docker run or docker-compose up)"
return 1
fi
docker exec -it $containerID /bin/bash
}
if [ "$#" -eq 0 ]; then
usage
exit 1
fi
case "$1" in
--help)
usage
;;
list)
list
;;
buildImage)
shift
buildImage "$@"
;;
exec)
if [ -z "$2" ]; then
echo "exec: no container name supplied"
echo "usage: $SCRIPT_NAME exec CONTAINER COMMAND"
list
exit 1
fi
if [ -z "$3" ]; then
echo "exec: no command for execution supplied"
echo "usage: $SCRIPT_NAME exec CONTAINER COMMAND"
exit 1
fi
shift
execCommand "$@"
;;
compose)
shift
callCompose "$@"
;;
rmStopped)
removeStopped
;;
go)
if [ -z "$2" ]; then
echo "go: no container name supplied"
echo "usage: $SCRIPT_NAME go CONTAINER"
list
exit 1
fi
attachContainer $2
;;
*)
echo "unknown option or commad \"$1\" use --help for help"
exit 1
;;
esac