Scheletro docker per servizi accessori a Signal: postgres+redis+initdb Signal
This commit is contained in:
parent
2afa749372
commit
6b3653507d
3 changed files with 314 additions and 0 deletions
61
docker/Dockerfile
Normal file
61
docker/Dockerfile
Normal file
|
@ -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 <mario.incandenza@autistici.org>
|
||||
|
||||
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"
|
174
docker/accountsdb.xml
Normal file
174
docker/accountsdb.xml
Normal file
|
@ -0,0 +1,174 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
|
||||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
|
||||
|
||||
<changeSet id="1" author="moxie">
|
||||
<createTable tableName="accounts">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="number" type="varchar(255)">
|
||||
<constraints unique="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="auth_token" type="varchar(255)">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="salt" type="varchar(255)">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="signaling_key" type="varchar(255)"/>
|
||||
|
||||
<column name="gcm_id" type="text"/>
|
||||
|
||||
<column name="apn_id" type="text"/>
|
||||
|
||||
<column name="supports_sms" type="smallint" defaultValue="0"/>
|
||||
</createTable>
|
||||
|
||||
<createTable tableName="pending_accounts">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="number" type="varchar(255)">
|
||||
<constraints unique="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="verification_code" type="varchar(255)">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
|
||||
<createTable tableName="keys">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="number" type="varchar(255)">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="key_id" type="bigint">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="public_key" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="identity_key" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="last_resort" type="smallint" defaultValue="0"/>
|
||||
</createTable>
|
||||
|
||||
<createIndex tableName="keys" indexName="keys_number_index">
|
||||
<column name="number"/>
|
||||
</createIndex>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="2" author="matt">
|
||||
|
||||
<addColumn tableName="accounts">
|
||||
<column name="data" type="json" />
|
||||
</addColumn>
|
||||
|
||||
<sql>CREATE type device_t AS (id integer, "authToken" varchar(255), salt varchar(255), "signalingKey" varchar(255), "gcmId" text, "apnId" text);</sql>
|
||||
<sql>CREATE type account_t AS (number varchar(255), "supportsSms" smallint, devices device_t array);</sql>
|
||||
<sql>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)</sql>
|
||||
|
||||
<addNotNullConstraint tableName="accounts" columnName="data"/>
|
||||
|
||||
<dropColumn tableName="accounts" columnName="auth_token"/>
|
||||
<dropColumn tableName="accounts" columnName="salt"/>
|
||||
<dropColumn tableName="accounts" columnName="signaling_key"/>
|
||||
<dropColumn tableName="accounts" columnName="gcm_id"/>
|
||||
<dropColumn tableName="accounts" columnName="apn_id"/>
|
||||
<dropColumn tableName="accounts" columnName="supports_sms"/>
|
||||
|
||||
<sql>DROP type account_t;</sql>
|
||||
<sql>DROP type device_t;</sql>
|
||||
|
||||
<addColumn tableName="keys">
|
||||
<column name="device_id" type="bigint" defaultValue="1">
|
||||
<constraints nullable="false" />
|
||||
</column>
|
||||
</addColumn>
|
||||
|
||||
<createTable tableName="pending_devices">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="number" type="text">
|
||||
<constraints unique="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="verification_code" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
|
||||
<createTable tableName="messages">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="account_id" type="bigint">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="device_id" type="bigint">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="encrypted_message" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
|
||||
<createIndex tableName="messages" indexName="messages_account_and_device">
|
||||
<column name="account_id"/>
|
||||
<column name="device_id"/>
|
||||
</createIndex>
|
||||
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="3" author="moxie">
|
||||
<sql>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$;</sql>
|
||||
<sql>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;</sql>
|
||||
<sql>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';</sql>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="4" author="moxie">
|
||||
<dropColumn tableName="keys" columnName="identity_key"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
79
docker/messagedb.xml
Normal file
79
docker/messagedb.xml
Normal file
|
@ -0,0 +1,79 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
|
||||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
|
||||
|
||||
<changeSet id="1" author="moxie">
|
||||
<dropTable tableName="messages"/>
|
||||
<createTable tableName="messages">
|
||||
<column name="id" type="bigint" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="type" type="tinyint">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="relay" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="timestamp" type="bigint">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="source" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="source_device" type="int">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="destination" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="destination_device" type="int">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
|
||||
<column name="message" type="bytea">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
|
||||
<createIndex tableName="messages" indexName="destination_index">
|
||||
<column name="destination"></column>
|
||||
<column name="destination_device"></column>
|
||||
</createIndex>
|
||||
|
||||
<createIndex tableName="messages" indexName="destination_and_type_index">
|
||||
<column name="destination"></column>
|
||||
<column name="destination_device"></column>
|
||||
<column name="type"></column>
|
||||
</createIndex>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="2" author="moxie">
|
||||
<addColumn tableName="messages">
|
||||
<column name="content" type="bytea"/>
|
||||
</addColumn>
|
||||
|
||||
<dropNotNullConstraint tableName="messages" columnName="message"/>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="3" author="moxie">
|
||||
<sql>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);</sql>
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="4" author="moxie">
|
||||
<sql>DROP RULE bounded_message_queue ON messages;</sql>
|
||||
<sql>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);</sql>
|
||||
</changeSet>
|
||||
|
||||
|
||||
</databaseChangeLog>
|
Loading…
Reference in a new issue