-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
169 lines (153 loc) · 4.83 KB
/
script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
help()
{
cat << HELP
Usage:
bash script.sh <command> <node> <number>
The commands are:
init init geth
attach attach to geth js console
start start geth
status status geth
stop stop geth
log cat geth log
account make newaccount
The nodes are:
boot bootstrap node
peer peer node
The number is:
1 ~ (default : 0)
HELP
}
# Check the string list($1) include a str parameter($2)
# use : check_list $strArray $str
check_list()
{
check=0
for member in $1
do
[ "${member}" == "$2" ] && check=1 && break
done
echo $check
}
# Check the command line is correct
check_command()
{
return1=$(check_list "attach init start stop account log status" "$command")
return2=$(check_list "BOOT PEER" "$mode")
if [ -n "${index//[0-9]/}" ] || [ $return1 -eq 0 ] || [ $return2 -eq 0 ] ; then
echo "invalid command"
help
exit 100
fi
}
# Check the folder exists and create it if it does not exist
# use : check_directory $path
check_directory()
{
if [ ! -d $1 ]; then
mkdir $1
fi
}
# Check the file exists and create it if it does not exist
# use : check_file $path
check_file()
{
if [ ! -e $1 ];then
echo "" > $1
fi
}
# Create a json file in the format { "enode" : argument }
# use : generate_json $str
generate_json()
{
cat<<EOF
{
"enode":$1
}
EOF
}
# Showing help
[ -z "$1" ] || [ "$1" == help ] && help && exit 0
# init commandLine argument
command=$1
mode=`echo $2 | tr [a-z] [A-Z]`
index=$3
if [ -z "$index" ] ; then
index=0 # default value
fi
# Call check_command function
check_command
# Init values
GETHPATH=`cat config.json | jq '.BUILDPATH' | sed 's/\"//g'`
GENESIS=`cat config.json | jq '.GENESIS' | sed 's/\"//g'`
NETWORKID=`cat config.json | jq '.'$mode'['$index'].NETWORKID' | sed 's/\"//g'`
PORT=`cat config.json | jq '.'$mode'['$index'].PORT' | sed 's/\"//g'`
AUTHPORT=`cat config.json | jq '.'$mode'['$index'].AUTHPORT' | sed 's/\"//g'`
DATAPATH=`cat config.json | jq '.'$mode'['$index'].DATAPATH' | sed 's/\"//g'`
HTTPPORT=`cat config.json | jq '.'$mode'['$index'].HTTPPORT' | sed 's/\"//g'`
HTTPAPI=`cat config.json | jq '.'$mode'['$index'].HTTPAPI' | sed 's/\"//g'`
HTTPADDR=`cat config.json | jq '.'$mode'['$index'].HTTPADDR' | sed 's/\"//g'`
PWDPATH=`cat config.json | jq '.'$mode'['$index'].PWDPATH' | sed 's/\"//g'`
BOOTENODEPATH=`cat config.json | jq '.'$mode'['$index'].BOOTENODEPATH' | sed 's/\"//g'`
DEFAULTOPTION=`cat config.json | jq '.'$mode'['$index'].DEFAULTOPTION' | sed 's/\"//g'`
NEWACCOUNT="${GETHPATH} --datadir ${DATAPATH} account new"
if [ -n $BOOTENODEPATH ] ; then
BOOTENODEPATH="./" # default value
fi
# Get geth's pid list
PIDS=`pgrep geth`
# Command processing
case $1 in
"init")
${GETHPATH} init --datadir ${DATAPATH} ${GENESIS} ;;
"account")
${NEWACCOUNT} ;;
"attach")
${GETHPATH} attach $DATAPATH/geth.ipc ;;
"log")
tail -F ${DATAPATH}/geth.log ;;
"stop")
check_file ${DATAPATH}/pid.txt
check_directory $DATAPATH
PID=`cat ${DATAPATH}/pid.txt`
echo $DATAPATH
echo "> Stop geth pid:${PID}"
kill -9 ${PID}
rm -rf ${DATAPATH}/pid.txt
;;
"status")
if [[ "$PIDS" =~ ${PID} ]] ; then
echo "$2$index is running"
fi
;;
"start")
check_file ${DATAPATH}/pid.txt
check_directory $DATAPATH
PID=`cat ${DATAPATH}/pid.txt`
if [[ "$PIDS" =~ ${PID} ]] ; then
echo "$2$index is already running"
exit 100
fi
ENODENAME="enode"
case $2 in
"boot")
echo "> Start bootstrap"
BOOT="${GETH}PATH --datadir ${DATAPATH} --networkid ${NETWORKID} --nat "extip:${HTTPADDR}" --port ${PORT} --authrpc.port ${AUTHPORT}"
ENODENAME="boot$ENODENAME"
echo ">> $BOOT"
nohup ${BOOT} >> ${DATAPATH}/geth.log 2>&1 & ;;
"peer")
echo "> Start membership node"
BOOTENODE=`cat ${BOOTENODEPATH}/bootenode.json | jq '.enode' | sed 's/\"//g'`
PEER="${GETHPATH} --datadir ${DATAPATH} --http --http.port ${HTTPPORT} --http.api ${HTTPAPI} --http.addr ${HTTPADDR} --networkid ${NETWORKID} ${DEFAULTOPTION} --password ${PWDPATH} --bootnodes ${BOOTENODE} --port ${PORT} --authrpc.port ${AUTHPORT}"
echo ">> $PEER"
nohup ${PEER} >> ${DATAPATH}/geth.log 2>&1 & ;;
esac
echo $! > ${DATAPATH}/pid.txt
echo "pid : $!"
sleep 2
ENODE=`${GETHPATH} attach --exec admin.nodeInfo.enr ${DATAPATH}/geth.ipc`
echo $(generate_json ${ENODE}) > $ENODENAME.json
echo "enode : ${ENODE}" ;;
esac