-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.sh
More file actions
executable file
·162 lines (162 loc) · 5.08 KB
/
compose.sh
File metadata and controls
executable file
·162 lines (162 loc) · 5.08 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
#! /bin/bash
## Configuration
src=""
subdir=""
dest=""
remote=""
prefix=""
## Prefix calculation
if [ -z "$prefix" ]; then
basedir="$(basename `pwd`)"
basedir=${basedir/_/}
basedir=${basedir/-/}
basedir=${basedir/ /}
prefix="${basedir}_"
fi
## Full path calculation
fullsrc="$src"
fulldest="$dest"
[ "${subdir:0:1}" == "/" ] || subdir="/${subdir}"
if [ ! -z "$subdir" ]; then
fullsrc="${src}${subdir}"
fulldest="${dest}${subdir}"
fi
## Remote name calculation
len=$(( $(echo $prefix | wc -c) - 1 ))
if [ "${remote:0:$len}" != "$prefix" ]; then
remote="${prefix}${remote}"
fi
## Functions
function _help () {
echo ""
echo "${0:2} : Damn simple but useful docker/docker-compose wrapper"
echo "Copyright Thomas Sarboni <max-k@post.com> 2016"
echo ""
echo "Usage :"
echo " $0 [subcommand] [params]"
echo ""
echo "Available subcommands :"
echo " -h : Print this help message"
echo " help : Print this help message"
echo " up : Create all needed resources and start all services"
echo " rm : Delete all (running or not) services containers"
echo " sh : Run a shell in a running service container"
echo " dead : Run a shell in a non-running service container"
echo " sync : Sync codebase with running service container"
echo " logs : Show container(s) logs and follow them by default"
echo " clean : Delete volumes related to current docker-compose"
echo " * : Any other subcommand will be forwarded to docker-compose"
echo ""
echo "This wrapper can only be used with 'version 2' docker-compose.yml"
echo "So, please use service names instead of container names"
echo ""
echo "To show docker-compose help, please run \`${0:2} --help\`"
echo ""
[ -z "$1" ] && exit 1 || exit 0
}
function _sync () {
format="{{range .Mounts}}{{.Destination}} {{end}}"
for mountpoint in $(docker inspect --format="$format" $remote); do
if [ "$mountpoint" == "$fulldest" ]; then
echo ""
echo "Codebase already mounted as a volume"
echo "Synchronisation is not required"
echo ""
exit 1
fi
done
if [ -d "${src}/.git" ]; then
docker cp $fullsrc ${remote}:${fulldest}
exit 0
else
echo ""
echo "Please download codebase using git submodules first"
echo ""
echo "To do that, please run the following commands :"
echo " git submodule init"
echo " git submodule update"
echo ""
exit 1
fi
}
function _sh () {
if [ $# -eq 2 ]; then
docker exec -ti paristech-${2} /bin/bash
else
docker exec -ti paristech-${2} /bin/bash -c "${*:3}"
fi
}
function _clean () {
if [ $(./compose.sh ps | wc -l) -ne 2 ]; then
echo "ERROR : Please shutdown your environment first."
echo "You can use \`./compose.sh down\` command"
else
c=$(docker volume ls -qf dangling=true | grep ^${basedir}_* | wc -l )
if [ $c -eq 0 ]; then
echo "Nothing to delete"
exit 0
fi
echo "This volumes will be deleted"
echo "$(docker volume ls -qf dangling=true | grep ^${basedir}_*)"
read -r -p "Are you sure ? [y/n] " answer
case $answer in
y|Y)
for volume in $(docker volume ls -qf dangling=true); do
echo $volume | grep ^${basedir}_* 2>&1 > /dev/null
if [ $? -eq 0 ]; then
docker volume rm $volume 2>&1 > /dev/null
fi
done
echo "Operation completed"
exit 0 ;;
*)
echo "Operation cancelled"
exit 0 ;;
esac
fi
}
function _compose () {
if [ -z "`which docker-compose 2>/dev/null`" ]; then
echo ""
echo "docker-compose binary not found"
echo ""
echo "Please activate your Python virtualenv"
echo "and install compose using the following command :"
echo ""
echo " pip install -r requirements.txt"
echo ""
echo "More info about virtualenvs can be found here :"
echo "http://docs.python-guide.org/en/latest/dev/virtualenvs/"
echo ""
exit 1
else
err=0
docker-compose $@ 2>.err
if [ $? -ne 0 ]; then
err=1
if [ ! -z "`grep 'No such command' .err`" ]; then
echo ""
echo "docker-compose subcommand \`$1\` not found"
echo "or invalid docker-compose parameters."
echo ""
echo "To show docker-compose help, please run \`${0:2} --help\`"
echo ""
fi
fi
rm .err
exit $err
fi
}
## Arguments parsing
[ -z "$1" ] && _help
case $1 in
sh) _sh $@ ;;
logs) docker-compose logs -f ${@:2} ;;
dead) docker-compose run --entrypoint /bin/bash ${2} ;;
clean) _clean ;;
sync) _sync ;;
up) docker-compose up -d ;;
rm) docker-compose rm -f ${@:2} ;;
help|-h) _help ;;
*) _compose $@ ;;
esac