Skip to content

Commit be685f7

Browse files
author
Vadym Mudryi
committed
Added cleanup script
1 parent 34379f1 commit be685f7

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Please note that not all features from the Docker Swarm solution are supported y
2020

2121
This repository is used to store infrastructure code for deploying OpenCRVS.
2222

23-
24-
2523
---
2624

2725

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
#
5+
# OpenCRVS is also distributed under the terms of the Civil Registration
6+
# & Healthcare Disclaimer located at http://opencrvs.org/license.
7+
#
8+
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
9+
10+
set -e
11+
12+
13+
print_usage_and_exit () {
14+
echo 'Usage: ./clear-all-data.sh [-r REPLICAS] [-m MONGODB_HOST] [-e ELASTICSEARCH_HOST] [-n MINIO_HOST]'
15+
echo ""
16+
echo "Options:"
17+
echo " -n NAMESPACE Kubernetes namespace (default: opencrvs-dev)"
18+
echo " -r REPLICAS Number of MongoDB replicas"
19+
echo " -m MONGODB_HOST MongoDB host (default: mongo)"
20+
echo " -e ELASTICSEARCH_HOST Elasticsearch host (default: elasticsearch:9200)"
21+
echo " -s MINIO_HOST MinIO host (default: minio:3535)"
22+
echo " -i INFLUX_HOST InfluxDB host (default: influxdb)"
23+
echo ""
24+
echo "If your MongoDB is password protected, an admin user's credentials can be given as environment variables:"
25+
echo "MONGODB_ADMIN_USER=your_user MONGODB_ADMIN_PASSWORD=your_pass"
26+
echo ""
27+
echo "If your Elasticsearch is password protected, an admin user's credentials can be given as environment variables:"
28+
echo "ELASTICSEARCH_ADMIN_USER=your_user ELASTICSEARCH_ADMIN_PASSWORD=your_pass"
29+
exit 1
30+
}
31+
32+
if [ -z "$1" ] ; then
33+
echo 'Error: Argument REPLICAS is required in position 1.'
34+
print_usage_and_exit
35+
fi
36+
37+
# Default values
38+
REPLICAS="0"
39+
MONGODB_HOST="mongodb"
40+
ELASTICSEARCH_HOST="elasticsearch"
41+
MINIO_HOST="minio"
42+
MINIO_ROOT_USER=minioadmin
43+
MINIO_ROOT_PASSWORD=minioadmin
44+
INFLUX_HOST="influxdb"
45+
while getopts "n:r:m:e:s:i:" opt; do
46+
case $opt in
47+
n) NAMESPACE=$OPTARG ;;
48+
r) REPLICAS=$OPTARG ;;
49+
m) MONGODB_HOST=$OPTARG ;;
50+
e) ELASTICSEARCH_HOST=$OPTARG ;;
51+
s) MINIO_HOST=$OPTARG ;;
52+
i) INFLUX_HOST=$OPTARG ;;
53+
*) print_usage_and_exit ;;
54+
esac
55+
done
56+
57+
if ! [[ "$REPLICAS" =~ ^[0-9]+$ ]]; then
58+
echo "'Error: Script must be passed a positive integer number of replicas. Got '$REPLICAS'"
59+
print_usage_and_exit
60+
fi
61+
62+
if [ "$REPLICAS" = "0" ]; then
63+
HOST=$MONGODB_HOST
64+
echo "Working with no replicas"
65+
else
66+
# TODO: THIS PART IS NOT TESTED YET
67+
# Construct the HOST string rs0/mongo1,mongo2... based on the number of replicas
68+
HOST=""
69+
for (( i=1; i<=REPLICAS; i++ )); do
70+
if [ $i -gt 1 ]; then
71+
HOST="${HOST},"
72+
fi
73+
HOST="${HOST}mongo${i}"
74+
done
75+
fi
76+
77+
mongo_credentials() {
78+
if [ ! -z ${MONGODB_ADMIN_USER+x} ] || [ ! -z ${MONGODB_ADMIN_PASSWORD+x} ]; then
79+
echo "--username $MONGODB_ADMIN_USER --password $MONGODB_ADMIN_PASSWORD --authenticationDatabase admin";
80+
else
81+
echo "";
82+
fi
83+
}
84+
85+
elasticsearch_host() {
86+
if [ ! -z ${ELASTICSEARCH_ADMIN_USER+x} ] || [ ! -z ${ELASTICSEARCH_ADMIN_PASSWORD+x} ]; then
87+
echo "$ELASTICSEARCH_ADMIN_USER:$ELASTICSEARCH_ADMIN_PASSWORD@$ELASTICSEARCH_HOST:9200";
88+
else
89+
echo "$ELASTICSEARCH_HOST:9200";
90+
fi
91+
}
92+
93+
drop_database () {
94+
local database=${1}
95+
kubectl run mongo-drop-job --rm -i --image=mongo:4.4 --restart=Never -- \
96+
mongo $database $(mongo_credentials) --host $HOST --eval "db.dropDatabase()"
97+
}
98+
99+
# Delete all data from mongo
100+
#---------------------------
101+
drop_database hearth-dev;
102+
103+
drop_database openhim-dev;
104+
105+
drop_database user-mgnt;
106+
107+
drop_database application-config;
108+
109+
drop_database metrics;
110+
111+
drop_database performance;
112+
113+
# Delete all data from elasticsearch
114+
#-----------------------------------
115+
# TODO: FIX CALL TO INDEX ALIAS
116+
kubectl run elasticsearch-delete-job --rm -i --image=appropriate/curl --restart=Never -- \
117+
curl -XDELETE "http://$(elasticsearch_host)/ocrvs" -v
118+
119+
# Delete all data from metrics
120+
#-----------------------------
121+
kubectl run influxdb-drop-job --rm -i --image=appropriate/curl --restart=Never -- \
122+
curl -X POST http://$INFLUX_HOST:8086/query?db=ocrvs --data-urlencode "q=DROP SERIES FROM /.*/" -v
123+
124+
# Delete all data from minio
125+
#-----------------------------
126+
kubectl run minio-delete-job --rm -i --image=minio/mc --restart=Never --command -- sh -c "\
127+
mc alias set myminio http://$MINIO_HOST:3535 $MINIO_ROOT_USER $MINIO_ROOT_PASSWORD && \
128+
mc rm --recursive --force myminio/ocrvs && \
129+
mc rb myminio/ocrvs && \
130+
mc mb myminio/ocrvs"
131+
132+
# Restart the metabase service
133+
#-----------------------------
134+
# kubectl scale deployment opencrvs-dashboards --replicas=0
135+
# kubectl scale deployment opencrvs-dashboards --replicas=1

0 commit comments

Comments
 (0)