Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add galaxykit collection upload --skip-upload, --template #99

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions galaxykit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def __init__(
self._container_tls_verify = container_tls_verify
self.gw_root_url = gw_root_url

if not https_verify:
requests.packages.urllib3.disable_warnings()

if auth and not github_social_auth and not gw_auth:
if isinstance(auth, dict):
self.username = auth.get("username")
Expand Down
96 changes: 74 additions & 22 deletions galaxykit/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,70 @@ def get_all_collections(client):
return client.get(url)


def create_test_collection(
namespace=None,
name=None,
version="1.0.0",
tags=None,
template="skeleton",
):
config = {
"namespace": namespace,
"version": version,
}

if name is not None:
config["name"] = name

# cloud importer config requires at least one tag
if tags is not None:
config["tags"] = tags

return build_collection(template, config=config)


def save_test_collection(
namespace=None,
collection_name=None,
version="1.0.0",
tags=["tools"],
template="skeleton",
):
"""
Saves (locally) a test collection generated with orionutils
"""
artifact = create_test_collection(
namespace, collection_name, version, tags, template
)

return {
"namespace": artifact.namespace,
"name": artifact.name,
"version": artifact.version,
"published": artifact.published,
"filename": artifact.filename,
}


def upload_test_collection(
client,
namespace=None,
collection_name=None,
version="1.0.0",
path="staging",
tags=["tools"],
template="skeleton",
):
"""
Uploads a test collection generated with orionutils
"""
config = {
"namespace": namespace or client.username,
"version": version,
}
if collection_name is not None:
config["name"] = collection_name
# cloud importer config requires at least one tag
config["tags"] = tags
artifact = build_collection("skeleton", config=config)
upload_resp_url = upload_artifact(config, client, artifact, path=path)["task"]
artifact = create_test_collection(
namespace or client.username, collection_name, version, tags, template
)

resp = upload_artifact(None, client, artifact, path=path)
wait_for_task(client, resp)

ready = False
state = ""
while not ready:
sleep(1)
task_resp = client.get(upload_resp_url)
state = task_resp["state"]
ready = state in ["completed", "failed"]
if state == "failed":
raise GalaxyClientError(json.dumps(task_resp))
return {
"namespace": artifact.namespace,
"name": artifact.name,
Expand All @@ -106,9 +139,7 @@ def upload_artifact(
"""
Publishes a collection to a Galaxy server and returns the import task URI.

:param config: a configuration object to describe the artifact to be uploaded.
Example:
{"namespace": "foo", "name": "bar"}
:param config: unused, left for compatibility
:param client: a GalaxyClient object. Must be authenticated.
:param artifact: the collection artifact to be uploaded. Expects structure to be that
of collections produced using the orionutils build_collection function.
Expand Down Expand Up @@ -206,6 +237,18 @@ def to_bytes(s, errors=None):
return resp


def approve_collection(client, namespace, collection_name, version):
return move_or_copy_collection(
client,
namespace,
collection_name,
version,
source="staging",
destination="published",
operation="move",
)


def move_or_copy_collection(
client,
namespace,
Expand Down Expand Up @@ -270,6 +313,15 @@ def deprecate_collection(client, namespace, collection, repository):
return resp


def undeprecate_collection(client, namespace, collection, repository):
logger.debug(f"Undeprecating {collection} in {namespace} on {client.galaxy_root}")
url = f"v3/plugin/ansible/content/{repository}/collections/index/{namespace}/{collection}/"
body = {"deprecated": False}
resp = client.patch(url, body)
wait_for_task(client, resp)
return resp


def collection_sign(
client,
repository,
Expand Down
121 changes: 110 additions & 11 deletions galaxykit/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ def report_error(resp):
"nargs": "*",
"default": None,
},
"--skip-upload": {
"help": "Save to a local file instead of uploading.",
"action": "store_true",
"default": False,
},
"--template": {
"help": "orionutils collection template",
"nargs": "?",
"default": "skeleton",
},
},
},
"move": {
Expand Down Expand Up @@ -149,6 +159,27 @@ def report_error(resp):
"collection_name": {},
},
},
"approve": {
"args": {
"namespace": {},
"collection_name": {},
"version": {},
},
},
"deprecate": {
"args": {
"namespace": {},
"collection_name": {},
"repository": {"nargs": "?", "default": "published"},
},
},
"undeprecate": {
"args": {
"namespace": {},
"collection_name": {},
"repository": {"nargs": "?", "default": "published"},
},
},
},
},
"namespace": {
Expand Down Expand Up @@ -296,6 +327,9 @@ def report_error(resp):
"registry": {
"help": "Remote Registry",
"ops": {
"list": {
"args": None,
},
"delete": {
"args": {
"name": {},
Expand Down Expand Up @@ -888,7 +922,16 @@ def main():
sys.exit(EXIT_NOT_FOUND)

elif args.kind == "registry":
if args.operation == "delete":
if args.operation == "list":
try:
resp = registries.list_registries(client)
for name in map(lambda r: r["name"], resp):
print(name)
except ValueError as e:
if not args.ignore:
logger.error(e)
sys.exit(EXIT_NOT_FOUND)
elif args.operation == "delete":
name = args.name
try:
resp = registries.delete_registry(client, name)
Expand Down Expand Up @@ -1008,22 +1051,42 @@ def main():
if args.operation == "list":
print(json.dumps(collections.get_collection_list(client)))
elif args.operation == "upload":
namespace, collection_name, version, path, tags = (
(
namespace,
collection_name,
version,
path,
tags,
skip_upload,
template,
) = (
args.namespace or client.username,
args.collection_name,
args.version or "1.0.0",
args.path or "staging",
args.tags or ["tools"],
args.skip_upload or False,
args.template or "skeleton",
)
resp = namespaces.create_namespace(client, namespace, None)
artifact = collections.upload_test_collection(
client,
namespace=namespace,
collection_name=collection_name,
version=version,
path=path,
tags=tags,
)
if not skip_upload:
namespaces.create_namespace(client, namespace, None)
artifact = collections.upload_test_collection(
client,
namespace=namespace,
collection_name=collection_name,
version=version,
path=path,
tags=tags,
template=template,
)
else:
artifact = collections.save_test_collection(
namespace=namespace,
collection_name=collection_name,
version=version,
tags=tags,
template=template,
)
print(json.dumps(artifact))
elif args.operation == "move":
namespace, collection_name, version, source, destination = (
Expand Down Expand Up @@ -1115,6 +1178,42 @@ def main():
if not args.ignore:
logger.error(e)
sys.exit(EXIT_NOT_FOUND)
elif args.operation == "approve":
namespace, collection_name, version = (
args.namespace,
args.collection_name,
args.version,
)
collections.approve_collection(
client,
namespace,
collection_name,
version,
)
elif args.operation == "deprecate":
namespace, collection_name, repository = (
args.namespace,
args.collection_name,
args.repository or "published",
)
collections.deprecate_collection(
client,
namespace,
collection_name,
repository,
)
elif args.operation == "undeprecate":
namespace, collection_name, repository = (
args.namespace,
args.collection_name,
args.repository or "published",
)
collections.undeprecate_collection(
client,
namespace,
collection_name,
repository,
)
elif args.kind == "url":
if args.operation == "get":
url = args.url
Expand Down
9 changes: 9 additions & 0 deletions galaxykit/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ def create_registry(client, name, url):
"url": url,
}
return client.post(post_url, registry)


def list_registries(client):
"""
List registries
"""
url = f"_ui/v1/execution-environments/registries/?limit=100"
resp = client.get(url)
return resp["data"]
1 change: 0 additions & 1 deletion galaxykit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import re
import time
from urllib import request
from urllib.parse import urljoin

import requests
Expand Down
Loading