diff --git a/Makefile b/Makefile index 3e51c834..333553e5 100644 --- a/Makefile +++ b/Makefile @@ -178,7 +178,25 @@ metalinter_all: @echo "--> Running linter (all)" gometalinter.v2 --vendor --deadline=600s --enable-all --disable=lll ./... +########################################################### +### Local testnet using docker + +# Build linux binary on other platforms +build-linux: + GOOS=linux GOARCH=amd64 $(MAKE) build + +# Run a 4-node testnet locally +docker-start: + @echo "Wait until 'Attaching to node0, node1, node2, node3' message appears" + @if ! [ -f build/node0/config/genesis.json ]; then docker run --rm -v `pwd`/build:/tendermint tendermint/localnode testnet --v 4 --o build --populate-persistent-peers --starting-ip-address 10.100.0.2 ; fi + docker-compose up + +# Stop testnet +docker-stop: + docker-compose down + # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. # https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: check build build_race dist install check_tools get_tools update_tools get_vendor_deps draw_deps test_cover test_apps test_persistence test_p2p test test_race test_integrations test_release test100 vagrant_test fmt +.PHONY: check build build_race dist install check_tools get_tools update_tools get_vendor_deps draw_deps test_cover test_apps test_persistence test_p2p test test_race test_integrations test_release test100 vagrant_test fmt build-linux docker-start docker-stop + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..cc76d8db --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,68 @@ +version: '3' + +services: + node0: + container_name: node0 + image: "tendermint/localnode" + ports: + - "46656-46657:46656-46657" + environment: + - ID=0 + - LOG=${LOG:-tendermint.log} + volumes: + - ${FOLDER:-./build}:/tendermint + networks: + localnet: + ipv4_address: 10.100.0.2 + + node1: + container_name: node1 + image: "tendermint/localnode" + ports: + - "46659-46660:46656-46657" + environment: + - ID=1 + - LOG=${LOG:-tendermint.log} + volumes: + - ${FOLDER:-./build}:/tendermint + networks: + localnet: + ipv4_address: 10.100.0.3 + + node2: + container_name: node2 + image: "tendermint/localnode" + environment: + - ID=2 + - LOG=${LOG:-tendermint.log} + ports: + - "46661-46662:46656-46657" + volumes: + - ${FOLDER:-./build}:/tendermint + networks: + localnet: + ipv4_address: 10.100.0.4 + + node3: + container_name: node3 + image: "tendermint/localnode" + environment: + - ID=3 + - LOG=${LOG:-tendermint.log} + ports: + - "46663-46664:46656-46657" + volumes: + - ${FOLDER:-./build}:/tendermint + networks: + localnet: + ipv4_address: 10.100.0.5 + +networks: + localnet: + driver: bridge + ipam: + driver: default + config: + - + subnet: 10.100.0.0/16 + diff --git a/docker-compose/Makefile b/docker-compose/Makefile new file mode 100644 index 00000000..98517851 --- /dev/null +++ b/docker-compose/Makefile @@ -0,0 +1,7 @@ +# Makefile for the "localnode" docker image. + +all: + docker build --tag tendermint/localnode localnode + +.PHONY: all + diff --git a/docker-compose/README.rst b/docker-compose/README.rst new file mode 100644 index 00000000..d22a24d9 --- /dev/null +++ b/docker-compose/README.rst @@ -0,0 +1,40 @@ +localnode +========= + +It is assumed that you have already `setup docker `__. + +Description +----------- +Image for local testnets. + +Add the tendermint binary to the image by attaching it in a folder to the `/tendermint` mount point. + +It assumes that the configuration was created by the `tendermint testnet` command and it is also attached to the `/tendermint` mount point. + +Example: +This example builds a linux tendermint binary under the `build/` folder, creates tendermint configuration for a single-node validator and runs the node: +``` +cd $GOPATH/src/github.com/tendermint/tendermint + +#Build binary +make build-linux + +#Create configuration +docker run -e LOG="stdout" -v `pwd`/build:/tendermint tendermint/localnode testnet --o . --v 1 + +#Run the node +docker run -v `pwd`/build:/tendermint tendermint/localnode +``` + +Logging +------- +Log is saved under the attached volume, in the `tendermint.log` file. If the `LOG` environment variable is set to `stdout` at start, the log is not saved, but printed on the screen. + +Special binaries +---------------- +If you have multiple binaries with different names, you can specify which one to run with the BINARY environment variable. The path of the binary is relative to the attached volume. + +docker-compose.yml +================== +This file creates a 4-node network using the localnode image. The nodes of the network are exposed to the host machine on ports 46656-46657, 46659-46660, 46661-46662, 46663-46664 respectively. + diff --git a/docker-compose/localnode/Dockerfile b/docker-compose/localnode/Dockerfile new file mode 100644 index 00000000..9e5c4f8e --- /dev/null +++ b/docker-compose/localnode/Dockerfile @@ -0,0 +1,19 @@ +FROM alpine:3.7 +MAINTAINER Greg Szabo + +RUN apk update && \ + apk upgrade && \ + apk --no-cache add curl jq file && \ + addgroup tmuser && \ + adduser -S -G tmuser tmuser -h /tendermint + +USER tmuser +VOLUME [ /tendermint ] +WORKDIR /tendermint +EXPOSE 46656 46657 +ENTRYPOINT ["/usr/bin/wrapper.sh"] +CMD ["node", "--proxy_app dummy"] +STOPSIGNAL SIGTERM + +COPY --chown=tmuser:tmuser wrapper.sh /usr/bin/wrapper.sh + diff --git a/docker-compose/localnode/wrapper.sh b/docker-compose/localnode/wrapper.sh new file mode 100755 index 00000000..8b55cbc1 --- /dev/null +++ b/docker-compose/localnode/wrapper.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env sh + +## +## Input parameters +## +BINARY=/tendermint/${BINARY:-tendermint} +ID=${ID:-0} +LOG=${LOG:-tendermint.log} + +## +## Assert linux binary +## +if ! [ -f "${BINARY}" ]; then + echo "The binary `basename ${BINARY}` cannot be found. Please add the binary to the shared folder. Please use the BINARY environment variable if the name of the binary is not 'tendermint' E.g.: -e BINARY=tendermint_my_test_version" + exit 1 +fi +BINARY_CHECK="`file $BINARY | grep 'ELF 64-bit LSB executable, x86-64'`" +if [ -z "${BINARY_CHECK}" ]; then + echo "Binary needs to be OS linux, ARCH amd64" + exit 1 +fi + +## +## Run binary with all parameters +## +export TMHOME="/tendermint/node${ID}" + +if [ -d "${TMHOME}/${LOG}" ]; then + "$BINARY" $@ | tee "${TMHOME}/${LOG}" +else + "$BINARY" $@ +fi +