-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizard
executable file
·184 lines (166 loc) · 5.55 KB
/
wizard
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
#!/usr/bin/env bash
trap "exit" INT TERM ERR
trap "kill 0" EXIT
local_dev(){
docker run --rm -p 5432:5432 -v \
sleeptracker-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
if iex -S mix phx.server; then
docker stop sleep_tracker_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
start_wizard(){
echo "Starting wizard"
docker run --rm -d -p 5432:5432 -v \
sleeptracker-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_db postgres &> /dev/null
# shellcheck disable=SC1091
source ./grimoire
if _build/prod/rel/sleep_tracker/bin/sleep_tracker daemon; then
echo "Wizard summoned."
echo "Visit http://localhost:4000 to start using it"
exit 0
fi;
docker stop sleep_tracker_db &> /dev/null
echo "Something went wrong while summoning the Wizard. Please pray harder."
exit 1
}
stop_wizard(){
echo "Stopping wizard"
docker stop sleep_tracker_db &> /dev/null
if ./_build/prod/rel/sleep_tracker/bin/sleep_tracker pid &> /dev/null; then
./_build/prod/rel/sleep_tracker/bin/sleep_tracker stop
fi;
echo "Wizard stopped"
}
reset_db(){
docker run --rm -p 5432:5432 -v \
sleeptracker-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
if mix ecto.drop && mix ecto.setup; then
docker stop sleep_tracker_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
run_migrations(){
docker run --rm -p 5432:5432 -v \
sleeptracker-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
if mix ecto.migrate; then
docker stop sleep_tracker_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
run_tests_one_at_time(){
docker run --rm -p 5432:5432 -v \
sleeptracker-test-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_test_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
mix ecto.drop &> /dev/null && mix ecto.setup &> /dev/null
if mix test --max-failures 1; then
docker stop sleep_tracker_test_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_test_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
run_tests_once(){
docker run --rm -p 5432:5432 -v \
sleeptracker-test-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_test_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
mix ecto.drop &> /dev/null && mix ecto.setup &> /dev/null
if mix test; then
docker stop sleep_tracker_test_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_test_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
watch_tests(){
docker run --rm -p 5432:5432 -v \
sleeptracker-test-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=postgres --name sleep_tracker_test_db postgres 2> /dev/null &
# shellcheck disable=SC1091
source ./grimoire
mix ecto.drop &> /dev/null && mix ecto.setup &> /dev/null
if mix test.watch; then
docker stop sleep_tracker_db &> /dev/null
exit 0;
fi;
docker stop sleep_tracker_test_db &> /dev/null
echo "Something went wrong. Exiting development mode."
exit 1;
}
# SSH_STRING_SLEEPTRACKER
# export SSH_STRING_SLEEPTRACKER=user@server -i ~/.ssh/privatekey
remote_terminal(){
ssh -tt $SSH_STRING_SLEEPTRACKER "/bin/bash -c 'docker-compose -f \
/app/docker-compose.yml exec app /bin/bash /app/bin/sleep_tracker remote'"
}
usage(){
echo -e "Available commands: \\n\\n" \
"dev - Start local development environment. \\n" \
"test - Runs test suite \\n" \
"defend - Watches files for any changes and runs tests for those files \\n" \
"dbreset - Drops current db and recreates it. \\n" \
"dbmigrate - Runs ecto migrations \\n" \
"summon - Start Wizard \\n" \
"prepare - Prepare the system for magic"
}
graceful_exit() {
# from: https://gist.github.com/mrbar42/529cf0db529c0408ba1ef414653becdd
# include this line: trap graceful_exit TERM INT HUP
local timeout=${1:-4}
local list=""
for c in $(ps -o pid= --ppid $$); do
# request children shutdown
kill -0 "$c" 2>/dev/null && kill -TERM "$c" && list="$list $c" #|| true
done
if [ -n "$list" ]; then
# schedule hard kill after timeout
(sleep "$timeout"; kill -9 "$list" 2>/dev/null || true) &
local killer=$!
wait "$list" 2>/dev/null || true
# children exited gracfully - cancel timer
sleep 0.5 && kill -9 "$killer" 2>/dev/null && list=""
fi
[ -z "$list" ] && exit 0 || echo "Something went wrong." && exit 1
}
# this line exectues when process exits
trap 'graceful_exit 5' TERM INT HUP
if [[ "$#" -gt 0 ]]; then
case $1 in
dev) local_dev;;
dbreset) reset_db;;
dbmigrate) run_migrations;;
tests) run_tests_once;;
test) run_tests_once;;
remote_terminal) remote_terminal;;
defend) watch_tests;;
summon) start_wizard;;
seal) stop_wizard;;
*) usage;;
esac
else usage
fi