Skip to content

Commit

Permalink
Adapt Dockerfile for BTCPay deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier authored and Kukks committed Jun 12, 2024
1 parent 11586ab commit c910f0f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 16 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Docker packaging
on:
push:
tags:
- 'basedon-*'

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Environment variables
run: env
-
name: Create images
env:
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
DOCKERHUB_PASS: ${{ secrets.DOCKERHUB_PASS }}
DOCKERHUB_REPO: ${{ vars.DOCKERHUB_REPO }}
shell: bash
run: |
LATEST_TAG=${GITHUB_REF#refs/tags/}
LATEST_TAG=${LATEST_TAG:8} #trim "basedon-" from tag
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USER" --password-stdin
docker buildx create --use
DOCKER_BUILDX_OPTS="--platform linux/amd64,linux/arm64,linux/arm/v7 --push"
docker buildx build $DOCKER_BUILDX_OPTS -t $DOCKERHUB_REPO:$LATEST_TAG .
23 changes: 21 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ RUN set -ex \

FROM base-downloader as base-downloader-linux-amd64
ENV TARBALL_ARCH_FINAL=x86_64-linux-gnu
ENV DESCHASHPLUGIN_ARCH=linux-amd64
ENV DESCHASHPLUGIN_HASH=deadc00c68fac80b2718d92f69bf06acd8fff646228d497bbb76a4f0a12ca217

FROM base-downloader as base-downloader-linux-arm64
ENV TARBALL_ARCH_FINAL=aarch64-linux-gnu
ENV DESCHASHPLUGIN_ARCH=linux-arm64
ENV DESCHASHPLUGIN_HASH=d48c3e5aede77bd9cb72d78689ce12c0327f624435cb0496b3eacb92df416363

FROM base-downloader as base-downloader-linux-arm
ENV TARBALL_ARCH_FINAL=arm-linux-gnueabihf
ENV DESCHASHPLUGIN_ARCH=linux-arm
ENV DESCHASHPLUGIN_HASH=f7df336c72dd1674bd18ff23862a410b6a9691a3e13752264dcffa0950e21c74

FROM base-downloader-${TARGETOS}-${TARGETARCH} as downloader

Expand Down Expand Up @@ -54,6 +60,14 @@ RUN mkdir /opt/litecoin && cd /opt/litecoin \
&& tar -xzvf litecoin.tar.gz litecoin-$LITECOIN_VERSION/bin/litecoin-cli --strip-components=1 --exclude=*-qt \
&& rm litecoin.tar.gz

ENV DESCHASHPLUGIN_URL https://github.com/nbd-wtf/invoicewithdescriptionhash/releases/download/v1.4/invoicewithdescriptionhash-v1.4-${DESCHASHPLUGIN_ARCH}.tar.gz
ENV DESCHASHPLUGIN_SHA256 ${DESCHASHPLUGIN_HASH}
RUN mkdir /opt/deschashplugin && cd /opt/deschashplugin \
&& wget -qO invoicewithdescriptionhash.tar.gz "$DESCHASHPLUGIN_URL" \
&& echo "$DESCHASHPLUGIN_SHA256 invoicewithdescriptionhash.tar.gz" | sha256sum -c - \
&& tar -xzvf invoicewithdescriptionhash.tar.gz && rm invoicewithdescriptionhash.tar.gz \
&& chmod a+x invoicewithdescriptionhash

FROM --platform=linux/amd64 ${BASE_DISTRO} as base-builder
RUN apt-get update -qq && \
apt-get install -qq -y --no-install-recommends \
Expand Down Expand Up @@ -223,8 +237,9 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN rustup toolchain install stable --component rustfmt --allow-downgrade

WORKDIR /opt/lightningd
COPY plugins/clnrest/requirements.txt plugins/clnrest/requirements.txt
COPY plugins/wss-proxy/requirements.txt plugins/wss-proxy/requirements.txt
COPY . .
ENV PYTHON_VERSION=3
RUN pip3 install -r plugins/clnrest/requirements.txt && pip3 cache purge
ENV PYTHON_VERSION=3
RUN pip3 install -r plugins/clnrest/requirements.txt && \
pip3 install -r plugins/wss-proxy/requirements.txt && \
Expand All @@ -250,13 +265,17 @@ ENV LIGHTNINGD_PORT=9735
ENV LIGHTNINGD_NETWORK=bitcoin

RUN mkdir $LIGHTNINGD_DATA && \
mkdir /etc/bundledplugins && \
mkdir $LIGHTNINGD_DATA/plugins && \
touch $LIGHTNINGD_DATA/config
VOLUME [ "/root/.lightning" ]

COPY --from=builder /tmp/lightning_install/ /usr/local/
COPY --from=builder-python /usr/local/lib/python3.9/dist-packages/ /usr/local/lib/python3.9/dist-packages/
COPY --from=downloader /opt/bitcoin/bin /usr/bin
COPY --from=downloader /opt/litecoin/bin /usr/bin
COPY --from=downloader /opt/deschashplugin $LIGHTNINGD_DATA/plugins
COPY --from=downloader /opt/deschashplugin /etc/bundledplugins
COPY tools/docker-entrypoint.sh entrypoint.sh

EXPOSE 9735 9835
Expand Down
2 changes: 1 addition & 1 deletion contrib/msggen/msggen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


logging.basicConfig(
level=logging.DEBUG,
level=logging.ERROR,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.StreamHandler()
Expand Down
22 changes: 13 additions & 9 deletions plugins/bcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ static void wait_and_check_bitcoind(struct plugin *p)
pid_t child;
const char **cmd = gather_args(bitcoind, "getnetworkinfo", NULL);
bool printed = false;
bool isWarmup = false;
char *output = NULL;

for (;;) {
Expand Down Expand Up @@ -1135,17 +1136,20 @@ static void wait_and_check_bitcoind(struct plugin *p)
/* bitcoin/src/rpc/protocol.h:
* RPC_IN_WARMUP = -28, //!< Client still warming up
*/
if (WEXITSTATUS(status) != 28) {
if (WEXITSTATUS(status) == 1)
bitcoind_failure(p, "Could not connect to bitcoind using"
" bitcoin-cli. Is bitcoind running?");
bitcoind_failure(p, tal_fmt(bitcoind, "%s exited with code %i: %s",
cmd[0], WEXITSTATUS(status), output));
}
isWarmup = WEXITSTATUS(status) == 28;

if (!printed) {
plugin_log(p, LOG_UNUSUAL,
"Waiting for bitcoind to warm up...");
if (isWarmup)
{
plugin_log(p, LOG_UNUSUAL,
"Waiting for bitcoind to warm up...");
}
else
{
plugin_log(p, LOG_UNUSUAL,
tal_fmt(bitcoind, "%s exited with code %i: %s... retrying",
cmd[0], WEXITSTATUS(status), output));
}
printed = true;
}
sleep(1);
Expand Down
98 changes: 94 additions & 4 deletions tools/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,77 @@

: "${EXPOSE_TCP:=false}"

networkdatadir="${LIGHTNINGD_DATA}/${LIGHTNINGD_NETWORK}"
cat <<-EOF > "$LIGHTNINGD_DATA/config"
${LIGHTNINGD_OPT}
bind-addr=0.0.0.0:${LIGHTNINGD_PORT}
EOF

LIGHTNINGD_NETWORK_NAME=""

if [ "$LIGHTNINGD_CHAIN" == "btc" ] && [ "$LIGHTNINGD_NETWORK" == "mainnet" ]; then
LIGHTNINGD_NETWORK_NAME="bitcoin"
elif [ "$LIGHTNINGD_CHAIN" == "btc" ] && [ "$LIGHTNINGD_NETWORK" == "testnet" ]; then
LIGHTNINGD_NETWORK_NAME="testnet"
elif [ "$LIGHTNINGD_CHAIN" == "btc" ] && [ "$LIGHTNINGD_NETWORK" == "regtest" ]; then
LIGHTNINGD_NETWORK_NAME="regtest"
elif [ "$LIGHTNINGD_CHAIN" == "ltc" ] && [ "$LIGHTNINGD_NETWORK" == "mainnet" ]; then
LIGHTNINGD_NETWORK_NAME="litecoin"
elif [ "$LIGHTNINGD_CHAIN" == "ltc" ] && [ "$LIGHTNINGD_NETWORK" == "testnet" ]; then
LIGHTNINGD_NETWORK_NAME="litecoin-testnet"
else
echo "Invalid combinaion of LIGHTNINGD_NETWORK and LIGHTNINGD_CHAIN. LIGHTNINGD_CHAIN should be btc or ltc. LIGHTNINGD_NETWORK should be mainnet, testnet or regtest."
echo "ltc regtest is not supported"
exit
fi

echo "network=$LIGHTNINGD_NETWORK_NAME" >> "$LIGHTNINGD_DATA/config"
echo "network=$LIGHTNINGD_NETWORK_NAME added in $LIGHTNINGD_DATA/config"

if [[ "${LIGHTNINGD_ANNOUNCEADDR}" ]]; then
echo "announce-addr=$LIGHTNINGD_ANNOUNCEADDR:${LIGHTNINGD_PORT}" >> "$LIGHTNINGD_DATA/config"
fi

if [[ "${LIGHTNINGD_ALIAS}" ]]; then
# This allow to strip this parameter if LND_ALIGHTNINGD_ALIASLIAS is empty or null, and truncate it
LIGHTNINGD_ALIAS="$(echo "$LIGHTNINGD_ALIAS" | cut -c -32)"
echo "alias=$LIGHTNINGD_ALIAS" >> "$LIGHTNINGD_DATA/config"
echo "alias=$LIGHTNINGD_ALIAS added to $LIGHTNINGD_DATA/config"
fi

if [[ "${LIGHTNINGD_READY_FILE}" ]]; then
echo "Waiting $LIGHTNINGD_READY_FILE to be created..."
while [ ! -f "$LIGHTNINGD_READY_FILE" ]; do sleep 1; done
echo "The chain is fully synched"
fi

if [[ "${LIGHTNINGD_HIDDENSERVICE_HOSTNAME_FILE}" ]]; then
echo "Waiting $LIGHTNINGD_HIDDENSERVICE_HOSTNAME_FILE to be created by tor..."
while [ ! -f "$LIGHTNINGD_HIDDENSERVICE_HOSTNAME_FILE" ]; do sleep 1; done
HIDDENSERVICE_ONION="$(head -n 1 "$LIGHTNINGD_HIDDENSERVICE_HOSTNAME_FILE"):${LIGHTNINGD_PORT}"
echo "announce-addr=$HIDDENSERVICE_ONION" >> "$LIGHTNINGD_DATA/config"
echo "announce-addr=$HIDDENSERVICE_ONION added to $LIGHTNINGD_DATA/config"
fi

if ! grep -q "^rpc-file=" "$LIGHTNINGD_DATA/config"; then
echo "rpc-file=$LIGHTNINGD_DATA/lightning-rpc" >> "$LIGHTNINGD_DATA/config"
echo "rpc-file=$LIGHTNINGD_DATA/lightning-rpc added to $LIGHTNINGD_DATA/config"
fi

echo "Installing bundled plugins"
mkdir -p "$LIGHTNINGD_DATA/plugins"
cp -u /etc/bundledplugins/* $LIGHTNINGD_DATA/plugins/

set -m
lightningd --network="${LIGHTNINGD_NETWORK}" "$@" &
lightningd "$@" &

echo "Core-Lightning starting"
while read -r i; do if [ "$i" = "lightning-rpc" ]; then break; fi; done \
< <(inotifywait -e create,open --format '%f' --quiet "${networkdatadir}" --monitor)
< <(inotifywait -e create,open --format '%f' --quiet "$LIGHTNINGD_DATA" --monitor)

if [ "$EXPOSE_TCP" == "true" ]; then
echo "Core-Lightning started, RPC available on port $LIGHTNINGD_RPC_PORT"

socat "TCP4-listen:$LIGHTNINGD_RPC_PORT,fork,reuseaddr" "UNIX-CONNECT:${networkdatadir}/lightning-rpc" &
socat "TCP4-listen:$LIGHTNINGD_RPC_PORT,fork,reuseaddr" "UNIX-CONNECT:$LIGHTNINGD_DATA/lightning-rpc" &
fi

# Now run any scripts which exist in the lightning-poststart.d directory
Expand All @@ -24,4 +82,36 @@ if [ -d "$LIGHTNINGD_DATA"/lightning-poststart.d ]; then
done
fi

RUNE_PATH="$LIGHTNINGD_DATA/rune.env"
if [ -f "$RUNE_PATH" ]; then
source "$RUNE_PATH"
matches=$(lightning-cli showrunes | jq --arg rune "$LIGHTNING_RUNE" --arg unique_id "$UNIQUE_ID" \
'.runes[] | select(.rune == $rune and .unique_id == $unique_id)')
if [[ -n $matches ]]; then
echo "Rune already created"
else
LIGHTNING_RUNE=""
UNIQUE_ID=""
echo "Rune not found, re-creating..."
fi
fi

if ! [[ "$LIGHTNING_RUNE" ]]; then
echo "Creating rune..."
RUNE_RESPONSE=$(lightning-cli createrune 'restrictions=[["For Applications#"]]')
LIGHTNING_RUNE=$(echo "$RUNE_RESPONSE" | jq -r '.rune')
UNIQUE_ID=$(echo "$RUNE_RESPONSE" | jq -r '.unique_id')
if [[ "$LIGHTNING_RUNE" ]]; then
echo "LIGHTNING_RUNE=\"${LIGHTNING_RUNE}\"" > "$RUNE_PATH"
echo "UNIQUE_ID=${UNIQUE_ID}" >> "$RUNE_PATH"
echo "Rune created"
source "$RUNE_PATH"
fi
fi

if ! [[ "$LIGHTNING_RUNE" ]]; then
echo "Error while creating a rune..."
echo "$RUNE_RESPONSE"
fi

fg %-

0 comments on commit c910f0f

Please sign in to comment.