This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
75 lines (52 loc) · 1.95 KB
/
app.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
import os
from flask import Flask, render_template, redirect, url_for, g
from haproxyadmin import haproxy, server
app = Flask(__name__)
config = os.environ.get('HAPLO_CONFIG', 'config.DevelopmentConfig')
app.config.from_object(config)
hap = haproxy.HAProxy(uris=app.config['HAPROXY_URIS'],
socket_dir=app.config['HAPROXY_SOCKET_DIR'],
socket_file=app.config['HAPROXY_SOCKET_FILE'])
@app.route('/')
def index():
refresh_servers()
return render_template('index.html.j2', title=app.config['PAGE_TITLE'])
@app.route('/disable_server/<backend>/<s_name>')
def disable_server(backend=None, s_name=None):
b = hap.backend(backend)
s = b.server(s_name)
s.setstate(server.STATE_DISABLE)
return redirect(url_for('index'))
@app.route('/enable_server/<backend>/<s_name>')
def enable_server(backend=None, s_name=None):
b = hap.backend(backend)
s = b.server(s_name)
s.setstate(server.STATE_ENABLE)
return redirect(url_for('index'))
def refresh_servers():
backends = []
for backend in hap.backends():
b_d = {'name': backend.name,
'status': backend.status,
'scur': backend.metric('scur'),
'smax': backend.metric('smax'),
'slim': backend.metric('slim'),
'servers': []}
servers = backend.servers()
for backend_server in servers:
s = {'name': backend_server.name,
'status': backend_server.status,
'scur': backend_server.metric('scur'),
'smax': backend_server.metric('smax'),
'bck': backend_server.metric('bck'),
}
if 'UP' in s['status']:
s['status'] = 'UP'
if s['bck'] == 1:
s['status'] = 'BACKUP'
b_d['servers'].append(s)
backends.append(b_d)
g.backends = backends
return True
if __name__ == '__main__':
app.run()