forked from pluosi/app-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher
executable file
·171 lines (144 loc) · 3.78 KB
/
launcher
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
#!/bin/bash
usage () {
echo "Usage: launcher COMMAND [--docker-args STRING]"
echo "Commands:"
echo " start: Start the container"
echo " stop: Stop a running container"
echo " restart: Restart the container"
echo " destroy: Stop and remove the container"
echo " bootstrap: Bootstrap the container based on a template"
echo " rebuild: Rebuild the container (destroy old, bootstrap, start new)"
echo
echo " console: Rails console in containner"
echo " ssh: attach to containner"
echo
echo "Options:"
echo " --docker-args Extra arguments to pass when running docker"
exit 1
}
set -e
set -o pipefail
version=0.2.3
local_port=3000
local_image=local_pluosi/app_host:$version
container_name=app_host
dockerdir=$(cd "$(dirname "$(dirname $0)")"; pwd -P)
sharedir=$dockerdir/shared
loginfo() {
if [[ -t 1 ]]; then
>&2 printf "[$(date +'%Y-%m-%d %H:%M:%S')] \033[32m%s\033[m\n" "$1"
else
>&2 echo "[$(date +'%Y-%m-%d %H:%M:%S')] " "$1"
fi
}
err_and_quit () {
if [[ -t 1 ]]; then
>&2 printf "\n\n\033[33mError: %s\033[m\n\n" "$1"
else
>&2 echo "$1"
fi
exit 1
}
console(){
docker exec -it $container_name rails c
}
ssh(){
docker exec -it $container_name bash
}
set_ruby_version(){
ruby_version=$(cat .ruby-version)
}
init_sharedir(){
mkdir -p "$sharedir"
}
bootstrap() {
init_sharedir
rebuild
}
rebuild(){
loginfo "Now building the local docker image."
docker image rm local_image || true
if [[ "$quiet" == "" ]]; then
(
set -x
docker build -f Dockerfile -t $local_image .
)
else
(
set -x
docker build -f Dockerfile -t $local_image . >/dev/null
)
fi
loginfo "Image built."
}
destroy(){
(
set -x
docker stop -t 10 $container_name || true
docker rm $container_name || true
docker image rm local_image || true
)
}
start(){
existing=$(docker ps | awk '{ print $(NF) }' | grep "$container_name" || true)
if [[ $existing != "" ]]; then
loginfo "Nothing to do, your container $container_name has already started!"
exit 0
fi
existing=$(docker ps -a | awk '{ print $(NF) }' | grep "$container_name" || true)
if [[ $existing != "" ]]; then
loginfo "starting up existing container"
(
set -x
docker start $container_name
)
exit 0
fi
loginfo "Starting up new server container"
(
set -x
docker run $user_args -it -d --name $container_name -h $container_name -v $dockerdir/shared:/app/shared -p $local_port:8686 $local_image
)
}
stop(){
(
set -x
docker stop -t 10 $container_name
)
}
restart() {
stop
start
}
check_prereqs() {
# check docker
if ! which docker >/dev/null; then
err_and_quit "Docker is not installed?"
err_and_quit "See https://docs.docker.com"
fi
}
main() {
local action
while [[ $# -gt 0 ]]
do
case "$1" in
bootstrap|start|stop|restart|destroy|rebuild|console|ssh)
action=${1//-/_} ; shift 1 ;;
-h|--help) ( usage ; exit 1) ; shift 1 ;;
-v|--verbose) verbose=true ; shift 1 ;;
--docker-args) user_args=$2 ; shift 2 ;;
*) err_and_quit "Argument error. Please see help." ;;
esac
done
quiet="--quiet"
if [[ $verbose == "true" ]]; then
quiet=""
fi
if [[ "$action" == "" ]]; then
usage
else
"$action"
fi
}
check_prereqs
main "$@"