docker: general improvements

* rename set_env->set_default
* fix bug with return from bash func
* make '--miningaddr' optional
This commit is contained in:
Andrey Samokhvalov 2017-01-13 23:23:30 +03:00 committed by Olaoluwa Osuntokun
parent 5c6d196ff8
commit be66e039f1
4 changed files with 111 additions and 57 deletions

@ -1,32 +1,46 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Check env variable and in case of empty value and default value specified # exit from script if error was raised.
# returns default value, in case of non-empty value returns value. set -e
set_env() {
# 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 # docker initialized env variables with blank string and we can't just
# use -z flag as usually. # use -z flag as usually.
BLANK_STRING='""' BLANK_STRING='""'
VARIABLE="$1" VARIABLE="$1"
NAME="$2" DEFAULT="$2"
DEFAULT="$3"
if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then
if [ -z "$DEFAULT" ]; then if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable" error "You should specify default variable"
exit 0
else else
VARIABLE="$DEFAULT" VARIABLE="$DEFAULT"
fi fi
fi fi
# echo is used as return in case if string values return "$VARIABLE"
echo "$VARIABLE"
} }
RPCUSER=$(set_env "$RPCUSER" "RPCUSER") # Set default variables if needed.
RPCPASS=$(set_env "$RPCPASS" "RPCPASS") RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")
btcctl \ btcctl \
--simnet \ --simnet \

@ -1,47 +1,69 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Check env variable and in case of empty value and default value specified # exit from script if error was raised.
# returns default value, in case of non-empty value returns value. set -e
set_env() {
# 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 # docker initialized env variables with blank string and we can't just
# use -z flag as usually. # use -z flag as usually.
BLANK_STRING='""' BLANK_STRING='""'
VARIABLE="$1" VARIABLE="$1"
NAME="$2" DEFAULT="$2"
DEFAULT="$3"
if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then
if [ -z "$DEFAULT" ]; then if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable" error "You should specify default variable"
exit 0
else else
VARIABLE="$DEFAULT" VARIABLE="$DEFAULT"
fi fi
fi fi
# echo is used as return in case if string values return "$VARIABLE"
echo "$VARIABLE"
} }
RPCUSER=$(set_env "$RPCUSER" "RPCUSER") # Set default variables if needed.
RPCPASS=$(set_env "$RPCPASS" "RPCPASS") RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")
DEBUG=$(set_default "$DEBUG" "info")
DEBUG=$(set_env "$DEBUG" "DEBUG") 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"
)
MINING_ADDRESS=$(set_env "$MINING_ADDRESS" "MINING_ADDRESS" "ScoDuqH7kYA9nvxuRg4Xk7E31AhsSc5zxp") # Set the mining flag only if address is non empty.
if [[ -n "$MINING_ADDRESS" ]]; then
PARAMS="$PARAMS --miningaddr=$MINING_ADDRESS"
fi
btcd \ # Add user parameters to command.
--debuglevel="$DEBUG" \ PARAMS="$PARAMS $@"
--datadir="/data" \
--logdir="/data" \ # Print command and start bitcoin node.
--simnet \ echo "Command: btcd $PARAMS"
--rpccert="/rpc/rpc.cert" \ btcd $PARAMS
--rpckey="/rpc/rpc.key" \
--rpcuser="$RPCUSER" \
--rpcpass="$RPCPASS" \
--miningaddr="$MINING_ADDRESS" \
--rpclisten="0.0.0.0" \
"$@"

@ -1,6 +1,9 @@
version: '2' version: '2'
services: 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: btc:
image: btcd image: btcd
build: build:
@ -8,14 +11,14 @@ services:
volumes: volumes:
- shared:/rpc - shared:/rpc
environment: environment:
- RPCUSER="devuser" - RPCUSER
- RPCPASS="devpass" - RPCPASS
btcd: btcd:
extends: btc extends: btc
container_name: btcd container_name: btcd
environment: environment:
- DEBUG="debug" - DEBUG
- MINING_ADDRESS - MINING_ADDRESS
entrypoint: ["./start-btcd.sh"] entrypoint: ["./start-btcd.sh"]
@ -32,9 +35,9 @@ services:
context: ../ context: ../
dockerfile: docker/lnd/Dockerfile dockerfile: docker/lnd/Dockerfile
environment: environment:
- RPCUSER="devuser" - RPCUSER
- RPCPASS="devpass" - RPCPASS
- DEBUG="debug" - DEBUG
volumes: volumes:
- shared:/rpc - shared:/rpc
entrypoint: ["./start-lnd.sh"] entrypoint: ["./start-lnd.sh"]
@ -43,14 +46,16 @@ services:
extends: lnd extends: lnd
container_name: alice container_name: alice
links: links:
- btcd - "btcd:btcd"
bob: bob:
extends: lnd extends: lnd
container_name: bob container_name: bob
links: links:
- btcd - "btcd:btcd"
volumes: volumes:
# shared volume is need to store the btcd rpc certificates and us it within
# btcctl and lnd containers.
shared: shared:
driver: local driver: local

@ -1,34 +1,47 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Check env variable and in case of empty value and default value specified # exit from script if error was raised.
# returns default value, in case of non-empty value returns value. set -e
set_env() {
# 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 # docker initialized env variables with blank string and we can't just
# use -z flag as usually. # use -z flag as usually.
BLANK_STRING='""' BLANK_STRING='""'
VARIABLE="$1" VARIABLE="$1"
NAME="$2" DEFAULT="$2"
DEFAULT="$3"
if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then
if [ -z "$DEFAULT" ]; then if [ -z "$DEFAULT" ]; then
echo "You should specify '$NAME' env variable" error "You should specify default variable"
exit 0
else else
VARIABLE="$DEFAULT" VARIABLE="$DEFAULT"
fi fi
fi fi
# echo is used as return in case if string values return "$VARIABLE"
echo "$VARIABLE"
} }
RPCUSER=$(set_env "$RPCUSER" "RPCUSER") # Set default variables if needed.
RPCPASS=$(set_env "$RPCPASS" "RPCPASS") RPCUSER=$(set_default "$RPCUSER" "devuser")
RPCPASS=$(set_default "$RPCPASS" "devpass")
DEBUG=$(set_env "$DEBUG" "DEBUG") DEBUG=$(set_default "$DEBUG" "debug")
lnd \ lnd \
--datadir="/data" \ --datadir="/data" \