Skip to content

Commit

Permalink
moving to playground
Browse files Browse the repository at this point in the history
  • Loading branch information
ilude committed May 1, 2024
1 parent e9e510d commit 416d816
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"eamodio",
"esbenp",
"euskadi",
"fqdns",
"geerlingguy",
"htmx",
"HTMX",
Expand Down
46 changes: 30 additions & 16 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,40 @@
# Create a Docker client instance
client = docker.from_env()

# Define a function to extract FQDNs from Traefik labels


def extract_fqdns_from_labels(labels):
traefik_host_labels = {k: v for k, v in labels.items() if k.startswith('traefik.http.routers.')}
fqdns = []
for label_key, label_value in traefik_host_labels.items():
match = re.search(r'rule=Host\(`(.+?)`\)', label_value)
if match:
fqdn = match.group(1)
fqdns.append(fqdn)
return fqdns

# Define a callback function to handle events


def event_callback(event):
if event["Action"] == "start":
container_name = event["Actor"]["Attributes"]["name"]
if event['Action'] == 'start':
container_name = event['Actor']['Attributes']['name']
container = client.containers.get(container_name)
labels = container.labels
fqdns = extract_fqdns_from_labels(labels)

traefik_host_labels = {
k: v for k, v in labels.items() if k.startswith("traefik.http.routers.")
}

if traefik_host_labels:
fqdns = []
for label_key, label_value in traefik_host_labels.items():
match = re.search(r"rule=Host\(`(.+?)`\)", label_value)
if match:
fqdn = match.group(1)
fqdns.append(fqdn)

if fqdns:
print(f"Container {container_name} started with Traefik FQDNs: {', '.join(fqdns)}")
else:
print(f"Container {container_name} started without Traefik host labels.")

elif event["Action"] == "stop":
elif event['Action'] == 'stop':
print(f"Container {event['Actor']['Attributes']['name']} stopped.")


# Define a signal handler function


def signal_handler(signal, frame):
print("Received signal, stopping event listener...")
sys.exit(0)
Expand All @@ -45,6 +50,15 @@ def signal_handler(signal, frame):
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

# Get running containers with Traefik labels on startup
print("Fetching running containers with Traefik labels...")
running_containers = client.containers.list(filters={'status': 'running'})
for container in running_containers:
labels = container.labels
fqdns = extract_fqdns_from_labels(labels)
if fqdns:
print(f"Container {container.name} is running with Traefik FQDNs: {', '.join(fqdns)}")

# Start listening for events
print("Listening for Docker events...")
try:
Expand Down
20 changes: 1 addition & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
aiohttp[speedups]
argcomplete
bs4
cssmin
feedparser
flask-admin
flask[async]
flask-assets
flask-bcrypt
flask-caching
flask-minify
flask-sqlalchemy
hypercorn==0.15.0
lxml==5.1.1
python-dateutil
docker
python-dotenv
pyyaml
requests
unidecode
url-normalize
w3lib

0 comments on commit 416d816

Please sign in to comment.