-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.sh
executable file
·162 lines (135 loc) · 3.49 KB
/
host.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
#!/bin/bash
set -euo pipefail
imagename=damast-dev:latest
dbimagename=damast-local-db
run() {
sudo docker start $dbimagename
version=$(echo -n $(git describe --tags) | tr -sc '[a-zA-Z0-9_.-]' '-')
sudo docker run \
--name damast-dev-server \
-it \
--init \
--rm \
--volume="$(pwd)/damast:/damast:Z" \
--volume="$(pwd)/devdata:/data/:Z" \
--net=host \
--env FLASK_DEBUG=1 \
--env FLASK_ACCESS_LOG=/data/access_log \
--env FLASK_ERROR_LOG=/data/error_log \
--env PGPASSWORD=apipassword \
--env DAMAST_REPORT_FILE=/data/reports.db \
--env DAMAST_USER_FILE=/data/users.db \
--env DAMAST_SECRET_FILE=/data/secrets.json \
--env DAMAST_VERSION="$version" \
--env DAMAST_OVERRIDE_PATH="/data/override" \
--env DAMAST_VISITOR_ROLES="readdb,vis,reporting" \
--env DAMAST_MAP_STYLES="map-styles.json" \
--env DAMAST_REPORT_EVICTION_DEFERRAL=1 \
--env DAMAST_MAP_TILE_PATH=http://localhost:8001/tiles/{z}/{x}/{y}.png \
$imagename
sudo docker stop $dbimagename
}
build_damast() {
www_user_id=997
www_group_id=1001
env="TESTING"
port=8000
cat util/docker/{base,dev}.in > Dockerfile
sudo docker build \
-t $imagename \
--build-arg=USER_ID=$www_user_id \
--build-arg=GROUP_ID=$www_group_id \
--build-arg=DAMAST_ENVIRONMENT=$env \
--build-arg=DAMAST_PORT=$port \
--build-arg=OWN_USER_ID=$(id -u) \
--build-arg=OWN_USER_NAME=$(whoami) \
.
}
build_database() {
tmpdir=$(mktemp -d)
mkdir -p $tmpdir/init.d
if [[ $dump_gzipped = 1 ]]
then
zcat $dump_file > $tmpdir/dump.sql
else
cp $dump_file $tmpdir/dump.sql
fi
cat > $tmpdir/init.d/init-user-db.sh <<-EOF
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "\$POSTGRES_USER" --dbname "\$POSTGRES_DB" <<-EOSQL
CREATE USER users;
CREATE USER api PASSWORD 'apipassword';
CREATE USER ro_dump;
EOSQL
EOF
sudo docker run \
--detach \
--name $dbimagename \
--net=host \
-e POSTGRES_PASSWORD=postgres \
-v $tmpdir/init.d/:/docker-entrypoint-initdb.d:Z \
postgis/postgis:10-3.1
while ! pg_isready -h localhost; do sleep 1; done
PGPASSWORD=postgres psql \
-h localhost \
-U postgres \
--no-password \
-f $tmpdir/dump.sql
PGPASSWORD=postgres psql \
-h localhost \
-U postgres \
--no-password \
-c "ALTER DATABASE ocn RENAME TO testing;"
sudo docker stop $dbimagename
rm -rf $tmpdir
}
build() {
build_database
build_damast
}
about() {
cat 1>&2 <<EOF
Usage: host.sh [-h] [-b [-d dump [-z]]]
Host the Flask server locally. This will run a special variant of the Docker
container, which mounts the local devdata/ directory as the /data volume on the
Docker host, for runtime configuration and logging. The local damast/ source
folder is mounted to the /damast volume.
-h Show this help and exit
-b Instead of hosting the server, build the necessary Docker images
-d dump Use the file "dump" to populate the dev database
-z Notify that dump file is GZIP:ed
EOF
}
do_build=0
dump_file=/dev/null
dump_gzipped=0
OPTIND=0
while getopts hbd:z opt
do
case $opt in
h)
about
exit 0
;;
b)
do_build=1
;;
z)
dump_gzipped=1
;;
d)
dump_file=$OPTARG
;;
*)
about
exit 1
;;
esac
done
if [[ $do_build = 1 ]]
then
build
else
run
fi