diff --git a/scripts/install-moonraker.sh b/scripts/install-moonraker.sh index 98a135b..642152b 100755 --- a/scripts/install-moonraker.sh +++ b/scripts/install-moonraker.sh @@ -15,6 +15,17 @@ INSTANCE_ALIAS="${MOONRAKER_ALIAS:-moonraker}" SERVICE_VERSION="1" MACHINE_PROVIDER="systemd_cli" +package_decode_script=$( cat << EOF +import sys +import json +try: + ret = json.load(sys.stdin) +except Exception: + exit(0) +sys.stdout.write(' '.join(ret['debian'])) +EOF +) + # Step 2: Clean up legacy installation cleanup_legacy() { if [ -f "/etc/init.d/moonraker" ]; then @@ -30,18 +41,30 @@ cleanup_legacy() { # Step 3: Install packages install_packages() { - PKGLIST="python3-virtualenv python3-dev" - PKGLIST="${PKGLIST} libopenjp2-7 python3-libgpiod liblmdb-dev" - PKGLIST="${PKGLIST} libsodium-dev zlib1g-dev libjpeg-dev packagekit" - PKGLIST="${PKGLIST} wireless-tools curl" - # Update system package info report_status "Running apt-get update..." sudo apt-get update --allow-releaseinfo-change + system_deps="${SRCDIR}/scripts/system-dependencies.json" + if [ -f "${system_deps}" ]; then + if [ ! -x "$(command -v python3)" ]; then + report_status "Installing python3 base package..." + sudo apt-get install --yes python3 + fi + PKGS="$( cat ${system_deps} | python3 -c "${package_decode_script}" )" + + else + echo "Error: system-dependencies.json not found, falling back to legacy pacakge list" + PKGLIST="${PKGLIST} python3-virtualenv python3-dev python3-libgpiod liblmdb-dev" + PKGLIST="${PKGLIST} libopenjp2-7 libsodium-dev zlib1g-dev libjpeg-dev packagekit" + PKGLIST="${PKGLIST} wireless-tools curl" + PKGS=${PKGLIST} + fi + # Install desired packages - report_status "Installing packages..." - sudo apt-get install --yes ${PKGLIST} + report_status "Installing Moonraker Dependencies:" + report_status "${PKGS}" + sudo apt-get install --yes ${PKGS} } # Step 4: Create python virtual environment diff --git a/scripts/make_sysdeps.py b/scripts/make_sysdeps.py new file mode 100755 index 0000000..d052d60 --- /dev/null +++ b/scripts/make_sysdeps.py @@ -0,0 +1,57 @@ +#! /usr/bin/python3 +# Create system dependencies json file from the install script +# +# Copyright (C) 2023 Eric Callahan +# +# This file may be distributed under the terms of the GNU GPLv3 license +from __future__ import annotations +import argparse +import pathlib +import json +import re +from typing import List, Dict + +def make_sysdeps(input: str, output: str, distro: str, truncate: bool) -> None: + sysdeps: Dict[str, List[str]] = {} + outpath = pathlib.Path(output).expanduser().resolve() + if outpath.is_file() and not truncate: + sysdeps = json.loads(outpath.read_bytes()) + inst_path: pathlib.Path = pathlib.Path(input).expanduser().resolve() + if not inst_path.is_file(): + raise Exception(f"Unable to locate install script: {inst_path}") + data = inst_path.read_text() + plines: List[str] = re.findall(r'PKGLIST="(.*)"', data) + plines = [p.lstrip("${PKGLIST}").strip() for p in plines] + packages: List[str] = [] + for line in plines: + packages.extend(line.split()) + sysdeps[distro] = packages + outpath.write_text(json.dumps(sysdeps, indent=4)) + + +if __name__ == "__main__": + def_path = pathlib.Path(__file__).parent + desc = ( + "make_sysdeps - generate system dependency json file from an install script" + ) + parser = argparse.ArgumentParser(description=desc) + parser.add_argument( + "-i", "--input", metavar="", + help="path of the install script to read", + default=f"{def_path}/install-moonraker.sh" + ) + parser.add_argument( + "-o", "--output", metavar="", + help="path of the system dependency file to write", + default=f"{def_path}/system-dependencies.json" + ) + parser.add_argument( + "-d", "--distro", metavar="", + help="linux distro for dependencies", default="debian" + ) + parser.add_argument( + "-t", "--truncate", action="store_true", + help="truncate output file" + ) + args = parser.parse_args() + make_sysdeps(args.input, args.output, args.distro, args.truncate) diff --git a/scripts/system-dependencies.json b/scripts/system-dependencies.json new file mode 100644 index 0000000..b993e66 --- /dev/null +++ b/scripts/system-dependencies.json @@ -0,0 +1,15 @@ +{ + "debian": [ + "python3-virtualenv", + "python3-dev", + "python3-libgpiod", + "liblmdb-dev", + "libopenjp2-7", + "libsodium-dev", + "zlib1g-dev", + "libjpeg-dev", + "packagekit", + "wireless-tools", + "curl" + ] +} \ No newline at end of file