Skip to content

Commit c035442

Browse files
committed
start of work to add private remotes
Signed-off-by: vsoch <[email protected]>
1 parent ec85590 commit c035442

File tree

7 files changed

+82
-21
lines changed

7 files changed

+82
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/singularityhub/singularity-hpc/tree/main) (0.0.x)
17+
- Support for remotes that do not expose library.json (0.0.12)
1718
- Update add to return container yaml (0.1.11)
1819
- Fixing bug with writing package file in update (0.1.1)
1920
- Add support for remote registry and sync commands --all (0.1.0)

shpc/client/sync.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
__copyright__ = "Copyright 2021-2022, Vanessa Sochat"
33
__license__ = "MPL 2.0"
44

5+
import os
6+
57
import shpc.logger as logger
68
import shpc.utils
7-
import os
89

910

1011
def sync_registry(args, parser, extra, subparser):

shpc/main/modules/base.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import shpc.main.modules.template as templatectl
1616
import shpc.main.modules.versions as versionfile
1717
import shpc.main.modules.views as views
18-
import shpc.main.registry as registry
18+
import shpc.main.registry
1919
import shpc.utils as utils
2020
from shpc.logger import logger
2121
from shpc.main.client import Client as BaseClient
@@ -198,7 +198,7 @@ def add(self, image, module_name=None, **kwargs):
198198

199199
# Load config (but don't validate yet!)
200200
config = container.ContainerConfig(
201-
registry.FilesystemResult(module_name, template), validate=False
201+
shpc.main.registry.FilesystemResult(module_name, template), validate=False
202202
)
203203
return self.container.add(
204204
module_name, image, config, container_yaml=dest, **kwargs
@@ -236,16 +236,8 @@ def docgen(self, module_name, registry=None, out=None, branch="main"):
236236
template = self.template.load("docs.md")
237237
registry = registry or defaults.github_url
238238
github_url = "%s/blob/%s/%s/container.yaml" % (registry, branch, module_name)
239-
registry_bare = registry.split(".com")[-1]
240-
raw = (
241-
"https://gitlab.com/%s/-/raw/%s/%s/container.yaml"
242-
if "gitlab" in registry
243-
else "https://raw.githubusercontent.com/%s/%s/%s/container.yaml"
244-
)
245-
raw_github_url = raw % (
246-
registry_bare,
247-
branch,
248-
module_name,
239+
raw_github_url = shpc.main.registry.get_module_config_url(
240+
registry, module_name, branch
249241
)
250242

251243
# Currently one doc is rendered for all containers

shpc/main/registry/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from shpc.main.settings import SettingsBase
1515

1616
from .filesystem import Filesystem, FilesystemResult
17-
from .remote import GitHub, GitLab
17+
from .remote import GitHub, GitLab, get_module_config_url
1818

1919

2020
def update_container_module(module, from_path, existing_path):

shpc/main/registry/remote.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import os
77
import re
8+
import shutil
89
import subprocess as sp
9-
import sys
1010

1111
import requests
1212

@@ -16,6 +16,23 @@
1616
from .provider import Provider, Result
1717

1818

19+
def get_module_config_url(registry, module_name, branch="main"):
20+
"""
21+
Get the raw address of the config (container.yaml)
22+
"""
23+
registry_bare = registry.split(".com")[-1]
24+
raw = (
25+
"https://gitlab.com/%s/-/raw/%s/%s/container.yaml"
26+
if "gitlab" in registry
27+
else "https://raw.githubusercontent.com/%s/%s/%s/container.yaml"
28+
)
29+
return raw % (
30+
registry_bare,
31+
branch,
32+
module_name,
33+
)
34+
35+
1936
class RemoteResult(Result):
2037
"""
2138
A remote result provides courtesy functions for interacting with
@@ -117,6 +134,9 @@ def exists(self, name):
117134
"""
118135
Determine if a module exists in the registry.
119136
"""
137+
name = name.split(":")[0]
138+
if self._cache and name in self._cache:
139+
return True
120140
dirname = self.source
121141
if self.subdir:
122142
dirname = os.path.join(dirname, self.subdir)
@@ -158,7 +178,8 @@ def find(self, name):
158178
"""
159179
Find a particular entry in a registry
160180
"""
161-
self._update_cache()
181+
if not self._cache:
182+
self._update_cache()
162183
if name in self._cache:
163184
return RemoteResult(name, self._cache[name])
164185

@@ -172,12 +193,29 @@ def _update_cache(self, force=False):
172193
# Check for exposed library API on GitHub or GitLab pages
173194
response = requests.get(self.web_url)
174195
if response.status_code != 200:
175-
sys.exit(
176-
"Remote %s is not deploying a Registry API (%s). Open a GitHub issue to ask for help."
177-
% (self.source, self.web_url)
178-
)
196+
return self._update_clone_cache()
179197
self._cache = response.json()
180198

199+
def _update_clone_cache(self):
200+
"""
201+
Given a remote that does not expose a library.json, handle via clone.
202+
"""
203+
logger.warning(
204+
"Remote %s is not deploying a Registry API, falling back to clone."
205+
% self.source
206+
)
207+
tmpdir = self.clone()
208+
for dirname, module in self.iter_modules():
209+
# Minimum amount of metadata to function here
210+
config_url = get_module_config_url(self.source, module)
211+
self._cache[module] = {
212+
"config": shpc.utils.read_yaml(
213+
os.path.join(dirname, module, "container.yaml")
214+
),
215+
"config_url": config_url,
216+
}
217+
shutil.rmtree(tmpdir)
218+
181219
def iter_registry(self, filter_string=None):
182220
"""
183221
Yield metadata about containers in a remote registry.

shpc/tests/test_sync.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def test_sync_from_file(tmp_path):
106106
[
107107
"https://github.com/singularityhub/shpc-registry",
108108
"https://gitlab.com/singularityhub/shpc-registry",
109+
# This registry does not expose a web UI
110+
"https://github.com/researchapps/shpc-test-registry",
109111
],
110112
)
111113
def test_remote_upgrade(tmp_path, remote):
@@ -133,3 +135,30 @@ def test_remote_upgrade(tmp_path, remote):
133135

134136
client.registry.sync(sync_registry=remote)
135137
assert list(client.registry.iter_modules())
138+
139+
140+
@pytest.mark.parametrize(
141+
"remote",
142+
[
143+
"https://github.com/singularityhub/shpc-registry",
144+
"https://gitlab.com/singularityhub/shpc-registry",
145+
# This registry does not expose a web UI
146+
"https://github.com/researchapps/shpc-test-registry",
147+
],
148+
)
149+
def test_registry_interaction(tmp_path, remote):
150+
"""
151+
Test interactions with registries of different types
152+
"""
153+
client = init_client(str(tmp_path), "lmod", "singularity")
154+
reg = client.registry.get_registry(remote)
155+
156+
assert not reg.is_filesystem_registry
157+
158+
# This will hit the underlying logic to list/show
159+
mods = list(reg.iter_registry())
160+
assert mods
161+
162+
# Should use the cache
163+
assert reg.exists("vanessa/salad")
164+
assert reg.find("vanessa/salad") is not None

shpc/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__copyright__ = "Copyright 2021-2022, Vanessa Sochat"
33
__license__ = "MPL 2.0"
44

5-
__version__ = "0.1.11"
5+
__version__ = "0.1.12"
66
AUTHOR = "Vanessa Sochat"
77
88
NAME = "singularity-hpc"

0 commit comments

Comments
 (0)