diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..6214048 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,61 @@ +# VERSION: 0.2 +# DESCRIPTION: Create the PostgreSQL 9.4 Databasee in a container +# AUTHOR: Dennis Mojado (denmojo@gmail.com) +# COMMENTS: +# This file describe how to install PostgreSQL with main +# dependencies installed. +# USAGE: +# # Download this Dockerfile +# +# # Build PostgreSQL image +# docker build -t postgresql-9.4 +# +# docker run -v /var/lib/postgresql/9.4:/var/lib/postgresql/9.4 \ +# postgresql-9.4 +# +# +FROM debian:stretch + +MAINTAINER Mario Incandenza + +ENV DEBIAN_FRONTEND noninteractive + +ADD ./accountsdb.xml /tmp +ADD ./messagedb.xml /tmp + +RUN apt-get update -qq && apt-get upgrade -y + +RUN apt-get install redis-server wget openjdk-8-jre-headless -y + +RUN apt-get install postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 libpostgresql-jdbc-java -y + +RUN wget https://github.com/liquibase/liquibase/releases/download/liquibase-parent-3.5.3/liquibase-debian_3.5.3_all.deb + +RUN dpkg -i liquibase-debian_3.5.3_all.deb + +RUN apt-get clean + +RUN sed 's/^bind\s\+127\.0\.0\.1/bind 0\.0\.0\.0/g' < /etc/redis/redis.conf > /tmp/redis.conf +RUN cat /tmp/redis.conf > /etc/redis/redis.conf && rm /tmp/redis.conf + +RUN echo "/etc/init.d/postgresql start && exit 0" > /etc/rc.local +RUN /etc/init.d/postgresql start &&\ + su postgres -c "psql --command \"CREATE USER signal with encrypted password 's1gn4l';\" " &&\ + su postgres -c "psql --command \"CREATE DATABASE signaldb WITH OWNER signal;\" " &&\ + su postgres -c "liquibase --driver=org.postgresql.Driver --classpath=/usr/share/java/postgresql-jdbc4.jar --url=jdbc:postgresql://localhost:5432/signaldb --username=signal --password=s1gn4l --changeLogFile=/tmp/accountsdb.xml update" &&\ + su postgres -c "liquibase --driver=org.postgresql.Driver --classpath=/usr/share/java/postgresql-jdbc4.jar --url=jdbc:postgresql://localhost:5432/signaldb --username=signal --password=s1gn4l --changeLogFile=/tmp/messagedb.xml update" + +RUN dpkg -P liquibase +RUN rm liquibase-debian_3.5.3_all.deb + +USER postgres + +RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.6/main/pg_hba.conf + +RUN echo "listen_addresses='*'" >> /etc/postgresql/9.6/main/postgresql.conf + +EXPOSE 5432 6379 + +USER root +# VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] /usr/share/java/postgresql-jdbc4.jar +CMD service redis-server start && su postgres -c "/usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf" diff --git a/docker/accountsdb.xml b/docker/accountsdb.xml new file mode 100644 index 0000000..e6d225c --- /dev/null +++ b/docker/accountsdb.xml @@ -0,0 +1,174 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CREATE type device_t AS (id integer, "authToken" varchar(255), salt varchar(255), "signalingKey" varchar(255), "gcmId" text, "apnId" text); + CREATE type account_t AS (number varchar(255), "supportsSms" smallint, devices device_t array); + UPDATE accounts SET data = row_to_json(row(number, supports_sms, array[row(1, auth_token, salt, signaling_key, gcm_id, apn_id)::device_t])::account_t) + + + + + + + + + + + DROP type account_t; + DROP type device_t; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CREATE OR REPLACE FUNCTION "custom_json_object_set_key"( + "json" json, + "key_to_set" TEXT, + "value_to_set" anyelement + ) + RETURNS json + LANGUAGE sql + IMMUTABLE + STRICT + AS $function$ + SELECT COALESCE( + (SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}') + FROM (SELECT * + FROM json_each("json") + WHERE "key" <> "key_to_set" + UNION ALL + SELECT "key_to_set", to_json("value_to_set")) AS "fields"), + '{}' + )::json + $function$; + UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number' AND k.last_resort = 1; + UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number'; + + + + + + diff --git a/docker/messagedb.xml b/docker/messagedb.xml new file mode 100644 index 0000000..e1d16a6 --- /dev/null +++ b/docker/messagedb.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 5000); + + + + DROP RULE bounded_message_queue ON messages; + CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 1000); + + + +