-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit-container-script.sh
89 lines (77 loc) · 3.09 KB
/
init-container-script.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
#!/bin/bash
# This script will be executed once on a `docker run`.
# Create a new PostgreSQL user.
create_pg_user () {
# Create the given user in PostgreSQL.
# If the user already exists PostgreSQL will return an error, but we don't care.
echo "CREATE USER $PG_USERNAME WITH SUPERUSER PASSWORD '$PG_PASSWORD';" | su - postgres -c "psql"
echo "CREATE DATABASE $PG_USERNAME OWNER $PG_USERNAME;" | su - postgres -c "psql"
}
# Add $SSH_PUBLIC_KEY to the authorized_keys file.
add_ssh_key () {
if grep -q "$SSH_PUBLIC_KEY" ~/.ssh/authorized_keys
then
# Do nothing if the key is already there.
echo "The SSH public key already exists"
else
# Add the key if not already there.
echo $SSH_PUBLIC_KEY >> /root/.ssh/authorized_keys
fi
}
# Move PostgreSQL data folder to the shared volume.
move_postgresql_data_to_shared_volume () {
# Test if /var/lib/postgresql/9.3/main is a symlink.
if [[ -L "/var/lib/postgresql/9.3/main" ]]
then
echo "Data have already been moved."
else
# Change the ownership of the folder.
mkdir -p /srv/pgdata/data
mkdir -p /srv/pgdata/logs
chown -R postgres:postgres /srv/pgdata
# Stop PostgreSQL.
service postgresql stop
# Ensure postgresql is stopped and the dirs are empty.
service postgresql status
if [ $? = 3 ] && [ ! "$(ls -A /srv/pgdata/data)" ] && [ ! "$(ls -A /srv/pgdata/logs)" ]
then
# Move the data dir to the mounted volume.
su - postgres -c "mv /var/lib/postgresql/9.3/main /srv/pgdata/data"
su - postgres -c "ln -s /srv/pgdata/data/main /var/lib/postgresql/9.3/main"
# Move the logs to the mounted volume.
mv /var/log/postgresql /srv/pgdata/logs
ln -s /srv/pgdata/logs/postgresql /var/log/postgresql
echo "PostgreSQL data moved, symlinks created."
else
echo "/srv/pgdata/data or /srv/pgdata/logs are not empty dirs. Operation aborted."
fi
# Start PostgreSQL.
service postgresql start
fi
}
# STEP 1: add the given SSH public key.
# During a `docker run` the environment variable SSH_PUBLIC_KEY must be passed.
# This key will be added to the authorized_keys of the SSH server of the container.
# This way the key's owner is allowed to SSH into the container.
echo " * Adding public SSH key..."
if [ ! -z "$SSH_PUBLIC_KEY" ] # If the env var $SSH_PUBLIC_KEY is set.
then
add_ssh_key
fi
# STEP 2: create the given PostgreSQL user.
# During a `docker run` the environment variable PG_USERNAME and PG_PASSWORD must be passed.
echo " * Creating PostgreSQL user $PG_USERNAME..."
if [ ! -z "$PG_USERNAME" ] && [ ! -z "$PG_PASSWORD" ]
then
create_pg_user
fi
# STEP 3: move PostgreSQL data folder to the shared volume.
# Ensure /srv/pgdata is mounted, which means the container was run with:
# docker run ... --volume=/localpath:/srv/pgdata ...
echo " * Moving PostgreSQL data..."
if [[ ! "$(mount)" =~ \ /srv/pgdata\ type ]]
then
echo "There is no mounted volume"
else
move_postgresql_data_to_shared_volume
fi