-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
87 additions
and
144 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,6 @@ | ||
--- | ||
- name: Setup container | ||
hosts: localhost | ||
connection: local | ||
gather_facts: true | ||
vars: | ||
# docker defaults | ||
docker_edition: 'ce' | ||
docker_package_state: present | ||
docker_service_manage: false | ||
docker_install_compose_plugin: true | ||
docker_compose_package: docker-compose-plugin | ||
docker_compose_package_state: present | ||
|
||
docker_users: | ||
- "{{ lookup('env', 'USER') }}" | ||
roles: | ||
- role: setup-container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
- name: Setup Docker | ||
hosts: localhost | ||
connection: local | ||
gather_facts: true | ||
become: true | ||
vars: | ||
# docker defaults | ||
docker_edition: 'ce' | ||
docker_package_state: present | ||
docker_service_manage: false | ||
docker_install_compose_plugin: true | ||
docker_compose_package: docker-compose-plugin | ||
docker_compose_package_state: present | ||
|
||
docker_users: | ||
- "{{ lookup('env', 'USER') }}" | ||
roles: | ||
- role: geerlingguy.docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,56 @@ | ||
import asyncio | ||
import logging | ||
import os | ||
import secrets | ||
import docker | ||
import time | ||
import signal | ||
import sys | ||
from datetime import datetime | ||
from typing import Any | ||
from flask import Flask, render_template | ||
from flask_assets import Environment, Bundle | ||
from flask_caching import Cache | ||
|
||
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.DEBUG) | ||
|
||
app = Flask(__name__) | ||
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True | ||
app.secret_key = os.environ.get("FLASK_SECRET_KEY", secrets.token_hex()) | ||
|
||
if os.environ.get("FLASK_DEBUG", "False") == "True": | ||
cache_config={ | ||
'CACHE_TYPE': 'null' | ||
} | ||
else: | ||
# 600 seconds = 10 minutes | ||
cache_config={ | ||
'CACHE_TYPE': 'simple', | ||
'CACHE_DEFAULT_TIMEOUT': 600 | ||
} | ||
from flask_minify import Minify | ||
Minify(app=app, html=True, js=True, cssless=True) | ||
|
||
cache = Cache(app, config=cache_config) | ||
page_timeout = int(os.environ.get('CACHE_PAGE_TIMEOUT', 600)) | ||
|
||
assets = Environment(app) | ||
|
||
css = Bundle( | ||
'css/*.css', | ||
filters="cssmin", | ||
output="assets/common.css" | ||
) | ||
assets.register('css_all', css) | ||
css.build() | ||
|
||
@app.context_processor | ||
def inject_current_date(): | ||
return { | ||
'today_date': datetime.now(), | ||
'site_title': os.environ.get('SITE_TITLE', 'Prototype') | ||
} | ||
|
||
|
||
@app.route('/') | ||
def index(tab_name=None): | ||
return render_template('index.html') | ||
|
||
|
||
############################################################################### | ||
# | ||
# Main Startup Code | ||
# | ||
############################################################################### | ||
|
||
if __name__ == '__main__': | ||
port = int(os.environ.get("FLASK_PORT", os.environ.get("ONBOARD_PORT", 9830))) | ||
development = bool(os.environ.get("FLASK_ENV", "development") == "development") | ||
if development: | ||
app.run(port=port, debug=bool(os.environ.get("FLASK_DEBUG", "True"))) | ||
if bool(os.environ.get('WERKZEUG_RUN_MAIN')): | ||
print("") | ||
app.logger.info("Shutting down...") | ||
sys.exit() | ||
else: | ||
try: | ||
from hypercorn.config import Config | ||
from hypercorn.asyncio import serve | ||
|
||
shutdown_event = asyncio.Event() | ||
|
||
def _signal_handler(*_: Any) -> None: | ||
app.logger.info("Shutting down...") | ||
shutdown_event.set() | ||
|
||
config = Config() | ||
config.accesslog="-" | ||
config.errorlog="-" | ||
config.loglevel="DEBUG" | ||
config.bind = f"0.0.0.0:{port}" | ||
loop = asyncio.new_event_loop() | ||
loop.add_signal_handler(signal.SIGTERM, _signal_handler) | ||
loop.run_until_complete( | ||
serve(app, config, shutdown_trigger=shutdown_event.wait) | ||
) | ||
except KeyboardInterrupt: | ||
app.logger.info("Shutting down...") | ||
sys.exit() | ||
|
||
import re | ||
|
||
# Create a Docker client instance | ||
client = docker.from_env() | ||
|
||
|
||
# Define a callback function to handle events | ||
def event_callback(event): | ||
if event["Action"] == "start": | ||
container_name = event["Actor"]["Attributes"]["name"] | ||
container = client.containers.get(container_name) | ||
labels = container.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) | ||
|
||
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": | ||
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) | ||
|
||
|
||
# Set up signal handlers | ||
signal.signal(signal.SIGTERM, signal_handler) | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
# Start listening for events | ||
print("Listening for Docker events...") | ||
try: | ||
for event in client.events(decode=True): | ||
event_callback(event) | ||
# Flush the output buffer to ensure the message is printed immediately | ||
time.sleep(0.1) | ||
except KeyboardInterrupt: | ||
print("Keyboard interrupt received, stopping event listener...") |
This file was deleted.
Oops, something went wrong.