-
Notifications
You must be signed in to change notification settings - Fork 2
/
run
executable file
·116 lines (96 loc) · 3.65 KB
/
run
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
#!/bin/bash
# taken from https://github.com/universitas/universitas.no
# utility script / entry point for the repo
main__(){
while (( $# > 0 )); do
rest=${@:2}
case $1 in
help ) show_help; exit 0 ;; # show help for commands
dc ) docker-compose $rest; exit ;; # docker-compose [...args]
da ) docker-compose run --rm web python manage.py $rest; exit ;; # django-admin [...args]
dump_db ) dump_db $rest; exit ;; # dump database, optionally to [file]
reload ) (2>&1 reload_service > reload-service.log) &;; # reload service when code changed
webpack ) docker-compose run --entrypoint bash --rm webpack; exit;; # login to webpack container
django ) docker-compose run --rm web "${rest:-sh}"; exit ;; # login to django container
nginx ) docker-compose run --rm webserver "${rest:-sh}"; exit ;; # login to nginx container
logs ) docker-compose logs --tail=50 -f $rest ; exit ;; # show docker logs
prod ) # production settings
production_settings; echo 'production settings, image version:' $IMAGE_VERSION ;;
dev ) # development settings
echo "development settings" ;;
build_and_tag ) production_settings; build_and_tag ;;
* ) show_help ${@:1}; exit 1 ;;
esac;
shift 1;
done
}
production_settings() {
export BRANCH=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
# if [[ $BRANCH == 'master' ]]; then
export IMAGE_VERSION=$(git tag --merged | tail -n1)
# fi
export COMPOSE_FILE=docker-compose.base.yml:docker-compose.prod.yml
}
show_help() {
# ansi codes
esc=$(printf '\033')
yellow="$esc[33;1m"
red="$esc[31;1m"
bold="$esc[1m"
reset="$esc[0m"
# help output
printf "\n usage: %s [...commmands]\n\n" "$0"
cat run \
| sed -n '/^main__()/,$p;/^}/q'\
| awk '/[a-zA-Z] *\) /{print}/case/{p=1}'\
| sed 's/; exit//' | sed 's/;;//'\
| sed "s/).*#\(.*\)$/)$yellow\1$reset/"\
| sed "s/^ *\(\w* *\))/ $bold\1$reset/"\
| sort
if [[ $# > 0 ]]; then
printf "\n %sunknown command: %s%s\n" "$red" "$*" "$reset" >&2
fi
}
load_db() {
dumpfile=$(ls *.sql -1t 2>/dev/null | head -n1)
dumpfile=${1:-$dumpfile}
[[ -e $dumpfile ]] || exit 1
echo "found database dump: $dumpfile"
cp "$dumpfile" django/dbdump.sql
# loading data
docker-compose run --rm web load_db
# cleanup
rm django/dbdump.sql
echo "done"
}
dump_db() {
DUMP=$(date +"db_%d-%m-%Y_%H.%M.%S.sql")
docker-compose up -d db
echo "dumping database to $DUMP"
docker-compose exec db pg_dump --no-owner -U thesaurus -d thesaurus > "$DUMP"
}
reload_service() {
echo "--- reload start $(date -Iseconds) ---"
production_settings
docker-compose run --rm webpack
docker-compose run --rm web python manage.py migrate
docker-compose run --rm web python manage.py collectstatic --clear --noinput
echo '--- reload OK ---'
}
build_and_tag() {
export DOCKER_DEFAULT_PLATFORM=linux/amd64
docker build --tag thejoeejoee/thesaurus-django --tag thejoeejoee/thesaurus-django:${IMAGE_VERSION} django/
docker build --tag thejoeejoee/thesaurus-nginx --tag thejoeejoee/thesaurus-nginx:${IMAGE_VERSION} nginx/
docker build --tag thejoeejoee/thesaurus-webpack --tag thejoeejoee/thesaurus-webpack:${IMAGE_VERSION} webpack/
docker push thejoeejoee/thesaurus-django
docker push thejoeejoee/thesaurus-django:${IMAGE_VERSION}
docker push thejoeejoee/thesaurus-nginx
docker push thejoeejoee/thesaurus-nginx:${IMAGE_VERSION}
docker push thejoeejoee/thesaurus-webpack
docker push thejoeejoee/thesaurus-webpack:${IMAGE_VERSION}
}
cd $(dirname $0)
unset CDPATH
export COMPOSE_FILE=docker-compose.base.yml:docker-compose.dev.yml
export IMAGE_VERSION=${IMAGE_VERSION:-develop}
main__ $@