-
Notifications
You must be signed in to change notification settings - Fork 6
Add portainer registry configuration 🚨 #1125
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
Merged
YuryHrytsuk
merged 6 commits into
ITISFoundation:main
from
YuryHrytsuk:add-portainer-registry-configuration
Jul 16, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7089d1
Automatically configure registry in portainer
YuryHrytsuk 669f2cb
Merge remote-tracking branch 'upstream/main' into add-portainer-regis…
YuryHrytsuk b264a2e
Merge remote-tracking branch 'upstream/main' into add-portainer-regis…
YuryHrytsuk d672244
Update
YuryHrytsuk 603160c
Imrpove wait for it installation
YuryHrytsuk d1eadae
Merge branch 'main' into add-portainer-registry-configuration
YuryHrytsuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| wait4x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
services/portainer/scripts/configure_portainer_registry.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import logging | ||
| import os | ||
| from enum import Enum | ||
| from typing import TypedDict | ||
|
|
||
| import requests | ||
| from tenacity import retry | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/portainer.Registry | ||
| class RegistryType(Enum): | ||
| DOCKER_HUB = 6 | ||
|
|
||
|
|
||
| class Registry(TypedDict): | ||
| Id: int | ||
| Name: str | ||
| URL: str | ||
| Authentication: bool | ||
| Username: str | ||
| Type: RegistryType | ||
|
|
||
|
|
||
| @retry | ||
| def get_portainer_api_auth_token( | ||
| portainer_api_url: str, portainer_username: str, portainer_password: str | ||
| ) -> str: | ||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/auth/AuthenticateUser | ||
| response = requests.post( | ||
| f"{portainer_api_url}/auth", | ||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/auth.authenticatePayload | ||
| json={"Username": portainer_username, "Password": portainer_password}, | ||
| ) | ||
|
|
||
| try: | ||
| response.raise_for_status() | ||
| except requests.HTTPError as e: | ||
| logger.error("Failed to authenticate with Portainer API: %s", e.response.text) | ||
| raise | ||
|
|
||
| return response.json()["jwt"] | ||
|
|
||
|
|
||
| @retry | ||
| def get_registries(portainer_api_url: str, auth_token: str) -> list[Registry]: | ||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/registries/RegistryList | ||
| response = requests.get( | ||
| f"{portainer_api_url}/registries", | ||
| headers={"Authorization": f"Bearer {auth_token}"}, | ||
| ) | ||
|
|
||
| try: | ||
| response.raise_for_status() | ||
| except requests.HTTPError as e: | ||
| logger.error("Failed to fetch registries: %s", e.response.text) | ||
| raise | ||
|
|
||
| return response.json() | ||
|
|
||
|
|
||
| @retry | ||
| def create_authenticated_dockerhub_registry( | ||
| portainer_api_url: str, | ||
| auth_token: str, | ||
| dockerhub_username: str, | ||
| dockerhub_password: str, | ||
| registry_name: str = "IT'IS Foundation", | ||
| ) -> None: | ||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/registries/RegistryCreate | ||
| response = requests.post( | ||
| f"{portainer_api_url}/registries", | ||
| headers={"Authorization": f"Bearer {auth_token}"}, | ||
| # https://app.swaggerhub.com/apis/portainer/portainer-ce/2.27.6#/registries.registryCreatePayload | ||
| json={ | ||
| "name": registry_name, | ||
| "url": "docker.io", | ||
| "authentication": True, | ||
| "username": dockerhub_username, | ||
| "password": dockerhub_password, | ||
| "type": RegistryType.DOCKER_HUB.value, | ||
| }, | ||
| ) | ||
|
|
||
| try: | ||
| response.raise_for_status() | ||
| except requests.HTTPError as e: | ||
| logger.error( | ||
| "Failed to create authenticated Docker Hub registry: %s", e.response.text | ||
| ) | ||
| raise | ||
|
|
||
| return response.json() | ||
|
|
||
|
|
||
| def main(): | ||
| logger.info("Configuring Portainer registries...") | ||
|
|
||
| portainer_username = os.environ["SERVICES_USER"] | ||
mrnicegyu11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| portainer_password = os.environ["SERVICES_PASSWORD"] | ||
| portainer_api_url = os.environ["PORTAINER_URL"] + "/api" | ||
|
|
||
| dockerhub_username = os.environ["DOCKER_HUB_LOGIN"] | ||
| dockerhub_password = os.environ["DOCKER_HUB_PASSWORD"] | ||
|
|
||
| portainer_jwt_token = get_portainer_api_auth_token( | ||
| portainer_api_url, portainer_username, portainer_password | ||
| ) | ||
|
|
||
| registries = get_registries(portainer_api_url, portainer_jwt_token) | ||
|
|
||
| if not any( | ||
| r["Type"] == RegistryType.DOCKER_HUB.value and r["Authentication"] is True | ||
| for r in registries | ||
| ): | ||
| logging.info("Creating authenticated Docker Hub registry in Portainer...") | ||
| create_authenticated_dockerhub_registry( | ||
| portainer_api_url, | ||
| portainer_jwt_token, | ||
| dockerhub_username, | ||
| dockerhub_password, | ||
| ) | ||
| else: | ||
| logging.info("Portainer already has an authenticated Docker Hub registry.") | ||
|
|
||
| logging.info("Portainer registries configuration completed.") | ||
|
|
||
|
|
||
| def configure_logging(): | ||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| configure_logging() | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| tenacity==9.1.2 | ||
| requests==2.32.4 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.