chore: always assume TRAEFIK_INSTANCE
points to host.docker.internal
This commit is contained in:
parent
79492a47c8
commit
5830244af0
1 changed files with 34 additions and 6 deletions
40
app.py
40
app.py
|
@ -2,6 +2,7 @@ import base64
|
|||
import os
|
||||
import re
|
||||
import requests
|
||||
import urllib3
|
||||
from concurrent.futures import Future, ThreadPoolExecutor, wait as gather_futures
|
||||
|
||||
import fabric
|
||||
|
@ -14,6 +15,8 @@ TRAEFIK_HOST = os.getenv('TRAEFIK_HOST') or ''
|
|||
INTERNAL_DESTS = (os.getenv('INTERNAL_DESTS') or '').split(',') or []
|
||||
EXTERNAL_DESTS = (os.getenv('EXTERNAL_DESTS') or '').split(',') or []
|
||||
PRIVATE_KEY = os.getenv('PRIVATE_KEY') or ''
|
||||
HOST = os.getenv('HOST') or '0.0.0.0'
|
||||
PORT = os.getenv('PORT') or '80'
|
||||
|
||||
assert NODE_NAME != '_traefik'
|
||||
assert TRAEFIK_INSTANCE
|
||||
|
@ -49,10 +52,35 @@ class Observer:
|
|||
@classmethod
|
||||
def _init(cls):
|
||||
try:
|
||||
data = requests.get(TRAEFIK_INSTANCE + '/rawdata').json()
|
||||
except Exception: ...
|
||||
else:
|
||||
cls.update(data)
|
||||
with urllib3.HTTPSConnectionPool(
|
||||
'host.docker.internal',
|
||||
server_hostname=TRAEFIK_INSTANCE
|
||||
) as pool:
|
||||
runtime_conf = pool.request(
|
||||
'GET',
|
||||
'/api/rawdata',
|
||||
headers={'Host': TRAEFIK_INSTANCE},
|
||||
assert_same_host=False
|
||||
).json()
|
||||
except Exception:
|
||||
return
|
||||
dynamic_conf = {
|
||||
'http': {
|
||||
'middlewares': runtime_conf.get('middlewares') or {},
|
||||
'routers': runtime_conf.get('routers') or {},
|
||||
'services': runtime_conf.get('services') or {},
|
||||
},
|
||||
'tcp': {
|
||||
'middlewares': runtime_conf.get('tcpMiddlewares') or {},
|
||||
'routers': runtime_conf.get('tcpRouters') or {},
|
||||
'services': runtime_conf.get('tcpServices') or {},
|
||||
},
|
||||
'udp': {
|
||||
'routers': runtime_conf.get('udpRouters') or {},
|
||||
'services': runtime_conf.get('udpServices') or {},
|
||||
}
|
||||
}
|
||||
cls.update(dynamic_conf)
|
||||
print("Initialized!")
|
||||
|
||||
|
||||
|
@ -97,7 +125,7 @@ class Observer:
|
|||
|
||||
@classmethod
|
||||
def parse_raw_data(cls, data: dict):
|
||||
http = (data.get('http') or {}) | (data.get('tcp') or {}) | (data.get('udp') or {}) or data
|
||||
http = data.get('http') or {}
|
||||
flt = lambda ep: set([x.group(1) for x in [re.match(PATTERN, x['rule']) for x in http['routers'].values() if ep in x['entryPoints']] if x])
|
||||
|
||||
internal = flt(INTERNAL_ENTRYPOINT)
|
||||
|
@ -120,6 +148,6 @@ def callback():
|
|||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
app.run(host='0.0.0.0', port=80)
|
||||
app.run(host=HOST, port=int(PORT))
|
||||
finally:
|
||||
Observer._close()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue