Skip to content

Commit 1cb4368

Browse files
committed
Install Postgres locally for the lulz
fix #2 Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
1 parent 3f83e14 commit 1cb4368

File tree

6 files changed

+60
-31
lines changed

6 files changed

+60
-31
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ EXPOSE 22 80 443
1313
# Runit
1414
ADD nginx.sh /etc/service/nginx/run
1515
ADD sshd.sh /etc/service/sshd/run
16+
ADD postgres.sh /etc/service/postgres/run
1617
RUN chmod +x /etc/service/nginx/run \
17-
&& chmod +x /etc/service/sshd/run
18+
&& chmod +x /etc/service/sshd/run \
19+
&& chmod +x /etc/service/postgres/run
1820

1921
# Bootstrap
2022
ADD boot.sh /usr/local/bin/boot.sh

boot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ if [ 0 -eq `ls /etc/ssh/ssh_host_* | wc -l` ]; then
99
fi
1010

1111
# create the users and finalize the setup.
12-
/usr/local/bin/configure.py
12+
/usr/local/bin/configure.py &
1313

1414
exec /usr/bin/runsvdir -P /etc/service

configure.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import subprocess
2323
import sys
2424
import tempfile
25+
import time
2526

2627
# Logging
2728
logger = logging.getLogger(__name__)
@@ -30,9 +31,6 @@
3031
with open('/root/accounts.json', 'r', encoding='utf-8') as f:
3132
USERS = json.load(f)
3233

33-
SYSTEMD = True
34-
RUNIT = False
35-
3634
if not len(USERS):
3735
logger.info('accounts.json is empty. Using the hardcoded folks.')
3836
USERS = {
@@ -60,9 +58,12 @@
6058
# }
6159
}
6260

63-
HOSTNAME = "capistrano"
64-
POSTGRES_HOST = "localhost"
65-
POSTGRES_ROOT_PASSWORD = "root"
61+
HOSTNAME = os.environ.get("HOSTNAME", "capistrano")
62+
POSTGRES_HOST = os.environ.get("POSTGRES_HOST", "localhost")
63+
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "root")
64+
65+
SYSTEMD = "RUNIT" not in os.environ
66+
RUNIT = "RUNIT" in os.environ
6667

6768

6869
def pwgen(length=64):
@@ -278,19 +279,46 @@ def init_user(user, keys, environ):
278279
return 0
279280

280281

282+
def init_postgres(password):
283+
p = pwd.getpwnam('postgres')
284+
uid, gid = p.pw_uid, p.pw_gid
285+
homedir = p.pw_dir
286+
287+
os.initgroups('postgres', gid)
288+
os.setgid(gid)
289+
os.setuid(uid)
290+
os.chdir(homedir)
291+
292+
subprocess.check_call([
293+
'psql', '-c',
294+
"ALTER USER postgres WITH PASSWORD '{}';".format(password)
295+
])
296+
297+
281298
def main(argv):
282299
environ = {}
283300
environ["GEM_HOME"] = ".gem/ruby/2.4.0"
284-
environ["POSTGRES_HOST"] = os.environ.get('POSTGRES_HOST', POSTGRES_HOST)
301+
environ["POSTGRES_HOST"] = POSTGRES_HOST
285302
environ["POSTGRES_PORT"] = "5432"
286303

287304
delete = False if len(argv) < 2 else argv[1] == 'DELETE'
288305

306+
# Handle a local Postgres setup.
307+
if POSTGRES_HOST == 'localhost':
308+
logger.info('init postgres (with 5s of pause)')
309+
310+
# Wait for Postgres
311+
time.sleep(5)
312+
p = multiprocessing.Process(
313+
target=init_postgres, args=(POSTGRES_PASSWORD, ))
314+
p.start()
315+
p.join()
316+
289317
with tempfile.NamedTemporaryFile() as fp:
290318
logging.info('create temporary .pgpass')
291319
fp.write(
292-
bytearray('{}:*:*:postgres:{}'.format(environ[
293-
"POSTGRES_HOST"], POSTGRES_ROOT_PASSWORD), 'utf-8'))
320+
bytearray('{}:*:*:postgres:{}'.format(environ["POSTGRES_HOST"],
321+
POSTGRES_PASSWORD), 'utf-8'))
294322
fp.seek(0)
295323
os.chmod(fp.name, mode=0o600)
296324
environ['PGPASSFILE'] = fp.name

docker-compose.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ services:
99
- "2222:22"
1010
- "8080:80"
1111
environment:
12-
- POSTGRES_HOST=postgres
13-
- RACK_ENV=development
14-
depends_on:
15-
- postgres
16-
17-
postgres:
18-
image: postgres:9.6-alpine
19-
environment:
12+
- RUNIT=1
2013
- POSTGRES_PASSWORD=root
21-
- POSTGRES_PGDATA=/var/lib/postgresql/data/pgdata
22-
ports:
23-
- "5432:5432"
24-
volumes:
25-
- postgres:/var/lib/postgresql/data
26-
27-
volumes:
28-
postgres:
14+
- RACK_ENV=development

install.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,22 @@ apt-get update
9494
apt-get install -q -y \
9595
libpq-dev \
9696
nodejs \
97+
postgresql-${POSTGRES_VERSION} \
9798
postgresql-client-${POSTGRES_VERSION} \
99+
postgresql-contrib-${POSTGRES_VERSION} \
98100
ruby${RUBY_VERSION} \
99101
ruby${RUBY_VERSION}-dev \
100102
ruby-switch
101103

102-
if [ "$DOCKER" = "" ]; then
103-
apt-get install -q -y \
104-
postgresql-${POSTGRES_VERSION} \
105-
postgresql-contrib-${POSTGRES_VERSION}
104+
if [ "$DOCKER" != "" ]; then
105+
f=/etc/postgresql/9.6/main/postgresql.conf
106+
sed -i 's/^#log_destination/log_destination/g' $f
107+
sed -i 's/^#logging_collector/logging_collector/g' $f
108+
sed -i 's/^ssl/#ssl/g' $f
109+
110+
d=/var/run/postgresql/9.6-main.pg_stat_tmp
111+
mkdir $d
112+
chown postgres:postgres $d
106113
fi
107114

108115
# Post Installation

postgres.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
exec 2>&1
3+
export HOME=/var/lib/postgres
4+
exec chpst -u postgres -U postgres \
5+
/usr/lib/postgresql/9.6/bin/postgres \
6+
-D /etc/postgresql/9.6/main/

0 commit comments

Comments
 (0)