From 9a4a52ed89684593fa7ef10e0e10976598423baf Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 29 Aug 2016 15:17:48 -0700 Subject: [PATCH] docker: revamp docker set up to allow for an LN test cluster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit revamps the existing docker configuration to allow for developer’s to easily bring up/down a Lightning Network testbed environment. Configuration related bugs within the prior swarm set up have been fixed. The launched lnd nodes are now able to properly communicate with the primary btcd node over RPC. The auto-generated RPC script has been scrapped in favor of hard-coding a developer-only set of RPC credentials. With this change, it’s now possible to add/remove additional lnd nodes in order to test more complex scenarios. Additionally, the containers now build off of the latest Go version (1.7). --- docker/btcd/Dockerfile | 17 ++++++----- docker/btcd/btcd-start.sh | 7 +++++ docker/btcd/initrpc.go | 64 --------------------------------------- docker/docker-compose.yml | 29 +++++++++++------- docker/lnd/Dockerfile | 26 +++++++++++++--- docker/lnd/lnd-start.sh | 5 +++ 6 files changed, 61 insertions(+), 87 deletions(-) create mode 100755 docker/btcd/btcd-start.sh delete mode 100644 docker/btcd/initrpc.go create mode 100755 docker/lnd/lnd-start.sh diff --git a/docker/btcd/Dockerfile b/docker/btcd/Dockerfile index 088d474e9..9c6c9ec50 100644 --- a/docker/btcd/Dockerfile +++ b/docker/btcd/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.6.2 +FROM golang:1.7 MAINTAINER Olaoluwa Osuntokun @@ -12,14 +12,17 @@ EXPOSE 8333 18333 18335 28901 # Expose the mainnet, testnet, simnet, and segnet rpc ports. EXPOSE 8333 18333 18336 28902 +# Create a volume to house the RPC credentials. This will be shared with any +# lnd containers so they can securely query btcd's RPC server. +VOLUME ["/rpc"] + VOLUME ["/data"] RUN mkdir /root/.btcd && mkdir /root/.btcctl -# Generate an automatic RPC conf. -ADD initrpc.go /root/ -WORKDIR /root -RUN go build -o gen-config && ./gen-config +COPY btcd-start.sh / -# TODO(roabeef): ENV or prog to parse --no-tls? -ENTRYPOINT ["/go/bin/btcd", "--datadir=/data", "--logdir=/data", "--segnet", "--rpccert=/data/rpc.cert", "--rpckey=/data/rpc.key"] +# Finally, execute the shell script that will start btcd. We use a shell script +# rather than executing the command directly with ENTRYPOINT in order to ensure +# environment variables get properly substitued. +ENTRYPOINT ["/btcd-start.sh"] diff --git a/docker/btcd/btcd-start.sh b/docker/btcd/btcd-start.sh new file mode 100755 index 000000000..04334c9fe --- /dev/null +++ b/docker/btcd/btcd-start.sh @@ -0,0 +1,7 @@ +#!/bin/bash + + +/go/bin/btcd --datadir=/data --logdir=/data --simnet \ + --rpccert=/rpc/rpc.cert --rpckey=/rpc/rpc.key \ + --rpcuser=${RPCUSER} --rpcpass=${RPCPASS} --rpclisten=0.0.0.0 \ + --debuglevel=debug diff --git a/docker/btcd/initrpc.go b/docker/btcd/initrpc.go deleted file mode 100644 index 331da39b1..000000000 --- a/docker/btcd/initrpc.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "bytes" - "crypto/rand" - "encoding/base64" - "flag" - "fmt" - "io/ioutil" - "log" - "text/template" - - "github.com/roasbeef/btcutil" -) - -var ( - numRandBytes = flag.Int("num_rand_bytes", 32, "Number of random bytes to read for both the username and password") -) - -const ( - autoRpcTemplate = "[Application Options]\nrpcuser={{.Username}}\nrpcpass={{.Password}}" -) - -type basicRpcOptions struct { - Username string - Password string -} - -func randBase64string(numBytes int) string { - randBuf := make([]byte, numBytes) - if _, err := rand.Read(randBuf); err != nil { - log.Fatalf("unable to read random bytes: %v", err) - } - return base64.StdEncoding.EncodeToString(randBuf) -} - -func main() { - fmt.Println("Creating random rpc config for btcd") - t := template.Must(template.New("rpcOptions").Parse(autoRpcTemplate)) - - randRpcOptions := basicRpcOptions{ - Username: randBase64string(*numRandBytes), - Password: randBase64string(*numRandBytes), - } - - var autoAuth bytes.Buffer - if err := t.Execute(&autoAuth, randRpcOptions); err != nil { - log.Fatalf("unable to generate random auth: %v") - } - - btcdHomeDir := btcutil.AppDataDir("btcd", false) - btcctlHomeDir := btcutil.AppDataDir("btcctl", false) - btcdConfigPath := fmt.Sprintf("%s/btcd.conf", btcdHomeDir) - btcctlConfigPath := fmt.Sprintf("%s/btcctl.conf", btcctlHomeDir) - - if err := ioutil.WriteFile(btcdConfigPath, autoAuth.Bytes(), 0644); err != nil { - log.Fatalf("unable to write config for btcd: %v", err) - } - - if err := ioutil.WriteFile(btcctlConfigPath, autoAuth.Bytes(), 0644); err != nil { - log.Fatalf("unable to write config for btcctl: %v", err) - } - fmt.Println("fin.") -} diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index e790fb360..eb62f9e09 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,20 +1,27 @@ version: '2' services: btcd: - container_name: btcd - build: btcd/ - ports: - # TODO(roasbeef): switch to testnet after fixing peer discovery. - - "28901:28901" + environment: + - RPCUSER=devuser + - RPCPASS=devpass + build: + context: btcd/ + expose: + - "18336" volumes: - - shared-volume:/data + - shared-volume:/rpc lnd: - container_name: lnd - build: lnd/ - ports: - - "10009:10009" + environment: + - RPCUSER=devuser + - RPCPASS=devpass + build: + context: ../ + dockerfile: docker/lnd/Dockerfile + expose: + - "10009" + - "10011" volumes: - - shared-volume:/data + - shared-volume:/rpc links: - btcd volumes: diff --git a/docker/lnd/Dockerfile b/docker/lnd/Dockerfile index 4d2de759f..bb4effa1e 100644 --- a/docker/lnd/Dockerfile +++ b/docker/lnd/Dockerfile @@ -1,14 +1,30 @@ -FROM golang:1.6.2 +FROM golang:1.7 MAINTAINER Olaoluwa Osuntokun -# Grab and install the latest version of lnd and all related dependencies. -# TODO(roasbeef): replace with glide install -RUN go get -u -v github.com/lightningnetwork/lnd/... +# TODO(roasbeef): just mount a volume from the build context to the GOPATH? +ADD . /go/src/github.com/lightningnetwork/lnd +WORKDIR /go/src/github.com/lightningnetwork/lnd + +# Force Go to use the cgo based DNS resolver. This is required to ensure DNS +# queries required to connect to linked containers succeed. +ENV GODEBUG netdns=cgo + +RUN go build +RUN go install . ./cmd/... + +# Mount a volume where btcd's RPC credentials are stored. We'll need to read +# the TLS cert from this directory. +VOLUME ["/rpc"] VOLUME ["/data"] # Expose the p2p listening port, and the current RPC port. EXPOSE 10009 10011 -ENTRYPOINT ["/go/bin/lnd", "--datadir=/data", "--logdir=/data", "--segnet", "--btcdhost=btcd", "--rpccert=/data/rpc.cert"] +COPY docker/lnd/lnd-start.sh / + +# Finally, execute the shell script that will start lnd. We use a shell script +# rather than executing the command directly with ENTRYPOINT in order to ensure +# environment variables get properly substitued. +ENTRYPOINT ["/lnd-start.sh"] diff --git a/docker/lnd/lnd-start.sh b/docker/lnd/lnd-start.sh new file mode 100755 index 000000000..c5465fa6d --- /dev/null +++ b/docker/lnd/lnd-start.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +/go/bin/lnd --datadir=/data --logdir=/data --simnet \ + --btcdhost=btcd --rpccert=/rpc/rpc.cert \ + --rpcuser=${RPCUSER} --rpcpass=${RPCPASS} --debuglevel=debug