-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.sh
executable file
·243 lines (213 loc) · 6.39 KB
/
cli.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# 工具环境变量
export FABRIC_CFG_PATH=${PWD}
# 打印帮助
function printHelp () {
echo "使用说明:"
echo " cli.sh -m start|stop|restart|clean|generate [-c <channel>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>]"
echo " cli.sh -h|--help (print this message)"
echo " -m <mode> - 子命令有 'start', 'stop', 'restart', 'clean' 或 'generate'"
echo " - 'start' - 启动网络服务"
echo " - 'stop' - 停止网络服务"
echo " - 'restart' - 重启网络服务"
echo " - 'clean' - 清理/删除网络服务"
echo " - 'generate' - 生成所需的网络服务、证书以及创世块"
echo " -c <channel> - 要使用的通道名 (默认为 \"channel_lab\")"
echo " -t <timeout> - 命令超时时间,单位:秒 (默认为 10)"
echo " -d <delay> - 命令延时等待时间,单位:秒 (默认为 3)"
echo " -f <docker-compose-file> - 指定要使用的 docker-compose 文件 (默认为 \"docker-compose.yaml\")"
echo
}
function confirm () {
read -p "是否继续 (y/n)? " ans
case "$ans" in
y|Y )
echo "继续 ..."
;;
n|N )
echo "退出..."
exit 1
;;
* )
confirm
;;
esac
}
# 生成证书
function generateCerts (){
which cryptogen
if [ "$?" -ne 0 ]; then
echo "cryptogen tool not found. exiting"
exit 1
fi
echo
echo "##########################################################"
echo "##### Generate certificates using cryptogen tool #########"
echo "##########################################################"
if [ -d "crypto-config" ]; then
rm -Rf crypto-config
fi
cryptogen generate --config=./crypto-config.yaml
if [ "$?" -ne 0 ]; then
echo "Failed to generate certificates..."
exit 1
fi
echo
}
# 生成创世块、通道交易配置以及节点更新交易
function generateChannelArtifacts() {
which configtxgen
if [ "$?" -ne 0 ]; then
echo "configtxgen tool not found. exiting"
exit 1
fi
if [ ! -d "channel-artifacts" ]; then
mkdir channel-artifacts
fi
echo "##########################################################"
echo "######### Generating Orderer Genesis block ##############"
echo "##########################################################"
configtxgen -profile OrdererGenesis -outputBlock ./channel-artifacts/genesis.block
if [ "$?" -ne 0 ]; then
echo "Failed to generate orderer genesis block..."
exit 1
fi
echo
echo "#################################################################"
echo "### Generating channel configuration transaction 'channel.tx' ###"
echo "#################################################################"
configtxgen -profile OrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
if [ "$?" -ne 0 ]; then
echo "Failed to generate channel configuration transaction..."
exit 1
fi
for OrgMSP in BankMSP OwnerMSP StorageMSP SupervisorMSP; do
echo
echo "#################################################################"
echo "####### Generating anchor peer update for ${OrgMSP} ##########"
echo "#################################################################"
configtxgen -profile OrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/${OrgMSP}anchors.tx -channelID $CHANNEL_NAME -asOrg ${OrgMSP}
if [ "$?" -ne 0 ]; then
echo "Failed to generate anchor peer update for ${OrgMSP}..."
exit 1
fi
done
echo
}
function replacePrivateKey () {
# MaxOSX 特殊
ARCH=`uname -s | grep Darwin`
if [ "$ARCH" == "Darwin" ]; then
OPTS="-it"
else
OPTS="-i"
fi
cp docker-compose-tpl.yaml $COMPOSE_FILE
CURRENT_DIR=$PWD
cd crypto-config/peerOrganizations/bank.samples.cn/ca/
PRIV_KEY=$(ls *_sk)
cd "$CURRENT_DIR"
sed $OPTS "s/CA_BANK_PRIVATE_KEY/${PRIV_KEY}/g" $COMPOSE_FILE
cd crypto-config/peerOrganizations/owner.samples.cn/ca/
PRIV_KEY=$(ls *_sk)
cd "$CURRENT_DIR"
sed $OPTS "s/CA_OWNER_PRIVATE_KEY/${PRIV_KEY}/g" $COMPOSE_FILE
cd crypto-config/peerOrganizations/storage.samples.cn/ca/
PRIV_KEY=$(ls *_sk)
cd "$CURRENT_DIR"
sed $OPTS "s/CA_STORAGE_PRIVATE_KEY/${PRIV_KEY}/g" $COMPOSE_FILE
cd crypto-config/peerOrganizations/supervisor.samples.cn/ca/
PRIV_KEY=$(ls *_sk)
cd "$CURRENT_DIR"
sed $OPTS "s/CA_SUPERVISOR_PRIVATE_KEY/${PRIV_KEY}/g" $COMPOSE_FILE
# MaxOSX 特殊
if [ "$ARCH" == "Darwin" ]; then
rm -f ${COMPOSE_FILE}t
fi
}
# 启动网络
function networkStart () {
CHANNEL_NAME=$CHANNEL_NAME TIMEOUT=$CLI_TIMEOUT DELAY=$CLI_DELAY docker-compose -f $COMPOSE_FILE up -d
if [ $? -ne 0 ]; then
echo "ERROR !!!! Unable to start network"
exit 1
fi
}
# 停止网络
function networkStop () {
docker-compose -f $COMPOSE_FILE stop
}
# 清理网络和相关资源
function cleanAll () {
if [ -a "$COMPOSE_FILE" ]; then
docker-compose -f $COMPOSE_FILE down
rm -f $COMPOSE_FILE
fi
if [ -d "channel-artifacts" ]; then
rm -fr channel-artifacts
fi
if [ -d "crypto-config" ]; then
rm -fr crypto-config
fi
}
#######################
# 变量初始
OS_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')
CLI_TIMEOUT=10
CLI_DELAY=3
CHANNEL_NAME="channel_lab"
COMPOSE_FILE=docker-compose.yaml
# 读取参数
while getopts "h?m:c:t:d:f:" opt; do
case "$opt" in
h|\?)
printHelp
exit 0
;;
m) MODE=$OPTARG
;;
c) CHANNEL_NAME=$OPTARG
;;
t) CLI_TIMEOUT=$OPTARG
;;
d) CLI_DELAY=$OPTARG
;;
f) COMPOSE_FILE=$OPTARG
;;
esac
done
if [ "$MODE" == "start" ]; then
EXPMODE="Starting"
elif [ "$MODE" == "stop" ]; then
EXPMODE="Stopping"
elif [ "$MODE" == "restart" ]; then
EXPMODE="Restarting"
elif [ "$MODE" == "clean" ]; then
EXPMODE="Cleaning"
elif [ "$MODE" == "generate" ]; then
EXPMODE="Generating network, certs and genesis block"
else
printHelp
exit 1
fi
echo "${EXPMODE} with channel '${CHANNEL_NAME}' and CLI timeout of '${CLI_TIMEOUT}'"
# confirm
# 执行子命令
if [ "${MODE}" == "generate" ]; then
cleanAll
generateCerts
generateChannelArtifacts
replacePrivateKey
elif [ "${MODE}" == "start" ]; then
networkStart
elif [ "${MODE}" == "stop" ]; then
networkStop
elif [ "${MODE}" == "clean" ]; then
cleanAll
elif [ "${MODE}" == "restart" ]; then
networkStop
networkStart
else
printHelp
exit 1
fi