forked from digibib/koha-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-wait_until_ready.py
executable file
·131 lines (107 loc) · 3.22 KB
/
docker-wait_until_ready.py
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
#!/usr/bin/env python
from __future__ import print_function
# Script to test sanity of koha docker container
# Verifies that Koha OPAC, Intra and SIP Server are reachable
import socket, requests, time, sys
def validateArguments():
if ((len(sys.argv) > 4)):
printUsage()
sys.exit(1)
if ((len(sys.argv) >= 2)):
if (not sys.argv[1].isdigit()):
print("ERROR: sleep_period ", sys.argv[1], "is not a valid period!")
printUsage()
sys.exit(1)
if ((len(sys.argv) >= 3)):
if (not sys.argv[2].isdigit()):
print("ERROR: retry_period ", sys.argv[2], "is not a period!")
printUsage()
sys.exit(1)
def printUsage():
print("\nUsage: python wait_until_ready.py [sleep_period] [retry_period]")
print("\t Default sleep_period (between retries) is 5 seconds")
print("\t Default retry_period (how long to continue to retry) is 300 seconds")
def getSleepPeriod():
if ((len(sys.argv) >= 2)):
if (sys.argv[1].isdigit()):
return int(sys.argv[1])
else:
return DEFAULT_SLEEP_PERIOD
def getRetryPeriod():
if ((len(sys.argv) >= 3)):
if (sys.argv[2].isdigit()):
return int(sys.argv[2])
else:
return DEFAULT_RETRY_PERIOD
def doRequest(url):
statusCode = 0
try:
# Note: do NOT follow redirect:
statusCode = requests.get(url, allow_redirects=False).status_code
except Exception as e:
pass
return statusCode
def kohaStatusCode(url, sleepPeriod, retryPeriod):
startTime = time.time()
statusCode = doRequest(url)
elapsedTime = time.time() - startTime
while ((statusCode != 200) and (elapsedTime < retryPeriod)):
time.sleep(sleepPeriod)
statusCode = doRequest(url)
print('.', end=""); sys.stdout.flush()
elapsedTime = time.time() - startTime
print()
return statusCode
def isSipServerRunning(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect((host, port))
s.close
return True
except Exception as e:
return False
def waitForSipServer(host, port, sleepPeriod, retryPeriod):
startTime = time.time()
status = False
elapsedTime = time.time() - startTime
while ((status == False) and (elapsedTime < retryPeriod)):
time.sleep(sleepPeriod)
status = isSipServerRunning(host, port)
print('.', end=""); sys.stdout.flush()
elapsedTime = time.time() - startTime
print()
return status
# main
DEFAULT_SLEEP_PERIOD = 5
DEFAULT_RETRY_PERIOD = 300
validateArguments()
sleepPeriod = getSleepPeriod()
retryPeriod = getRetryPeriod()
opac_url = "http://localhost:8080"
intra_url = "http://localhost:8081"
sip_host = "localhost"
sip_port = 6001
exitCode = 0
print("INFO: opac_url:\t\t", opac_url)
print("INFO: intra_url:\t", intra_url)
print("INFO: sleepPeriod:\t", sleepPeriod)
print("INFO: retryPeriod:\t", retryPeriod, "\n")
if (kohaStatusCode(opac_url, sleepPeriod, retryPeriod) == 200):
print("INFO: OPAC is running")
else:
print("ERROR: OPAC is NOT running")
exitCode = 1
if (exitCode > 0):
sys.exit(exitCode)
if (kohaStatusCode(intra_url, sleepPeriod, retryPeriod) == 200):
print("INFO: INTRA is running")
else:
print("ERROR: INTRA is NOT running")
exitCode = 1
if (waitForSipServer(sip_host, sip_port, sleepPeriod, retryPeriod = 200)):
print("INFO: SIP SERVER is running")
else:
print("ERROR: SIP SERVER is NOT running")
exitCode = 1
sys.exit(exitCode)