Skip to content

Commit

Permalink
docker: general improvements
Browse files Browse the repository at this point in the history
* rename set_env->set_default
* fix bug with return from bash func
* make '--miningaddr' optional
  • Loading branch information
andrewshvv authored and Roasbeef committed Jan 18, 2017
1 parent 5c6d196 commit be66e03
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 60 deletions.
36 changes: 25 additions & 11 deletions docker/btcd/start-btcctl.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
#!/usr/bin/env bash

# Check env variable and in case of empty value and default value specified
# returns default value, in case of non-empty value returns value.
set_env() {
# exit from script if error was raised.
set -e

# error function is used within a bash function in order to send the error
# message directly to the stderr output and exit.
error() {
echo "$1" > /dev/stderr
exit 0
}

# return is used within bash function in order to return the value.
return() {
echo "$1"
}

# set_default function gives the ability to move the setting of default
# env variable from docker file to the script thereby giving the ability to the
# user override it durin container start.
set_default() {
# docker initialized env variables with blank string and we can't just
# use -z flag as usually.
BLANK_STRING='""'

VARIABLE="$1"
NAME="$2"
DEFAULT="$3"
DEFAULT="$2"

if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then

if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable"
exit 0
error "You should specify default variable"
else
VARIABLE="$DEFAULT"
fi
fi

# echo is used as return in case if string values
echo "$VARIABLE"
return "$VARIABLE"
}

RPCUSER=$(set_env "$RPCUSER" "RPCUSER")
RPCPASS=$(set_env "$RPCPASS" "RPCPASS")
# Set default variables if needed.
RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")

btcctl \
--simnet \
Expand Down
78 changes: 50 additions & 28 deletions docker/btcd/start-btcd.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,69 @@
#!/usr/bin/env bash

# Check env variable and in case of empty value and default value specified
# returns default value, in case of non-empty value returns value.
set_env() {
# exit from script if error was raised.
set -e

# error function is used within a bash function in order to send the error
# message directly to the stderr output and exit.
error() {
echo "$1" > /dev/stderr
exit 0
}

# return is used within bash function in order to return the value.
return() {
echo "$1"
}

# set_default function gives the ability to move the setting of default
# env variable from docker file to the script thereby giving the ability to the
# user override it durin container start.
set_default() {
# docker initialized env variables with blank string and we can't just
# use -z flag as usually.
BLANK_STRING='""'

VARIABLE="$1"
NAME="$2"
DEFAULT="$3"
DEFAULT="$2"

if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then

if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable"
exit 0
error "You should specify default variable"
else
VARIABLE="$DEFAULT"
fi
fi

# echo is used as return in case if string values
echo "$VARIABLE"
return "$VARIABLE"
}

RPCUSER=$(set_env "$RPCUSER" "RPCUSER")
RPCPASS=$(set_env "$RPCPASS" "RPCPASS")

DEBUG=$(set_env "$DEBUG" "DEBUG")

MINING_ADDRESS=$(set_env "$MINING_ADDRESS" "MINING_ADDRESS" "ScoDuqH7kYA9nvxuRg4Xk7E31AhsSc5zxp")

btcd \
--debuglevel="$DEBUG" \
--datadir="/data" \
--logdir="/data" \
--simnet \
--rpccert="/rpc/rpc.cert" \
--rpckey="/rpc/rpc.key" \
--rpcuser="$RPCUSER" \
--rpcpass="$RPCPASS" \
--miningaddr="$MINING_ADDRESS" \
--rpclisten="0.0.0.0" \
"$@"
# Set default variables if needed.
RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")
DEBUG=$(set_default "$DEBUG" "info")

PARAMS=$(echo \
"--simnet" \
"--debuglevel=$DEBUG" \
"--rpcuser=$RPCUSER" \
"--rpcpass=$RPCPASS" \
"--datadir=/data" \
"--logdir=/data" \
"--rpccert=/rpc/rpc.cert" \
"--rpckey=/rpc/rpc.key" \
"--rpclisten=0.0.0.0"
)

# Set the mining flag only if address is non empty.
if [[ -n "$MINING_ADDRESS" ]]; then
PARAMS="$PARAMS --miningaddr=$MINING_ADDRESS"
fi

# Add user parameters to command.
PARAMS="$PARAMS $@"

# Print command and start bitcoin node.
echo "Command: btcd $PARAMS"
btcd $PARAMS

21 changes: 13 additions & 8 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
version: '2'
services:

# btc is an image of bitcoin node which used as base image for btcd and
# btccli. The environment variables default values determined on stage of
# container start within starting script.
btc:
image: btcd
build:
context: btcd/
volumes:
- shared:/rpc
environment:
- RPCUSER="devuser"
- RPCPASS="devpass"
- RPCUSER
- RPCPASS

btcd:
extends: btc
container_name: btcd
environment:
- DEBUG="debug"
- DEBUG
- MINING_ADDRESS
entrypoint: ["./start-btcd.sh"]

Expand All @@ -32,9 +35,9 @@ services:
context: ../
dockerfile: docker/lnd/Dockerfile
environment:
- RPCUSER="devuser"
- RPCPASS="devpass"
- DEBUG="debug"
- RPCUSER
- RPCPASS
- DEBUG
volumes:
- shared:/rpc
entrypoint: ["./start-lnd.sh"]
Expand All @@ -43,14 +46,16 @@ services:
extends: lnd
container_name: alice
links:
- btcd
- "btcd:btcd"

bob:
extends: lnd
container_name: bob
links:
- btcd
- "btcd:btcd"

volumes:
# shared volume is need to store the btcd rpc certificates and us it within
# btcctl and lnd containers.
shared:
driver: local
39 changes: 26 additions & 13 deletions docker/lnd/start-lnd.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
#!/usr/bin/env bash

# Check env variable and in case of empty value and default value specified
# returns default value, in case of non-empty value returns value.
set_env() {
# exit from script if error was raised.
set -e

# error function is used within a bash function in order to send the error
# mesage directly to the stderr output and exit.
error() {
echo "$1" > /dev/stderr
exit 0
}

# return is used within bash function in order to return the value.
return() {
echo "$1"
}

# set_default function gives the ability to move the setting of default
# env variable from docker file to the script thereby giving the ability to the
# user override it durin container start.
set_default() {
# docker initialized env variables with blank string and we can't just
# use -z flag as usually.
BLANK_STRING='""'

VARIABLE="$1"
NAME="$2"
DEFAULT="$3"
DEFAULT="$2"

if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then

if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable"
exit 0
error "You should specify default variable"
else
VARIABLE="$DEFAULT"
fi
fi

# echo is used as return in case if string values
echo "$VARIABLE"
return "$VARIABLE"
}

RPCUSER=$(set_env "$RPCUSER" "RPCUSER")
RPCPASS=$(set_env "$RPCPASS" "RPCPASS")

DEBUG=$(set_env "$DEBUG" "DEBUG")
# Set default variables if needed.
RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")
DEBUG=$(set_default "$DEBUG" "debug")

lnd \
--datadir="/data" \
Expand Down

0 comments on commit be66e03

Please sign in to comment.