Skip to content

Commit

Permalink
Add support to Docker images tags in Kathará settings (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiazzi committed Jun 14, 2023
1 parent 9fe1829 commit 430666a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/Kathara/cli/ui/setting/CommonOptionsHandler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from . import utils as setting_utils
from ....webhooks.DockerHubApi import DockerHubApi
from ....exceptions import HTTPConnectionError
from ....foundation.cli.ui.setting.OptionsHandler import OptionsHandler
from ....manager.Kathara import Kathara
Expand All @@ -10,6 +9,7 @@
from ....utils import exec_by_platform
from ....validator.ImageValidator import ImageValidator
from ....validator.TerminalValidator import TerminalValidator
from ....webhooks.DockerHubApi import DockerHubApi

SHELLS_HINT = ["/bin/bash", "/bin/sh", "/bin/ash", "/bin/ksh", "/bin/zsh", "/bin/fish", "/bin/csh", "/bin/tcsh"]
TERMINALS_OSX = ["Terminal", "iTerm"]
Expand Down Expand Up @@ -52,9 +52,7 @@ def add_items(self, current_menu: ConsoleMenu, menu_formatter: MenuFormatBuilder
)

try:
for image in DockerHubApi.get_images():
image_name = "%s/%s" % (image['namespace'], image['name'])

for image_name in DockerHubApi.get_tagged_images():
select_image_menu.append_item(FunctionItem(text=image_name,
function=setting_utils.update_setting_value,
args=['image', image_name],
Expand Down
2 changes: 1 addition & 1 deletion src/Kathara/setting/Setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ONE_WEEK: int = 604800

DEFAULTS: Dict[str, Any] = {
"image": 'kathara/quagga',
"image": 'kathara/base',
"manager_type": 'docker',
"terminal": utils.exec_by_platform(lambda: '/usr/bin/xterm', lambda: '', lambda: 'Terminal'),
"open_terminals": True,
Expand Down
49 changes: 43 additions & 6 deletions src/Kathara/webhooks/DockerHubApi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from functools import partial
from multiprocessing.dummy import Pool

import requests

from ..exceptions import HTTPConnectionError
from ..utils import get_pool_size, chunk_list

DOCKER_HUB_KATHARA_URL = "https://hub.docker.com/v2/repositories/kathara/?page_size=-1"

Expand All @@ -12,17 +15,17 @@
class DockerHubApi(object):
@staticmethod
def get_images() -> filter:
"""
Get the Kathara Docker Hub account image list, excluding:
- Private Images
- Plugins or other types of images
- Images in the EXCLUDED_IMAGES list
"""Retrieve the list of available Kathara images on Docker Hub.
This method retrieves the list of images from the Kathara Docker Hub account, excluding private images,
plugins, and images specified in the EXCLUDED_IMAGES list.
Returns:
filter: filtered list of currently available images that satisfy the previous conditions.
filter: A filtered list of currently available images that satisfy the specified conditions.
Raises:
HTTPConnectionError: If there is a connection error with the Docker Hub.
"""
try:
result = requests.get(DOCKER_HUB_KATHARA_URL)
Expand All @@ -37,3 +40,37 @@ def get_images() -> filter:
lambda x: not x['is_private'] and x['repository_type'] == 'image' and x['name'] not in EXCLUDED_IMAGES,
result.json()['results']
)

@staticmethod
def get_tagged_images() -> list[str]:
"""Returns the tagged names of all the active Kathara Docker on from Docker Hub.
Returns:
list[str]: A list of strings representing the tagged Docker images in the
format "kathara/{image_name}:{tag}".
Raises:
HTTPConnectionError: If there is a connection error with the Docker Hub.
"""
images = list(DockerHubApi.get_images())

tagged_images = []

def get_image_tag(tags, image):
image_name = f"{image['namespace']}/{image['name']}"
res = requests.get(f"https://hub.docker.com/v2/repositories/{image_name}/tags/?page_size=-1&ordering")
tags.extend(list(dict(map(lambda x:
(x['digest'],
f"{image_name}:{x['name']}" if x['name'] != "latest" else image_name),
filter(lambda x: x['tag_status'] == 'active',
res.json()['results']))).values()))

pool_size = get_pool_size()
machines_pool = Pool(pool_size)

items = chunk_list(images, pool_size)

for chunk in items:
machines_pool.map(func=partial(get_image_tag, tagged_images), iterable=chunk)

return sorted(tagged_images)

0 comments on commit 430666a

Please sign in to comment.