|
| 1 | +# ======================================================================= # |
| 2 | +# Copyright (C) 2020 - 2025 Dominik Willner <th33xitus@gmail.com> # |
| 3 | +# # |
| 4 | +# This file is part of KIAUH - Klipper Installation And Update Helper # |
| 5 | +# https://github.com/dw-0/kiauh # |
| 6 | +# # |
| 7 | +# This file may be distributed under the terms of the GNU GPLv3 license # |
| 8 | +# ======================================================================= # |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import shutil |
| 12 | +from dataclasses import dataclass, field |
| 13 | +from pathlib import Path |
| 14 | +from subprocess import CalledProcessError, run |
| 15 | + |
| 16 | +from components.moonraker.moonraker import Moonraker |
| 17 | +from core.instance_manager.base_instance import BaseInstance |
| 18 | +from core.logger import Logger |
| 19 | +from extensions.spoolman import ( |
| 20 | + MODULE_PATH, |
| 21 | + SPOOLMAN_COMPOSE_FILE, |
| 22 | + SPOOLMAN_DIR, |
| 23 | + SPOOLMAN_DOCKER_IMAGE, |
| 24 | +) |
| 25 | +from utils.sys_utils import get_system_timezone |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class Spoolman: |
| 30 | + suffix: str |
| 31 | + base: BaseInstance = field(init=False, repr=False) |
| 32 | + dir: Path = SPOOLMAN_DIR |
| 33 | + data_dir: Path = field(init=False) |
| 34 | + |
| 35 | + def __post_init__(self): |
| 36 | + self.base: BaseInstance = BaseInstance(Moonraker, self.suffix) |
| 37 | + self.data_dir = self.base.data_dir |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def is_container_running() -> bool: |
| 41 | + """Check if the Spoolman container is running""" |
| 42 | + try: |
| 43 | + result = run( |
| 44 | + ["docker", "compose", "-f", str(SPOOLMAN_COMPOSE_FILE), "ps", "-q"], |
| 45 | + capture_output=True, |
| 46 | + text=True, |
| 47 | + check=True, |
| 48 | + ) |
| 49 | + return bool(result.stdout.strip()) |
| 50 | + except CalledProcessError: |
| 51 | + return False |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def is_docker_available() -> bool: |
| 55 | + """Check if Docker is installed and available""" |
| 56 | + try: |
| 57 | + run(["docker", "--version"], capture_output=True, check=True) |
| 58 | + return True |
| 59 | + except (CalledProcessError, FileNotFoundError): |
| 60 | + return False |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def is_docker_compose_available() -> bool: |
| 64 | + """Check if Docker Compose is installed and available""" |
| 65 | + try: |
| 66 | + # Try modern docker compose command |
| 67 | + run(["docker", "compose", "version"], capture_output=True, check=True) |
| 68 | + return True |
| 69 | + except (CalledProcessError, FileNotFoundError): |
| 70 | + # Try legacy docker-compose command |
| 71 | + try: |
| 72 | + run(["docker-compose", "--version"], capture_output=True, check=True) |
| 73 | + return True |
| 74 | + except (CalledProcessError, FileNotFoundError): |
| 75 | + return False |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def create_docker_compose() -> bool: |
| 79 | + """Copy the docker-compose.yml file for Spoolman and set system timezone""" |
| 80 | + try: |
| 81 | + shutil.copy( |
| 82 | + MODULE_PATH.joinpath("assets/docker-compose.yml"), |
| 83 | + SPOOLMAN_COMPOSE_FILE, |
| 84 | + ) |
| 85 | + |
| 86 | + # get system timezone |
| 87 | + timezone = get_system_timezone() |
| 88 | + |
| 89 | + with open(SPOOLMAN_COMPOSE_FILE, "r") as f: |
| 90 | + content = f.read() |
| 91 | + |
| 92 | + content = content.replace("TZ=Europe/Stockholm", f"TZ={timezone}") |
| 93 | + |
| 94 | + with open(SPOOLMAN_COMPOSE_FILE, "w") as f: |
| 95 | + f.write(content) |
| 96 | + |
| 97 | + return True |
| 98 | + except Exception as e: |
| 99 | + Logger.print_error(f"Error creating Docker Compose file: {e}") |
| 100 | + return False |
| 101 | + |
| 102 | + @staticmethod |
| 103 | + def start_container() -> bool: |
| 104 | + """Start the Spoolman container""" |
| 105 | + try: |
| 106 | + run( |
| 107 | + ["docker", "compose", "-f", str(SPOOLMAN_COMPOSE_FILE), "up", "-d"], |
| 108 | + check=True, |
| 109 | + ) |
| 110 | + return True |
| 111 | + except CalledProcessError as e: |
| 112 | + Logger.print_error(f"Failed to start Spoolman container: {e}") |
| 113 | + return False |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def update_container() -> bool: |
| 117 | + """Update the Spoolman container""" |
| 118 | + |
| 119 | + def __get_image_id() -> str: |
| 120 | + """Get the image ID of the Spoolman Docker image""" |
| 121 | + try: |
| 122 | + result = run( |
| 123 | + ["docker", "images", "-q", SPOOLMAN_DOCKER_IMAGE], |
| 124 | + capture_output=True, |
| 125 | + text=True, |
| 126 | + check=True, |
| 127 | + ) |
| 128 | + return result.stdout.strip() |
| 129 | + except CalledProcessError: |
| 130 | + raise Exception("Failed to get Spoolman Docker image ID") |
| 131 | + |
| 132 | + try: |
| 133 | + old_image_id = __get_image_id() |
| 134 | + Logger.print_status("Pulling latest Spoolman image...") |
| 135 | + Spoolman.pull_image() |
| 136 | + new_image_id = __get_image_id() |
| 137 | + Logger.print_status("Tearing down old Spoolman container...") |
| 138 | + Spoolman.tear_down_container() |
| 139 | + Logger.print_status("Spinning up new Spoolman container...") |
| 140 | + Spoolman.start_container() |
| 141 | + if old_image_id != new_image_id: |
| 142 | + Logger.print_status("Removing old Spoolman image...") |
| 143 | + run(["docker", "rmi", old_image_id], check=True) |
| 144 | + return True |
| 145 | + |
| 146 | + except CalledProcessError as e: |
| 147 | + Logger.print_error(f"Failed to update Spoolman container: {e}") |
| 148 | + return False |
| 149 | + |
| 150 | + @staticmethod |
| 151 | + def tear_down_container() -> bool: |
| 152 | + """Stop and remove the Spoolman container""" |
| 153 | + try: |
| 154 | + run( |
| 155 | + ["docker", "compose", "-f", str(SPOOLMAN_COMPOSE_FILE), "down"], |
| 156 | + check=True, |
| 157 | + ) |
| 158 | + return True |
| 159 | + except CalledProcessError as e: |
| 160 | + Logger.print_error(f"Failed to tear down Spoolman container: {e}") |
| 161 | + return False |
| 162 | + |
| 163 | + @staticmethod |
| 164 | + def pull_image() -> bool: |
| 165 | + """Pull the Spoolman Docker image""" |
| 166 | + try: |
| 167 | + run(["docker", "pull", SPOOLMAN_DOCKER_IMAGE], check=True) |
| 168 | + return True |
| 169 | + except CalledProcessError as e: |
| 170 | + Logger.print_error(f"Failed to pull Spoolman Docker image: {e}") |
| 171 | + return False |
| 172 | + |
| 173 | + @staticmethod |
| 174 | + def remove_image() -> bool: |
| 175 | + """Remove the Spoolman Docker image""" |
| 176 | + try: |
| 177 | + image_exists = run( |
| 178 | + ["docker", "images", "-q", SPOOLMAN_DOCKER_IMAGE], |
| 179 | + capture_output=True, |
| 180 | + text=True, |
| 181 | + ).stdout.strip() |
| 182 | + if not image_exists: |
| 183 | + Logger.print_info("Spoolman Docker image not found. Nothing to remove.") |
| 184 | + return False |
| 185 | + |
| 186 | + run(["docker", "rmi", SPOOLMAN_DOCKER_IMAGE], check=True) |
| 187 | + return True |
| 188 | + except CalledProcessError as e: |
| 189 | + Logger.print_error(f"Failed to remove Spoolman Docker image: {e}") |
| 190 | + return False |
0 commit comments