From cb7b2b28bf6ac63ee0ce5fb08c72edd6267025fc Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Fri, 10 Dec 2021 11:06:55 -0500 Subject: [PATCH] [lbry] contrib: add linode deployment using docker --- contrib/linode/Dockerfile.deploy | 38 ++++++++++++++ contrib/linode/docker-compose.yml | 19 +++++++ contrib/linode/lbcd_stack_script.sh | 79 +++++++++++++++++++++++++++++ contrib/linode/run.sh | 2 + 4 files changed, 138 insertions(+) create mode 100644 contrib/linode/Dockerfile.deploy create mode 100644 contrib/linode/docker-compose.yml create mode 100644 contrib/linode/lbcd_stack_script.sh create mode 100755 contrib/linode/run.sh diff --git a/contrib/linode/Dockerfile.deploy b/contrib/linode/Dockerfile.deploy new file mode 100644 index 00000000..b9440234 --- /dev/null +++ b/contrib/linode/Dockerfile.deploy @@ -0,0 +1,38 @@ +# This Dockerfile builds btcd from source and creates a small (55 MB) docker container based on alpine linux. +# +# Clone this repository and run the following command to build and tag a fresh btcd amd64 container: +# +# docker build . -t yourregistry/btcd +# +# You can use the following command to buid an arm64v8 container: +# +# docker build . -t yourregistry/btcd --build-arg ARCH=arm64v8 +# +# For more information how to use this docker image visit: +# https://github.com/lbryio/lbcd/tree/master/docs +# +# 9246 Mainnet Bitcoin peer-to-peer port +# 9245 Mainet RPC port + +FROM golang AS build-container + +# ENV GO111MODULE=on + +ADD . /app +WORKDIR /app +RUN set -ex \ + && if [ "${ARCH}" = "amd64" ]; then export GOARCH=amd64; fi \ + && if [ "${ARCH}" = "arm64v8" ]; then export GOARCH=arm64; fi \ + && echo "Compiling for $GOARCH" \ + && CGO_ENABLED=0 go build . + +FROM debian:11-slim + +COPY --from=build-container /app/lbcd / +COPY --from=build-container /app/contrib/linode/run.sh / + +VOLUME ["/root/.lbcd"] + +EXPOSE 9245 9246 + +ENTRYPOINT ["/run.sh"] diff --git a/contrib/linode/docker-compose.yml b/contrib/linode/docker-compose.yml new file mode 100644 index 00000000..23b396d3 --- /dev/null +++ b/contrib/linode/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3" + +volumes: + lbcd: + external: true + +services: + lbcd: + image: lbry/lbcd:linode_deployment + container_name: lbcd + ulimits: + memlock: + soft: -1 + hard: -1 + volumes: + - lbcd:/root/.lbcd:rw + ports: + - "REPLACE_ME:9245:9245" + - "9246:9246" diff --git a/contrib/linode/lbcd_stack_script.sh b/contrib/linode/lbcd_stack_script.sh new file mode 100644 index 00000000..8348d144 --- /dev/null +++ b/contrib/linode/lbcd_stack_script.sh @@ -0,0 +1,79 @@ +#!/bin/bash +# +# +# +# +# +# +# +# + +source + +# For debugging +exec > >(tee -i /var/log/stackscript.log) 2>&1 +set -xeo pipefail + +function user_add_sudo { + USERNAME="$1" + USERPASS="$2" + if [ ! -n "$USERNAME" ] || [ ! -n "$USERPASS" ]; then + echo "No new username and/or password entered" + return 1; + fi + adduser "$USERNAME" --disabled-password --gecos "" + echo "$USERNAME:$USERPASS" | chpasswd + apt-get install -y sudo + usermod -aG sudo "$USERNAME" +} + +function download_snapshot { + if [ -z "${AWS_ACCESS_KEY_ID}" ]; then + wget "${SNAPSHOT_URL}" + else + echo "[default] + access_key = ${AWS_ACCESS_KEY_ID} + secret_key = ${AWS_SECRET_ACCESS_KEY}" > ~/.s3cfg + if [ -z "${ENDPOINT_URL}" ]; then + s4cmd --verbose get "${SNAPSHOT_URL}" + else + s4cmd --verbose get "${SNAPSHOT_URL}" --endpoint-url "${ENDPOINT_URL}" + fi + fi +} + +function download_and_start { + download_snapshot + # get the snapshot data into place + SNAPSHOT_FILE_NAME=$(echo "${SNAPSHOT_URL}" | rev | cut -d/ -f1 | rev) + unzip "${SNAPSHOT_FILE_NAME}" -d ~/.lbcd/ + mv ~/.lbcd/"${SNAPSHOT_FILE_NAME}" ~/.lbcd/data + rm "${SNAPSHOT_FILE_NAME}" + # get our private ip + PRIVATE_IP=$(ip addr | grep "${PRIVATE_IP_PREFIX}" | cut -d'/' -f1 | rev | cut -d" " -f 1 | rev) + # download the compose-compose and put our private ip in the for RPC endpoint + wget "${DOCKER_COMPOSE_FILE_URL}" -O - | sed 's/REPLACE_ME/'"${PRIVATE_IP}"'/g' > docker-compose.yml + # Create our volume and start lbcd + docker volume create --driver local \ + --opt type=none \ + --opt device=~/.lbcd\ + --opt o=bind lbcd + docker-compose up -d +} +# add a non-root sudo user +user_add_sudo "${USERNAME}" "${PASSWORD}" +# Update and install dependencies +sudo apt-get update && sudo apt-get upgrade -y +sudo apt-get install -y unzip wget s4cmd +apt install -y apt-transport-https ca-certificates curl software-properties-common && \ + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \ + add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \ + apt install -y docker-ce docker-ce-cli containerd.io && \ + systemctl enable docker && sudo systemctl start docker && \ + curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \ + chmod +x /usr/local/bin/docker-compose +# make sure we can use docker +usermod -aG docker $USERNAME +export -f download_and_start +export -f download_snapshot +su "${USERNAME}" -c 'bash -c "cd ~ && download_and_start"' \ No newline at end of file diff --git a/contrib/linode/run.sh b/contrib/linode/run.sh new file mode 100755 index 00000000..2966339c --- /dev/null +++ b/contrib/linode/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/lbcd --txindex --notls --rpcuser=lbry --rpcpass=lbry --rpclisten=