Commit a6c79789 authored by mdj33's avatar mdj33 Committed by vipwzw

add auto deploy para chain

parent c3d02276
FROM ubuntu:16.04
WORKDIR /root
COPY chain33 chain33
COPY chain33-cli chain33-cli
COPY entrypoint.sh entrypoint.sh
COPY chain33.toml chain33*.toml ./
CMD ["/root/chain33", "-f", "/root/chain33.toml"]
op := "start"
.PHONY: docker-compose help
docker-compose: ## build docker-compose for chain33 run
@./docker-compose.sh $(op)
docker-compose-down: ## build docker-compose for chain33 run
@cd temp ;docker-compose down;cd ..
help: ## Display this help screen
@printf "Help doc:\nUsage: make docker-compose op=[command]\n"
@printf "[command]\n"
@printf "[nodegroup]: create super node group if not create \n"
@printf "[wallet]: set node wallet private key if not set \n"
\ No newline at end of file
paraName="test"
#genesisAccount=""
#genesisAmount=100000000
#mainStartHeight=4800000
#authAccount=()
#authPrikey=()
##docker8901端口暴露到宿主机的端口
#authPort=("18901" "18902" "18903" "18904")
#
##需要和chain33 主链保持一致
#superManager="['1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP']"
#
##nodegroup create
##申请超级账户需要在主链冻结币数量
#authFrozenCoins=0
#nodeGroupApplier=""
#applierPrikey=""
#superManagerPrikey=""
#### 测试链配置,主链配置需要把如下测试链配置屏蔽 ##########
genesisAccount="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
genesisAmount=100000000
mainStartHeight=4000000
authAccount=( "1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4" "1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR" "1NLHPEcbTWWxxU3dGUZBhayjrCHD3psX7k" "1MCftFynyvG2F4ED5mdHYgziDxx6vDrScs")
authPrikey=("0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b" "0x19c069234f9d3e61135fefbeb7791b149cdf6af536f26bebb310d4cd22c3fee4" "0x7a80a1f75d7360c6123c32a78ecf978c1ac55636f87892df38d8b85a9aeff115" "0xcacb1f5d51700aea07fca2246ab43b0917d70405c65edea9b5063d72eb5c6b71")
authPort=("18901" "18902" "18903" "18904")
#需要和chain33 主链保持一致
superManager="['12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv']"
tokenApprs="['12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv']"
#nodegroup create
authFrozenCoins=0
nodeGroupApplier="1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4"
applierPrikey="0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b"
superManagerPrikey="4257D8692EF7FE13C68B65D6A52F03933DB2FA5CE8FAF210B5B8B80C721CED01"
\ No newline at end of file
#!/usr/bin/env bash
# shellcheck disable=SC2154
set -e
PWD=$(cd "$(dirname "$0")" && pwd)
export PATH="$PWD:$PATH"
buildpath="temp"
NODE1="$buildpath""_parachain1_1"
CLI="docker exec ${NODE1} /root/chain33-cli"
NODE2="$buildpath""_parachain2_1"
NODE3="$buildpath""_parachain3_1"
NODE4="$buildpath""_parachain4_1"
CHAIN33_CLI="chain33-cli"
containers=("${NODE1}" "${NODE2}" "${NODE3}" "${NODE4}")
## global config ###
sedfix=""
if [ "$(uname)" == "Darwin" ]; then
sedfix=".bak"
fi
# shellcheck source=/dev/null
source config
####################
function para_init() {
local index=1
for auth in "${authAccount[@]}"; do
tomlfile="chain33.para.$index.toml"
para_set_toml "$tomlfile"
sed -i $sedfix 's/^authAccount=.*/authAccount="'''"$auth"'''"/g' "$tomlfile"
(( index++ ))
done
}
function para_set_toml() {
cp chain33.para.toml "${1}"
sed -i $sedfix 's/^Title.*/Title="user.p.'''"$paraName"'''."/g' "${1}"
sed -i $sedfix 's/^startHeight=.*/startHeight='''"$mainStartHeight"'''/g' "${1}"
# rpc
sed -i $sedfix 's/^jrpcBindAddr=.*/jrpcBindAddr="0.0.0.0:8901"/g' "${1}"
sed -i $sedfix 's/^grpcBindAddr=.*/grpcBindAddr="0.0.0.0:8902"/g' "${1}"
sed -i $sedfix 's/^whitelist=.*/whitelist=["localhost","127.0.0.1","0.0.0.0"]/g' "${1}"
if [ -n "$superManager" ];then
# shellcheck disable=SC1004
sed -i $sedfix 's/^superManager=.*/superManager='''"$superManager"'''/g' "${1}"
fi
if [ -n "$tokenApprs" ];then
# shellcheck disable=SC1004
sed -i $sedfix 's/^tokenApprs=.*/tokenApprs='''"$tokenApprs"'''/g' "${1}"
fi
}
function para_set_wallet() {
echo "=========== # para set wallet ============="
for ((i=0;i<${#authAccount[@]};i++))
do
para_import_wallet "${authPort[$i]}" "${authPrikey[$i]}"
done
}
function para_import_wallet() {
local key=$2
local port=$1
echo "=========== # save seed to wallet ============="
./$CHAIN33_CLI --rpc_laddr "http://localhost:$port" seed save -p 1314fuzamei -s "tortoise main civil member grace happy century convince father cage beach hip maid merry rib"
echo "=========== # unlock wallet ============="
./$CHAIN33_CLI --rpc_laddr "http://localhost:$port" wallet unlock -p 1314fuzamei -t 0
echo "=========== # import private key ============="
echo "key: ${key}"
./$CHAIN33_CLI --rpc_laddr "http://localhost:$port" account import_key -k "${key}" -l "paraAuthAccount"
echo "=========== # close auto mining ============="
./$CHAIN33_CLI --rpc_laddr "http://localhost:$port" wallet auto_mine -f 0
echo "=========== # wallet status ============="
./$CHAIN33_CLI --rpc_laddr "http://localhost:$port" wallet status
}
function start() {
echo "=========== # docker-compose ps ============="
docker-compose ps
docker-compose down
# create and run docker-compose container
docker-compose up --build -d
local SLEEP=10
echo "=========== sleep ${SLEEP}s ============="
sleep ${SLEEP}
docker-compose ps
# query node run status
echo "status"
check_docker_status
# ./chain33-cli --rpc_laddr http://localhost:18901 block last_header
$CLI --rpc_laddr http://localhost:8901 block last_header
}
function check_docker_status() {
status=$(docker-compose ps | grep parachain1_1 | awk '{print $6}')
statusPara=$(docker-compose ps | grep parachain1_1 | awk '{print $3}')
if [ "${status}" == "Exit" ] || [ "${statusPara}" == "Exit" ]; then
echo "=========== chain33 service Exit logs ========== "
docker-compose logs parachain1
echo "=========== chain33 service Exit logs End========== "
fi
}
function check_docker_container() {
echo "============== check_docker_container ==============================="
for con in "${containers[@]}"; do
runing=$(docker inspect "${con}" | jq '.[0].State.Running')
if [ ! "${runing}" ]; then
docker inspect "${con}"
echo "check ${con} not actived!"
exit 1
fi
done
}
function query_tx() {
sleep 5
local times=100
while true; do
ret=$(${CLI} --rpc_laddr http://localhost:8901 tx query -s "${1}" | jq -r ".tx.hash")
echo "query hash is ${1}, return ${ret} "
if [ "${ret}" != "${1}" ]; then
sleep 5
times=$((times - 1))
if [ $times -le 0 ]; then
echo "query tx=$1 failed"
exit 1
fi
else
echo "query tx=$1 success"
break
fi
done
}
function create_yml() {
touch docker-compose.yml
cat >> docker-compose.yml << EOF
version: '3'
services:
EOF
for ((i=1;i<=${#authAccount[@]};i++))
do
cat >> docker-compose.yml << EOF
parachain$i:
build:
context: .
entrypoint: /root/entrypoint.sh
environment:
PARAFILE: "/root/chain33.para.$i.toml"
ports:
- "1890$i:8901"
volumes:
- "../storage/parachain$i/paradatadir:/root/paradatadir"
- "../storage/parachain$i/logs:/root/logs"
EOF
done
}
function create_storage() {
mkdir -p storage
cd storage
for ((i=0;i<${#authAccount[@]};i++))
do
dirfile="parachain$i"
mkdir -p "$dirfile"
done
cd ..
}
function create_build() {
rm -rf $buildpath
mkdir -p $buildpath
cp chain33* Dockerfile ./*.sh "$buildpath"/
cd $buildpath
create_yml
}
function para_create_nodegroup() {
echo "=========== # para chain create node group ============="
local auths=""
for auth in "${authAccount[@]}"; do
if [ -z $auths ];then
auths="$auth"
else
auths="$auths,$auth"
fi
done
echo "auths=$auths"
##apply
txhash=$(${CLI} --rpc_laddr http://localhost:8901 --paraName "user.p.$paraName." send para nodegroup apply -a "$auths" -c "${authFrozenCoins}" -k "$applierPrikey")
echo "tx=$txhash"
query_tx "${txhash}"
id=$txhash
echo "need super manager approve id=$txhash"
if [ -n "$superManagerPrikey" ];then
echo "=========== # para chain approve node group ============="
##approve
txhash=$(${CLI} --rpc_laddr http://localhost:8901 --paraName "user.p.$paraName." send para nodegroup approve -i "$id" -c "${authFrozenCoins}" -k "$superManagerPrikey")
echo "tx=$txhash"
query_tx "${CLI}" "${txhash}"
status=$(${CLI} --rpc_laddr http://localhost:8901 --paraName "user.p.$paraName." para nodegroup status | jq -r ".status")
if [ "$status" != 2 ]; then
echo "status not approve status=$status"
exit 1
fi
${CLI} --rpc_laddr http://localhost:8901 --paraName "user.p.$paraName." para nodegroup addrs
fi
echo "======== super node group config end ==================="
}
function main() {
echo "==============================parachain startup op=$1========================================================"
### init para ####
if [ "$1" == "start" ];then
create_storage
create_build
para_init
### start docker ####
start
### finish ###
check_docker_container
fi
if [ "$1" == "nodegroup" ];then
para_create_nodegroup
fi
if [ "$1" == "wallet" ];then
para_set_wallet
fi
echo "===============================parachain startup end========================================================="
}
# run script
main "$1"
version: '3'
services:
\ No newline at end of file
#!/usr/bin/env bash
/root/chain33 -f "$PARAFILE"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment