-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
test_network.sh
executable file
·204 lines (164 loc) · 5.6 KB
/
test_network.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
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
function launch_orderers() {
push_fn "Launching orderers"
apply_template kube/org0/org0-orderer1.yaml $ORG0_NS
apply_template kube/org0/org0-orderer2.yaml $ORG0_NS
apply_template kube/org0/org0-orderer3.yaml $ORG0_NS
kubectl -n $ORG0_NS rollout status deploy/org0-orderer1
kubectl -n $ORG0_NS rollout status deploy/org0-orderer2
kubectl -n $ORG0_NS rollout status deploy/org0-orderer3
pop_fn
}
function launch_peers() {
push_fn "Launching peers"
apply_template kube/org1/org1-peer1.yaml $ORG1_NS
apply_template kube/org1/org1-peer2.yaml $ORG1_NS
apply_template kube/org2/org2-peer1.yaml $ORG2_NS
apply_template kube/org2/org2-peer2.yaml $ORG2_NS
kubectl -n $ORG1_NS rollout status deploy/org1-peer1
kubectl -n $ORG1_NS rollout status deploy/org1-peer2
kubectl -n $ORG2_NS rollout status deploy/org2-peer1
kubectl -n $ORG2_NS rollout status deploy/org2-peer2
pop_fn
}
# Each network node needs a registration, enrollment, and MSP config.yaml
function create_node_local_MSP() {
local node_type=$1
local org=$2
local node=$3
local csr_hosts=$4
local ns=$5
local id_name=${org}-${node}
local id_secret=${node_type}pw
local ca_name=${org}-ca
# Register the node admin
rc=0
fabric-ca-client register \
--id.name ${id_name} \
--id.secret ${id_secret} \
--id.type ${node_type} \
--url https://${ca_name}.${DOMAIN}:${NGINX_HTTPS_PORT} \
--tls.certfiles $TEMP_DIR/cas/${ca_name}/tlsca-cert.pem \
--mspdir $TEMP_DIR/enrollments/${org}/users/${RCAADMIN_USER}/msp \
|| rc=$? # trap error code from registration without exiting the network driver script"
if [ $rc -eq 1 ]; then
echo "CA admin was (probably) previously registered - continuing"
fi
# Enroll the node admin user from within k8s. This will leave the certificates available on a volume share in the
# cluster for access by the nodes when launching in a container.
cat <<EOF | kubectl -n ${ns} exec deploy/${ca_name} -i -- /bin/sh
set -x
export FABRIC_CA_CLIENT_HOME=/var/hyperledger/fabric-ca-client
export FABRIC_CA_CLIENT_TLS_CERTFILES=/var/hyperledger/fabric/config/tls/ca.crt
fabric-ca-client enroll \
--url https://${id_name}:${id_secret}@${ca_name} \
--csr.hosts ${csr_hosts} \
--mspdir /var/hyperledger/fabric/organizations/${node_type}Organizations/${org}.example.com/${node_type}s/${id_name}.${org}.example.com/msp
# Create local MSP config.yaml
echo "NodeOUs:
Enable: true
ClientOUIdentifier:
Certificate: cacerts/${org}-ca.pem
OrganizationalUnitIdentifier: client
PeerOUIdentifier:
Certificate: cacerts/${org}-ca.pem
OrganizationalUnitIdentifier: peer
AdminOUIdentifier:
Certificate: cacerts/${org}-ca.pem
OrganizationalUnitIdentifier: admin
OrdererOUIdentifier:
Certificate: cacerts/${org}-ca.pem
OrganizationalUnitIdentifier: orderer" > /var/hyperledger/fabric/organizations/${node_type}Organizations/${org}.example.com/${node_type}s/${id_name}.${org}.example.com/msp/config.yaml
EOF
}
function create_orderer_local_MSP() {
local org=$1
local orderer=$2
local csr_hosts=${org}-${orderer}
create_node_local_MSP orderer $org $orderer $csr_hosts $ORG0_NS
}
function create_peer_local_MSP() {
local org=$1
local peer=$2
local ns=$3
local csr_hosts=localhost,${org}-${peer},${org}-peer-gateway-svc
create_node_local_MSP peer $org $peer $csr_hosts ${ns}
}
function create_local_MSP() {
push_fn "Creating local node MSP"
create_orderer_local_MSP org0 orderer1
create_orderer_local_MSP org0 orderer2
create_orderer_local_MSP org0 orderer3
create_peer_local_MSP org1 peer1 $ORG1_NS
create_peer_local_MSP org1 peer2 $ORG1_NS
create_peer_local_MSP org2 peer1 $ORG2_NS
create_peer_local_MSP org2 peer2 $ORG2_NS
pop_fn
}
function network_up() {
# Kube config
init_namespace
init_storage_volumes
load_org_config
# Service account permissions for the k8s builder
if [ "${CHAINCODE_BUILDER}" == "k8s" ]; then
apply_k8s_builder_roles
apply_k8s_builders
fi
# Network TLS CAs
init_tls_cert_issuers
# Network ECert CAs
launch_ECert_CAs
enroll_bootstrap_ECert_CA_users
# Test Network
create_local_MSP
launch_orderers
launch_peers
}
function stop_services() {
push_fn "Stopping Fabric services"
for ns in $ORG0_NS $ORG1_NS $ORG2_NS; do
kubectl -n $ns delete ingress --all
kubectl -n $ns delete deployment --all
kubectl -n $ns delete pod --all
kubectl -n $ns delete service --all
kubectl -n $ns delete configmap --all
kubectl -n $ns delete cert --all
kubectl -n $ns delete issuer --all
kubectl -n $ns delete secret --all
done
pop_fn
}
function scrub_org_volumes() {
push_fn "Scrubbing Fabric volumes"
for org in org0 org1 org2; do
# clean job to make this function can be rerun
local namespace_variable=${org^^}_NS
kubectl -n ${!namespace_variable} delete jobs --all
# scrub all pv contents
kubectl -n ${!namespace_variable} create -f kube/${org}/${org}-job-scrub-fabric-volumes.yaml
kubectl -n ${!namespace_variable} wait --for=condition=complete --timeout=60s job/job-scrub-fabric-volumes
kubectl -n ${!namespace_variable} delete jobs --all
done
pop_fn
}
function network_down() {
set +e
for ns in $ORG0_NS $ORG1_NS $ORG2_NS; do
kubectl get namespace $ns > /dev/null
if [[ $? -ne 0 ]]; then
echo "No namespace $ns found - nothing to do."
return
fi
done
set -e
stop_services
scrub_org_volumes
delete_namespace
rm -rf $PWD/build
}