diff --git a/RegistryPy/db.sqlite3 b/RegistryPy/db.sqlite3 new file mode 100644 index 0000000..8a4ee61 Binary files /dev/null and b/RegistryPy/db.sqlite3 differ diff --git a/RegistryPy/docker_registry_viewer/__init__.py b/RegistryPy/docker_registry_viewer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/RegistryPy/docker_registry_viewer/asgi.py b/RegistryPy/docker_registry_viewer/asgi.py new file mode 100644 index 0000000..6e9bb8e --- /dev/null +++ b/RegistryPy/docker_registry_viewer/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for docker_registry_viewer project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'docker_registry_viewer.settings') + +application = get_asgi_application() diff --git a/RegistryPy/docker_registry_viewer/settings.py b/RegistryPy/docker_registry_viewer/settings.py new file mode 100644 index 0000000..7919a46 --- /dev/null +++ b/RegistryPy/docker_registry_viewer/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for docker_registry_viewer project. + +Generated by 'django-admin startproject' using Django 5.1. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-37m6875*ir!1px(=^*^2-_yro*2i#gsrxy#wgqdjy(nx!a!r&l' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'registry', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'docker_registry_viewer.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'docker_registry_viewer.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/RegistryPy/docker_registry_viewer/urls.py b/RegistryPy/docker_registry_viewer/urls.py new file mode 100644 index 0000000..7f6c067 --- /dev/null +++ b/RegistryPy/docker_registry_viewer/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for docker_registry_viewer project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('registry/', include('registry.urls')), +] diff --git a/RegistryPy/docker_registry_viewer/wsgi.py b/RegistryPy/docker_registry_viewer/wsgi.py new file mode 100644 index 0000000..e13c06c --- /dev/null +++ b/RegistryPy/docker_registry_viewer/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for docker_registry_viewer project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'docker_registry_viewer.settings') + +application = get_wsgi_application() diff --git a/RegistryPy/manage.py b/RegistryPy/manage.py new file mode 100755 index 0000000..5edae8a --- /dev/null +++ b/RegistryPy/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'docker_registry_viewer.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/RegistryPy/registry/__init__.py b/RegistryPy/registry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/RegistryPy/registry/admin.py b/RegistryPy/registry/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/RegistryPy/registry/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/RegistryPy/registry/apps.py b/RegistryPy/registry/apps.py new file mode 100644 index 0000000..1177aea --- /dev/null +++ b/RegistryPy/registry/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class RegistryConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'registry' diff --git a/RegistryPy/registry/migrations/__init__.py b/RegistryPy/registry/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/RegistryPy/registry/models.py b/RegistryPy/registry/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/RegistryPy/registry/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/RegistryPy/registry/templates/registry/registry_list.html b/RegistryPy/registry/templates/registry/registry_list.html new file mode 100644 index 0000000..b7de4e1 --- /dev/null +++ b/RegistryPy/registry/templates/registry/registry_list.html @@ -0,0 +1,43 @@ + + + + Docker Registry Viewer + + + +

Docker Registry Viewer

+ + + + + + + + + {% for repo in data %} + + + + + {% endfor %} + +
RepositoryTags
{{ repo.repository }} + {% for tag in repo.tags %} + {{ tag }}{% if not forloop.last %}, {% endif %} + {% endfor %} +
+ + diff --git a/RegistryPy/registry/templates/registry/repository_detail.html b/RegistryPy/registry/templates/registry/repository_detail.html new file mode 100644 index 0000000..89ec019 --- /dev/null +++ b/RegistryPy/registry/templates/registry/repository_detail.html @@ -0,0 +1,48 @@ + + + + Repository Details - {{ repository }} + + + +

Tags for {{ repository }}

+ {% if error %} +

{{ error }}

+ {% endif %} + + + + + + + + + {% for tag in tags %} + + + + + {% endfor %} + +
TagsActions
{{ tag }}Delete
+

Back to Repository List

+ + diff --git a/RegistryPy/registry/tests.py b/RegistryPy/registry/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/RegistryPy/registry/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/RegistryPy/registry/urls.py b/RegistryPy/registry/urls.py new file mode 100644 index 0000000..5af8725 --- /dev/null +++ b/RegistryPy/registry/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.registry_list, name='registry_list'), + path('/', views.repository_detail, name='repository_detail'), + path('/delete//', views.delete_tag, name='delete_tag'), +] + diff --git a/RegistryPy/registry/views.py b/RegistryPy/registry/views.py new file mode 100644 index 0000000..2cb6ee8 --- /dev/null +++ b/RegistryPy/registry/views.py @@ -0,0 +1,64 @@ +import requests +from django.shortcuts import render, redirect +from django.urls import reverse + +DOCKER_REGISTRY_URL = "https://registry.reymota.es/v2" +REGISTRY_USERNAME = "creylopez" +REGISTRY_PASSWORD = "Rey-1176" + +def get_docker_registry_data(): + # Autenticarse en el registro Docker + response = requests.get(f"{DOCKER_REGISTRY_URL}/_catalog", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + + if response.status_code == 200: + repositories = response.json().get('repositories', []) + registry_data = [] + + for repo in repositories: + tags_response = requests.get(f"{DOCKER_REGISTRY_URL}/{repo}/tags/list", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + if tags_response.status_code == 200: + tags = tags_response.json().get('tags', []) + registry_data.append({'repository': repo, 'tags': tags}) + + return registry_data + else: + return [] + +def registry_list(request): + data = get_docker_registry_data() + return render(request, 'registry/registry_list.html', {'data': data}) + +# Nueva vista para mostrar los detalles de un repositorio especĂ­fico +def repository_detail(request, repository_name): + tags_response = requests.get(f"{DOCKER_REGISTRY_URL}/{repository_name}/tags/list", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + + if tags_response.status_code == 200: + tags = tags_response.json().get('tags', []) + else: + tags = [] + + return render(request, 'registry/repository_detail.html', {'repository': repository_name, 'tags': tags}) + +# Vista para eliminar un tag especĂ­fico +def delete_tag(request, repository_name, tag): + manifest_url = f"{DOCKER_REGISTRY_URL}/{repository_name}/manifests/{tag}" + + # Obtener el digest del tag + manifest_response = requests.head(manifest_url, auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD), headers={"Accept": "application/vnd.docker.distribution.manifest.v2+json"}) + print("delete_tag: manifest_url ", manifest_url) + if manifest_response.status_code == 200: + digest = manifest_response.headers.get('Docker-Content-Digest') + delete_response = requests.delete(f"{DOCKER_REGISTRY_URL}/{repository_name}/manifests/{digest}", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + print("delete_tag: digest->",digest) + if delete_response.status_code == 202: + return redirect(reverse('repository_detail', args=[repository_name])) + else: + # Obtener las etiquetas nuevamente para renderizar la plantilla + tags_response = requests.get(f"{DOCKER_REGISTRY_URL}/{repository_name}/tags/list", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + tags = tags_response.json().get('tags', []) if tags_response.status_code == 200 else [] + return render(request, 'registry/repository_detail.html', {'repository': repository_name, 'tags': tags, 'error': 'Error deleting the tag.'}) + else: + # Obtener las etiquetas nuevamente para renderizar la plantilla + tags_response = requests.get(f"{DOCKER_REGISTRY_URL}/{repository_name}/tags/list", auth=(REGISTRY_USERNAME, REGISTRY_PASSWORD)) + tags = tags_response.json().get('tags', []) if tags_response.status_code == 200 else [] + return render(request, 'registry/repository_detail.html', {'repository': repository_name, 'tags': tags, 'error': 'Tag not found.'}) \ No newline at end of file