forked from GaloyMoney/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initLocalTest.sh
executable file
·227 lines (179 loc) · 7.45 KB
/
initLocalTest.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
set -e
# for helm < 3.4
# https://helm.sh/blog/new-location-stable-incubator-charts/
helm repo add stable https://charts.helm.sh/stable --force-update
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo add jetstack https://charts.jetstack.io
helm repo add galoy https://galoymoney.github.io/charts/
helm repo update
lndVersion="1.2.3"
bitcoindVersion="0.1.15"
if [ ${LOCAL} ]
then
localdevpath="--skip-refresh"
fi
cd ./charts/galoy
helm dependency build $localdevpath
cd -
cd ./charts/monitoring
helm dependency build $localdevpath
cd -
INGRESS_NAMESPACE="ingress-nginx"
INFRADIR=./charts
backupMongodb () {
JOB_DATE=$(date -u '+%s')
kubectl -n=$NAMESPACE create job --from=cronjob/mongo-backup "$JOB_DATE"
kubectl -n=$NAMESPACE wait --for=condition=complete --timeout=120s job/$JOB_DATE
kubectl -n=$NAMESPACE delete job/$JOB_DATE
}
if [ "$1" == "testnet" ] || [ "$1" == "mainnet" ];
then
NETWORK="$1"
NAMESPACE="$1"
backupMongodb
# create namespaces if not exists
kubectl create namespace $INGRESS_NAMESPACE --dry-run -o yaml | kubectl apply -f -
kubectl create namespace cert-manager --dry-run -o yaml | kubectl apply -f -
helm -n cert-manager upgrade -i cert-manager jetstack/cert-manager --set installCRDs=true --version=v1.2.0
# Uncomment the following line if not using Google cloud and enter a static ip obtained from your cloud provider
# export STATIC_IP=xxx.xxx.xxx.xxx
# Comment the following line if not using Google cloud
export STATIC_IP=$(gcloud compute addresses list | awk '/nginx-ingress/ {print $2}')
helm -n $INGRESS_NAMESPACE upgrade -i ingress-nginx ingress-nginx/ingress-nginx --set controller.service.loadBalancerIP=$STATIC_IP \
-f $INFRADIR/ingress-nginx-values.yaml
else
NETWORK="regtest"
if [ ${LOCAL} ]; then
MINIKUBEIP=$(minikube ip)
NAMESPACE="default"
fi
fi
helmUpgrade () {
echo ""
echo ""
echo "---"
echo "executing upgrade: helm upgrade -i -n=$NAMESPACE $@"
command helm upgrade -i -n=$NAMESPACE "$@"
}
helmUpgradeDebug () {
echo ""
echo ""
echo "---"
echo "executing upgrade: helm install --dry-run --debug -n=$NAMESPACE $@ > debug.yaml"
command helm install --dry-run --debug -n=$NAMESPACE "$@" > debug.yaml
}
kubectlWait () {
echo "waiting for -n=$NAMESPACE -l $@"
sleep 6
kubectl wait -n=$NAMESPACE --for=condition=ready --timeout=1200s pod -l "$@"
}
kubectlLndDeletionWait () {
# if the lnd pod needs upgrade, we want to make sure we wait for it to be removed.
# otherwise it could be seen as ready by `kubectlWait app=lnd` while it could just have been in the process of still winding down
# we use || : to not return an error if the pod doesn't exist, or if no update is requiered (will timeout in this case)
# TODO: using --wait on upgrade would simplify this upgrade, but is currently running into some issues
echo "waiting for pod deletion"
kubectl wait -n=$NAMESPACE --for=delete --timeout=45s pod -l app.kubernetes.io/name=lnd || :
}
if [ ${LOCAL} ]
then
localdevpath="-f $INFRADIR/configs/bitcoind/localdev.yaml"
fi
rm -rf $INFRADIR/configs
git clone $CONFIG_REPO $INFRADIR/configs
helmUpgrade bitcoind $localdevpath -f $INFRADIR/configs/bitcoind/$NETWORK.yaml galoy/bitcoind --version=$bitcoindVersion
# bug with --wait: https://github.com/helm/helm/issues/7139 ?
kubectlWait app.kubernetes.io/name=bitcoind
sleep 8
if [ ${LOCAL} ]
then
kubectlLndDeletionWait
localdevpath="-f $INFRADIR/configs/lnd/localdev.yaml \
--set service.staticIP=$MINIKUBEIP"
localdevpathOutside="-f $INFRADIR/configs/lnd/localdev-outside.yaml \
--set service.staticIP=$MINIKUBEIP"
fi
rm -rf $INFRADIR/lnd
helm pull --version=$lndVersion galoy/lnd -d $INFRADIR/ --untar
cp "$INFRADIR/configs/lnd/RTL-Config.json" $INFRADIR/lnd/charts/rtl
kubectl apply -f $INFRADIR/configs/lnd/templates
helmUpgrade lnd --version=$lndVersion -f $INFRADIR/configs/lnd/$NETWORK.yaml $localdevpath $INFRADIR/lnd/
# avoiding to spend time with circleci regtest with this condition
if [ "$NETWORK" == "testnet" ] || [ "$NETWORK" == "mainnet" ];
then
kubectlLndDeletionWait
else
helmUpgrade lnd-outside-1 --version=$lndVersion -f $INFRADIR/configs/lnd/$NETWORK.yaml $localdevpathOutside $INFRADIR/lnd/
helmUpgrade lnd-outside-2 --version=$lndVersion -f $INFRADIR/configs/lnd/$NETWORK.yaml $localdevpathOutside $INFRADIR/lnd/
fi
# # add extra sleep time... seems lnd is quite long to show up some time
sleep 15
kubectlWait app.kubernetes.io/name=lnd
if [ ${LOCAL} ]
then
localdevpath="-f $INFRADIR/galoy/localdev.yaml"
fi
if [ "$NETWORK" == "testnet" ] || [ "$NETWORK" == "mainnet" ];
then
configpath="-f $INFRADIR/configs/galoy/$NETWORK.yaml"
else
configpath="-f $INFRADIR/galoy/$NETWORK.yaml"
fi
#FIXME: Fetch the entire secret once, then extract and decode the necessary fields
export MONGODB_ROOT_PASSWORD=$(kubectl get secret -n $NAMESPACE galoy-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 -d)
export MONGODB_PASSWORD=$(kubectl get secret -n $NAMESPACE galoy-mongodb -o jsonpath="{.data.mongodb-password}" | base64 -d)
export MONGODB_REPLICA_SET_KEY=$(kubectl get secret -n $NAMESPACE galoy-mongodb -o jsonpath="{.data.mongodb-replica-set-key}" | base64 -d)
# helmUpgradeDebug galoy \
# $configpath $localdevpath \
# --set mongodb.auth.password=$MONGODB_PASSWORD,mongodb.auth.rootPassword=$MONGODB_ROOT_PASSWORD,mongodb.auth.replicaSetKey=$MONGODB_REPLICA_SET_KEY,image.tag=$CIRCLE_SHA1 \
# $INFRADIR/galoy/
helmUpgrade galoy \
$configpath $localdevpath \
--set mongodb.auth.password=$MONGODB_PASSWORD,mongodb.auth.rootPassword=$MONGODB_ROOT_PASSWORD,mongodb.auth.replicaSetKey=$MONGODB_REPLICA_SET_KEY,image.tag=$CIRCLE_SHA1 \
$INFRADIR/galoy/
kubectlWait app.kubernetes.io/instance=galoy
if [ ${LOCAL} ]
then
exit 0
fi
if [ "$NETWORK" == "regtest" ]
then
kubectlWait app=testpod
fi
if [ "$NETWORK" == "testnet" ]
then
monitoringDeploymentsUpgrade() {
SECRET=alertmanager-keys
local NAMESPACE=monitoring
helmUpgrade monitoring $INFRADIR/monitoring \
--set prometheus-blackbox-exporter.config.modules.walletTestnetAuth.http.headers.Authorization="Bearer $TESTNET_TOKEN" \
--set prometheus-blackbox-exporter.config.modules.walletMainnetAuth.http.headers.Authorization="Bearer $MAINNET_TOKEN"
# FIXME: pass this directory to above command
export SLACK_API_URL=$(kubectl get secret -n $NAMESPACE $SECRET -o jsonpath="{.data.SLACK_API_URL}" | base64 -d)
export SERVICE_KEY=$(kubectl get secret -n $NAMESPACE $SECRET -o jsonpath="{.data.SERVICE_KEY}" | base64 -d)
kubectl -n $NAMESPACE get configmaps monitoring-prometheus-alertmanager -o yaml | sed -e "s|SLACK_API_URL|$SLACK_API_URL|; s|SERVICE_KEY|$SERVICE_KEY|" | kubectl -n $NAMESPACE apply -f -
}
monitoringDeploymentsUpgrade
fi
echo $(kubectl get -n=$NAMESPACE pods)
if [[ "$?" -ne 0 ]]; then
echo "Deployment for graphql failed"
exit 1
fi
if [ "$NETWORK" == "testnet" ] || [ "$NETWORK" == "mainnet" ];
then
kubectl -n $NAMESPACE annotate deployment graphql kubernetes.io/change-cause="$CIRCLE_SHA1-$(date -u)"
kubectl -n $NAMESPACE rollout status deployments/trigger
if [[ "$?" -ne 0 ]]; then
echo "Deployment for trigger failed"
exit 1
fi
kubectl -n $NAMESPACE rollout status deployments/exporter
if [[ "$?" -ne 0 ]]; then
echo "Deployment for exporter failed"
exit 1
fi
fi