2024-01-15 22:59:08 +00:00
|
|
|
from command.command import Command
|
|
|
|
from traefik.traefikConfig import TraefikConfig
|
|
|
|
from utils.dockerUtils import restartTraefik
|
|
|
|
|
|
|
|
class AddCommand(Command):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("add", "Add a domain", "add <name> <domain> <service host>")
|
|
|
|
|
2024-04-17 02:24:45 +01:00
|
|
|
def execute(self, traefikConfig: TraefikConfig, args):
|
2024-01-15 22:59:08 +00:00
|
|
|
if len(args) < 3:
|
|
|
|
self.printUsage()
|
|
|
|
return
|
|
|
|
|
|
|
|
name = args[0]
|
|
|
|
domain = args[1]
|
|
|
|
serviceHost = args[2]
|
|
|
|
|
|
|
|
if traefikConfig.hasRouter(name):
|
|
|
|
print(f"Router \"{name}\" already exists")
|
|
|
|
return
|
2024-01-15 23:15:54 +00:00
|
|
|
|
|
|
|
# Validate if the service host is a valid URL
|
|
|
|
if not serviceHost.startswith("http://") and not serviceHost.startswith("https://"):
|
|
|
|
print(f"Service host \"{serviceHost}\" is not a valid URL")
|
|
|
|
return
|
2024-01-15 22:59:08 +00:00
|
|
|
|
|
|
|
print(f"Adding \"{domain}\" -> \"{serviceHost}\"")
|
|
|
|
|
|
|
|
traefikConfig.addRouter(name, domain, serviceHost)
|
|
|
|
traefikConfig.addService(name, serviceHost)
|
|
|
|
|
|
|
|
traefikConfig.save()
|
|
|
|
|
2024-04-17 02:24:45 +01:00
|
|
|
# restartTraefik()
|
2024-01-15 23:14:45 +00:00
|
|
|
|
|
|
|
print(f"Access your service at http://{domain}")
|