traefik-helper/src/manage.py

171 lines
3.9 KiB
Python
Raw Normal View History

2024-01-13 19:37:10 +00:00
import sys
import subprocess
import yaml
2024-01-13 19:51:28 +00:00
import os
2024-01-13 21:09:14 +00:00
from colorama import Fore
2024-01-13 19:37:10 +00:00
# Variables
2024-01-13 19:51:28 +00:00
configFile = "./config.yml"
2024-01-13 20:06:08 +00:00
containerName = "traefik"
2024-01-13 19:51:28 +00:00
# Are we running in a Docker container?
if os.environ.get("CONFIG_FILE"):
configFile = os.environ.get("CONFIG_FILE")
2024-01-13 20:06:08 +00:00
if os.environ.get("CONTAINER_NAME"):
containerName = os.environ.get("CONTAINER_NAME")
2024-01-13 19:37:10 +00:00
# DO NOT TOUCH
2024-01-13 20:49:05 +00:00
commands = ["add", "remove", "list", "update"]
2024-01-13 19:37:10 +00:00
command = len(sys.argv) > 1 and sys.argv[1]
if command not in commands:
print("")
print("Usage: manage.py [command]")
print("")
print("Commands:")
print(" - add [name] [domain] [service host]")
print(" - remove [name]")
2024-01-13 20:49:05 +00:00
print(" - update [name] [new service host]")
2024-01-13 19:37:10 +00:00
print(" - list")
exit()
with open(configFile, "r") as config:
configYml = yaml.safe_load(config)
http = configYml["http"]
routers = http["routers"]
services = http["services"]
def restartTraefik():
2024-01-13 20:06:08 +00:00
print("Restarting Traefik, please wait this can take a while...")
2024-01-13 19:37:10 +00:00
# Restart Traefik in the base directory
2024-01-13 19:51:28 +00:00
subprocess.run(["docker", "restart", "traefik"])
2024-01-13 19:37:10 +00:00
2024-01-13 21:09:14 +00:00
print(f"{Fore.GREEN}Done!{Fore.RESET}")
2024-01-13 20:06:08 +00:00
2024-01-13 19:37:10 +00:00
def addDomain(name, domain, serviceHost):
# Check if name already exists
if name in routers:
2024-01-13 21:09:14 +00:00
print(f"Name \"{Fore.RED}{name}{Fore.RESET}\" already exists")
2024-01-13 19:37:10 +00:00
exit()
print(f"Adding domain \"{Fore.LIGHTBLUE_EX}{name}{Fore.RESET}\" -> \"{Fore.YELLOW}{serviceHost}{Fore.RESET}\"")
2024-01-13 21:09:14 +00:00
print(f"Domain: {Fore.GREEN}http://{domain}{Fore.RESET}")
2024-01-13 19:37:10 +00:00
# Add router
routers[name] = {
"entryPoints": ["https"],
"rule": "Host(`%s`)" % domain,
"middlewares": ["default-headers", "https-redirectscheme"],
"tls": {},
"service": name
}
# Add service
services[name] = {
"loadBalancer": {
"servers": [
{
"url": serviceHost
}
]
}
}
# Write to file
with open(configFile, "w") as config:
yaml.dump(configYml, config)
# Restart Traefik
restartTraefik()
def removeDomain(name):
# Check if name exists
if name not in routers:
2024-01-13 21:09:14 +00:00
print(f"Name \"{Fore.RED}{name}{Fore.RESET}\" does not exist")
2024-01-13 19:37:10 +00:00
exit()
2024-01-13 21:09:14 +00:00
print(f"Removing domain \"{Fore.BLUE}{name}{Fore.RESET}\"")
2024-01-13 19:37:10 +00:00
# Remove router
del routers[name]
# Remove service
del services[name]
# Write to file
with open(configFile, "w") as config:
yaml.dump(configYml, config)
# Restart Traefik
restartTraefik()
def listDomains():
print("Listing domains:")
2024-01-13 21:03:40 +00:00
# name and domain -> service host
2024-01-13 19:37:10 +00:00
domains = {}
2024-01-13 21:03:40 +00:00
# Loop through routers
2024-01-13 19:37:10 +00:00
for name, router in routers.items():
2024-01-13 21:03:40 +00:00
# Get domain
domain = router["rule"].split("`")[1]
# Get service host
serviceHost = services[name]["loadBalancer"]["servers"][0]["url"]
# Add to domains
domains[name] = {
"domain": domain,
"serviceHost": serviceHost
}
2024-01-13 19:37:10 +00:00
# Print domains
2024-01-13 21:03:40 +00:00
for name, domain in domains.items():
2024-01-13 21:09:14 +00:00
print(f" - {Fore.BLUE}[{name}] {Fore.GREEN}http://{domain['domain']} {Fore.RESET}-> {Fore.YELLOW}{domain['serviceHost']}{Fore.RESET}")
2024-01-13 21:03:40 +00:00
2024-01-13 20:39:52 +00:00
print("")
print("Total: %s" % len(domains))
2024-01-13 19:37:10 +00:00
2024-01-13 20:49:05 +00:00
def updateDomain(name, serviceHost):
# Check if name exists
if name not in routers:
print("Name \"%s\" does not exist" % name)
exit()
2024-01-13 21:09:14 +00:00
print(f"Updating domain \"{Fore.BLUE}{name}{Fore.RESET}\" -> \"{Fore.YELLOW}{serviceHost}{Fore.RESET}\"")
2024-01-13 20:49:05 +00:00
# Update service
services[name] = {
"loadBalancer": {
"servers": [
{
"url": serviceHost
}
]
}
}
# Write to file
with open(configFile, "w") as config:
yaml.dump(configYml, config)
# Restart Traefik
restartTraefik()
2024-01-13 19:37:10 +00:00
match command:
case "add":
name = sys.argv[2]
domain = sys.argv[3]
serviceHost = sys.argv[4]
addDomain(name, domain, serviceHost)
case "remove":
name = sys.argv[2]
removeDomain(name)
2024-01-13 20:49:05 +00:00
case "update":
name = sys.argv[2]
serviceHost = sys.argv[3]
updateDomain(name, serviceHost)
2024-01-13 19:37:10 +00:00
case "list":
listDomains()