-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecho_container.py
84 lines (68 loc) · 1.9 KB
/
echo_container.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
from flask import Flask, request
import subprocess
import time
import os
import threading
import socket
app = Flask(__name__)
ready = True
def get_app_in_rotation():
'''
Will make readiness probe success after 220 seconds automatically if not SLEEP TIME
'''
time_sleep = os.getenv('ECHO_SLEEP_TIME', 220)
global ready
while True:
if not ready:
time.sleep(int(time_sleep))
ready = True
@app.route('/')
@app.route('/health')
@app.route('/lprobe')
@app.route('/liveness_probe')
def hello():
global ready
if ready:
return f"Hello World! : {socket.gethostname()}", 200
return f"Failing Health as set in maintainance : {ready}", 503
@app.route('/ip')
@app.route('/getip')
def hello_name():
status, output = subprocess.getstatusoutput('hostname -I')
return output, 200
@app.route('/rprobe')
@app.route('/rediness_probe')
def rediness_probe():
if ready:
return 'ok', 200
else:
return f'readiness probe failing : {ready}', 503
@app.route('/setm')
@app.route('/set_maintenance')
def set_maintenance():
'''
will fail rediness
'''
global ready
ready = False
return f'Readiness Probe, will fail, ready_value: {ready}', 200
@app.route('/remom')
@app.route('/remove_maintenance')
def remove_maintenance():
'''
rediness probe will succed
'''
global ready
ready = True
return f'Readiness Probe, will succeed, ready_value: {ready}', 200
@app.route('/echo')
def refresh_index():
'''
Echo string passed inside environment variable
'''
env_variable = os.getenv('ECHO_RESPONSE', "default_echo")
return f'Hello from: {env_variable}.', 200
if __name__ == '__main__':
readiness_app = threading.Thread(target=get_app_in_rotation)
readiness_app.start()
app.run('0.0.0.0', port=os.getenv('ECHO_PORT', 8080), debug=os.getenv('ECHO_DEBUG', True))