From d8f07e8dde5be339b7bb632e430bce74a1b5727f Mon Sep 17 00:00:00 2001 From: sh <37271604+shumvgolove@users.noreply.github.com> Date: Wed, 20 Jul 2022 13:21:10 +0300 Subject: [PATCH] add hosted docker container (#479) * add docker container * Apply suggestions from code review * Update README.md Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- README.md | 32 ++++++++++++++++++++------------ img/docker.svg | 1 + img/linode.svg | 2 +- scripts/docker/Dockerfile | 20 ++++++++++++++++++++ scripts/docker/README.md | 20 ++++++++++++++++++++ scripts/docker/config/README.md | 1 + scripts/docker/entrypoint | 23 +++++++++++++++++++++++ scripts/docker/logs/README.md | 1 + 8 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 img/docker.svg create mode 100644 scripts/docker/Dockerfile create mode 100644 scripts/docker/README.md create mode 100644 scripts/docker/config/README.md create mode 100755 scripts/docker/entrypoint create mode 100644 scripts/docker/logs/README.md diff --git a/README.md b/README.md index bb94bf4..b8f3928 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ If you have a server deployed please deploy a new server to a new host and retir ## Message broker for unidirectional (simplex) queues -SimpleXMQ is a message broker for managing message queues and sending messages over public network. It consists of SMP server, SMP client library and SMP agent that implement [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md) for client-server communication and [SMP agent protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/agent-protocol.md) to manage duplex connections via simplex queues on multiple SMP servers. +SimpleXMQ is a message broker for managing message queues and sending messages over public network. It consists of SMP server, SMP client library and SMP agent that implement [SMP protocol](./protocol/simplex-messaging.md) for client-server communication and [SMP agent protocol](./protocol/agent-protocol.md) to manage duplex connections via simplex queues on multiple SMP servers. SMP protocol is inspired by [Redis serialization protocol](https://redis.io/topics/protocol), but it is much simpler - it currently has only 10 client commands and 8 server responses. @@ -27,7 +27,7 @@ SimpleXMQ is implemented in Haskell - it benefits from robust software transacti ### SMP server -[SMP server](https://github.com/simplex-chat/simplexmq/blob/master/apps/smp-server/Main.hs) can be run on any Linux distribution, including low power/low memory devices. OpenSSL library is required for initialization. +[SMP server](./apps/smp-server/Main.hs) can be run on any Linux distribution, including low power/low memory devices. OpenSSL library is required for initialization. To initialize the server use `smp-server init -n ` (or `smp-server init --ip ` for IP based address) command - it will generate keys and certificates for TLS transport. The fingerprint of offline certificate is used as part of the server address to protect client/server connection against man-in-the-middle attacks: `smp://@[:5223]`. @@ -39,7 +39,7 @@ Starting from version 2.3.0, when store log is enabled, the server would also en > **Please note:** On initialization SMP server creates a chain of two certificates: a self-signed CA certificate ("offline") and a server certificate used for TLS handshake ("online"). **You should store CA certificate private key securely and delete it from the server. If server TLS credential is compromised this key can be used to sign a new one, keeping the same server identity and established connections.** CA private key location by default is `/etc/opt/simplex/ca.key`. -SMP server implements [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md). +SMP server implements [SMP protocol](./protocol/simplex-messaging.md). #### Running SMP server on MacOS @@ -62,7 +62,7 @@ Now `openssl version` should be saying "OpenSSL". You can now run `smp-server in ### SMP client library -[SMP client](https://github.com/simplex-chat/simplexmq/blob/master/src/Simplex/Messaging/Client.hs) is a Haskell library to connect to SMP servers that allows to: +[SMP client](./src/Simplex/Messaging/Client.hs) is a Haskell library to connect to SMP servers that allows to: - execute commands with a functional API. - receive messages and other notifications via STM queue. @@ -70,13 +70,13 @@ Now `openssl version` should be saying "OpenSSL". You can now run `smp-server in ### SMP agent -[SMP agent library](https://github.com/simplex-chat/simplexmq/blob/master/src/Simplex/Messaging/Agent.hs) can be used to run SMP agent as part of another application and to communicate with the agent via STM queues, without serializing and parsing commands and responses. +[SMP agent library](./src/Simplex/Messaging/Agent.hs) can be used to run SMP agent as part of another application and to communicate with the agent via STM queues, without serializing and parsing commands and responses. -Haskell type [ACommand](https://github.com/simplex-chat/simplexmq/blob/master/src/Simplex/Messaging/Agent/Protocol.hs) represents SMP agent protocol to communicate via STM queues. +Haskell type [ACommand](./src/Simplex/Messaging/Agent/Protocol.hs) represents SMP agent protocol to communicate via STM queues. See [simplex-chat](https://github.com/simplex-chat/simplex-chat) terminal UI for the example of integrating SMP agent into another application. -[SMP agent executable](https://github.com/simplex-chat/simplexmq/blob/master/apps/smp-agent/Main.hs) can be used to run a standalone SMP agent process that implements plaintext [SMP agent protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/agent-protocol.md) via TCP port 5224, so it can be used via telnet. It can be deployed in private networks to share access to the connections between multiple applications and services. +[SMP agent executable](./apps/smp-agent/Main.hs) can be used to run a standalone SMP agent process that implements plaintext [SMP agent protocol](./protocol/agent-protocol.md) via TCP port 5224, so it can be used via telnet. It can be deployed in private networks to share access to the connections between multiple applications and services. ## Using SMP server and SMP agent @@ -110,7 +110,15 @@ You can run your SMP server as a Linux process, optionally using a service manag See [this section](#smp-server) for more information. Run `smp-server -h` and `smp-server init -h` for explanation of commands and options. -[Linode](https://cloud.linode.com/stackscripts/748014) +Docker + +## Deploy SMP server with Docker + +SMP server could also be deployed using `Docker`. + +See: [`scripts/docker`](./scripts/docker/) + +[Linode](https://cloud.linode.com/stackscripts/748014) ## Deploy SMP server on Linode @@ -134,7 +142,7 @@ Deployment on Linode is performed via StackScripts, which serve as recipes for L Please submit an [issue](https://github.com/simplex-chat/simplexmq/issues) if any problems occur. -[DigitalOcean](https://marketplace.digitalocean.com/apps/simplex-server) +[DigitalOcean](https://marketplace.digitalocean.com/apps/simplex-server) ## Deploy SMP server on DigitalOcean @@ -162,12 +170,12 @@ smp-server init [-l] -n ## SMP server design -![SMP server design](https://raw.githubusercontent.com/simplex-chat/simplexmq/master/design/server.svg) +![SMP server design](./design/server.svg) ## SMP agent design -![SMP agent design](https://raw.githubusercontent.com/simplex-chat/simplexmq/master/design/agent2.svg) +![SMP agent design](./design/agent2.svg) ## License -[AGPL v3](https://github.com/simplex-chat/simplexmq/blob/master/LICENSE) +[AGPL v3](./LICENSE) diff --git a/img/docker.svg b/img/docker.svg new file mode 100644 index 0000000..c3690c1 --- /dev/null +++ b/img/docker.svg @@ -0,0 +1 @@ + diff --git a/img/linode.svg b/img/linode.svg index 14ebd13..b99cd41 100644 --- a/img/linode.svg +++ b/img/linode.svg @@ -1 +1 @@ - \ No newline at end of file + diff --git a/scripts/docker/Dockerfile b/scripts/docker/Dockerfile new file mode 100644 index 0000000..21eeb00 --- /dev/null +++ b/scripts/docker/Dockerfile @@ -0,0 +1,20 @@ +FROM ubuntu:focal + +# Install curl +RUN apt-get update && apt-get install -y curl + +# Download latest smp-server release and assign executable permission +RUN curl -L https://github.com/simplex-chat/simplexmq/releases/latest/download/smp-server-ubuntu-20_04-x86-64 -o /usr/bin/smp-server && \ + chmod +x /usr/bin/smp-server + +# Copy our helper script +COPY ./entrypoint /usr/bin/entrypoint + +# Open smp-server listening port +EXPOSE 5223 + +# SimpleX requires using SIGINT to correctly preserve undelivered messages and restore them on restart +STOPSIGNAL SIGINT + +# Finally, execute helper script +ENTRYPOINT [ "/usr/bin/entrypoint" ] diff --git a/scripts/docker/README.md b/scripts/docker/README.md new file mode 100644 index 0000000..cc2e4b5 --- /dev/null +++ b/scripts/docker/README.md @@ -0,0 +1,20 @@ +# smp-server docker container +0. Install `docker` to your host. + +1. Build your `smp-server` image: +```sh +DOCKER_BUILDKIT=1 docker build -t smp-server . +``` + +2. Run new docker container: +```sh +docker run -d \ + --name smp-server \ + -e addr="your_ip_or_domain" \ + -p 5223:5223 \ + -v ${PWD}/config:/etc/opt/simplex \ + -v ${PWD}/logs:/var/opt/simplex \ + smp-server +``` + +Configuration files and logs will be written to [`config`](./config) and [`logs`](./logs) folders respectively. diff --git a/scripts/docker/config/README.md b/scripts/docker/config/README.md new file mode 100644 index 0000000..50d25bd --- /dev/null +++ b/scripts/docker/config/README.md @@ -0,0 +1 @@ +smp-server configuration, certificate and fingerprint will be stored here diff --git a/scripts/docker/entrypoint b/scripts/docker/entrypoint new file mode 100755 index 0000000..b1d4fd4 --- /dev/null +++ b/scripts/docker/entrypoint @@ -0,0 +1,23 @@ +#!/usr/bin/env sh +confd="/etc/opt/simplex" +logd="/var/opt/simplex/" + +# Check if server has been initialized +if [ ! -f "$confd/smp-server.ini" ]; then + # If not, determine ip or domain + case $addr in + '') printf "Please specify \$addr environment variable.\n"; exit 1 ;; + *[a-zA-Z]*) smp-server init -l -n "$addr" ;; + *) smp-server init -l --ip "$addr" ;; + esac + +fi + +# backup store log +[ -f "$logd/smp-server-store.log" ] && cp "$logd"/smp-server-store.log "$logd"/smp-server-store.log.bak +# rotate server log +[ -f "$logd/smp-server.log" ] && mv "$logd"/smp-server.log "$logd"/smp-server-"$(date +'%FT%T')".log + +# Finally, run smp-sever. Notice that "exec" here is important: +# smp-server replaces our helper script, so that it can catch INT signal +exec smp-server start > "$logd"/smp-server.log 2>&1 diff --git a/scripts/docker/logs/README.md b/scripts/docker/logs/README.md new file mode 100644 index 0000000..f25e7ce --- /dev/null +++ b/scripts/docker/logs/README.md @@ -0,0 +1 @@ +smp-server general logs, stored messages and statistics (if enabled) will be stored here