Skip to content

Commit

Permalink
feat: fix namespacing for subdomain (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar5526 authored Feb 9, 2024
1 parent ee74705 commit f6a15bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
7 changes: 4 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from urllib.parse import urlparse

import jwt
from dotenv import load_dotenv
Expand Down Expand Up @@ -32,13 +33,13 @@ def verify_token(token):

# Your deployment endpoint
@app.route("/deploy", methods=["POST", "DELETE"])
@auth.login_required
def deploy():
data = request.get_json()

# Create DeploymentConfig object
project_url_split = data.get("project_git_url").split("/")
project_name = f"{project_url_split[-2]}_{project_url_split[-1]}".split(".git")[0]
project_name = urlparse(data.get("project_git_url")).path[
:-4
] # remove .git from the end
config = DeploymentConfig(
project_name=project_name,
branch_name=data.get("branch"),
Expand Down
2 changes: 1 addition & 1 deletion server/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _clone_project(self):
"git",
"clone",
"-b",
self._config.branch_name,
self._config.branch_name_raw,
self._config.project_git_url,
self._project_path,
],
Expand Down
25 changes: 21 additions & 4 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import pathlib
import re
import socket
import subprocess
import typing
Expand All @@ -23,12 +24,27 @@ class DeploymentConfig:
compose_file_location: str = "docker-compose.yml"
rest_action: str = "POST"

def __post_init__(self):
self.branch_name_raw = self.branch_name
self.project_name_raw = self.project_name

self.project_name = re.sub(r"[^a-zA-Z]", "", self.project_name.lower())
self.project_name = (
self.project_name[-10:]
if len(self.project_name) > 10
else self.project_name
)
self.branch_name = re.sub(r"[^a-zA-Z]", "", self.branch_name.lower())
self.branch_name = (
self.branch_name[:20] if len(self.branch_name) > 20 else self.branch_name
)

def get_project_hash(self):
return get_random_stub(f"{self.project_name}:{self.branch_name}")
return get_random_stub(f"{self.project_name}:{self.branch_name}", 10)

def __repr__(self):
return (
f"DeploymentConfig({self.project_name!r}, {self.branch_name!r}, {self.project_git_url!r}, "
f"DeploymentConfig({self.project_name_raw!r}, {self.branch_name_raw!r}, {self.project_git_url!r}, "
f"{self.compose_file_location!r}, {self.rest_action!r})"
)

Expand Down Expand Up @@ -350,8 +366,9 @@ def cleanup_deployment_variables(self):
return response


def get_random_stub(project_name: str) -> str:
return hashlib.md5(project_name.encode()).hexdigest()[:16]
def get_random_stub(project_name: str, length: int = 64) -> str:
hash_string = hashlib.md5(project_name.encode()).hexdigest()
return hash_string[:length] if length else hash_string


def load_yaml_file(filename: str):
Expand Down
21 changes: 7 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_verify_project_hash_format(deployment_config):

# Then
assert type(project_hash) == str
assert len(project_hash) == 16
assert len(project_hash) == 10


def test_dont_load_compose_file_in_compose_helper():
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker):
== """
server {
listen 80;
server_name ~c7866191e5a92858.localhost;
server_name ~7a38dba0c2.localhost;
location / {
proxy_pass http://host.docker.internal:12345;
Expand All @@ -160,9 +160,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker):
}
"""
)
mock_open.assert_called_with(
"/path/to/outer/conf/test-project-name-c7866191e5a92858.conf", "w"
)
mock_open.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf", "w")


def test_generate_project_proxy_conf_file(nginx_helper, mocker):
Expand All @@ -177,13 +175,10 @@ def test_generate_project_proxy_conf_file(nginx_helper, mocker):
proxy_conf_path, urls = nginx_helper.generate_project_proxy_conf_file(services)

# Then
assert (
proxy_conf_path
== "/path/to/deployment/project/test-project-name-c7866191e5a92858.conf"
)
assert proxy_conf_path == "/path/to/deployment/project/rojectname-7a38dba0c2.conf"
assert urls == [
"http://test-project-name-test-branch-name-1000-c7866191e5a92858.localhost",
"http://test-project-name-test-branch-name-2000-c7866191e5a92858.localhost",
"http://rojectname-testbranchname-1000-7a38dba0c2.localhost",
"http://rojectname-testbranchname-2000-7a38dba0c2.localhost",
]


Expand Down Expand Up @@ -225,9 +220,7 @@ def test_remove_outer_proxy(nginx_helper, mocker):
nginx_helper.remove_outer_proxy()

# Then
mock_remove.assert_called_with(
"/path/to/outer/conf/test-project-name-c7866191e5a92858.conf"
)
mock_remove.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf")


def test_remove_outer_proxy_when_file_is_deleted_already(nginx_helper, mocker):
Expand Down

0 comments on commit f6a15bd

Please sign in to comment.