Implement version checking and basic refactoring
All checks were successful
Build Docker Image / build (push) Successful in 27s

This commit is contained in:
2025-11-29 13:58:22 -06:00
parent dcfb2b89d5
commit cdc72f1557
5 changed files with 64 additions and 47 deletions

View File

@@ -6,7 +6,7 @@ WORKDIR /app
COPY . . COPY . .
RUN crontab autopkg.cron RUN crontab docker/5autopkg
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
CMD python main.py; crond -f CMD python main.py; crond -f

49
main.py
View File

@@ -1,17 +1,21 @@
import os import os
import re
import logging import logging
from configparser import ConfigParser from configparser import ConfigParser
import requests import requests
from logger import setup_log from logger import setup_log
from package import pkg from package import Package
## Autopkg Apps ## Autopkg Apps
apps = { apps = {
"bitcoinknots": "https://api.github.com/repos/bitcoinknots/bitcoin/releases/latest", "bitcoinknots": {
"electrs": "https://api.github.com/repos/romanz/electrs/releases/latest" "url": "https://api.github.com/repos/bitcoinknots/bitcoin/releases/latest",
"versioningScheme": r"^v(\d+)\.(\d+)\.(\d+)$"
},
"electrs": {"url": "https://api.github.com/repos/romanz/electrs/releases/latest"}
} }
@@ -24,16 +28,18 @@ class Autopkg:
# Environment Setup # Environment Setup
target = os.getenv("APKG_TARGET") target = os.getenv("APKG_TARGET")
os.environ["APKG_RUNTIME_DIR"] = os.path.dirname(os.path.abspath(__file__)) # Set runtime directory
if target == "prod": match target:
case "prod":
self.datafile = "/etc/autopkg/data.ini" self.datafile = "/etc/autopkg/data.ini"
elif target == "test" or target == "debug": case "test" | "debug":
logging.warning("Running autopkg in testing mode!") logging.warning("Running autopkg in testing mode!")
self.datafile = os.path.dirname(os.path.abspath(__file__)) + "/test/data.ini" self.datafile = os.getenv("APKG_RUNTIME_DIR") + "/test/data.ini"
if os.path.exists(self.datafile): os.remove(self.datafile) if os.path.exists(self.datafile): os.remove(self.datafile)
else: case _:
raise ValueError("APKG_TARGET environment variable not setup correctly!") raise ValueError("APKG_TARGET environment variable not setup correctly!")
## Config File Setup ## Config File Setup
@@ -57,24 +63,37 @@ class Autopkg:
# Main Application # Main Application
def main(self): def main(self):
# Handle apps (get versions) # Handle apps (get versions)
for app, url in apps.items(): for app, info in apps.items():
ini_version = self.data.getint("Versions", app, fallback=0) # Version previously packaged from data.ini ini_version = self.data.get("Versions", app, fallback=None) # Version previously packaged from data.ini
response = requests.get(url) # Fetch data from API response = requests.get(info["url"]) # Fetch data from API
# Parse through JSON data # Parse through JSON data
if response.status_code == 200: if response.status_code == 200:
json = response.json() json = response.json()
version = json["id"] # App version from API tag = json["tag_name"] # App version from API
pre_release = json["prerelease"]
# Package new versions if available # Package new versions if available
if version != ini_version: if tag != ini_version and not pre_release:
logging.info(f"Found new version ({version}) for {app}, packaging..") logging.info(f"Found new version ({tag}) for {app}, packaging..")
if pkg(app, version): try:
ini_version = str(version) if Package(app, tag).build():
ini_version = str(tag)
self.write_data() self.write_data()
except Exception as e:
logging.error(f"Error starting docker container for {app}")
def version_is_higher(tag: str, info: dict) -> (str, None):
regular_scheme = r"^v(\d+)\.(\d+)\.(\d+)$"
try:
version = re.match(info["versioningScheme"], tag)
except:
version = re.match(regular_scheme)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -1,37 +1,32 @@
import os
import logging import logging
import docker import docker
from docker import DockerClient
from logger import setup_log from logger import setup_log
# Container Management # Container Management
## Build Image ## Build Image
def build(client: DockerClient, app: str): class Package:
with open(f"{app}/Dockerfile") as dockerfile: def __init__(self, app: str, version: str):
image, _ = client.images.build(fileobj=dockerfile, tag=app)
return image
## Run Image
def run(client: DockerClient, image, app: str):
container = client.containers.run(image, detach=True, name=f"autopkg-{app}")
return container
# Main Packaging Function
def pkg(app, version):
# Logging Setup # Logging Setup
setup_log() setup_log()
client = docker.from_env() # Docker.sock connection # Variable Setup
self.runtime_dir = os.getenv("APKG_RUNTIME_DIR")
self.client = docker.from_env() # Docker.sock connection
self.app = app
self.version = version
try: # Main Packaging Function
image = build(client, app) def build(self):
with open(f"{self.runtime_dir}/{self.app}/Dockerfile") as dockerfile:
self.image, _ = self.client.images.build(fileobj=dockerfile, tag=f"{self.app}-{self.version}")
container = run(client, image, app) # Run Container
self.run()
return True def run(self, image):
except Exception as e: container = self.client.containers.run(self.image, detach=True, name=f"autopkg-{self.app}")
logging.error(f"Error starting docker container for {app}") return container
return False

3
versioning.py Normal file
View File

@@ -0,0 +1,3 @@
import re