Skip to content

Commit

Permalink
feat: support for private repos (#47)
Browse files Browse the repository at this point in the history
* feat: support for private repos

* upgrade to docker compose v2

* fix: add support for private repos

* fix: test

* fix: add tests
  • Loading branch information
tushar5526 authored May 22, 2024
1 parent fcb3dfe commit 34a05f7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ FROM python:3.11

WORKDIR /app

RUN apt update -y && apt install docker-compose -y
# Install dependencies and Docker's official GPG key
RUN apt-get update && \
apt-get install -y ca-certificates curl && \
install -m 0755 -d /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && \
chmod a+r /etc/apt/keyrings/docker.asc

# Add Docker's official repository to Apt sources
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
apt-get update

RUN apt-get update -y && apt-get install docker-compose-plugin docker-ce-cli -y

COPY requirements.txt ./

Expand Down
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async def deploy(request: Request, token: dict = Depends(verify_token)):
branch_name=data.get("branch"),
project_git_url=data.get("project_git_url"),
compose_file_location=data.get("compose_file_location"),
gh_token=data.get("gh_token"),
rest_action=request.method,
)
deployer = Deployer(config)
Expand Down
10 changes: 8 additions & 2 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DeploymentConfig:
project_name: str
branch_name: str
project_git_url: str
gh_token: str = None
compose_file_location: str = constants.COMPOSE_FILE
rest_action: str = constants.POST

Expand All @@ -45,6 +46,11 @@ def __post_init__(self):
f"{constants.DEFAULT_SECRETS_PATH} is a reserved keyword in Sarthi. Please use a different branch name",
)

if self.gh_token:
self.project_git_url = (
f"{self.project_git_url[:8]}{self.gh_token}:@{self.project_git_url[8:]}"
)

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

Expand Down Expand Up @@ -88,7 +94,7 @@ def start_services(
logger.error(f"Error generating processed compose file: {e}")
raise HTTPException(500, e)

command = ["docker-compose", "up", "-d", "--build"]
command = ["docker", "compose", "up", "-d", "--build"]
project_dir = pathlib.Path(self._compose_file_location).parent

try:
Expand All @@ -103,7 +109,7 @@ def remove_services(self):
if not os.path.exists(pathlib.Path(self._compose_file_location).parent):
logger.info(f"{self._compose_file_location} is already deleted!")
return "Deployment already deleted"
command = ["docker-compose", "down", "-v"]
command = ["docker", "compose", "down", "-v"]
project_dir = pathlib.Path(self._compose_file_location).parent
try:
subprocess.run(command, check=True, cwd=project_dir)
Expand Down
18 changes: 16 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_start_services_success(compose_helper, mocker):
), "Deployment (Nginx) Proxy is missing in processed services"

mocked_run.assert_called_once_with(
["docker-compose", "up", "-d", "--build"], check=True, cwd=pathlib.Path(".")
["docker", "compose", "up", "-d", "--build"], check=True, cwd=pathlib.Path(".")
)


Expand Down Expand Up @@ -110,7 +110,7 @@ def test_remove_services_success(compose_helper, mocker):

# Then
mocked_run.assert_called_once_with(
["docker-compose", "down", "-v"], check=True, cwd=pathlib.Path(".")
["docker", "compose", "down", "-v"], check=True, cwd=pathlib.Path(".")
)


Expand Down Expand Up @@ -322,6 +322,20 @@ def test_create_deployment_config_with_reserved_branch_name():
assert deployment_config.branch_name == "defaultdevsecrets"


def test_create_deployment_config_for_private_repos():
deployment_config = DeploymentConfig(
project_name="test-project-name",
branch_name=constants.DEFAULT_SECRETS_PATH,
project_git_url="https://github.com/tushar5526/test-project-name.git",
rest_action="POST",
gh_token="random-pat-token",
)
assert (
deployment_config.project_git_url
== "https://random-pat-token:@github.com/tushar5526/test-project-name.git"
)


@patch("server.utils.os")
@patch("server.utils.requests")
def test_create_env_placeholder_with_sample_env_file(
Expand Down

0 comments on commit 34a05f7

Please sign in to comment.