forked from opencrvs/opencrvs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·156 lines (127 loc) · 7.2 KB
/
deploy.sh
File metadata and controls
executable file
·156 lines (127 loc) · 7.2 KB
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# OpenCRVS is also distributed under the terms of the Civil Registration
# & Healthcare Disclaimer located at http://opencrvs.org/license.
#
# Copyright (C) The OpenCRVS Authors. OpenCRVS and the OpenCRVS
# graphic logo are (registered/a) trademark(s) of Plan International.
set -e
print_usage_and_exit () {
echo 'Usage: ./deploy.sh --clear-data=yes|no --restore-metadata=yes|no --update-metadata=yes|no HOST ENV VERSION COUNTRY_CONFIG_VERSION COUNTRY_CONFIG_PATH'
echo " --clear-data must have a value of 'yes' or 'no' set e.g. --clear-data=yes"
echo " --restore-metadata must have a value of 'yes' or 'no' set e.g. --restore-metadata=yes"
echo " --update-metadata must have a value of 'yes' or 'no' set e.g. --update-metadata=yes"
echo " ENV can be 'production' or 'development' or 'qa'"
echo ' HOST is the server to deploy to'
echo " VERSION can be any OpenCRVS Core docker image tag or 'latest'"
echo " COUNTRY_CONFIG_VERSION can be any OpenCRVS Country Configuration docker image tag or 'latest'"
echo " COUNTRY_CONFIG_PATH path to where your resources package is located"
exit 1
}
if [ -z "$1" ] || { [ $1 != '--clear-data=no' ] && [ $1 != '--clear-data=yes' ] ;} ; then
echo 'Error: Argument --clear-data is required in position 1.'
print_usage_and_exit
fi
if [ -z "$2" ] || { [ $2 != '--restore-metadata=no' ] && [ $2 != '--restore-metadata=yes' ] ;} ; then
echo 'Error: Argument --restore-metadata is required in position 2.'
print_usage_and_exit
fi
if [ -z "$3" ] || { [ $3 != '--update-metadata=no' ] && [ $3 != '--update-metadata=yes' ] ;} ; then
echo 'Error: Argument --update-metadata is required in postition 3.'
print_usage_and_exit
fi
if [ -z "$4" ] ; then
echo 'Error: Argument ENV is required in position 4.'
print_usage_and_exit
fi
if [ -z "$5" ] ; then
echo 'Error: Argument HOST is required in position 5.'
print_usage_and_exit
fi
if [ -z "$6" ] ; then
echo 'Error: Argument VERSION is required in position 6.'
print_usage_and_exit
fi
if [ -z "$7" ] ; then
echo 'Error: Argument COUNTRY_CONFIG_VERSION is required in position 7.'
print_usage_and_exit
fi
if [ -z "$8" ] ; then
echo 'Error: Argument COUNTRY_CONFIG_PATH is required in position 8.'
print_usage_and_exit
fi
ENV=$4
HOST=$5
VERSION=$6
COUNTRY_CONFIG_VERSION=$7
COUNTRY_CONFIG_PATH=$8
SSH_USER=${SSH_USER:-root}
SSH_HOST=${SSH_HOST:-$HOST}
LOG_LOCATION=${LOG_LOCATION:-/var/log}
# Netdata user and passwrod
NETDATA_USER=${NETDATA_USER:-monitor}
NETDATA_PASSWORD=${NETDATA_PASSWORD:-monitor-password}
NETDATA_USER_DETAILS_BASE64=`echo $(htpasswd -nb $NETDATA_USER $NETDATA_PASSWORD) | base64`
echo $NETDATA_USER $NETDATA_PASSWORD $NETDATA_USER_DETAILS_BASE64
echo
echo "Deploying VERSION $VERSION to $SSH_HOST..."
echo
echo "Deploying COUNTRY_CONFIG_VERSION $COUNTRY_CONFIG_VERSION to $SSH_HOST..."
echo
mkdir -p /tmp/compose/infrastructure/default_backups
mkdir -p /tmp/compose/infrastructure/default_updates
# Copy selected country default backups to infrastructure default_backups folder
cp $COUNTRY_CONFIG_PATH/backups/hearth-dev.gz /tmp/compose/infrastructure/default_backups/hearth-dev.gz
cp $COUNTRY_CONFIG_PATH/backups/openhim-dev.gz /tmp/compose/infrastructure/default_backups/openhim-dev.gz
cp $COUNTRY_CONFIG_PATH/backups/user-mgnt.gz /tmp/compose/infrastructure/default_backups/user-mgnt.gz
cp $COUNTRY_CONFIG_PATH/backups/application-config.gz /tmp/compose/infrastructure/default_backups/application-config.gz
# Copy selected country default updates to infrastructure default_updates folder
[[ -d $COUNTRY_CONFIG_PATH/updates/generated ]] && cp $COUNTRY_CONFIG_PATH/updates/generated/*.json /tmp/compose/infrastructure/default_updates
# Copy all infrastructure files to the server
rsync -rP docker-compose* infrastructure $SSH_USER@$SSH_HOST:/tmp/compose/
# Copy all country compose files to the server
rsync -rP $COUNTRY_CONFIG_PATH/docker-compose.countryconfig* infrastructure $SSH_USER@$SSH_HOST:/tmp/compose/
# Override configuration files with country specific files
rsync -rP /tmp/compose/infrastructure $SSH_USER@$SSH_HOST:/tmp/compose
# Prepare docker-compose.deploy.yml and docker-compose.<COUNTRY>.yml file - rotate secrets etc
if [[ "$ENV" = "development" ]]; then
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/rotate-secrets.sh /tmp/compose/docker-compose.deploy.yml /tmp/compose/docker-compose.countryconfig.deploy.yml | tee -a '$LOG_LOCATION'/rotate-secrets.log'
elif [[ "$ENV" = "qa" ]]; then
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/rotate-secrets.sh /tmp/compose/docker-compose.deploy.yml /tmp/compose/docker-compose.qa-deploy.yml /tmp/compose/docker-compose.countryconfig.deploy.yml | tee -a '$LOG_LOCATION'/rotate-secrets.log'
else
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/rotate-secrets.sh /tmp/compose/docker-compose.deploy.yml /tmp/compose/docker-compose.prod-deploy.yml /tmp/compose/docker-compose.countryconfig.deploy.yml | tee -a '$LOG_LOCATION'/rotate-secrets.log'
fi
# Setup configuration files and compose file for the deployment domain
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/setup-deploy-config.sh '$HOST' '$NETDATA_USER_DETAILS_BASE64' | tee -a '$LOG_LOCATION'/setup-deploy-config.log'
# Deploy the OpenCRVS stack onto the swarm
if [[ "$ENV" = "development" ]]; then
ssh $SSH_USER@$SSH_HOST 'cd /tmp/compose && HOSTNAME='$HOST' VERSION='$VERSION' COUNTRY_CONFIG_VERSION='$COUNTRY_CONFIG_VERSION' PAPERTRAIL='$PAPERTRAIL' docker stack deploy -c docker-compose.deps.yml -c docker-compose.yml -c docker-compose.deploy.yml -c docker-compose.countryconfig.deploy.yml --with-registry-auth opencrvs'
elif [[ "$ENV" = "qa" ]]; then
ssh $SSH_USER@$SSH_HOST 'cd /tmp/compose && HOSTNAME='$HOST' VERSION='$VERSION' COUNTRY_CONFIG_VERSION='$COUNTRY_CONFIG_VERSION' PAPERTRAIL='$PAPERTRAIL' docker stack deploy -c docker-compose.deps.yml -c docker-compose.yml -c docker-compose.deploy.yml -c docker-compose.qa-deploy.yml -c docker-compose.countryconfig.deploy.yml -c docker-compose.countryconfig.qa-deploy.yml --with-registry-auth opencrvs'
else
ssh $SSH_USER@$SSH_HOST 'cd /tmp/compose && HOSTNAME='$HOST' VERSION='$VERSION' COUNTRY_CONFIG_VERSION='$COUNTRY_CONFIG_VERSION' PAPERTRAIL='$PAPERTRAIL' docker stack deploy -c docker-compose.deps.yml -c docker-compose.yml -c docker-compose.deploy.yml -c docker-compose.prod-deploy.yml -c docker-compose.countryconfig.deploy.yml --with-registry-auth opencrvs'
fi
echo
echo "Waiting 2 mins for stack to deploy before working with data..."
echo
sleep 120
if [ $1 == "--clear-data=yes" ] ; then
echo
echo "Clearing all existing data..."
echo
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/clear-all-data.sh'
fi
if [ $2 == "--restore-metadata=yes" ] ; then
echo
echo "Restoring metadata..."
echo
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/restore-metadata.sh'
fi
if [ $3 == "--update-metadata=yes" ] ; then
echo
echo "Updating existing metadata..."
echo
ssh $SSH_USER@$SSH_HOST '/tmp/compose/infrastructure/restore-metadata-updates.sh'
fi