This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
/
setup_blockchainNetwork_v1.sh
215 lines (177 loc) · 8.23 KB
/
setup_blockchainNetwork_v1.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
#!/bin/bash
if [ -d "${PWD}/configFiles" ]; then
KUBECONFIG_FOLDER=${PWD}/configFiles
else
echo "Configuration files are not found."
exit
fi
# Create Docker deployment
if [ "$(cat configFiles/peersDeployment.yaml | grep -c tcp://docker:2375)" != "0" ]; then
echo "peersDeployment.yaml file was configured to use Docker in a container."
echo "Creating Docker deployment"
kubectl create -f ${KUBECONFIG_FOLDER}/docker-volume.yaml
kubectl create -f ${KUBECONFIG_FOLDER}/docker.yaml
sleep 5
dockerPodStatus=$(kubectl get pods --selector=name=docker --output=jsonpath={.items..phase})
while [ "${dockerPodStatus}" != "Running" ]; do
echo "Wating for Docker container to run. Current status of Docker is ${dockerPodStatus}"
sleep 5;
if [ "${dockerPodStatus}" == "Error" ]; then
echo "There is an error in the Docker pod. Please check logs."
exit 1
fi
dockerPodStatus=$(kubectl get pods --selector=name=docker --output=jsonpath={.items..phase})
done
fi
# Creating Persistant Volume
echo -e "\nCreating volume"
if [ "$(kubectl get pvc | grep shared-pvc | awk '{print $2}')" != "Bound" ]; then
echo "The Persistant Volume does not seem to exist or is not bound"
echo "Creating Persistant Volume"
if [ "$1" == "--paid" ]; then
echo "You passed argument --paid. Make sure you have an IBM Cloud Kubernetes - Standard tier. Else, remove --paid option"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/createVolume-paid.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/createVolume-paid.yaml
sleep 5
else
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/createVolume.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/createVolume.yaml
sleep 5
fi
if [ "kubectl get pvc | grep shared-pvc | awk '{print $3}'" != "shared-pv" ]; then
echo "Success creating Persistant Volume"
else
echo "Failed to create Persistant Volume"
fi
else
echo "The Persistant Volume exists, not creating again"
fi
# Copy the required files(configtx.yaml, cruypto-config.yaml, sample chaincode etc.) into volume
echo -e "\nCreating Copy artifacts job."
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/copyArtifactsJob.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/copyArtifactsJob.yaml
pod=$(kubectl get pods --selector=job-name=copyartifacts --output=jsonpath={.items..metadata.name})
podSTATUS=$(kubectl get pods --selector=job-name=copyartifacts --output=jsonpath={.items..phase})
while [ "${podSTATUS}" != "Running" ]; do
echo "Wating for container of copy artifact pod to run. Current status of ${pod} is ${podSTATUS}"
sleep 5;
if [ "${podSTATUS}" == "Error" ]; then
echo "There is an error in copyartifacts job. Please check logs."
exit 1
fi
podSTATUS=$(kubectl get pods --selector=job-name=copyartifacts --output=jsonpath={.items..phase})
done
echo -e "${pod} is now ${podSTATUS}"
echo -e "\nStarting to copy artifacts in persistent volume."
#fix for this script to work on icp and ICS
kubectl cp ./artifacts $pod:/shared/
echo "Waiting for 10 more seconds for copying artifacts to avoid any network delay"
sleep 10
JOBSTATUS=$(kubectl get jobs |grep "copyartifacts" |awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for copyartifacts job to complete"
sleep 1;
PODSTATUS=$(kubectl get pods | grep "copyartifacts" | awk '{print $3}')
if [ "${PODSTATUS}" == "Error" ]; then
echo "There is an error in copyartifacts job. Please check logs."
exit 1
fi
JOBSTATUS=$(kubectl get jobs |grep "copyartifacts" |awk '{print $3}')
done
echo "Copy artifacts job completed"
# Generate Network artifacts using configtx.yaml and crypto-config.yaml
echo -e "\nGenerating the required artifacts for Blockchain network"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/generateArtifactsJob.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/generateArtifactsJob.yaml
JOBSTATUS=$(kubectl get jobs |grep utils|awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for generateArtifacts job to complete"
sleep 1;
# UTILSLEFT=$(kubectl get pods | grep utils | awk '{print $2}')
UTILSSTATUS=$(kubectl get pods | grep "utils" | awk '{print $3}')
if [ "${UTILSSTATUS}" == "Error" ]; then
echo "There is an error in utils job. Please check logs."
exit 1
fi
# UTILSLEFT=$(kubectl get pods | grep utils | awk '{print $2}')
JOBSTATUS=$(kubectl get jobs |grep utils|awk '{print $3}')
done
# Create services for all peers, ca, orderer
echo -e "\nCreating Services for blockchain network"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/blockchain-services.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/blockchain-services.yaml
# Create peers, ca, orderer using Kubernetes Deployments
echo -e "\nCreating new Deployment to create four peers in network"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/peersDeployment.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/peersDeployment.yaml
echo "Checking if all deployments are ready"
NUMPENDING=$(kubectl get deployments | grep blockchain | awk '{print $5}' | grep 0 | wc -l | awk '{print $1}')
while [ "${NUMPENDING}" != "0" ]; do
echo "Waiting on pending deployments. Deployments pending = ${NUMPENDING}"
NUMPENDING=$(kubectl get deployments | grep blockchain | awk '{print $5}' | grep 0 | wc -l | awk '{print $1}')
sleep 1
done
echo "Waiting for 15 seconds for peers and orderer to settle"
sleep 15
# Generate channel artifacts using configtx.yaml and then create channel
echo -e "\nCreating channel transaction artifact and a channel"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/create_channel.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/create_channel.yaml
JOBSTATUS=$(kubectl get jobs |grep createchannel |awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for createchannel job to be completed"
sleep 1;
if [ "$(kubectl get pods | grep createchannel | awk '{print $3}')" == "Error" ]; then
echo "Create Channel Failed"
exit 1
fi
JOBSTATUS=$(kubectl get jobs |grep createchannel |awk '{print $3}')
done
echo "Create Channel Completed Successfully"
# Join all peers on a channel
echo -e "\nCreating joinchannel job"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/join_channel.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/join_channel.yaml
JOBSTATUS=$(kubectl get jobs |grep joinchannel |awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for joinchannel job to be completed"
sleep 1;
if [ "$(kubectl get pods | grep joinchannel | awk '{print $3}')" == "Error" ]; then
echo "Join Channel Failed"
exit 1
fi
JOBSTATUS=$(kubectl get jobs |grep joinchannel |awk '{print $3}')
done
echo "Join Channel Completed Successfully"
# Install chaincode on each peer
echo -e "\nCreating installchaincode job"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/chaincode_install.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/chaincode_install.yaml
JOBSTATUS=$(kubectl get jobs |grep chaincodeinstall |awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for chaincodeinstall job to be completed"
sleep 1;
if [ "$(kubectl get pods | grep chaincodeinstall | awk '{print $3}')" == "Error" ]; then
echo "Chaincode Install Failed"
exit 1
fi
JOBSTATUS=$(kubectl get jobs |grep chaincodeinstall |awk '{print $3}')
done
echo "Chaincode Install Completed Successfully"
# Instantiate chaincode on channel
echo -e "\nCreating chaincodeinstantiate job"
echo "Running: kubectl create -f ${KUBECONFIG_FOLDER}/chaincode_instantiate.yaml"
kubectl create -f ${KUBECONFIG_FOLDER}/chaincode_instantiate.yaml
JOBSTATUS=$(kubectl get jobs |grep chaincodeinstantiate |awk '{print $3}')
while [ "${JOBSTATUS}" != "1" ]; do
echo "Waiting for chaincodeinstantiate job to be completed"
sleep 1;
if [ "$(kubectl get pods | grep chaincodeinstantiate | awk '{print $3}')" == "Error" ]; then
echo "Chaincode Instantiation Failed"
exit 1
fi
JOBSTATUS=$(kubectl get jobs |grep chaincodeinstantiate |awk '{print $3}')
done
echo "Chaincode Instantiation Completed Successfully"
sleep 15
echo -e "\nNetwork Setup Completed !!"