-
Notifications
You must be signed in to change notification settings - Fork 3
/
restart.sh
executable file
·158 lines (129 loc) · 3.72 KB
/
restart.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
#!/bin/bash
[[ -e scripts/util.sh ]] || { echo >&2 "Please cd into repositories main directory before running this script."; exit 1; }
source ./scripts/util.sh
print_usage() {
printf "🧙 Usage: ./restart [-d] [-t] [-b <version|release>]\n"
printf "Use -b to specify the version|release to use (default is latest)\n"
printf "Use -t for test mode\n"
printf "Use -d to reset all clusters\n"
}
PARAMS=""
while getopts 'vtdb:' flag; do
case "${flag}" in
b) CHAINCODE_VERSION_PARAM="${OPTARG}"
PARAMS="$PARAMS -b $CHAINCODE_VERSION_PARAM"
printf 'Using chaincode version|release "%s"\n' "$CHAINCODE_VERSION_PARAM" ;;
t) PARAMS="$PARAMS -t"
printf 'Using test mode\n' ;;
d) printf "🐼 Delete all clusters and files\n"
set +e
kind delete clusters cluster-1 cluster-2
sudo rm -rf /data/development-1
sudo rm -rf /data/development-2
printf "🥳 Done!\n"
exit 1 ;;
?) print_usage
exit 1 ;;
esac
done
echo $PARAMS
####################################################################################################
# Functions
####################################################################################################
function createCluster() {
set +e
sudo rm -rf /data/development-$1
set -e
if [[ "$2" == "quiet" ]]
then
kind create cluster \
--config assets/kind-$1.yaml \
--name "cluster-$1" \
--kubeconfig $(mktemp)\
>>log.txt 2>&1
echo "Successfully created cluster $1" >> log.txt
else
kind create cluster \
--config assets/kind-$1.yaml \
--name "cluster-$1" \
--kubeconfig $(mktemp)
fi
}
function createNextCluster() {
if [[ "$1" == "again" ]]
then
echo "😰 No speedup, need to create next cluster..."
fi
set +e
kind delete clusters cluster-$NEXT
set -e
if [[ "$NEXT" == "1" ]]
then
createCluster 1
else
createCluster 2
fi
}
function restartOldClusterInBackground() {
echo "🤯 Restart old cluster in background..."
kind delete clusters cluster-$LAST>>log.txt 2>&1
if [[ "$LAST" == "1" ]]
then
createCluster 1 quiet
else
createCluster 2 quiet
fi
printf "\n--------------------------------------------------------------------------------\n😎 Backup cluster is ready!\n👉 You can now fast restart the cluster!\n--------------------------------------------------------------------------------\n"
}
####################################################################################################
# Main script
####################################################################################################
# Prompt password
header "🤖 Fast restart script (for development only)"
sudo printf ""
LAST_CONTEXT=""
set +e
rm log.txt
LAST_CONTEXT=$(kubectl config current-context)
set -e
NEXT=1
LAST=2
# Check if cluster 1 was last used
if [[ "$LAST_CONTEXT" == *"1" ]]
then
LAST=1
NEXT=2
fi
NEXT_PATH=/data/development-$NEXT
restartOldClusterInBackground &
# For better ux
sleep 1
# Ensure next cluster exists
CLUSTERS=$(kind get clusters)
printf "🐨 Ensure next cluster is available\n"
small_sep
if [[ "$CLUSTERS" != *"cluster-$NEXT"* ]]
then
echo "😀 Create initial cluster..."
createNextCluster
fi
(
kind export kubeconfig --name "cluster-$NEXT" &&\
kubectl wait --for=condition=Ready node --all --timeout 30s
) || (
# Try again
echo "😱 Cluster not working" &&\
echo "🙄 Restart cluster..." &&\
createNextCluster again &&\
kind export kubeconfig --name "cluster-$NEXT"
) || exit 1
sudo mkdir -p $NEXT_PATH/hyperledger
sudo chmod -R 777 $NEXT_PATH
set -e
small_sep
echo "🦥 Cluster ready!"
echo "😎 Deploy network..."
small_sep
./deploy.sh -c $NEXT_PATH/hyperledger $PARAMS
wait
printf "🥳 Done!\n"