| @ -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() | |||
| @ -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' | |||
| @ -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')), | |||
| ] | |||
| @ -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() | |||
| @ -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() | |||
| @ -0,0 +1,3 @@ | |||
| from django.contrib import admin | |||
| # Register your models here. | |||
| @ -0,0 +1,6 @@ | |||
| from django.apps import AppConfig | |||
| class RegistryConfig(AppConfig): | |||
| default_auto_field = 'django.db.models.BigAutoField' | |||
| name = 'registry' | |||
| @ -0,0 +1,3 @@ | |||
| from django.db import models | |||
| # Create your models here. | |||
| @ -0,0 +1,43 @@ | |||
| <!DOCTYPE html> | |||
| <html> | |||
| <head> | |||
| <title>Docker Registry Viewer</title> | |||
| <style> | |||
| table { | |||
| width: 100%; | |||
| border-collapse: collapse; | |||
| } | |||
| th, td { | |||
| padding: 8px 12px; | |||
| border: 1px solid #ddd; | |||
| text-align: left; | |||
| } | |||
| th { | |||
| background-color: #f4f4f4; | |||
| } | |||
| </style> | |||
| </head> | |||
| <body> | |||
| <h1>Docker Registry Viewer</h1> | |||
| <table> | |||
| <thead> | |||
| <tr> | |||
| <th>Repository</th> | |||
| <th>Tags</th> | |||
| </tr> | |||
| </thead> | |||
| <tbody> | |||
| {% for repo in data %} | |||
| <tr> | |||
| <td><a href="{% url 'repository_detail' repo.repository %}">{{ repo.repository }}</a></td> | |||
| <td> | |||
| {% for tag in repo.tags %} | |||
| <span>{{ tag }}</span>{% if not forloop.last %}, {% endif %} | |||
| {% endfor %} | |||
| </td> | |||
| </tr> | |||
| {% endfor %} | |||
| </tbody> | |||
| </table> | |||
| </body> | |||
| </html> | |||
| @ -0,0 +1,48 @@ | |||
| <!DOCTYPE html> | |||
| <html> | |||
| <head> | |||
| <title>Repository Details - {{ repository }}</title> | |||
| <style> | |||
| table { | |||
| width: 100%; | |||
| border-collapse: collapse; | |||
| } | |||
| th, td { | |||
| padding: 8px 12px; | |||
| border: 1px solid #ddd; | |||
| text-align: left; | |||
| } | |||
| th { | |||
| background-color: #f4f4f4; | |||
| } | |||
| .delete-btn { | |||
| color: red; | |||
| cursor: pointer; | |||
| text-decoration: none; | |||
| } | |||
| </style> | |||
| </head> | |||
| <body> | |||
| <h1>Tags for {{ repository }}</h1> | |||
| {% if error %} | |||
| <p style="color: red;">{{ error }}</p> | |||
| {% endif %} | |||
| <table> | |||
| <thead> | |||
| <tr> | |||
| <th>Tags</th> | |||
| <th>Actions</th> | |||
| </tr> | |||
| </thead> | |||
| <tbody> | |||
| {% for tag in tags %} | |||
| <tr> | |||
| <td>{{ tag }}</td> | |||
| <td><a href="{% url 'delete_tag' repository tag %}" class="delete-btn" onclick="return confirm('Are you sure you want to delete this tag?');">Delete</a></td> | |||
| </tr> | |||
| {% endfor %} | |||
| </tbody> | |||
| </table> | |||
| <p><a href="{% url 'registry_list' %}">Back to Repository List</a></p> | |||
| </body> | |||
| </html> | |||
| @ -0,0 +1,3 @@ | |||
| from django.test import TestCase | |||
| # Create your tests here. | |||
| @ -0,0 +1,9 @@ | |||
| from django.urls import path | |||
| from . import views | |||
| urlpatterns = [ | |||
| path('', views.registry_list, name='registry_list'), | |||
| path('<str:repository_name>/', views.repository_detail, name='repository_detail'), | |||
| path('<str:repository_name>/delete/<str:tag>/', views.delete_tag, name='delete_tag'), | |||
| ] | |||
| @ -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.'}) | |||