diff --git a/docker/Dockerfile b/docker/Dockerfile index def57f5..3f7b934 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # A container for PyBitmessage daemon -FROM ubuntu:bionic-20220401 +FROM ubuntu:bionic SHELL ["/bin/bash", "-exo", "pipefail", "-c"] @@ -11,10 +11,8 @@ RUN apt-get install -yq --no-install-suggests --no-install-recommends \ python-all-dev python-msgpack python-pip python-setuptools \ git -WORKDIR /root -RUN git clone https://github.com/Bitmessage/PyBitmessage - WORKDIR /root/PyBitmessage +RUN git clone https://github.com/Bitmessage/PyBitmessage . # Install RUN pip2 install jsonrpclib . @@ -23,20 +21,15 @@ FROM ubuntu:bionic-20220401 EXPOSE 8442 -ENV BITMESSAGE_API_USER=bitmessage_api_user -ENV BITMESSAGE_API_PASSWORD=bitmessage_api_password -ENV BITMESSAGE_SEED_PHRASE=bitmessage_seed_phrase -ENV BITMESSAGE_SEED_ADDRESSES=1 -ENV BITMESSAGE_API_PORT=8442 -ENV BITMESSAGE_TTL=172800 -ENV BITMESSAGE_STOPRESENDINGAFTERXDAYS=30 - ENV USER_UID=2000 ENV USER_GID=2000 ENV HOME=/home/bitmessage ENV BITMESSAGE_HOME=${HOME} COPY --from=0 /usr/local/ /usr/local/ +COPY --from=0 /root/PyBitmessage/docker/healthy_check.py /usr/local/bin/ +COPY --from=0 /root/PyBitmessage/docker/seed_addr_gen.py /usr/local/bin/ +COPY --from=0 /root/PyBitmessage/docker/run.sh /usr/local/bin/ # Install dependencies RUN apt-get update \ @@ -45,29 +38,16 @@ RUN apt-get update \ # Create a user RUN addgroup --gid $USER_GID bitmessage ;\ - useradd --uid $USER_UID --gid $USER_GID -m -d $HOME bitmessage + useradd --uid $USER_UID --gid $USER_GID --skel /dev/null --create-home --home-dir $HOME bitmessage WORKDIR ${HOME} # Generate default config RUN su bitmessage -c "pybitmessage -t" -# Setup environment -CMD chown bitmessage:bitmessage keys.dat; chmod 600 keys.dat; \ - cat keys.dat | \ - sed -e "s|\(apiinterface = \).*|\10\.0\.0\.0|g" \ - #-e "s|\(apivariant = \).*|\1json|g" \ - -e "s|\(apiusername = \).*|\1$BITMESSAGE_API_USER|g" \ - -e "s|\(apipassword = \).*|\1$BITMESSAGE_API_PASSWORD|g" \ - -e "s|\(apiport = \).*|\1$BITMESSAGE_API_PORT|g" \ - -e "s|\(apienabled = \).*|\1True|g" \ - -e "s|\(ttl = \).*|\1$BITMESSAGE_TTL|g" \ - -e "s|\(stopresendingafterxdays = \).*|\1$BITMESSAGE_STOPRESENDINGAFTERXDAYS|g" \ - -e "s|\(udp = \).*|\1False|g" | \ - gosu bitmessage sponge keys.dat && \ - exec gosu bitmessage nice -n 19 pybitmessage -d +CMD ["sh", "/usr/local/bin/run.sh"] -# Generate Deterministic address -HEALTHCHECK --retries=3 --interval=15s \ - CMD python -c "import xmlrpclib; api_link='http://$BITMESSAGE_API_USER:$BITMESSAGE_API_PASSWORD@127.0.0.1:8442/'; api = xmlrpclib.ServerProxy(api_link); print api.createDeterministicAddresses('$BITMESSAGE_SEED_PHRASE'.encode('base64'),$BITMESSAGE_SEED_ADDRESSES)" || exit 1 +## Check PyBitmessage active network connections +HEALTHCHECK --retries=0 --interval=15s \ + CMD ["python", "/usr/local/bin/healthy_check.py"] diff --git a/docker/healthy_check.py b/docker/healthy_check.py new file mode 100644 index 0000000..337b0fb --- /dev/null +++ b/docker/healthy_check.py @@ -0,0 +1,23 @@ +#!/usr/bin/python + +import sys +import os +import xmlrpclib +import json + +api_user=os.getenv('BITMESSAGE_API_USER', 'bitmessage_api_user') ; +api_password=os.getenv('BITMESSAGE_API_PASSWORD', 'bitmessage_api_password') ; +api_port=os.getenv('BITMESSAGE_API_PORT', '8442') ; + +api_link="http://{}:{}@127.0.0.1:{}/".format(api_user, api_password, api_port) + +api = xmlrpclib.ServerProxy(api_link) + +network_connections=json.loads(api.clientStatus())['networkConnections'] +print "networkConnections:", network_connections + +if network_connections > 0: + sys.exit(0) +else: + sys.exit(1) + diff --git a/docker/run.sh b/docker/run.sh new file mode 100644 index 0000000..2d2efe9 --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +export BITMESSAGE_API_USER=${BITMESSAGE_API_USER:-bitmessage_api_user} +export BITMESSAGE_API_PASSWORD=${BITMESSAGE_API_PASSWORD:-bitmessage_api_password} +export BITMESSAGE_SEED_ADDRESSES=${BITMESSAGE_SEED_ADDRESSES:-1} +export BITMESSAGE_API_PORT=${BITMESSAGE_API_PORT:-8442} +export BITMESSAGE_TTL=${BITMESSAGE_TTL:-172800} +export BITMESSAGE_STOPRESENDINGAFTERXDAYS=${BITMESSAGE_STOPRESENDINGAFTERXDAYS:-30} + +SEED_FILE="address_seed.txt" +test -e "$SEED_FILE" || gosu bitmessage touch "$SEED_FILE" + +# Save seed to file, or use saved seed +if [ -n "$BITMESSAGE_SEED_PHRASE" ] +then + export BITMESSAGE_SEED_PHRASE + grep -q "$BITMESSAGE_SEED_PHRASE" "$SEED_FILE" \ + || echo "$BITMESSAGE_SEED_PHRASE" >> "$SEED_FILE" +else + OLD_SEED="$(tail -n1 $SEED_FILE)" + NEW_SEED="$(cat /dev/random | tr -dc "a-z" | head -c32)" + export BITMESSAGE_SEED_PHRASE="${OLD_SEED:-$NEW_SEED}" +fi + +# this command must be run as root (for bind mounts to container) +chown bitmessage:bitmessage keys.dat +chmod 600 keys.dat + +# set config values +gosu bitmessage sed -i -e "s|\(apiinterface = \).*|\10\.0\.0\.0|g" \ + -e "s|\(apivariant = \).*|\1legacy|g" \ + -e "s|\(apiusername = \).*|\1$BITMESSAGE_API_USER|g" \ + -e "s|\(apipassword = \).*|\1$BITMESSAGE_API_PASSWORD|g" \ + -e "s|\(apiport = \).*|\1$BITMESSAGE_API_PORT|g" \ + -e "s|\(apienabled = \).*|\1True|g" \ + -e "s|\(ttl = \).*|\1$BITMESSAGE_TTL|g" \ + -e "s|\(stopresendingafterxdays = \).*|\1$BITMESSAGE_STOPRESENDINGAFTERXDAYS|g" \ + -e "s|\(udp = \).*|\1False|g" keys.dat + +# generate address from seed +for i in {1..4} +do + sleep 15 + gosu bitmessage /usr/bin/python /usr/local/bin/seed_addr_gen.py +done & + +exec gosu bitmessage pybitmessage -d + diff --git a/docker/seed_addr_gen.py b/docker/seed_addr_gen.py new file mode 100644 index 0000000..7baec5d --- /dev/null +++ b/docker/seed_addr_gen.py @@ -0,0 +1,17 @@ +#!/usr/bin/python + +import os +import xmlrpclib + +api_user=os.getenv('BITMESSAGE_API_USER', 'bitmessage_api_user') ; +api_password=os.getenv('BITMESSAGE_API_PASSWORD', 'bitmessage_api_password') ; +api_port=os.getenv('BITMESSAGE_API_PORT', '8442') ; +addr_num=os.getenv('BITMESSAGE_SEED_ADDRESSES', '1') ; +addr_seed=os.getenv('BITMESSAGE_SEED_PHRASE') ; + +api_link="http://{}:{}@127.0.0.1:{}/".format(api_user, api_password, api_port) + +api = xmlrpclib.ServerProxy(api_link) + +print api.createDeterministicAddresses(addr_seed.encode('base64'),int(addr_num)) +