From 8bbe2f79eaf2f9586b93fe5a031161c7445b39e7 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Wed, 20 Jul 2022 22:33:33 +0200 Subject: [PATCH] feat: save multi instance klipper names/identifier to kiauh.ini Signed-off-by: Dominik Willner --- scripts/utilities.sh | 67 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index b034ae4..8d4d385 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -138,6 +138,7 @@ function init_ini() { echo -e "# DO NOT edit this file! #" echo -e "#=================================================#" echo -e "# KIAUH v4.0.0" + echo -e "#" } >> "${INI_FILE}" fi @@ -175,7 +176,20 @@ function init_ini() { echo -e "\nfluidd_install_unstable=false\c" >> "${INI_FILE}" fi + if ! grep -Eq "^multi_instance_names=" "${INI_FILE}"; then + echo -e "\nmulti_instance_names=\c" >> "${INI_FILE}" + else + sed -i "/multi_instance_names=/s/=.*/=/" "${INI_FILE}" + fi + + ### save all installed webinterface ports to the ini file fetch_webui_ports + + ### save all klipper multi-instance names to the ini file + fetch_multi_instance_names + + ### strip all empty lines out of the file + sed -i "/^[[:blank:]]*$/ d" "${INI_FILE}" } function change_klipper_cfg_folder() { @@ -668,4 +682,55 @@ function get_instance_name() { local instance=${1} name name=$(echo "${instance}" | rev | cut -d"/" -f1 | rev | cut -d"-" -f2 | cut -d"." -f1) echo "${name}" -} \ No newline at end of file +} + +### +# returns the instance name/identifier of the klipper service +# if the klipper service is part of a multi instance setup +# otherwise returns an emtpy string +# +# @param {string}: name - klipper service name (e.g. klipper-name.service) +# +function get_klipper_instance_name() { + local instance=${1} + local name + + name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | rev) + + local regex="^klipper-[0-9a-zA-Z]+$" + if [[ ${name} =~ ${regex} ]]; then + name=$(echo "${name}" | cut -d"-" -f2) + else + name="" + fi + + echo "${name}" +} + +### +# combines and saves each instance name/identifier +# to the kiauh.ini file in a comma separated format +# +function add_to_multi_instance_names() { + read_kiauh_ini "${FUNCNAME[0]}" + + local name="${1}" + local names="${multi_instance_names}" + + if ! grep -Eq "${name}" <<< "${names}"; then + names="${names}${name}," + sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" + fi +} + +### +# loops through all installed klipper services and +# calls the 'add_to_multi_instance_names' on each one +# +function fetch_multi_instance_names() { + for service in $(klipper_systemd); do + local name + name=$(get_klipper_instance_name "${service}") + add_to_multi_instance_names "${name}" + done +}