-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
shift
executable file
·232 lines (192 loc) · 6.51 KB
/
shift
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
#!/usr/bin/env bash
set -e
#### DB ####
export MYSQL_ROOT_PASSWORD=da642315cb2d390714590dad93e07c50
export MYSQL_DATABASE=shift
export MYSQL_USER=shift
export MYSQL_PASSWORD=ok124
# can force the node version to use a sqlite in memory database instead of mysql:
# export MYSQL_DATABASE=sqlite
export NGINX_HTTPS_PORT=4443
export NGINX_HTTP_PORT=4480
export LETSENCRYPT_WEBROOT="/tmp/letsencrypt-auto"
export SHIFT_DOMAIN="localhost"
#### DOCKER ####
export COMPOSE_PROJECT_NAME="shift"
#### SMTP ####
# with this configuration, emails will log to console, and to a file
# ./shift emails
# - or -
# ./shift attach node
# $ tail -f /var/log/shift-mail.log
#
# it will also try to "send" ethereal emails
# https://ethereal.email/messages
#
# export SMTP_HOST=smtp.ethereal.email
# export [email protected]
# export SMTP_PASS=E18RgFGSnhwNMzmBdJ
# export SMTP_LOGIN=587
SMTP_HOST=
SMTP_USER=
SMTP_PASS=
SMTP_LOGIN=
SMTP_DOMAIN=
#####
# === HERE BE DRAGONS ===
# These are some convenience pattens and functions that [email protected] has found most helpful
# Feel free to copy into your own projects, or mostly don't worry about it!
#####
# ENV
export ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export EVENTIMAGES="${ROOT}/backend/eventimages"
export SHIFT_IMAGE_DIR="/opt/backend/eventimages"
# Check for an overrides file
if [ -f ${ROOT}/shift.overrides ]; then
source ${ROOT}/shift.overrides
fi
# Check for secrets
if [ -f $ROOT/secrets ]; then
source $ROOT/secrets
fi
SCRIPT_NAME=$(basename $0)
# ====================
# https://www.marksayson.com/blog/wait-until-docker-containers-initialized/
# Max query attempts before consider setup failed
MAX_TRIES=5
# Return true-like values if and only if logs
# contain the expected "ready" line
function nodeIsReady() {
# todo: need a new docker version to use --since
# use --since 1 minute to try to filter out old log messages
docker-compose logs node | grep "SHIFT to Bikes listening"
}
function waitUntilServiceIsReady() {
attempt=1
while [ $attempt -le $MAX_TRIES ]; do
if "$@"; then
echo "$2 container is up!"
break
fi
echo "Waiting for $2 container... (attempt: $((attempt++)))"
# wait more each time; hopefully less than --since
sleep $((3*(attempt-1)))
done
if [ $attempt -gt $MAX_TRIES ]; then
echo "Error: $2 not responding, cancelling set up"
exit 1
fi
}
# ====================
sub_help() { # - Show this text
echo "Usage: ./${SCRIPT_NAME} <subcommand> [options]"
echo "Subcommands:"
# Extracts everything after "sub_" and outputs with the stuff after the comment
cat ${ROOT}/shift | grep '^sub_.*#.*' | sed -E 's/sub_([a-z-]*).*#(.*)/ \1\2/'
}
sub_up() { # - Bring all services up
if [ ! -d $LETSENCRYPT_WEBROOT ] ; then
mkdir $LETSENCRYPT_WEBROOT
fi
sub_compose up --force-recreate -d
# wait for node to be ready
# tbd: for staging, could also check for "mysqld: ready for connections."
waitUntilServiceIsReady nodeIsReady "node"
echo "Assuming no fatal errors are shown above, you can now use the environment!"
echo "You can run './${SCRIPT_NAME} watch' to monitor for changes and rebuild."
}
sub_watch() { # - Watch the site directory for changes and rebuild the site
sub_compose run --rm hugo -w -e development
wait
}
sub_down() { # - Stop and remove services
sub_compose down
}
sub_ps() { # - Print service information
sub_compose ps
}
sub_pull() { # - Update the codebase to HEAD of currently deployed branch from https://github.com/shift-org/shift-docs
echo "pulling most recent commit from deployed branch of https://github.com/shift-org/shift-docs"
cd ${ROOT}
git pull || exit 1
# forces a recreation and restart of the containers from their images
sub_up
}
sub_reload() { # <service>, ... - Trigger a code/conf reload
echo Reloading $@
sub_compose kill -s SIGHUP $@
}
sub_start() { # <service>, .. - Start service(s)
sub_compose up --force-recreate -d $@
}
sub_stop() { # <service>, ... - Stop service(s)
sub_compose kill $@
}
sub_restart() { # <service>, ... - Trigger entrypoint.sh, required after modifying anything in borzoi
sub_stop $@
sub_start $@
}
sub_rebuild() { # <service>, ... - Remove and rebuild a service
echo Rebuilding $@
sub_stop $@
sub_compose rm -f $@
sub_compose build $@
sub_compose up -d $@
}
sub_attach() { # <service> - Run bash on a running service
sub_compose exec $@ bash
}
sub_compose() { # <cmd...> - Run a compose with associated files
OS=`uname`
OSAlt=`uname -o`
if [ "$OS" == "Darwin" ] ; then
docker compose $@
elif [ "$OS" == "Linux" ] ; then
docker-compose $@
elif [ "$OSAlt" == "Msys" ] ; then
docker-compose $@
else
echo "if docker compose isn't working, please email [email protected] to let us know"
exit 255
fi
}
sub_logs() { # <service>, ... - Show last 50 lines and attach to a service
sub_compose logs --tail=50 -f $@
}
sub_emails() { # - Show recent emails and update with new ones
sub_compose logs node
}
sub_mysql() { # - Open a mysql prompt with the db selected
cd "${ROOT}"
sub_compose exec db mysql -u ${MYSQL_USER} -h db -P 3306 -p"${MYSQL_PASSWORD}" ${MYSQL_DATABASE} $@
}
sub_mysql-pipe() { # - Pipe sql into this command to send to db
cd "${ROOT}"
docker exec -i $(sub_compose ps -q db) mysql -u ${MYSQL_USER} -h db -P 3306 -p"${MYSQL_PASSWORD}" ${MYSQL_DATABASE} $@
}
sub_mysqldump() { # - Dump the mysql database
cd "${ROOT}"
sub_compose exec db mysqldump -u ${MYSQL_USER} -h db -P 3306 -p"${MYSQL_PASSWORD}" ${MYSQL_DATABASE} $@
}
sub_backup() { # - Backup the mysql database
cd "${ROOT}"
docker-compose exec -T db mysqldump -u ${MYSQL_USER} -h db -P 3306 -p"${MYSQL_PASSWORD}" --no-tablespaces ${MYSQL_DATABASE} | gzip -c > /tmp/mysql-$(date +%F).out.gz
aws s3 cp /tmp/mysql-$(date +%F).out.gz s3://shift2bikes-backups-us-west-2/
}
SUB_CMD=$1
case ${SUB_CMD} in
"" | "-h" | "--help")
sub_help
;;
*)
shift
set +e
sub_${SUB_CMD} $@
if [ $? = 127 ]; then
echo "Error: '${SUB_CMD}' is not a known subcommand." >&2
echo " Run '${SCRIPT_NAME} --help' for a list of known subcommands." >&2
exit 1
fi
set -e
;;
esac