Skip to content

Commit

Permalink
list public packages
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilsAutumn committed Sep 18, 2024
1 parent 0753855 commit 78e2690
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
7 changes: 4 additions & 3 deletions backend/src/zango/apps/shared/tenancy/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import re
import requests
import tempfile
import uuid

from collections import namedtuple

import requests

from django_tenants.models import DomainMixin, TenantMixin

from django.conf import settings
Expand Down Expand Up @@ -151,12 +152,12 @@ def create(cls, name, schema_name, description, app_template_name, **other_param
# initialize tenant's workspace
init_task = initialize_workspace.delay(str(obj.uuid), app_template_path)
return obj, init_task.id

@staticmethod
def download_file(url):
response = requests.get(url)
response.raise_for_status() # Raise an exception for bad status codes

# Create a temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(response.content)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/zango/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
CELERY_RESULT_EXTENDED = True
X_FRAME_OPTIONS = "ALLOW"

PACKAGE_BUCKET_NAME = "zelthy3-packages"
PACKAGE_BUCKET_NAME = "zelthy3-oss-packages"
CODEASSIST_ENABLED = True

# Session Security
Expand Down
108 changes: 62 additions & 46 deletions backend/src/zango/core/package_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,78 @@ def get_installed_packages(tenant):


def get_all_packages(request, tenant=None):
installed_packages = {}
if tenant is not None:
installed_packages = get_installed_packages(tenant.name)
packages = {}
s3 = boto3.client(
"s3",
config=Config(signature_version=UNSIGNED),
)
installed_packages = get_installed_packages(tenant.name) if tenant else {}
s3_public_packages = get_s3_public_packages()

packages = merge_package_data(s3_public_packages, installed_packages, tenant)

resp_data = format_response_data(packages, installed_packages)

add_config_urls(resp_data, request, tenant)

return resp_data


def get_s3_public_packages():
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
s3_package_data = s3.list_objects(
Bucket=settings.PACKAGE_BUCKET_NAME, Prefix="packages/"
Bucket=settings.PACKAGE_BUCKET_NAME, Prefix="packages/public/"
)
for package in s3_package_data["Contents"]:
name = package["Key"]
name = name[9:]
version = name.split("/")[1]
name = name.split("/")[0]

packages = {}
for package in s3_package_data.get("Contents", []):
parts = package["Key"].split("/")
if len(parts) < 4:
continue
name, version = parts[2], parts[3]
if name not in packages:
packages[name] = {"versions": [Version(version)]}
else:
packages[name]["versions"].append(Version(version))
if tenant is not None:
if installed_packages.get(name):
packages[name]["status"] = "Installed"
packages[name]["installed_version"] = installed_packages[name]
packages[name] = {"versions": []}
packages[name]["versions"].append(Version(version))

return packages


def merge_package_data(s3_public_packages, installed_packages, tenant):
for name, data in s3_public_packages.items():
data["versions"] = sorted(data["versions"], reverse=True)
data["versions"] = [str(version) for version in data["versions"]]
data["config_url"] = None

if tenant:
if name in installed_packages:
data["status"] = "Installed"
data["installed_version"] = installed_packages[name]
else:
name = name.split("/")[0]
packages[name]["status"] = "Not Installed"
resp_data = []
for package in packages.keys():
if packages[package].get("versions"):
packages[package]["versions"] = sorted(
packages[package]["versions"], reverse=True
)
packages[package]["versions"] = [
str(version) for version in packages[package]["versions"]
]
packages[package]["config_url"] = None
for package, data in packages.items():
resp_data.append({"name": package, **data})
for local_package in installed_packages.keys():
if local_package not in packages.keys():
data["status"] = "Not Installed"

return s3_public_packages


def format_response_data(packages, installed_packages):
resp_data = [{"name": name, **data} for name, data in packages.items()]

for name, version in installed_packages.items():
if name not in packages:
resp_data.append(
{
"name": local_package,
"name": name,
"status": "Installed",
"installed_version": installed_packages[local_package],
"installed_version": version,
}
)

return resp_data


def add_config_urls(resp_data, request, tenant):
for package in resp_data:
url = get_package_configuration_url(request, tenant, package["name"])
resp = requests.get(url)
if resp.status_code == 200:
package["config_url"] = f"{url}?token={signing.dumps(request.user.id)}"
else:
package["config_url"] = None
return resp_data
if url:
resp = requests.get(url)
if resp.status_code == 200:
package["config_url"] = f"{url}?token={signing.dumps(request.user.id)}"
else:
package["config_url"] = None


def update_settings_json(tenant, package_name, version):
Expand Down Expand Up @@ -154,7 +170,7 @@ def install_package(package_name, version, tenant, release=False):
)
bucket = resource.Bucket(settings.PACKAGE_BUCKET_NAME)
bucket.download_file(
f"packages/{package_name}/{version}/{package_name}.zip",
f"packages/public/{package_name}/{version}/{package_name}.zip",
f"workspaces/{tenant}/packages/{package_name}.zip",
)
with zipfile.ZipFile(
Expand Down

0 comments on commit 78e2690

Please sign in to comment.