diff --git a/Dockerfile b/Dockerfile index 82717e7..6bed1af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM python:alpine as base +FROM python:alpine + +ENV APKG_IS_DOCKER=true WORKDIR /app diff --git a/main.py b/main.py index 24b416a..8d29186 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import os import logging -import configparser +from configparser import ConfigParser import requests @@ -15,56 +15,68 @@ apps = { } -def main(): - # Handle apps (get versions) - for app, url in apps.items(): - ini_version = data["Versions"][app] # Version previously packaged from data.ini +class Autopkg: + def __init__(self): + # Logging Setup + setup_log() - response = requests.get(url) + logging.info("Running autopkg (c) 2025 phantom https://shadeouts.net/") - # Parse through JSON data - if response.status_code == 200: - json = response.json() - version = json["id"] + # Environment Setup + target = os.getenv("APKG_TARGET") - ini_version = str(version) + if target == "prod": + self.datafile = "/etc/autopkg/data.ini" + elif target == "test" or target == "debug": + logging.warning("Running autopkg in testing mode!") - # Package new versions if available - if version != ini_version: - logging.info(f"Found new version ({version}) for {app}, packaging..") + self.datafile = os.path.dirname(os.path.abspath(__file__)) + "/test/data.ini" - pkg(app, version) + if os.path.exists(self.datafile): os.remove(self.datafile) + else: + raise ValueError("APKG_TARGET environment variable not setup correctly!") - # Write changes - with open("data.ini", "w") as datafile: - data.write(datafile) + ## Config File Setup + self.data = ConfigParser() + + self.data.read(self.datafile) + + ## Setup file if not present + if "Versions" not in self.data: + self.data.add_section("Versions") + + self.write_data() + + + # Write datafile changes + def write_data(self): + with open(self.datafile, "w") as f: + self.data.write(f) + + + # Main Application + def main(self): + # Handle apps (get versions) + for app, url in apps.items(): + ini_version = self.data.getint("Versions", app, fallback=0) # Version previously packaged from data.ini + + response = requests.get(url) # Fetch data from API + + # Parse through JSON data + if response.status_code == 200: + json = response.json() + version = json["id"] # App version from API + + # Package new versions if available + if version != ini_version: + logging.info(f"Found new version ({version}) for {app}, packaging..") + + if pkg(app, version): + ini_version = str(version) + + self.write_data() -# Main Application if __name__ == "__main__": - # Logging Setup - setup_log() - - logging.info("Running Autopkg (c) 2025 phantom https://shadeouts.net/") - - # Environment Setup - target = os.getenv("APKG_TARGET") - - if target == "prod": - datafile = "/etc/autopkg/data.ini" - elif target == "test" or None: - logging.warning("Running autopkg in testing mode!") - - datafile = "test/data.ini" - - ## Config File Setup - data = configparser.ConfigParser() - - data.read(datafile) - - ## Setup file if not present - if "Versions" not in data: - data["Versions"] = {} - # Call main function - main() + Autopkg().main() diff --git a/package.py b/package.py index f3fa476..1e18a3a 100644 --- a/package.py +++ b/package.py @@ -29,5 +29,9 @@ def pkg(app, version): image = build(client, app) container = run(client, image, app) + + return True except Exception as e: logging.error(f"Error starting docker container for {app}") + + return False