From 9e50120dec02fc2b2bbbf666e70e06a03cca80ac Mon Sep 17 00:00:00 2001 From: Antonin Hildebrand Date: Sat, 27 Apr 2019 22:01:10 +0200 Subject: [PATCH] docker: drop docker-compose dependency Instead of relying on docker-compose.yml, we use bash to pass configuration to docker directly via commnad-line args. We also use config template and prior each run we evaluate it with current environment. So that settings like LND_GRPC_HOST can be specified prior each run without a need to rebuild. --- README.md | 34 +-------------- docker/.gitignore | 3 +- docker/README.md | 43 +++++++++++++++++++ docker/_settings.sh | 26 ++++++++++- docker/build.sh | 11 ++++- docker/clean.sh | 5 ++- docker/docker-compose.yml | 28 ------------ docker/inspect.sh | 6 ++- docker/lntop.sh | 34 ++++++++++++++- docker/lntop/Dockerfile | 2 + ...nfig.toml => initial-config-template.toml} | 6 +-- docker/lntop/home/run-lntop | 28 +++++++++--- docker/lntop/home/run-service | 11 ----- 13 files changed, 150 insertions(+), 87 deletions(-) create mode 100644 docker/README.md delete mode 100644 docker/docker-compose.yml rename docker/lntop/home/{initial-config.toml => initial-config-template.toml} (56%) delete mode 100755 docker/lntop/home/run-service diff --git a/README.md b/README.md index b740f69..27a6a3f 100644 --- a/README.md +++ b/README.md @@ -42,39 +42,7 @@ Change macaroon path according to your network. ## Docker -If you prefer to run `lntop` from a docker container: - -```sh -cd docker - -# now you should review ./lntop/home/initial-config.toml -# if you have an existing .lntop directory, you can export it -# export LNTOP_HOME=~/.lntop -# ! change path to files in .lntop/config with user current directory /root ! - -# point LND_HOME to your actual lnd directory -# we recommend using .envrc with direnv -export LND_HOME=~/.lnd - -# build the container -./build.sh - -# run lntop from the container -./lntop.sh - -# lntop data will be mapped to host folder at ./_volumes/lntop-data -``` - -To see `lntop` logs, you can tail them in another terminal session via: -```sh -./logs.sh -f -``` - -To start from scratch: -```sh -./clean.sh -./build.sh --no-cache -``` +If you prefer to run `lntop` from a docker container, `cd docker` and follow [`README`](docker/README.md) there. ## Compatibility diff --git a/docker/.gitignore b/docker/.gitignore index 527e972..584ce97 100644 --- a/docker/.gitignore +++ b/docker/.gitignore @@ -1,2 +1,3 @@ lntop/_src -_volumes \ No newline at end of file +_volumes +.envrc \ No newline at end of file diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..5132915 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,43 @@ +## Docker + +To run `lntop` from a docker container: + +```sh +# you should first review ./lntop/home/initial-config-template.toml +# note that paths are relevant to situation inside docker and we run under root +# so $HOME directory is /root + +# build the container +./build.sh + +# if you have an existing .lntop directory on host machine, you can export it: +# export LNTOP_HOME=~/.lntop + +# if you have local lnd node on host machine, point LND_HOME to your actual lnd directory: +export LND_HOME=~/.lnd + +# or alternatively if you have remote lnd node, specify paths to auth files explicitly: +# export TLS_CERT_FILE=/path/to/tls.cert +# export ADMIN_MACAROON_FILE=/path/to/admin.macaroon +# export LND_GRPC_HOST=//:10009 + +# look into _settings.sh for more details on container configuration + +# run lntop from the container +./lntop.sh + +# lntop data will be mapped to host folder at ./_volumes/lntop-data +# note that you can review/tweak ./_volumes/lntop-data/config-template.toml after first run +# the ./_volumes/lntop-data/config.toml is the effective (generated) config used by lntop run +``` + +To see `lntop` logs, you can tail them in another terminal session via: +```sh +./logs.sh -f +``` + +To start from scratch: +```sh +./clean.sh +./build.sh --no-cache +``` \ No newline at end of file diff --git a/docker/_settings.sh b/docker/_settings.sh index 6c3b66b..6720f90 100755 --- a/docker/_settings.sh +++ b/docker/_settings.sh @@ -2,8 +2,30 @@ set -e -o pipefail -export LND_HOME=${LND_HOME:-$HOME/.lnd} -export LNTOP_HOME=${LNTOP_HOME:-./_volumes/lntop-data} +# you have two possible ways how to specify ADMIN_MACAROON_FILE and TLS_CERT_FILE +# 1. specify LND_HOME if it is located on your local machine, we derive default paths from there +# 2. specify env variables ADMIN_MACAROON_FILE and TLS_CERT_FILE + +# also you want to specify LND_GRPC_HOST if your node is remote +# other config tweaks have to be done by changing lntop/home/initial-config-template.toml before build +# or ./_volumes/lntop-data/config-template.toml if you want to just an ad-hoc tweak of existing container + +# note: docker uses network_mode: host + +if [[ -z "$ADMIN_MACAROON_FILE" || -z "$TLS_CERT_FILE" ]]; then + if [[ -z "$LND_HOME" ]]; then + export LND_HOME="$HOME/.lnd" + echo "warning: LND_HOME is not set, assuming '$LND_HOME'" + fi +fi + +export ADMIN_MACAROON_FILE=${ADMIN_MACAROON_FILE:-$LND_HOME/data/chain/bitcoin/mainnet/admin.macaroon} +export TLS_CERT_FILE=${TLS_CERT_FILE:-$LND_HOME/tls.cert} +export LND_GRPC_HOST=${LND_GRPC_HOST:-//127.0.0.1:10009} + export LNTOP_SRC_DIR=${LNTOP_SRC_DIR:-./..} +export LNTOP_HOME=${LNTOP_HOME:-./_volumes/lntop-data} +export LNTOP_AUX_DIR=${LNTOP_AUX_DIR:-./_volumes/lntop-aux} export LNTOP_HOST_UID=${LNTOP_HOST_UID:-$(id -u)} export LNTOP_HOST_GID=${LNTOP_HOST_GID:-$(id -g)} +export LNTOP_VERBOSE=${LNTOP_VERBOSE} diff --git a/docker/build.sh b/docker/build.sh index 3776d71..d7562ee 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -16,5 +16,14 @@ rsync -a \ "$LNTOP_SRC_DIR" \ lntop/_src +cd lntop + echo "Building lntop docker container..." -exec docker-compose build "$@" lntop \ No newline at end of file +if [[ -n "$LNTOP_VERBOSE" ]]; then + set -x +fi +exec docker build \ + --build-arg LNTOP_SRC_PATH=_src \ + -t lntop:local \ + "$@" \ + . \ No newline at end of file diff --git a/docker/clean.sh b/docker/clean.sh index 9e926f3..21aec96 100755 --- a/docker/clean.sh +++ b/docker/clean.sh @@ -11,4 +11,7 @@ if [[ -n "$CONTAINERS" ]]; then fi # clean source code stage -rm -rf lntop/_src \ No newline at end of file +rm -rf lntop/_src + +# clean volumes +rm -rf _volumes \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml deleted file mode 100644 index 2332fe4..0000000 --- a/docker/docker-compose.yml +++ /dev/null @@ -1,28 +0,0 @@ -# we have a really simple setup here -# we use docker-compose only as a convenient way to specify docker build parameters via a yaml file -# you could as well use Dockerfile directly with `docker build` and config passed via command-line args -# -# tips: -# - to run lntop from docker, you can use our wrapper script ./lntop.sh -# - see other scripts in this folder, also check the docker section in the main readme - -version: '3.7' - -services: - - lntop: - image: lntop - container_name: lntop - command: ["run-service"] - network_mode: host - build: - context: ./lntop - dockerfile: Dockerfile - args: - - LNTOP_SRC_PATH=_src - volumes: - - $LND_HOME:/root/.lnd - - $LNTOP_HOME:/root/.lntop - environment: - - LNTOP_HOST_UID - - LNTOP_HOST_GID diff --git a/docker/inspect.sh b/docker/inspect.sh index 7573d62..7326053 100755 --- a/docker/inspect.sh +++ b/docker/inspect.sh @@ -4,4 +4,8 @@ cd "$(dirname "${BASH_SOURCE[0]}")" . _settings.sh -exec docker exec -ti lntop fish \ No newline at end of file +if [[ $# -eq 0 ]]; then + exec ./lntop.sh inspect ${PREFERRED_SHELL} +else + exec ./lntop.sh inspect "$@" +fi diff --git a/docker/lntop.sh b/docker/lntop.sh index 25a9426..f8d26ca 100755 --- a/docker/lntop.sh +++ b/docker/lntop.sh @@ -4,4 +4,36 @@ cd "$(dirname "${BASH_SOURCE[0]}")" . _settings.sh -exec docker-compose run --rm --name lntop lntop /sbin/tini -- run-lntop \ No newline at end of file +abs_path() { + echo "$(cd "$1"; pwd -P)" +} + +if [[ ! -e "$LNTOP_HOME" ]]; then + mkdir -p "$LNTOP_HOME" +fi +LNTOP_HOME_ABSOLUTE=$(abs_path "$LNTOP_HOME") + +if [[ ! -e "$LNTOP_AUX_DIR" ]]; then + mkdir -p "$LNTOP_AUX_DIR" +fi +LNTOP_AUX_DIR_ABSOLUTE=$(abs_path "$LNTOP_AUX_DIR") + +# we use LNTOP_AUX_DIR as ad-hoc volume to pass admin.macaroon and tls.cert into our container +# it is mapped to /root/aux, config-template.toml assumes that +cp "$ADMIN_MACAROON_FILE" "$LNTOP_AUX_DIR/admin.macaroon" +cp "$TLS_CERT_FILE" "$LNTOP_AUX_DIR/tls.cert" + +if [[ -n "$LNTOP_VERBOSE" ]]; then + set -x +fi +exec docker run \ + --rm \ + --network host \ + -v "$LNTOP_HOME_ABSOLUTE:/root/.lntop" \ + -v "$LNTOP_AUX_DIR_ABSOLUTE:/root/aux" \ + -e "LNTOP_HOST_UID=${LNTOP_HOST_UID}" \ + -e "LNTOP_HOST_GID=${LNTOP_HOST_GID}" \ + -e "LND_GRPC_HOST=${LND_GRPC_HOST}" \ + -ti \ + lntop:local \ + run-lntop "$@" \ No newline at end of file diff --git a/docker/lntop/Dockerfile b/docker/lntop/Dockerfile index 9a3b48b..007ac92 100644 --- a/docker/lntop/Dockerfile +++ b/docker/lntop/Dockerfile @@ -26,6 +26,8 @@ RUN apk add --no-cache \ ca-certificates \ tini +ENTRYPOINT ["/sbin/tini", "--"] + ENV PATH $PATH:/root ARG LNTOP_CONF_PATH diff --git a/docker/lntop/home/initial-config.toml b/docker/lntop/home/initial-config-template.toml similarity index 56% rename from docker/lntop/home/initial-config.toml rename to docker/lntop/home/initial-config-template.toml index b5f518b..7ac9a2a 100644 --- a/docker/lntop/home/initial-config.toml +++ b/docker/lntop/home/initial-config-template.toml @@ -5,9 +5,9 @@ dest = "/root/.lntop/lntop.log" [network] name = "lnd" type = "lnd" -address = "//127.0.0.1:10009" -cert = "/root/.lnd/tls.cert" -macaroon = "/root/.lnd/data/chain/bitcoin/mainnet/admin.macaroon" +address = "${LND_GRPC_HOST}" +cert = "/root/aux/tls.cert" +macaroon = "/root/aux/admin.macaroon" macaroon_timeout = 60 max_msg_recv_size = 52428800 conn_timeout = 1000000 diff --git a/docker/lntop/home/run-lntop b/docker/lntop/home/run-lntop index ae49bd3..85abed9 100755 --- a/docker/lntop/home/run-lntop +++ b/docker/lntop/home/run-lntop @@ -2,10 +2,17 @@ set -e -o pipefail +# this is a special command to allow inspection on this container +if [[ "$1" == "inspect" ]]; then + shift + exec "$@" +fi + cd "$(dirname "${BASH_SOURCE[0]}")" LNTOP_HOME_DIR=.lntop LNTOP_CONFIG="$LNTOP_HOME_DIR/config.toml" +LNTOP_CONFIG_TEMPLATE="$LNTOP_HOME_DIR/config-template.toml" LNTOP_HOST_GID=${LNTOP_HOST_GID:?required} LNTOP_HOST_UID=${LNTOP_HOST_UID:?required} @@ -15,10 +22,21 @@ if [[ ! -d "$LNTOP_HOME_DIR" ]]; then chown ${LNTOP_HOST_UID}:${LNTOP_HOST_GID} "$LNTOP_HOME_DIR" fi -# prepare config file only if it does not already exist -if [[ ! -e "$LNTOP_CONFIG" ]]; then - cp initial-config.toml "$LNTOP_CONFIG" - chown ${LNTOP_HOST_UID}:${LNTOP_HOST_GID} "$LNTOP_CONFIG" +eval_template() { + local template_file=$1 + eval "cat < /dev/null +} + +# stage template file only if it does not already exist +if [[ ! -e "$LNTOP_CONFIG_TEMPLATE" ]]; then + cp initial-config-template.toml "$LNTOP_CONFIG_TEMPLATE" fi -exec lntop \ No newline at end of file +# we dynamically prepare config from template by baking in env variables +echo "# !!! GENERATED !!! DO NOT EDIT THIS FILE, EDIT config-template.toml INSTEAD" > "$LNTOP_CONFIG" +eval_template initial-config-template.toml >> "$LNTOP_CONFIG" + +exec lntop "$@" \ No newline at end of file diff --git a/docker/lntop/home/run-service b/docker/lntop/home/run-service deleted file mode 100755 index 71931db..0000000 --- a/docker/lntop/home/run-service +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -set -e -o pipefail - -if [[ ! $# -eq 0 ]]; then - exec "$@" -fi - -echo "this docker-compose service is not designed to be launched via docker-compose up" -echo "exec lntop via ./lntop.sh or directly via docker, e.g. \`docker exec -ti lntop lntop\`" -exit 1 \ No newline at end of file