This repository was archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Check update and trigger update #121
Draft
nsosio
wants to merge
11
commits into
premAI-io:main
Choose a base branch
from
nsosio:feat/premd-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6cf94af
first draft of update-available endpoint
nsosio e56ac7c
restart daemon with new image
nsosio 3f2eca8
updated endpoints
nsosio b72b68f
bugfix in start up event and making changes based on feedback
nsosio f9afaba
wrongly deleted utils.container_exists
nsosio b08d530
removed commentt in SENTRY_DNS
nsosio 625e39d
Merge remote-tracking branch 'upstream/main' into feat/premd-update
nsosio 5c4668c
Apply suggestions from code review
nsosio ef8fb48
Merge branch 'feat/premd-update' of https://github.com/nsosio/prem-da…
nsosio e4c0333
added requirement; removed reference to generate container name and r…
nsosio 41691fa
merged main
nsosio 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| import logging | ||
| import re | ||
| import subprocess | ||
| import time | ||
| import xml.etree.ElementTree as ET | ||
|
|
||
| import docker | ||
| import requests | ||
| import torch | ||
| from bs4 import BeautifulSoup | ||
| from packaging.version import parse as parse_version | ||
|
|
||
| from app.core import config | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| PREMD_IMAGE = config.PREMD_IMAGE | ||
| DEFAULT_PORT = config.DEFAULT_PORT | ||
| SERVICES = [] | ||
| REGISTRIES = config.PREM_REGISTRY_URL.strip().split() | ||
| INTERFACES = [ | ||
|
|
@@ -198,6 +204,128 @@ def get_gpu_info(): | |
| return gpu_name, total_memory_value, used_memory_value, mem_percentage | ||
|
|
||
|
|
||
| def extract_labels_from_html_file(html_content, class_names): | ||
| soup = BeautifulSoup(html_content, "html.parser") | ||
| labels = soup.select(class_names) | ||
| return (label.get_text() for label in labels) | ||
|
|
||
|
|
||
| def find_maximum_label(labels): | ||
| pattern = re.compile(r"v\d+\.\d+\.\d+$") | ||
| return max(filter(pattern.match, labels), default=None, key=parse_version) | ||
|
|
||
|
|
||
| def get_premd_last_tag(owner, repository, package): | ||
| response = requests.get( | ||
| f"https://github.com/{owner}/{repository}/pkgs/container/{package}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not calling the API instead?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we'd have to hardcode an auth token (this particular API requires authentication even to access public info) |
||
| ) | ||
| try: | ||
| labels = extract_labels_from_html_file( | ||
| response.content, ".Label.mr-1.mb-2.text-normal" | ||
| ) | ||
| except Exception as e: | ||
| logger.info(f"Unexpected error: {e}") | ||
|
nsosio marked this conversation as resolved.
|
||
| return "latest" | ||
| else: | ||
| return find_maximum_label(labels) | ||
|
|
||
|
|
||
| def get_local_docker_image_tags(owner, repository): | ||
| try: | ||
| client = get_docker_client() | ||
| image = client.images.get(f"ghcr.io/{owner}/{repository}") | ||
| return image.tags | ||
| except Exception as e: | ||
| logger.info(f"Unexpected error: {e}") | ||
| return [] | ||
|
|
||
|
|
||
| def create_new_container(image_name, image_tag, new_container_name, old_container_name): | ||
| client = get_docker_client() | ||
| old_container = client.containers.get(old_container_name) | ||
|
|
||
| if is_gpu_available(): | ||
| device_requests = [ | ||
| docker.types.DeviceRequest(device_ids=["all"], capabilities=[["gpu"]]) | ||
| ] | ||
| else: | ||
| device_requests = [] | ||
|
|
||
| volumes = {} | ||
| for mount in old_container.attrs["Mounts"]: | ||
| source = mount["Source"] | ||
| target = mount["Destination"] | ||
| mode = mount["Mode"] | ||
| volumes[source] = {"bind": target, "mode": mode} | ||
|
|
||
| current_ports = old_container.attrs["HostConfig"]["PortBindings"] | ||
| current_port = list(current_ports.items())[0] | ||
|
|
||
| logger.info( | ||
| f"Starting new container {new_container_name} with image {image_name}:{image_tag} at port {current_port[0]}" | ||
| ) | ||
| new_container = client.containers.create( | ||
| image=f"{image_name}:{image_tag}", | ||
| name=new_container_name, | ||
| ports={current_port[0]: current_port[1]}, | ||
| volumes=volumes, | ||
| environment=old_container.attrs["Config"]["Env"], | ||
| device_requests=device_requests, | ||
| network_mode=old_container.attrs["HostConfig"]["NetworkMode"], | ||
| detach=True, | ||
| ) | ||
| return new_container | ||
|
|
||
|
|
||
| def update_and_remove_old_container(old_container_name): | ||
| client = get_docker_client() | ||
| logger.info(f"Stopping {old_container_name}") | ||
| old_container = client.containers.get(old_container_name) | ||
| old_container.stop() | ||
|
|
||
|
|
||
| def update_container(): | ||
| new_container = create_new_container( | ||
| PREMD_IMAGE, "latest", "new_container", "premd" | ||
| ) | ||
| update_and_remove_old_container("premd") | ||
| new_container.start() | ||
| new_container.rename("premd") | ||
|
|
||
|
|
||
| def check_host_port_availability(host_port, timeout=30): | ||
| start_time = time.time() | ||
| client = get_docker_client() | ||
|
|
||
| while True: | ||
| if time.time() - start_time > timeout: | ||
| return False | ||
|
|
||
| containers = client.containers.list() | ||
| port_used = any( | ||
| f"{host_port}/tcp" in container.ports | ||
| for container in containers | ||
| if container.status == "running" | ||
| ) | ||
|
|
||
| if not port_used: | ||
| return True | ||
|
|
||
| time.sleep(1) | ||
|
|
||
|
|
||
| def container_exists(container_name): | ||
| try: | ||
| client = get_docker_client() | ||
| _ = client.containers.get(container_name) | ||
| return True | ||
| except docker.errors.NotFound: | ||
| return False | ||
| except docker.errors.APIError as e: | ||
| logging.error(f"Error checking container existence: {e}") | ||
| return False | ||
|
|
||
|
|
||
| cached_domain = None | ||
|
|
||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ torchvision==0.15.2 | |
| torchaudio==2.0.2 | ||
| sentry-sdk==1.26.0 | ||
| psutil==5.9.5 | ||
| beautifulsoup4==4.12.2 | ||
| packaging==23.1 | ||
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.