-
Notifications
You must be signed in to change notification settings - Fork 86
/
wait_for_start.sh
executable file
·113 lines (104 loc) · 3.83 KB
/
wait_for_start.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
#!/usr/bin/env bash
# Copyright 2021 Google LLC
#
# 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.
################################## WAIT FOR START #############################
# Script used in e2e-test that waits for OpenMRS and FHIR servers to start.
#
# Example usage:
# ./wait_for_start.sh --HAPI_SERVER_URLS=http://hapi-server1:8080,http://hapi-server2:8080 --OPENMRS_SERVER_URLS=http://openmrs:8080
# The above example waits for two hapi servers and one openmrs server to start
#################################################
# Set the global variables
# Globals:
# HAPI_SERVER_URLS
# OPENMRS_SERVER_URLS
#################################################
while [ $# -gt 0 ]; do
case "$1" in
--HAPI_SERVER_URLS=*)
HAPI_SERVER_URLS="${1#*=}"
;;
--OPENMRS_SERVER_URLS=*)
OPENMRS_SERVER_URLS="${1#*=}"
;;
*)
printf "Error: Invalid argument %s" "$1"
exit 1
esac
shift
done
#################################################
# Function that waits for all the Hapi and OpenMRS servers to start
#################################################
function wait_for_servers_to_start() {
if [ -n "$HAPI_SERVER_URLS" ]; then
IFS=',' read -r -a array <<< "$HAPI_SERVER_URLS"
for url in "${array[@]}"
do
hapi_server_check "$url"
done
fi
if [ -n "$OPENMRS_SERVER_URLS" ]; then
IFS=',' read -r -a array <<< "$OPENMRS_SERVER_URLS"
for url in "${array[@]}"
do
openmrs_server_check "$url/openmrs/ws/fhir2/R4"
done
fi
}
#################################################
# Function to check if fhir server completed initialization
#################################################
function openmrs_server_check() {
openmrs_start_wait_time=0
contenttype=$(curl -o /dev/null --head -w "%{content_type}\n" -X GET -u admin:Admin123 \
--connect-timeout 5 --max-time 20 ${1}/Patient \
2>/dev/null | cut -d ";" -f 1)
until [[ ${contenttype} == "application/fhir+json" ]]; do
echo "WAITING FOR OPENMRS SERVER TO START"
sleep 60s
contenttype=$(curl -o /dev/null --head -w "%{content_type}\n" -X GET -u admin:Admin123 \
--connect-timeout 5 --max-time 20 ${1}/Patient \
2>/dev/null | cut -d ";" -f 1)
((openmrs_start_wait_time += 1))
if [[ ${openmrs_start_wait_time} == 20 ]]; then
echo "TERMINATING TEST AS OPENMRS TOOK TOO LONG TO START"
exit 1
fi
done
echo "OPENMRS SERVER ${1} STARTED SUCCESSFULLY"
}
#################################################
# Function to check if HAPI server completed initialization
#################################################
function hapi_server_check() {
fhir_server_start_wait_time=0
fhir_server_status_code=$(curl -o /dev/null --head -w "%{http_code}" -L -X GET \
-u hapi:hapi --connect-timeout 5 --max-time 20 \
${1}/fhir/Observation 2>/dev/null)
until [[ ${fhir_server_status_code} -eq 200 ]]; do
sleep 30s
echo "WAITING FOR FHIR SERVER TO START"
fhir_server_status_code=$(curl -o /dev/null --head -w "%{http_code}" -L -X GET \
-u hapi:hapi --connect-timeout 5 --max-time 20 \
${1}/fhir/Observation 2>/dev/null)
((fhir_server_start_wait_time += 1))
if [[ $fhir_server_start_wait_time == 20 ]]; then
echo "TERMINATING AS FHIR SERVER TOOK TOO LONG TO START"
exit 1
fi
done
echo "FHIR SERVER ${1} STARTED SUCCESSFULLY"
}
wait_for_servers_to_start