Skip to content

Commit

Permalink
Add a runit service for Postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
Homme Zwaagstra committed Mar 27, 2014
1 parent 694f5bd commit f4eda8d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# <http://switch2osm.org/serving-tiles/manually-building-a-tile-server-12-04/>.
#

FROM phusion/baseimage:0.9.8
FROM phusion/baseimage:0.9.9
MAINTAINER Homme Zwaagstra <[email protected]>

# Set the locale. This affects the encoding of the Postgresql template
Expand All @@ -17,6 +17,7 @@ ENV LANG C.UTF-8
RUN update-locale LANG=C.UTF-8

# Ensure `add-apt-repository` is present
RUN apt-get update -y
RUN apt-get install -y software-properties-common python-software-properties

# Mapnik requires a recent version of Boost
Expand Down Expand Up @@ -96,12 +97,17 @@ RUN sed --file /tmp/postgresql.conf.sed --in-place /etc/postgresql/9.1/main/post
RUN mkdir -p /etc/my_init.d
ADD shmmax.sh /etc/my_init.d/shmmax.sh

# Create a `postgresql` `runit` service
ADD postgresql /etc/sv/postgresql
RUN update-service --add /etc/sv/postgresql

# Create an `apache2` `runit` service
ADD apache2 /etc/sv/apache2
RUN update-service --add /etc/sv/apache2

# Create a `renderd` `runit` service
ADD renderd /etc/sv/renderd
RUN update-service --add /etc/sv/renderd

# Clean up APT when done
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand Down
14 changes: 14 additions & 0 deletions postgresql/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

##
# Runit check script for postgresql
#
# This script loops infinitely until it sees the 4 postgres pids
#

pid_count=0
while [ "$pid_count" -lt 3 ]
do
sleep 1
pid_count=`pidof postgres | grep -o ' ' | wc -l`
done
Empty file added postgresql/down
Empty file.
13 changes: 13 additions & 0 deletions postgresql/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

##
# Runit run script for postgresql
#

# If the database has been persisted in a volume, it may need
# permissions setting.
chown -R postgres /var/lib/postgresql/ || exit 1

exec /sbin/setuser postgres /usr/lib/postgresql/9.1/bin/postgres \
-D /var/lib/postgresql/9.1/main \
-c config_file=/etc/postgresql/9.1/main/postgresql.conf
Empty file added renderd/down
Empty file.
2 changes: 1 addition & 1 deletion renderd/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

# Ensure postgresql is running
run startdb || exit 1
sv start postgresql || exit 1

#`/sbin/setuser www-data` runs the given command as the user `www-data`. If
# you omit that part, the command will be run as root.
Expand Down
46 changes: 26 additions & 20 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ die () {
exit
}

_startservice () {
sv start $1 || die "Could not start $1"
}

startdb () {
if ! pgrep postgres > /dev/null
then
chown -R postgres /var/lib/postgresql/ || die "Could not set permissions on /var/lib/postgresql"
service postgresql start || die "Could not start postgresql"
fi
_startservice postgresql
}

initdb () {
Expand Down Expand Up @@ -84,28 +84,34 @@ cli () {
exec bash
}

startrenderd () {
if ! update-service --check renderd
then
echo "Starting renderd"
update-service --add /etc/sv/renderd || die "Could not add renderd as a runit service"
else
echo "Starting renderd"
sv start renderd || die "Could not start renderd"
fi
}

startservices () {
startrenderd

echo "Starting web server"
sv start apache2 || die "Could not start apache"
_startservice renderd
_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
Expand Down

0 comments on commit f4eda8d

Please sign in to comment.