From d6837af2a2140f18c789cb8e493eeb7965e88cad Mon Sep 17 00:00:00 2001 From: th33xitus Date: Tue, 28 Feb 2023 18:29:21 +0100 Subject: [PATCH] refactor(klipper): implement blacklisted service names (#304) --- scripts/klipper.sh | 78 ++++++++++++++++++++++++-------------------- scripts/moonraker.sh | 2 +- scripts/octoprint.sh | 2 +- scripts/utilities.sh | 18 ++-------- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/scripts/klipper.sh b/scripts/klipper.sh index 1381e31..afe4332 100644 --- a/scripts/klipper.sh +++ b/scripts/klipper.sh @@ -19,8 +19,30 @@ 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 local python_version local instance_count @@ -28,20 +50,15 @@ function start_klipper_setup() { local use_custom_names local input local regex + local blacklist local error status_msg "Initializing Klipper installation ...\n" ### 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:" - error="${error}\n ➔ ${klipper_initd_service}" - error="${error}\n Please re-install Klipper with KIAUH!" - log_info "Unsupported Klipper SysVinit service detected: ${klipper_initd_service}" - elif [[ -n ${klipper_systemd_services} ]]; then + if [[ -n ${klipper_systemd_services} ]]; then error="At least one Klipper service is already installed:" for s in ${klipper_systemd_services}; do @@ -113,15 +130,19 @@ function start_klipper_setup() { fi ### user selection for setting the actual custom names + shopt -s nocasematch if (( instance_count > 1 )) && [[ ${use_custom_names} == "true" ]]; then local i i=1 regex="^[0-9a-zA-Z]+$" - while [[ ! ${input} =~ ${regex} || ${i} -le ${instance_count} ]]; do + blacklist="mcu" + while [[ ! ${input} =~ ${regex} || ${input} =~ ${blacklist} || ${i} -le ${instance_count} ]]; do read -p "${cyan}###### Name for instance #${i}:${white} " input - if [[ ${input} =~ ${regex} ]]; then + if [[ ${input} =~ ${blacklist} ]]; then + error_msg "Name not allowed! You are trying to use a reserved name." + elif [[ ${input} =~ ${regex} && ! ${input} =~ ${blacklist} ]]; then select_msg "Name: ${input}\n" if [[ ${input} =~ ^[0-9]+$ ]]; then instance_names+=("printer_${input}") @@ -138,6 +159,7 @@ function start_klipper_setup() { instance_names+=("printer_${i}") done fi + shopt -u nocasematch (( instance_count > 1 )) && status_msg "Installing ${instance_count} Klipper instances ..." (( instance_count == 1 )) && status_msg "Installing single Klipper instance ..." @@ -386,26 +408,18 @@ function write_example_printer_cfg() { #================================================# function remove_klipper_service() { - if [[ ! -e "${INITD}/klipper" ]] && [[ -z $(find_klipper_systemd) ]]; then - return - fi + [[ -z $(klipper_systemd) ]] && return status_msg "Removing Klipper services ..." - if [[ -e "${INITD}/klipper" ]]; then - sudo systemctl stop klipper - 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 - status_msg "Removing ${service} ..." - sudo systemctl stop "${service}" - sudo systemctl disable "${service}" - sudo rm -f "${SYSTEMD}/${service}" - sudo systemctl daemon-reload - sudo systemctl reset-failed - done - fi + for service in $(klipper_systemd | cut -d"/" -f5); do + status_msg "Removing ${service} ..." + sudo systemctl stop "${service}" + sudo systemctl disable "${service}" + sudo rm -f "${SYSTEMD}/${service}" + sudo systemctl daemon-reload + sudo systemctl reset-failed + done ok_msg "All Klipper services removed!" } @@ -534,13 +548,7 @@ function update_klipper() { function get_klipper_status() { local sf_count status py_ver - sf_count="$(find_klipper_systemd | wc -w)" - - ### detect an existing "legacy" klipper init.d installation - if [[ $(find_klipper_systemd | wc -w) -eq 0 ]] \ - && [[ $(find_klipper_initd | wc -w) -ge 1 ]]; then - sf_count=1 - fi + sf_count="$(klipper_systemd | wc -w)" py_ver=$(get_klipper_python_ver) 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..8490634 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -338,18 +338,6 @@ function fetch_webui_ports() { #=================== SYSTEM =====================# #================================================# -function find_klipper_initd() { - local services - services=$(find "${INITD}" -maxdepth 1 -regextype posix-extended -regex "${INITD}/klipper(-[^0])?[0-9]*" | sort) - echo "${services}" -} - -function find_klipper_systemd() { - local services - services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/klipper(-[0-9a-zA-Z]+)?.service" | sort) - echo "${services}" -} - function create_required_folders() { local printer_data=${1} folders folders=("backup" "certs" "config" "database" "gcodes" "comms" "logs" "systemd") @@ -599,7 +587,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 +652,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 +695,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}")