Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #137 from sekulicd/traefik_fixes
Browse files Browse the repository at this point in the history
Traefik fixes
  • Loading branch information
filopedraz authored Sep 5, 2023
2 parents 373beda + 9c3160a commit 5eb20fc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
5 changes: 5 additions & 0 deletions app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
# Constants
# ------------------------------------------------------------------------------
DNSD_DNS_EXIST_PATH = "/dns/existing"
DNSD_IP = "/dns/ip"


def dns_exists_url() -> str:
return f"{DNSD_URL}{DNSD_DNS_EXIST_PATH}"


def dns_ip() -> str:
return f"{DNSD_URL}{DNSD_IP}"
34 changes: 23 additions & 11 deletions app/core/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ def get_service_object(
service["downloadedDockerImage"] = service["dockerImage"]
else:
service["downloaded"] = False

if config.PROXY_ENABLED:
domain = utils.check_dns_exists()

service["fullURL"] = f"{service['id']}.docker.localhost"
if domain:
service["fullURL"] = f"{service['id']}.{domain}"

service["invokeMethod"] = {
"header": None,
"sendTo": f"{service['id']}.{domain}",
}
else:
ip = utils.get_deployment_ip()
service["invokeMethod"] = {
"header": f"X-Host-Override:{service['id']}",
"sendTo": ip,
}
return service


Expand Down Expand Up @@ -186,18 +191,25 @@ def run_container_with_retries(service_object):

labels = {}
if config.PROXY_ENABLED:
dns_exists = utils.check_dns_exists()
if dns_exists:
dns = utils.check_dns_exists()
if dns:
runningPort = service_object["runningPort"]
labels = {
"traefik.enable": "true",
f"traefik.http.routers.{service_id}.rule": f"Host(`{service_id}.domain`)",
f"traefik.http.routers.{service_id}.entrypoints": "websecure",
f"traefik.http.routers.{service_id}.tls.certresolver": "myresolver",
f"traefik.http.routers.{service_id}-http.rule": f"Host(`{service_id}.{dns}`)",
f"traefik.http.routers.{service_id}-http.entrypoints": "web",
f"traefik.http.routers.{service_id}-https.rule": f"Host(`{service_id}.{dns}`)",
f"traefik.http.routers.{service_id}-https.entrypoints": "websecure",
f"traefik.http.routers.{service_id}-https.tls.certresolver": "myresolver",
"traefik.http.middlewares.http-to-https.redirectscheme.scheme": "https",
f"traefik.http.routers.{service_id}-http.middlewares": "http-to-https",
f"traefik.http.services.{service_id}.loadbalancer.server.port": f"{runningPort}",
}
else:
labels = {
"traefik.enable": "true",
f"traefik.http.routers.{service_id}.rule": f"Host({service_id}.docker.localhost)",
f"traefik.http.routers.{service_id}.rule": f"HeadersRegexp(`X-Host-Override`,`{service_id}`) && "
f"PathPrefix(`/`)",
}

for _ in range(10):
Expand Down
21 changes: 18 additions & 3 deletions app/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,28 @@ def check_dns_exists():
cached_domain = json_response["domain"]
return cached_domain
else:
print("Domain field not found in response.")
logger.error("Domain field not found in response.")
return None
else:
print(
logger.error(
f"Failed to get a valid response. Status Code: {response.status_code}"
)
return None
except Exception as e:
print(f"An error occurred: {e}")
logger.error(f"An error occurred: {e}")
return None


def get_deployment_ip():
url = config.dns_ip()
try:
response = requests.get(url)
if response.status_code == 200:
# Strip the newline character at the end of the IP
return response.text.strip()
else:
logger.error(f"Failed to get the IP. Status Code: {response.status_code}")
return None
except Exception as e:
logger.error(f"An error occurred: {e}")
return None
4 changes: 1 addition & 3 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class ServiceResponse(BaseModel):
interfaces: list[str]
dockerImage: str
dockerImageSize: int
defaultPort: int
defaultExternalPort: int
runningPort: int = None
command: str = None
volumePath: str = None
volumeName: str = None
Expand All @@ -61,6 +58,7 @@ class ServiceResponse(BaseModel):
envVariables: list[str] = None
execCommands: list[str] = None
promptTemplate: str = None
invokeMethod: dict


class RegistryInput(BaseModel):
Expand Down

0 comments on commit 5eb20fc

Please sign in to comment.