-
Notifications
You must be signed in to change notification settings - Fork 14
/
deploy
executable file
·199 lines (177 loc) · 7 KB
/
deploy
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
#!/bin/bash
#
# Copyright 2018 Jérôme Gasperi
#
# Licensed under the Apache License, version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
####### DO NOT TOUCH THIS ######
ENV_FILE=./config.env
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
MYPWD=$(pwd)
PROJECT_NAME=itag
CONTACT="[email protected]"
################################
# Force script to exit on error
set -e
err_report() {
echo -e "${RED}[ERROR] Error on line $1 - see errors.log file${NC}"
}
trap 'err_report $LINENO' ERR
function showUsage {
echo -e ""
echo -e " Deploy an itag docker instance "
echo -e ""
echo -e " Usage $0 <target> [options]"
echo -e ""
echo -e " Where target is either \"prod\" or \"dev\""
echo -e ""
echo -e " Options:"
echo -e ""
echo -e " -e | --envfile Environnement file (default is ${GREEN}config.env${NC})"
echo -e " -h | --help show this help"
echo -e ""
echo -e " !!! This script requires docker and docker-compose !!!"
echo -e ""
}
# Parsing arguments
TARGET=""
while (( "$#" ));
do
case "$1" in
-e|--envfile)
if [[ "$2" == "" || ${2:0:1} == "-" ]]; then
showUsage
echo -e "${RED}[ERROR] Missing config file name${NC}"
echo -e ""
exit 1
fi
ENV_FILE="$2"
shift 2 # past argument
;;
-h|--help)
showUsage
shift # past argument
exit 0
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
showUsage
echo -e "${RED}[ERROR] Unsupported flag $1${NC}"
echo -e ""
exit 1
;;
*) # preserve positional arguments
TARGET="$1"
shift
;;
esac
done
#
# TARGET is mandatory - should be "prod" or "dev"
#
if [[ "${TARGET}" != "prod" && "${TARGET}" != "dev" ]]; then
showUsage
echo -e "${RED}[ERROR] Missing target - choose between \"prod\" or \"dev\"${NC}"
echo ""
exit 0
fi
#
# The environement file is mandatory
# It contains all configuration to build and run resto images
#
if [[ ! -f ${ENV_FILE} ]]; then
showUsage
echo -e "${RED}[ERROR] The \"${ENV_FILE}\" file does not exist!${NC}"
echo ""
exit 0
fi
ITAG_EXPOSED_PORT=$(grep ^ITAG_EXPOSED_PORT= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_EXPOSED_PORT=$(grep ^ITAG_DATABASE_EXPOSED_PORT= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_ENDPOINT=$(grep ^ITAG_ENDPOINT= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_USER_PASSWORD=$(grep ^ITAG_DATABASE_USER_PASSWORD= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_USER_NAME=$(grep ^ITAG_DATABASE_USER_NAME= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_NAME=$(grep ^ITAG_DATABASE_NAME= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_HOST=$(grep ^ITAG_DATABASE_HOST= ${ENV_FILE} | awk -F= '{print $2}')
ITAG_DATABASE_PORT=$(grep ^ITAG_DATABASE_PORT= ${ENV_FILE} | awk -F= '{print $2}')
#
# Containers are part of the external network "rnet" (resto network)
#
RNET_EXIST=$(docker network ls | grep rnet | wc | awk '{print $1}')
if [[ "${RNET_EXIST}" == "0" ]]; then
echo -e "[INFO] Creating external network ${GREEN}rnet${NC}"
docker network create rnet
else
echo -e "[INFO] Using existing network ${GREEN}rnet${NC}"
fi
# Clean errors.log file
rm -f errors.log
echo -e "${YELLOW}[INFO] You're now running itag installation in **${TARGET}** mode${NC}"
COMPOSE_FILE=docker-compose.yml
# echo -e "[INFO] Starting ${PROJECT_NAME} docker instance"
if [[ "${TARGET}" == "dev" ]]; then
COMPOSE_FILE=${COMPOSE_FILE}:docker-compose.dev.yml
fi
echo "[debug] using compose files: ${COMPOSE_FILE}"
COMPOSE_FILE=${COMPOSE_FILE} \
ITAG_EXPOSED_PORT=${ITAG_EXPOSED_PORT} \
ITAG_DATABASE_EXPOSED_PORT=${ITAG_DATABASE_EXPOSED_PORT} \
ITAG_DATABASE_HOST=${ITAG_DATABASE_HOST} \
ITAG_DATABASE_PORT=${ITAG_DATABASE_PORT} \
ITAG_DATABASE_NAME=${ITAG_DATABASE_NAME} \
ITAG_DATABASE_USER_NAME=${ITAG_DATABASE_USER_NAME} \
ITAG_DATABASE_USER_PASSWORD=${ITAG_DATABASE_USER_PASSWORD} \
ENV_FILE=${ENV_FILE} \
docker-compose --project-name ${PROJECT_NAME} up -d
if [ "${ITAG_DATABASE_HOST}" == "itagdb" ] || [ "${ITAG_DATABASE_HOST}" == "host.docker.internal" ]; then
DATABASE_HOST_SEEN_FROM_DOCKERHOST=localhost
else
DATABASE_HOST_SEEN_FROM_DOCKERHOST=${ITAG_DATABASE_HOST}
fi
# Wait for database to be ready
echo -e "[INFO] Waiting for database: ${DATABASE_HOST_SEEN_FROM_DOCKERHOST}:${ITAG_DATABASE_EXPOSED_PORT} to be ready..."
while ! pg_isready -h ${DATABASE_HOST_SEEN_FROM_DOCKERHOST} -U ${ITAG_DATABASE_USER_NAME} -p ${ITAG_DATABASE_EXPOSED_PORT} -d ${ITAG_DATABASE_NAME} 2>/dev/null
do
let elapsed=elapsed+1
if [ "$elapsed" -gt 90 ]
then
echo -e "${RED}[ERROR]${NC} Database startup timed out =("
exit 1
fi
sleep 1;
done
PHYSICAL_EXISTS=$(PGPASSWORD=${ITAG_DATABASE_USER_PASSWORD} psql -XAt -d ${ITAG_DATABASE_NAME} -U ${ITAG_DATABASE_USER_NAME} -h ${DATABASE_HOST_SEEN_FROM_DOCKERHOST} -p ${ITAG_DATABASE_EXPOSED_PORT} -c "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'datasources' AND table_name= 'physical')")
if [[ "${PHYSICAL_EXISTS}" == "f" ]]; then
echo -e "${YELLOW}[WARNING] iTag database is not installed - you should run the *populateDatabase* script${NC}"
else
echo -e "${GREEN}[INFO] iTag database is complete${NC}"
fi
GPW_COUNT=$(PGPASSWORD=${ITAG_DATABASE_USER_PASSWORD} psql -U ${ITAG_DATABASE_USER_NAME} -d ${ITAG_DATABASE_NAME} -h ${DATABASE_HOST_SEEN_FROM_DOCKERHOST} -p ${ITAG_DATABASE_EXPOSED_PORT} -c "SELECT COUNT(*) FROM gpw.glp15ag60 LIMIT 1;" | head -3 | tail -1 | awk '{print $1}')
if [[ "${GPW_COUNT}" == "0" ]]; then
echo -e "${YELLOW}[WARNING] Population data is not installed${NC} - please contact ${YELLOW}${CONTACT}${NC} if you need it"
else
echo -e "${GREEN}[INFO] Population data is installed${NC}"
fi
LANDCOVER_COUNT=$(PGPASSWORD=${ITAG_DATABASE_USER_PASSWORD} psql -U ${ITAG_DATABASE_USER_NAME} -d ${ITAG_DATABASE_NAME} -h ${DATABASE_HOST_SEEN_FROM_DOCKERHOST} -p ${ITAG_DATABASE_EXPOSED_PORT} -c "SELECT COUNT(*) FROM landcover.landcover LIMIT 1;" | head -3 | tail -1 | awk '{print $1}')
if [[ "${LANDCOVER_COUNT}" == "0" ]]; then
echo -e "${YELLOW}[WARNING] Landcover data is not installed${NC} - please contact ${YELLOW}${CONTACT}${NC} if you need it"
else
echo -e "${GREEN}[INFO] Landcover data is installed${NC}"
fi
echo ""
echo -e "[INFO] Application iTag up and running in ${GREEN}${TARGET}${NC} mode at ${GREEN}${ITAG_ENDPOINT}${NC}"
echo ""