Skip to content

Commit

Permalink
fix: mount repos in working dir, not (pvc)mount path (#2005)
Browse files Browse the repository at this point in the history
* fix: mount repos in working dir, not (pvc)mount path

* fix commit sha only being checked out as anonymous user

* address comments
  • Loading branch information
Panaetius authored Feb 24, 2025
1 parent 531cbd7 commit c47d351
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
17 changes: 11 additions & 6 deletions git_services/git_services/init/clone.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import sys
from pathlib import Path
from typing import cast

from git_services.cli.sentry import setup_sentry
from git_services.init import errors
from git_services.init.cloner import GitCloner
from git_services.init.cloner import GitCloner, Repository
from git_services.init.config import config_from_env

# NOTE: register exception handler
Expand All @@ -11,13 +14,15 @@
if __name__ == "__main__":
config = config_from_env()
setup_sentry(config.sentry)
logging.basicConfig(level=logging.INFO)
base_path = Path(config.mount_path)

git_cloner = GitCloner(
repositories=config.repositories,
git_providers=config.git_providers,
workspace_mount_path=config.workspace_mount_path,
repositories=[Repository.from_config_repo(r, mount_path=base_path) for r in config.repositories],
git_providers={p.id: p for p in config.git_providers},
mount_path=base_path,
user=config.user,
lfs_auto_fetch=config.lfs_auto_fetch,
is_git_proxy_enabled=config.is_git_proxy_enabled,
lfs_auto_fetch=cast(bool, config.lfs_auto_fetch),
is_git_proxy_enabled=cast(bool, config.is_git_proxy_enabled),
)
git_cloner.run(storage_mounts=config.storage_mounts)
43 changes: 14 additions & 29 deletions git_services/git_services/init/cloner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import re
from contextlib import contextmanager
from dataclasses import dataclass
from dataclasses import dataclass, field
from pathlib import Path
from shutil import disk_usage
from urllib.parse import urljoin, urlparse
Expand All @@ -28,15 +28,15 @@ class Repository:
_git_cli: GitCLI | None = None

@classmethod
def from_config_repo(cls, data: ConfigRepo, workspace_mount_path: Path):
def from_config_repo(cls, data: ConfigRepo, mount_path: Path):
dirname = data.dirname or cls._make_dirname(data.url)
provider = data.provider
branch = data.branch
commit_sha = data.commit_sha
return cls(
url=data.url,
dirname=dirname,
absolute_path=workspace_mount_path / dirname,
absolute_path=mount_path / dirname,
provider=provider,
branch=branch,
commit_sha=commit_sha,
Expand Down Expand Up @@ -67,31 +67,18 @@ def _make_dirname(url: str) -> str:
return path.rsplit("/", maxsplit=1).pop()


@dataclass
class GitCloner:
mount_path: Path
git_providers: dict[str, Provider]
user: User
repositories: list[Repository]
lfs_auto_fetch: bool = False
is_git_proxy_enabled: bool = False
remote_name = "origin"
remote_origin_prefix = f"remotes/{remote_name}"
proxy_url = "http://localhost:8080"

def __init__(
self,
repositories: list[ConfigRepo],
git_providers: list[Provider],
workspace_mount_path: str,
user: User,
lfs_auto_fetch=False,
is_git_proxy_enabled=False,
):
base_path = Path(workspace_mount_path)
logging.basicConfig(level=logging.INFO)
self.repositories: list[Repository] = [
Repository.from_config_repo(r, workspace_mount_path=base_path) for r in repositories
]
self.git_providers = {p.id: p for p in git_providers}
self.workspace_mount_path = Path(workspace_mount_path)
self.user = user
self.lfs_auto_fetch = lfs_auto_fetch
self.is_git_proxy_enabled = is_git_proxy_enabled
self._access_tokens: dict[str, str | None] = dict()
_access_tokens: dict[str, str | None] = field(default_factory=dict, repr=False)

def _initialize_repo(self, repository: Repository):
logging.info("Initializing repo")
Expand Down Expand Up @@ -264,15 +251,13 @@ def run_helper(self, repository: Repository, *, storage_mounts: list[str]):

self._initialize_repo(repository)
try:
if self.user.is_anonymous:
self._clone(repository)
if repository.commit_sha:
repository.git_cli.git_reset("--hard", repository.commit_sha)
elif git_access_token is None:
if self.user.is_anonymous or git_access_token is None:
self._clone(repository)
else:
with self._temp_plaintext_credentials(repository, git_user, git_access_token):
self._clone(repository)
if repository.commit_sha:
repository.git_cli.git_reset("--hard", repository.commit_sha)
except errors.GitFetchError as err:
logging.error(msg=f"Cannot clone {repository.url}", exc_info=err)
with open(repository.absolute_path / "ERROR", mode="w", encoding="utf-8") as f:
Expand Down
1 change: 0 additions & 1 deletion git_services/git_services/init/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class Provider:
@dataclass
class Config:
sentry: SentryConfig
workspace_mount_path: str
mount_path: str
user: User
repositories: list[Repository] = field(default_factory=list)
Expand Down
26 changes: 15 additions & 11 deletions git_services/tests/test_init_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from git_services.cli import GitCLI
from git_services.init import errors
from git_services.init.clone import GitCloner
from git_services.init.config import Repository, User
from git_services.init.clone import GitCloner, Repository
from git_services.init.config import Repository as ConfigRepo
from git_services.init.config import User


@pytest.fixture
Expand All @@ -31,11 +32,12 @@ def clone_dir(tmp_path: Path):
def test_simple_git_clone(test_user: User, clone_dir: str, mocker):
repo_url = "https://github.com/SwissDataScienceCenter/amalthea.git"
mocker.patch("git_services.init.cloner.GitCloner._temp_plaintext_credentials", autospec=True)
repositories = [Repository(url=repo_url)]
mount_path = Path(clone_dir)
repositories = [Repository.from_config_repo(ConfigRepo(url=repo_url), mount_path=mount_path)]
cloner = GitCloner(
repositories=repositories,
git_providers=[],
workspace_mount_path=clone_dir,
git_providers={},
mount_path=mount_path,
user=test_user,
)

Expand All @@ -55,11 +57,12 @@ def test_lfs_size_check(test_user, clone_dir, mocker):
mock_disk_usage = mocker.patch("git_services.init.cloner.disk_usage", autospec=True)
mock_get_lfs_total_size_bytes.return_value = 100
mock_disk_usage.return_value = 0, 0, 10
repositories = [Repository(url=repo_url)]
mount_path = Path(clone_dir)
repositories = [Repository.from_config_repo(ConfigRepo(url=repo_url), mount_path=mount_path)]
cloner = GitCloner(
repositories=repositories,
git_providers=[],
workspace_mount_path=clone_dir,
git_providers={},
mount_path=mount_path,
user=test_user,
lfs_auto_fetch=True,
)
Expand All @@ -74,11 +77,12 @@ def test_lfs_size_check(test_user, clone_dir, mocker):
)
def test_lfs_output_parse(test_user, clone_dir, mocker, lfs_lfs_files_output, expected_output):
repo_url = "https://github.com"
repositories = [Repository(url=repo_url)]
mount_path = Path(clone_dir)
repositories = [Repository.from_config_repo(ConfigRepo(url=repo_url), mount_path=mount_path)]
cloner = GitCloner(
repositories=repositories,
git_providers=[],
workspace_mount_path=clone_dir,
git_providers={},
mount_path=mount_path,
user=test_user,
)

Expand Down
4 changes: 1 addition & 3 deletions renku_notebooks/api/amalthea_patches/init_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ def certificates():
}
},
)
volume_etc_certs = client.V1Volume(
name="etc-ssl-certs", empty_dir=client.V1EmptyDirVolumeSource(medium="Memory")
)
volume_etc_certs = client.V1Volume(name="etc-ssl-certs", empty_dir=client.V1EmptyDirVolumeSource(medium="Memory"))
volume_custom_certs = client.V1Volume(
name="custom-ca-certs",
projected=client.V1ProjectedVolumeSource(
Expand Down

0 comments on commit c47d351

Please sign in to comment.