-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
200 lines (173 loc) · 7.02 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
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
198
199
200
from flask import Flask, render_template
from dotenv import load_dotenv
import yaml
import kube
import threading
import time
import global_bookmarks as gb
from waitress import serve
from uptime_kuma import UptimeKuma
load_dotenv()
app: Flask = Flask(__name__)
config = {}
uk = UptimeKuma()
kube = kube.Kube()
gb = gb.GlobalBookMarks()
@app.route("/")
def homepage():
return get_index()
@app.route("/health")
def health():
return "Ok"
@app.route("/<subpage>")
def subpages(subpage):
return get_index(subpage)
def get_index(subpage=""):
local_ingress = (kube.ingress or set([])).copy()
local_ingress_groups = (kube.ingress_groups or {}).copy()
local_global_bookmarks = (gb.global_bookmarks or {}).copy()
tmp_ingress = set([])
for i in local_ingress:
if (not i.sub_pages and not subpage) or (subpage in get_sub_pages(i.sub_pages)) or (
not subpage and "default" in get_sub_pages(i.sub_pages)):
tmp_ingress.add(i)
local_ingress = kube.get_sorted_ingress_list(tmp_ingress)
del tmp_ingress
tmp_ingress_group = {}
for group in local_ingress_groups:
tmp_ingress = set([])
for i in local_ingress_groups[group]:
if (not i.sub_pages and not subpage) or (subpage in get_sub_pages(i.sub_pages)) or (
not subpage and "default" in get_sub_pages(i.sub_pages)):
tmp_ingress.add(i)
if len(tmp_ingress) > 0:
tmp_ingress_group[group] = kube.get_sorted_ingress_list(tmp_ingress)
local_ingress_groups = tmp_ingress_group
local_sorted_ingress_groups_keys = sorted(local_ingress_groups.keys())
del tmp_ingress_group
tmp_book_marks_group = {}
for group in local_global_bookmarks:
tmp_book_marks = set([])
for bookmark in local_global_bookmarks[group]:
if (not bookmark.sub_pages and not subpage) or (subpage in get_sub_pages(bookmark.sub_pages)) or (
not subpage and "default" in get_sub_pages(bookmark.sub_pages)):
tmp_book_marks.add(bookmark)
if len(tmp_book_marks) > 0:
tmp_book_marks_group[group] = gb.get_sorted_bookmarks_list(tmp_book_marks)
local_global_bookmarks = tmp_book_marks_group
local_sorted_global_bookmarks_keys = sorted(local_global_bookmarks.keys())
del tmp_book_marks_group
greeting = "Welcome, Searcher!"
if "greeting" in config:
greeting = config["greeting"]
title = "Horus"
if "title" in config:
title = config["title"]
background_color = "#232530"
if "backgroundColor" in config:
background_color = config["backgroundColor"]
primary_color = "#232530"
if "primaryColor" in config:
primary_color = config["primaryColor"]
accent_color = "#232530"
if "accentColor" in config:
accent_color = config["accentColor"]
online_color = "#232530"
if "onlineColor" in config:
online_color = config["onlineColor"]
offline_color = "#232530"
if "offlineColor" in config:
offline_color = config["offlineColor"]
show_greeting = True
if "showGreeting" in config:
show_greeting = config["showGreeting"]
show_search = True
if "showSearch" in config:
show_greeting = config["showSearch"]
show_app_groups = True
if "showAppGroups" in config:
show_app_groups = config["showAppGroups"]
show_app_urls = True
if "showAppUrls" in config:
show_app_urls = config["showAppUrls"]
show_app_description = False
if "showAppDescription" in config:
show_app_description = config["showAppDescription"]
show_app_status = True
if "showAppStatus" in config:
show_app_status = config["showAppStatus"]
show_global_bookmarks = False
if "showGlobalBookmarks" in config:
show_global_bookmarks = config["showGlobalBookmarks"]
always_target_blank = False
if "alwaysTargetBlank" in config:
always_target_blank = config["alwaysTargetBlank"]
return render_template("index.html", title=title, showGreeting=show_greeting,
showSearch=show_search,
showAppGroups=show_app_groups,
showAppUrls=show_app_urls, showAppDescription=show_app_description,
showAppStatus=show_app_status,
showGlobalBookmarks=show_global_bookmarks,
alwaysTargetBlank=always_target_blank,
greeting=greeting, ingress=local_ingress,
sorted_ingress_groups_keys=local_sorted_ingress_groups_keys,
ingress_groups=local_ingress_groups,
sorted_global_bookmarks_keys=local_sorted_global_bookmarks_keys,
global_bookmarks=local_global_bookmarks,
uptime_kuma_status=uk.uptime_kuma_status, backgroundColor=background_color,
primaryColor=primary_color, accentColor=accent_color,
onlineColor=online_color, offlineColor=offline_color)
def get_sub_pages(sub_pages):
if sub_pages:
return sub_pages.split(",")
else:
return []
def load_config():
with open("config/config.yaml", "r") as stream:
app.logger.info("Loading config ...")
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return None
def uptime_kuma_polling(uptime_kuma_poll_seconds):
while True:
uk.update(kube.ingress)
time.sleep(uptime_kuma_poll_seconds)
def ingress_polling(ingress_poll_seconds):
while True:
kube.update_ingress()
time.sleep(ingress_poll_seconds)
def parse_config_items():
gb.parse_global_bookmarks(config)
kube.parse_custom_apps()
def parse_config_fixed():
global config
config = load_config()
uptime_kuma_poll_seconds = 30
ingress_poll_seconds = 60
if "uptimeKumaPollSeconds" in config:
uptime_kuma_poll_seconds = int(config["uptimeKumaPollSeconds"])
if "ingressPollSeconds" in config:
ingress_poll_seconds = int(config["ingressPollSeconds"])
uptime_kuma_disabled = False
ingress_disabled = False
if "uptime-kuma" in config and "disabled" in config["uptime-kuma"] and config["uptime-kuma"]["disabled"]:
uptime_kuma_disabled = True
if "ingress" in config and "disabled" in config["ingress"] and config["ingress"]["disabled"]:
ingress_disabled = True
return uptime_kuma_poll_seconds, ingress_poll_seconds, uptime_kuma_disabled, ingress_disabled
if __name__ == "__main__":
ukps, ips, ukd, ingd = parse_config_fixed()
uk.get_config(config)
kube.get_config(config)
parse_config_items()
if not ukd:
uk.connect()
uk.login()
uptime_kuma_thread = threading.Thread(target=uptime_kuma_polling, args=(ukps,))
uptime_kuma_thread.start()
if not ingd:
ingress_thread = threading.Thread(target=ingress_polling, args=(ips,))
ingress_thread.start()
serve(app, host="0.0.0.0", port=8080)