forked from devdaydresden/devday_website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·210 lines (202 loc) · 6.14 KB
/
run.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
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
#!/bin/bash
# vim: sw=2 ts=2 et si ai
set -e
dbdump=''
mediadump=''
container='app'
DOCKER_COMPOSE="docker-compose -f docker-compose.yml -f docker-compose.dev.yml"
export DOCKER_REGISTRY="${DOCKER_REGISTRY:-devdaydresden}/" # note trailing /
docker_compose_up() {
$DOCKER_COMPOSE up -d vault
while ! http_proxy= curl --silent --fail http://localhost:8200/v1/sys/health; do
echo -n '.'
done
echo " vault is running"
# fill vault with content
http_proxy= \
curl -X POST -H "X-Vault-Token: devday_root" --fail \
--data '{"data": {"postgresql_password": "devday", "secret_key": "s3cr3t", "confirmation_salt": "s4lt3d-stuff"}}' \
http://localhost:8200/v1/secret/data/devday
http_proxy= \
curl -X PUT -H "X-Vault-Token: devday_root" --fail \
--data "$(printf '{"policy": "%s"}' \
"$(sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' -e 's/"/\\"/g' < docker/vault/devday_policy.hcl)")" \
http://localhost:8200/v1/sys/policy/devday
http_proxy= \
curl -X POST -H "X-Vault-Token: devday_root" --fail \
--data '{"allowed_policies": ["devday"]}"' \
http://localhost:8200/v1/auth/token/roles/devday-app
http_proxy= \
APP_TOKEN=$(curl -X POST --silent -H "X-Vault-Token: devday_root" --fail \
--data '{"policies": ["devday"], "metadata": {"user": "devday"}, "ttl": "24h", "renewable": true}' \
http://localhost:8200/v1/auth/token/create/devday-app | \
python -c 'from __future__ import print_function; import json, sys; print(json.load(sys.stdin)["auth"]["client_token"])')
echo "VAULT_TOKEN=${APP_TOKEN}" > dev-env
$DOCKER_COMPOSE up -d
}
usage() {
cat >&2 <<EOD
usage: ./run.sh backup
./run.sh build
./run.sh compose [...]
./run.sh coverage
./run.sh coveralls
./run.sh devdata
./run.sh manage [...]
./run.sh messages
./run.sh purge
./run.sh -d databasedump.sql.gz -m mediadump.tar.gz restore
./run.sh [-c container] shell
./run.sh start
./run.sh stop
./run.sh test
EOD
}
OPTIND=1
while getopts 'd:m:c:h?' opt; do
case "$opt" in
h|\?)
usage
exit
;;
d)
dbdump="${OPTARG}"
;;
m)
mediadump="${OPTARG}"
;;
c)
container="${OPTARG}"
;;
*)
echo "unknown option \"$opt\"" >&2
usage
exit 64
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
cmd="$1"
shift || true
touch dev-env
case "$cmd" in
backup)
echo "*** Running backup"
$DOCKER_COMPOSE -f docker-compose.tools.yml run --rm backup
;;
build)
echo "*** Building Docker images"
$DOCKER_COMPOSE build $@
;;
compose)
$DOCKER_COMPOSE $@
;;
coverage)
if [ -z "$($DOCKER_COMPOSE ps -q)" ]; then
echo "*** Starting all containers"
docker_compose_up
fi
$DOCKER_COMPOSE exec "${container}" coverage run --branch manage.py test -v1 -k $@
$DOCKER_COMPOSE exec "${container}" coverage report -m
$DOCKER_COMPOSE exec "${container}" coverage html
;;
coveralls)
if [ -z "$($DOCKER_COMPOSE ps -q)" ]; then
echo "*** Starting all containers"
docker_compose_up
fi
$DOCKER_COMPOSE exec "${container}" env \
CI_BRANCH="${TRAVIS_BRANCH}" \
CI_BUILD_URL="${TRAVIS_BUILD_WEB_URL}" \
CI_NAME="Travis" \
COVERALLS_REPO_TOKEN="${COVERALLS_REPO_TOKEN}" \
coveralls
;;
devdata)
echo " Starting containers"
docker_compose_up
echo " Compiling translations"
$DOCKER_COMPOSE exec "${container}" python manage.py compilemessages
echo " Running migrations"
$DOCKER_COMPOSE exec "${container}" python manage.py migrate
echo " Filling database"
$DOCKER_COMPOSE exec "${container}" python manage.py devdata
;;
docker-push)
if [ -n "$DOCKER_USERNAME" ]; then
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
else
echo "WARNING: \$DOCKER_USERNAME is not set. Assuming you're already logged in to Docker" >&2
fi
echo "*** Pushing Docker images to Docker hub"
$DOCKER_COMPOSE push
;;
log|logs)
$DOCKER_COMPOSE logs -f "${container}"
;;
manage)
$DOCKER_COMPOSE exec "${container}" python manage.py $@
;;
messages)
$DOCKER_COMPOSE exec "${container}" python manage.py makemessages -l de --no-obsolete
$DOCKER_COMPOSE exec "${container}" python manage.py compilemessages -l de
;;
purge)
echo "*** Purge data"
echo " Deleting all containers and volumes"
$DOCKER_COMPOSE down --volumes
echo " Deleting media files"
rm -rf devday/media/*
;;
restore)
if [ -z "${dbdump}" ]; then
echo "error: must specify -d databasedump.sql.gz file to restore" >&2
exit 64
fi
if [ -z "${mediadump}" ]; then
echo "error: must specify -m mediafiles.tar.gz archive to restore" >&2
exit 64
fi
echo "*** Restoring database dump ${dbdump} and media dump ${mediadump}"
echo " Deleting all containers and volumes"
$DOCKER_COMPOSE down --volumes
echo " Starting containers"
docker_compose_up
echo " Waiting for database to be available"
$DOCKER_COMPOSE exec db sh -c 'until pg_isready -U devday -d devday; do sleep 1; done'
echo " Importing database dump"
gunzip -c "${dbdump}" | $DOCKER_COMPOSE exec -T db psql -U devday devday
echo " Unpacking media dump"
$DOCKER_COMPOSE exec -T "${container}" tar xz -C /srv/devday/media < "${mediadump}"
echo "*** Running migrations"
$DOCKER_COMPOSE exec "${container}" python manage.py migrate
echo "*** Import completed"
;;
shell)
echo "*** Starting shell in ${container} container"
$DOCKER_COMPOSE exec "${container}" bash
;;
start|'')
if [ -z "$($DOCKER_COMPOSE ps -q)" ]; then
echo "*** Starting all containers"
docker_compose_up
fi
$DOCKER_COMPOSE logs -f "${container}"
;;
stop)
$DOCKER_COMPOSE down
;;
test)
if [ -z "$($DOCKER_COMPOSE ps -q)" ]; then
echo "*** Starting all containers"
docker_compose_up
fi
$DOCKER_COMPOSE exec "${container}" python manage.py test -v1 -k $@
;;
*)
echo -e "error: unknown action \"${cmd}\":\n" >&2
usage
exit 64
;;
esac