From f5400e7e31541aff401f215317a72809aca9e3bb Mon Sep 17 00:00:00 2001 From: nic <nic@ubuntu22.myguest.virtualbox.org> Date: Mon, 25 Mar 2024 20:06:24 +0300 Subject: [PATCH] feat: docker run --- .env | 14 ++++++++++ Dockerfile | 42 +++++++++++++++++++++++++++++ docker-compose.yml | 63 +++++++++++++++++++++++++++++++++++++++++--- docker-entrypoint.sh | 8 ++++++ init-data.sh | 13 +++++++++ postgres/tables.sql | 35 ++++++++++++++++++++++++ 6 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-entrypoint.sh create mode 100644 init-data.sh create mode 100644 postgres/tables.sql diff --git a/.env b/.env new file mode 100644 index 0000000..3c87ab5 --- /dev/null +++ b/.env @@ -0,0 +1,14 @@ +# N8N data +POSTGRES_USER=changeUser +POSTGRES_PASSWORD=changePassword +POSTGRES_DB=n8n + +POSTGRES_NON_ROOT_USER=changeUser +POSTGRES_NON_ROOT_PASSWORD=changePassword + +N8N_CUSTOM_EXTENSIONS=/home/node/.n8n/custom + +# ChatGate data +POSTGRES_USER_CHAT=test +POSTGRES_PASSWORD_CHAT=test +POSTGRES_DB_CHAT=chatgate diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1a50242 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +ARG NODE_VERSION=18 +FROM n8nio/base:${NODE_VERSION} + +ARG N8N_VERSION +RUN if [ -z "$N8N_VERSION" ] ; then echo "The N8N_VERSION argument is missing!" ; exit 1; fi + +ENV N8N_VERSION=${N8N_VERSION} +ENV N8N_RELEASE_TYPE=stable +RUN set -eux; \ + npm install typescript -g && \ + npm install -g --omit=dev n8n@${N8N_VERSION} --ignore-scripts && \ + npm rebuild --prefix=/usr/local/lib/node_modules/n8n sqlite3 && \ + npm install gulp && \ + npm install -g gulp-cli && \ + npm install n8n-workflow && \ + rm -rf /usr/local/lib/node_modules/n8n/node_modules/@n8n/chat && \ + rm -rf /usr/local/lib/node_modules/n8n/node_modules/n8n-design-system && \ + rm -rf /usr/local/lib/node_modules/n8n/node_modules/n8n-editor-ui/node_modules && \ + find /usr/local/lib/node_modules/n8n -type f -name "*.ts" -o -name "*.js.map" -o -name "*.vue" | xargs rm -f && \ + rm -rf /root/.npm + +COPY docker-entrypoint.sh / +RUN chmod +x /docker-entrypoint.sh + +RUN \ + mkdir .n8n && \ + chown node:node .n8n + +RUN cd ./.n8n && \ + mkdir custom && \ + cd custom && \ + git clone https://github.com/pvpender/n8n-nodes-zulip-real-time && \ + npm --prefix ./n8n-nodes-zulip-real-time i && \ + npm --prefix ./n8n-nodes-zulip-real-time run build && \ + git clone https://github.com/bergi9/n8n-nodes-telegram-polling && \ + npm --prefix ./n8n-nodes-telegram-polling i && \ + npm --prefix ./n8n-nodes-telegram-polling run build + + +ENV SHELL /bin/sh +USER root +ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index fbdfa5b..e9ad576 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,65 @@ version: "3.9" services: postgres: - image: postgres:13.3 + image: postgres:16 + #restart: always environment: - POSTGRES_DB: "test" - POSTGRES_USER: "user" - POSTGRES_PASSWORD: "password" + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_NON_ROOT_USER=${POSTGRES_NON_ROOT_USER} + - POSTGRES_NON_ROOT_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD} + expose: + - "5434" + ports: + - "5434:5432" + volumes: + - postgres_data:/var/lib/postgresql/data:rw + - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh + healthcheck: + test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}'] + interval: 5s + timeout: 5s + retries: 10 + + postgresChatGate: + image: postgres:16 + #restart: always + environment: + - POSTGRES_USER=${POSTGRES_USER_CHAT} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD_CHAT} + - POSTGRES_DB=${POSTGRES_DB_CHAT} ports: - "5432:5432" + volumes: + - chat_gate_data:/var/lib/postgresql/data:rw + - ./postgres:/docker-entrypoint-initdb.d + + n8n: + build: + context: . + args: + - N8N_VERSION=1.34.1 + #restart: always + environment: + - DB_TYPE=postgresdb + - DB_POSTGRESDB_HOST=postgres + - DB_POSTGRESDB_PORT=5432 + - DB_POSTGRESDB_DATABASE=${POSTGRES_DB} + - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER} + - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD} + - N8N_CUSTOM_EXTENSIONS=${N8N_CUSTOM_EXTENSIONS} + ports: + - 5678:5678 + links: + - postgres + volumes: + - n8n_data:/home/node/.n8n + depends_on: + postgres: + condition: service_healthy + +volumes: + postgres_data: + n8n_data: + chat_gate_data: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..6d7148f --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/sh +if [ "$#" -gt 0 ]; then + # Got started with arguments + exec n8n "$@" +else + # Got started without arguments + exec n8n +fi \ No newline at end of file diff --git a/init-data.sh b/init-data.sh new file mode 100644 index 0000000..7f2e382 --- /dev/null +++ b/init-data.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -e; + + +if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}'; + GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER}; + GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER}; + EOSQL +else + echo "SETUP INFO: No Environment variables given!" +fi \ No newline at end of file diff --git a/postgres/tables.sql b/postgres/tables.sql new file mode 100644 index 0000000..f7ff85e --- /dev/null +++ b/postgres/tables.sql @@ -0,0 +1,35 @@ +CREATE TABLE users ( + tg_id BIGINT PRIMARY KEY , + zulip_id int, + email VARCHAR(64), + name VARCHAR(120) +); + +CREATE TABLE states ( + id BIGINT PRIMARY KEY , + state int4 NOT NULL +); + +CREATE TABLE IF NOT EXISTS last_message( + id int PRIMARY KEY, + last_id int +); + +CREATE TABLE states_zulip( + id int PRIMARY KEY , + state int4 NOT NULL +); + +CREATE TABLE forwarding_zulip( + id int , + channel_name VARCHAR(120), + channel_id int , + topic_name VARCHAR(120) +); + +CREATE TABLE forwarding_channels( + zulip_channel_id int, + zulip_topic_name VARCHAR(120), + telegram_chat_id bigint, + mode VARCHAR(8) +); -- GitLab