From 95eb4901efb5ff83b358cb52c1688d3140b4c39c Mon Sep 17 00:00:00 2001 From: th33xitus Date: Tue, 28 Feb 2023 15:54:04 +0100 Subject: [PATCH] refactor(klipper): implement blacklisted service names Signed-off-by: Dominik Willner --- scripts/klipper.sh | 33 ++++++++++++++++++++++++++++----- scripts/moonraker.sh | 2 +- scripts/octoprint.sh | 2 +- scripts/utilities.sh | 6 +++--- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/scripts/klipper.sh b/scripts/klipper.sh index 1381e31..4541c50 100644 --- a/scripts/klipper.sh +++ b/scripts/klipper.sh @@ -19,6 +19,29 @@ set -e #================ INSTALL KLIPPER ================# #=================================================# +### +# this function detects all installed klipper +# systemd instances and returns their absolute path +function klipper_systemd() { + local services + local blacklist + local ignore + local match + + ### + # any service that uses "klipper" in its own name but isn't a full klipper service must be blacklisted using + # this variable, otherwise they will be falsely recognized as klipper instances. E.g. "klipper-mcu.service" + # is not a klipper service, but related to klippers linux mcu, which also requires its own service file, hence + # it must be blacklisted. + blacklist="mcu" + + ignore="${SYSTEMD}/klipper-(${blacklist}).service" + match="${SYSTEMD}/klipper(-[0-9a-zA-Z]+)?.service" + + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype awk ! -regex "${ignore}" -regex "${match}" | sort) + echo "${services}" +} + function start_klipper_setup() { local klipper_initd_service local klipper_systemd_services @@ -34,7 +57,7 @@ function start_klipper_setup() { ### return early if klipper already exists klipper_initd_service=$(find_klipper_initd) - klipper_systemd_services=$(find_klipper_systemd) + klipper_systemd_services=$(klipper_systemd) if [[ -n ${klipper_initd_service} ]]; then error="Unsupported Klipper SysVinit service detected:" @@ -386,7 +409,7 @@ function write_example_printer_cfg() { #================================================# function remove_klipper_service() { - if [[ ! -e "${INITD}/klipper" ]] && [[ -z $(find_klipper_systemd) ]]; then + if [[ ! -e "${INITD}/klipper" ]] && [[ -z $(klipper_systemd) ]]; then return fi @@ -397,7 +420,7 @@ function remove_klipper_service() { sudo update-rc.d -f klipper remove sudo rm -f "${INITD}/klipper" "${ETCDEF}/klipper" else - for service in $(find_klipper_systemd | cut -d"/" -f5); do + for service in $(klipper_systemd | cut -d"/" -f5); do status_msg "Removing ${service} ..." sudo systemctl stop "${service}" sudo systemctl disable "${service}" @@ -534,10 +557,10 @@ function update_klipper() { function get_klipper_status() { local sf_count status py_ver - sf_count="$(find_klipper_systemd | wc -w)" + sf_count="$(klipper_systemd | wc -w)" ### detect an existing "legacy" klipper init.d installation - if [[ $(find_klipper_systemd | wc -w) -eq 0 ]] \ + if [[ $(klipper_systemd | wc -w) -eq 0 ]] \ && [[ $(find_klipper_initd | wc -w) -ge 1 ]]; then sf_count=1 fi diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 4ab5598..3baac95 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -60,7 +60,7 @@ function moonraker_setup_dialog() { ### return early if klipper is not installed local klipper_services - klipper_services=$(find_klipper_systemd) + klipper_services=$(klipper_systemd) if [[ -z ${klipper_services} ]]; then local error="Klipper not installed! Please install Klipper first!" log_error "Moonraker setup started without Klipper being installed. Aborting setup." diff --git a/scripts/octoprint.sh b/scripts/octoprint.sh index dee4851..b819373 100644 --- a/scripts/octoprint.sh +++ b/scripts/octoprint.sh @@ -25,7 +25,7 @@ function octoprint_setup_dialog() { status_msg "Initializing OctoPrint installation ..." local klipper_services - klipper_services=$(find_klipper_systemd) + klipper_services=$(klipper_systemd) if [[ -z ${klipper_services} ]]; then local error="Klipper not installed! Please install Klipper first!" log_error "OctoPrint setup started without Klipper being installed. Aborting setup." diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 5c3a6ed..30a4d18 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -599,7 +599,7 @@ function set_multi_instance_names() { local names="" local services - services=$(find_klipper_systemd) + services=$(klipper_systemd) ### # if value of 'multi_instance_names' is not an empty @@ -664,7 +664,7 @@ function get_config_folders() { cfg_dirs+=("${HOME}/${name}_data/config") fi done - elif [[ -z ${instance_names} && $(find_klipper_systemd | wc -w) -gt 0 ]]; then + elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) -gt 0 ]]; then cfg_dirs+=("${HOME}/printer_data/config") else cfg_dirs=() @@ -707,7 +707,7 @@ function get_instance_folder_path() { fi fi done - elif [[ -z ${instance_names} && $(find_klipper_systemd | wc -w) -gt 0 ]]; then + elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) -gt 0 ]]; then path="${HOME}/printer_data/${folder_name}" if [[ -d ${path} ]]; then folder_paths+=("${path}")