Skip to content

Commit

Permalink
adding sql migration support
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarault committed Oct 12, 2017
1 parent 64cba86 commit cc88546
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ FTP_DB_HOST= # db hostname or ip address, required
FTP_DB_NAME= # db name, required
FTP_DB_USER= # db user, required
FTP_DB_PASS= # db password, required
FTP_DB_ADMIN= # db user, required
FTP_DB_ADMIN_PASS= # db password, required
FTP_PG_MIGRATE= # ON/OFF, activate/deactivate

FTP_ROOT= # /path/to/ftp/root, optional, defaults to /data/ftp_root
LOGS= # /path/to/log/dir, optional, defaults to /var/log/proftpd
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ COPY vroot.conf /etc/proftpd/vroot.conf
COPY ./certs /etc/proftpd/certs
COPY ./exec /etc/proftpd/exec

# SQL MIGRATION TEMPLATE
COPY sql/proftp_tables.sql.tpl /etc/proftpd/proftp_tables.sql.tpl

COPY entrypoint.sh ./entrypoint.sh
RUN chmod a+x ./entrypoint.sh

Expand Down
6 changes: 5 additions & 1 deletion Dockerfile-alpine
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ RUN set -x && \
apk add --no-cache --virtual .persistent-deps \
ca-certificates \
curl \
postgresql-client && \
postgresql-client \
gettext && \
apk add --no-cache --virtual .build-deps \
git \
build-base \
Expand Down Expand Up @@ -44,6 +45,9 @@ COPY vroot.conf /etc/proftpd/vroot.conf
COPY ./certs /etc/proftpd/certs
COPY ./exec /etc/proftpd/exec

# SQL MIGRATION TEMPLATE
COPY sql/proftp_tables.sql.tpl /etc/proftpd/proftp_tables.sql.tpl

COPY entrypoint.sh ./entrypoint.sh
RUN chmod a+x ./entrypoint.sh

Expand Down
3 changes: 0 additions & 3 deletions docker-compose-alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ services:
- type: bind
source: "${MOD_EXEC_DIR:-./exec}"
target: "/etc/proftpd/exec"
- type: bind
source: "${SALT:-./.salt}"
target: "/etc/proftpd/.salt"
- type: bind
source: "${MOD_VROOT_CONF:-./vroot.conf}"
target: "/etc/proftpd/vroot.conf"
18 changes: 18 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ if [ ! -z "$MASQ_ADDR" ]; then
PROFTPD_ARGS="$PROFTPD_ARGS -DUSE_MASQ_ADDR"
fi

if [ "$FTP_PG_MIGRATE" = "ON" ]; then
## Create sql migration file from proftp_tables.sql.tpl template
envsubst < /etc/proftpd/proftp_tables.sql.tpl > /etc/proftpd/proftp_tables.sql
rm /etc/proftpd/proftp_tables.sql.tpl

## Init the PGPASSWORD env var so psql does not prompt it
export PGPASSWORD=$FTP_DB_ADMIN_PASS

## Wait for it : Postgres db is ready
until psql -h $FTP_DB_HOST -d $FTP_DB_NAME -U $FTP_DB_ADMIN -p 5432 -c '\l'; do
echo "Postgres is unavailable - sleeping"
sleep 1
done

echo "Postgres is up - executing command"
psql -h $FTP_DB_HOST -d $FTP_DB_NAME -U $FTP_DB_ADMIN -p 5432 < /etc/proftpd/proftp_tables.sql -w
fi

echo $PWD_SALT > /etc/proftpd/.salt

# allow proftpd writing custom logs
Expand Down
29 changes: 29 additions & 0 deletions sql/proftp_tables.sql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE SCHEMA ftp;
GRANT USAGE ON SCHEMA ftp TO $FTP_DB_USER;

CREATE TABLE ftp.users (
userid VARCHAR(30) NOT NULL UNIQUE,
passwd VARCHAR(80) NOT NULL,
uid INTEGER UNIQUE,
gid INTEGER,
homedir VARCHAR(255),
shell VARCHAR(255),
last_accessed TIMESTAMP WITH TIME ZONE
);
GRANT SELECT, UPDATE ON ftp.users TO $FTP_DB_USER;

CREATE TABLE ftp.groups (
groupname VARCHAR(30) NOT NULL,
gid INTEGER NOT NULL,
members VARCHAR(255)
);
GRANT SELECT ON ftp.groups TO $FTP_DB_USER;

CREATE TABLE ftp.login_history (
userid VARCHAR NOT NULL,
client_ip VARCHAR NOT NULL,
server_ip VARCHAR NOT NULL,
protocol VARCHAR NOT NULL,
login_date TIMESTAMP WITH TIME ZONE
);
GRANT INSERT ON ftp.login_history TO $FTP_DB_USER;

0 comments on commit cc88546

Please sign in to comment.