-
Notifications
You must be signed in to change notification settings - Fork 16
/
run.sh
executable file
·174 lines (144 loc) · 4.13 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
#!/bin/sh
##
# Run database operations
#
# The database affected depends on the value of the $RAILS_ENV
# environment variable. This defaults to 'development' but can be set
# to 'production'
#
# Command prefix that runs the command as the web user
asweb="setuser www-data"
die () {
msg=$1
echo "FATAL ERROR: " msg > 2
exit
}
_getdbname () {
if [ -z "$RAILS_ENV" ] || [ "$RAILS_ENV" = 'development' ]
then
dbname=openstreetmap # development
else
dbname=osm # production
fi
echo $dbname
}
_startservice () {
sv start $1 || die "Could not start $1"
}
startdb () {
_startservice postgresql
}
initdb () {
echo "Initialising postgresql"
if [ -d /var/lib/postgresql/9.1/main ] && [ $( ls -A /var/lib/postgresql/9.1/main | wc -c ) -ge 0 ]
then
die "Initialisation failed: the directory is not empty: /var/lib/postgresql/9.1/main"
fi
mkdir -p /var/lib/postgresql/9.1/main && chown -R postgres /var/lib/postgresql/
sudo -u postgres -i /usr/lib/postgresql/9.1/bin/initdb --pgdata /var/lib/postgresql/9.1/main
ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem /var/lib/postgresql/9.1/main/server.crt
ln -s /etc/ssl/private/ssl-cert-snakeoil.key /var/lib/postgresql/9.1/main/server.key
}
createuser () {
USER=www-data
echo "Creating user $USER"
setuser postgres createuser -s $USER
}
createdb () {
dbname=$( _getdbname )
echo "Creating database $dbname"
cd /var/www
$asweb bundle exec rake db:create
}
createdbfuncs () {
dbname=$( _getdbname )
echo "Creating functions in database $dbname"
cd /var/www
# Install the Postgresql Btree-gist extension
$asweb psql -d $dbname -c "CREATE EXTENSION btree_gist"
# Install the Postgresql functions
$asweb psql -d $dbname -c "CREATE FUNCTION maptile_for_point(int8, int8, int4) RETURNS int4 AS '`pwd`/db/functions/libpgosm', 'maptile_for_point' LANGUAGE C STRICT"
$asweb psql -d $dbname -c "CREATE FUNCTION tile_for_point(int4, int4) RETURNS int8 AS '`pwd`/db/functions/libpgosm', 'tile_for_point' LANGUAGE C STRICT"
$asweb psql -d $dbname -c "CREATE FUNCTION xid_to_int4(xid) RETURNS int4 AS '`pwd`/db/functions/libpgosm', 'xid_to_int4' LANGUAGE C STRICT"
}
migrate () {
echo "Migrating database"
cd /var/www
$asweb bundle exec rake db:migrate
}
import () {
dbname=$( _getdbname )
# Find the most recent import.pbf or import.osm
import=$( ls -1t /data/import.pbf /data/import.osm 2>/dev/null | head -1 )
test -n "${import}" || \
die "No import file present: expected /data/import.osm or /data/import.pbf"
# Decide whether we are reading an xml or pbf file
if echo $import | grep '.osm$'
then
read_cmd=xml
else
read_cmd=pbf
fi
echo "Importing ${import} into ${dbname}"
$asweb osmosis --read-$read_cmd file=$import --write-apidb database=$dbname user="www-data" validateSchemaVersion=no
}
dropdb () {
echo "Dropping database"
cd /var/www
$asweb bundle exec rake db:drop
}
cli () {
echo "Running bash"
cd /var/www
exec bash
}
startcgimap () {
_startservice cgimap
}
startdevserver () {
_startservice devserver
}
startservices () {
if [ "$RAILS_ENV" = 'development' ]
then
startdevserver
a2ensite development
else
startcgimap
a2ensite cgimap production
fi
_startservice apache2
}
help () {
cat /usr/local/share/doc/run/help.txt
}
_wait () {
WAIT=$1
NOW=`date +%s`
BOOT_TIME=`stat -c %X /etc/container_environment.sh`
UPTIME=`expr $NOW - $BOOT_TIME`
DELTA=`expr 5 - $UPTIME`
if [ $DELTA -gt 0 ]
then
sleep $DELTA
fi
}
# Unless there is a terminal attached wait until 5 seconds after boot
# when runit will have started supervising the services.
if ! tty --silent
then
_wait 5
fi
# Execute the specified command sequence
for arg
do
$arg;
done
# Unless there is a terminal attached don't exit, otherwise docker
# will also exit
if ! tty --silent
then
# Wait forever (see
# http://unix.stackexchange.com/questions/42901/how-to-do-nothing-forever-in-an-elegant-way).
tail -f /dev/null
fi