• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisiona52a2a0a6bb49ce3a4f158f22ab023421e82c6dc (tree)
Time2019-06-20 21:08:57
Authorapolloww <32606824+apolloww@user...>
CommiterPaladz

Log Message

dockerize vapord (#214)

Change Summary

Incremental Difference

--- /dev/null
+++ b/docker/vapord/README.md
@@ -0,0 +1,66 @@
1+# quick start
2+
3+execute deploy.sh at the root of vapor repo
4+
5+## usage
6+
7+```bash
8+bash deploy.sh --help
9+```
10+
11+## build and run a 2-node vapor nodes
12+
13+```bash
14+bash deploy.sh --scale=2
15+```
16+
17+## list available node images and public keys
18+
19+```bash
20+bash deploy.sh --list
21+```
22+
23+## remove all node images
24+
25+```bash
26+bash deploy.sh --rm-all
27+```
28+
29+## remove 2 images
30+
31+```bash
32+bash deploy.sh --rm=vapord_test-ade32,vapord_test-342de
33+```
34+
35+## build 2 vapord images (build only)
36+
37+```bash
38+bash deploy.sh --build=2
39+```
40+
41+## run 2 vapor nodes from existing images
42+
43+```bash
44+bash deploy.sh --run=vapord_test-ade32,vapord_test-342de
45+```
46+
47+## run vapor node from all existing images
48+
49+```bash
50+bash deploy.sh --run-all
51+```
52+
53+## bring down running nodes
54+
55+```bash
56+bash deploy.sh --down
57+```
58+
59+## node naming
60+
61+* id: first 5 chars of public key
62+* node_name : vapord-${id}
63+* image name: vapord_test-${id}:latest
64+* wallet port : start from 9889, and increases by 1 every time a new node image is created.
65+* log location: ~/vapord/log/${node_name}
66+* docker-compose.yml location: ~/vapord/docker-compose.yml
--- /dev/null
+++ b/docker/vapord/config.toml
@@ -0,0 +1,10 @@
1+# This is a TOML config file.
2+# For more information, see https://github.com/toml-lang/toml
3+fast_sync = true
4+db_backend = "leveldb"
5+api_addr = "0.0.0.0:9889"
6+moniker = ""
7+chain_id = "vapor"
8+[p2p]
9+laddr = "tcp://0.0.0.0:56659"
10+seeds = "52.83.133.152:56659"
--- /dev/null
+++ b/docker/vapord/deploy.sh
@@ -0,0 +1,265 @@
1+#!/bin/bash
2+
3+set -e
4+
5+usage() {
6+cat << EOF
7+bash deploy.sh operation [options]
8+operation
9+ --build=num : build node images
10+
11+ --run=image1,image2,...: run selected node images
12+
13+ --run-all: run all node images
14+
15+ --scale=num: build and run multiple node images
16+
17+ --list: list all node images
18+
19+ --remove=image1,image2,...: remove selected node images
20+
21+ --remove-all: remove all node images
22+
23+ --help: print usage message
24+EOF
25+}
26+
27+get_pubkey() {
28+ docker run --rm -it --entrypoint cat $1 /usr/local/vapord/node_pubkey.txt
29+}
30+
31+# text colors
32+RED='\033[0;31m'
33+GREEN='\033[0;32m'
34+CYAN='\033[0;36m'
35+NC='\033[0m' # No Color
36+
37+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
38+DOCKER_DIR=~/vapord
39+
40+mkdir -p ${DOCKER_DIR}
41+
42+# node and image names are PREFIX-pubkey[0:6]
43+NODE_PREFIX=vapord
44+IMG_PREFIX=vapord_test
45+WALLET_PORT_BEGIN=9889
46+
47+############## process commandline options ###############
48+# num of nodes
49+scale=1
50+# operation: build, run, run-all, all, list, remove, remove-all help
51+op="all"
52+op_arg="vapord_test-2951a1"
53+
54+# Call getopt to validate the provided input.
55+options=$(getopt -o brg --long build:run:run-all:list:rm:rm-all:scale:help:down -- "$@")
56+[ $? -eq 0 ] || {
57+ echo "Incorrect options provided"
58+ exit 1
59+}
60+
61+eval set -- "$options"
62+while true; do
63+ case "$1" in
64+ --build)
65+ shift;
66+ op="build"
67+ scale=$1
68+ ;;
69+ --run)
70+ shift;
71+ op="run"
72+ op_arg=$1
73+ ;;
74+ --run-all)
75+ op="run-all"
76+ ;;
77+ --list)
78+ op="list"
79+ ;;
80+ --rm)
81+ shift;
82+ op="remove"
83+ op_arg=$1
84+ ;;
85+ --rm-all)
86+ op="remove-all"
87+ ;;
88+ --scale)
89+ shift; # The arg is next in position args
90+ op="all"
91+ scale=$1
92+ ;;
93+ --help)
94+ op="help"
95+ ;;
96+ --down)
97+ op="down"
98+ ;;
99+ --)
100+ shift
101+ break
102+ ;;
103+ esac
104+ shift
105+done
106+
107+echo "options: scale:${scale}, op:${op} op_arg:${op_arg}"
108+
109+if [ "${op}" == "help" ]; then
110+ usage
111+ exit
112+fi
113+
114+if [ "${op}" == "down" ]; then
115+ docker-compose -f ${DOCKER_DIR}/docker-compose.yml down
116+ exit
117+fi
118+
119+if [ -z "${scale}" ]; then
120+ echo "please specify number of nodes to spawn."
121+ usage
122+ exit 1
123+elif [ "${scale}" -lt 1 ]; then
124+ echo "number of nodes must be greater than 0"
125+ usage
126+ exit 1
127+fi
128+
129+key_array=()
130+node_array=()
131+img_array=()
132+
133+############## remove images ################
134+if [ "${op}" == "remove-all" ]; then
135+ echo -e "${GREEN}removing all node images${NC}"
136+ docker rmi $(docker images --filter=reference="${IMG_PREFIX}-*:*" -q)
137+ exit
138+elif [ "${op}" == "remove" ]; then
139+ if [ -z "${op_arg}" ]; then
140+ echo -e "${RED}must specify which image(s) to remove${NC}"
141+ exit 1
142+ fi
143+ IFS=',' read -r -a img_array <<< "${op_arg}"
144+ for img in "${img_array[@]}"; do
145+ if [[ "${img}" =~ ^${IMG_PREFIX}-* ]]; then
146+ echo -e "${GREEN}removing image ${img}${NC}"
147+ docker rmi $img
148+ else
149+ echo -e "${RED}invalid image name ${img}${NC}"
150+ fi
151+ done
152+ exit
153+fi
154+
155+############### list images ################
156+if [ "${op}" == "list" ]; then
157+ echo -e "${GREEN}list all node images${NC}"
158+ docker images --filter=reference="${IMG_PREFIX}-*:*"
159+ echo
160+ echo "public keys"
161+ img_array=(`docker images --filter=reference="${IMG_PREFIX}-*:*" --format "{{.Repository}}"`)
162+ for img in "${img_array[@]}"; do
163+ pubkey=$( get_pubkey ${img} )
164+ if [ -z "${pubkey}" ]; then
165+ echo -e "${RED}failed to get public key${NC} for node ${img}"
166+ exit
167+ fi
168+ printf "${img}\t${pubkey}\n"
169+ done
170+ exit
171+fi
172+
173+############### build images ################
174+if [[ "${op}" == "build" || "${op}" == "all" ]]; then
175+for ((i = 1 ; i <= ${scale} ; i++)); do
176+ echo -e "${GREEN}building docker image for node #${i}${NC}"
177+ docker build --rm -t vapord_tmp . -f ${SCRIPT_DIR}/vapord.Dockerfile
178+ # /usr/local/vapord/node_pubkey.txt is the location storing pub_key of node defined in dockerfile
179+ pubkey=$( get_pubkey vapord_tmp )
180+ if [ -z "${pubkey}" ]; then
181+ echo -e "${RED}failed to get public key${NC} for node ${i}"
182+ exit
183+ fi
184+ node_tag=`echo "${pubkey}" | cut -c1-6`
185+ img=${IMG_PREFIX}-${node_tag}
186+ docker tag vapord_tmp ${img}:latest
187+ docker rmi vapord_tmp
188+ docker image prune -f --filter label=stage=vapord_builder > /dev/null
189+ key_array+=(${pubkey})
190+ node_array+=(${NODE_PREFIX}-${node_tag})
191+ img_array+=(${img})
192+done
193+fi
194+
195+############### generate docker-compose.yml for the network ###############
196+if [ "${op}" == "run" ]; then
197+ if [ -z "${op_arg}" ]; then
198+ echo -e "${RED}must specify which image(s) to run${NC}"
199+ exit 1
200+ fi
201+ img_array=()
202+ node_array=()
203+ IFS=',' read -r -a img_array <<< "${op_arg}"
204+ for img in "${img_array[@]}"; do
205+ if ! [[ "${img}" =~ ^${IMG_PREFIX}-* ]]; then
206+ echo -e "${RED}invalid image name ${img}${NC}"
207+ exit 1
208+ fi
209+ if [[ "$(docker images -q ${img}:latest 2> /dev/null)" == "" ]]; then
210+ echo -e "${RED}image ${img}:latest does not exist${NC}"
211+ exit 1
212+ fi
213+ hash=`echo ${img} | cut -d- -f 2`
214+ node_array+=(${NODE_PREFIX}-${hash})
215+
216+ pubkey=$( get_pubkey ${img} )
217+ if [ -z "${pubkey}" ]; then
218+ echo -e "${RED}failed to get public key${NC} for node ${img}"
219+ exit
220+ fi
221+ key_array+=(${pubkey})
222+ done
223+elif [ "${op}" == "run-all" ]; then
224+ img_array=(`docker images --filter=reference="${IMG_PREFIX}-*:*" --format "{{.Repository}}"`)
225+ for img in "${img_array[@]}"; do
226+ hash=`echo ${img} | cut -d- -f 2`
227+ node_array+=(${NODE_PREFIX}-${hash})
228+
229+ pubkey=$( get_pubkey ${img} )
230+ if [ -z "${pubkey}" ]; then
231+ echo -e "${RED}failed to get public key${NC} for node ${img}"
232+ exit
233+ fi
234+ key_array+=(${pubkey})
235+ done
236+fi
237+
238+echo -e "image/node/pkey"
239+
240+for id in "${!img_array[@]}"; do
241+ node=${node_array[id]}
242+ img=${img_array[id]}
243+ pubkey=${key_array[id]}
244+ printf "${img}\t${node}\t${pubkey}\n"
245+done
246+
247+if [ "${op}" == "build" ]; then
248+ exit
249+fi
250+
251+echo "### DO NOT MODIFY. THIS FILE IS AUTO GEN FROM deploy.sh ###" > ${DOCKER_DIR}/docker-compose.yml
252+echo "version: '2'" >> ${DOCKER_DIR}/docker-compose.yml
253+echo "services:" >> ${DOCKER_DIR}/docker-compose.yml
254+
255+for id in "${!img_array[@]}"; do
256+ node=${node_array[id]}
257+ img=${img_array[id]}
258+ echo -e "${GREEN}setup service for node ${node}${NC}"
259+ name=${node} image=${img}:latest port=$(( id + WALLET_PORT_BEGIN )) envsubst '${name} ${image} ${port}' < ${SCRIPT_DIR}/docker-compose.yml.tpl >> ${DOCKER_DIR}/docker-compose.yml
260+ echo >> ${DOCKER_DIR}/docker-compose.yml
261+done
262+
263+############### start network ###############
264+echo -e "${GREEN}network up...${NC}"
265+docker-compose -f ${DOCKER_DIR}/docker-compose.yml up -d
--- /dev/null
+++ b/docker/vapord/docker-compose.yml.tpl
@@ -0,0 +1,13 @@
1+ ${name}:
2+ container_name: ${name}
3+ image: ${image}
4+ privileged: true
5+ restart: always
6+ #network_mode: host
7+ ports:
8+ - ${port}:9889
9+ #- 56659:56659
10+ volumes:
11+ #- /etc/localtime:/etc/localtime
12+ - ~/vapord/log/${name}:/var/log/vapord
13+
\ No newline at end of file
--- /dev/null
+++ b/docker/vapord/federation.json
@@ -0,0 +1,6 @@
1+{
2+ "xpubs": [
3+ "50ef22b3a3fca7bc08916187cc9ec2f4005c9c6b1353aa1decbd4be3f3bb0fbe1967589f0d9dec13a388c0412002d2c267bdf3b920864e1ddc50581be5604ce1"
4+ ],
5+ "quorum": 1
6+}
--- /dev/null
+++ b/docker/vapord/supervisord.conf
@@ -0,0 +1,8 @@
1+[program:vapord]
2+command=/usr/local/vapord/vapord node -r /usr/local/vapord --auth.disable ; the program (relative uses PATH, can take args)
3+autorestart=true
4+
5+stdout_logfile=/var/log/vapord/node.log ; stdout log path, NONE for none; default AUTO
6+stderr_logfile=/var/log/vapord/node_err.log
7+stdout_logfile_backups = 15
8+stdout_logfile_maxbytes=100MB ; max # logfile bytes b4 rotation (default 50MB)
--- /dev/null
+++ b/docker/vapord/vapord.Dockerfile
@@ -0,0 +1,19 @@
1+FROM registry.cn-hangzhou.aliyuncs.com/matpool/golang-gcc-gpp:1.11.2 as builder
2+LABEL stage=vapord_builder
3+WORKDIR /go/src/github.com/vapor
4+COPY . .
5+RUN go build -o /usr/local/vapord/vapord ./cmd/bytomd/main.go
6+# save node public key in /usr/local/vapord/node_pubkey.txt
7+RUN /usr/local/vapord/vapord init --chain_id vapor -r /usr/local/vapord 2>&1 | grep -o 'pubkey=[a-z0-9]*' | cut -d'=' -f 2 > /usr/local/vapord/node_pubkey.txt
8+COPY ./docker/vapord/config.toml /usr/local/vapord/config.toml
9+COPY ./docker/vapord/federation.json /usr/local/vapord/federation.json
10+
11+###
12+FROM registry.cn-hangzhou.aliyuncs.com/matpool/alpine-ca-supervisord:latest
13+COPY ./docker/vapord/supervisord.conf /etc/supervisor/conf.d/vapord.conf
14+COPY --from=builder /usr/local/vapord /usr/local/vapord
15+RUN mkdir -p /var/log/vapord
16+
17+EXPOSE 9889 56659
18+
19+CMD []
\ No newline at end of file
Show on old repository browser