From 181a5d965aa437cac4df364b2ea44ae7ee54fc37 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:14:33 +0200 Subject: [PATCH 01/12] shellcheck: fix SC2086 Signed-off-by: Dominik Willner --- scripts/obico.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index faf6e37..3f18483 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -38,7 +38,7 @@ function cfg_dir() { function is_moonraker_obico_linked() { local name=${1} - moonraker_obico_cfg="$(cfg_dir ${name})/moonraker-obico.cfg" + moonraker_obico_cfg="$(cfg_dir "${name}")/moonraker-obico.cfg" grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token' return $? } @@ -186,13 +186,13 @@ function moonraker_obico_setup_dialog() { if (( moonraker_count == 1 )); then moonraker_cfg="$(cfg_dir '')/moonraker.conf" - "${MOONRAKER_OBICO_DIR}/install.sh" -C "${moonraker_cfg}" -p ${port} -H 127.0.0.1 -l "${KLIPPER_LOGS}" -s -L -S "${obico_server_url}" + "${MOONRAKER_OBICO_DIR}/install.sh" -C "${moonraker_cfg}" -p "${port}" -H 127.0.0.1 -l "${KLIPPER_LOGS}" -s -L -S "${obico_server_url}" elif (( moonraker_count > 1 )); then local j=${existing_moonraker_obico_count} for (( i=1; i <= new_moonraker_obico_count; i++ )); do local name=${moonraker_names[${j}]} - moonraker_cfg="$(cfg_dir ${name})/moonraker.conf" + moonraker_cfg="$(cfg_dir "${name}")/moonraker.conf" "${MOONRAKER_OBICO_DIR}/install.sh" -n "${name}" -C "${moonraker_cfg}" -p $((port+j)) -H 127.0.0.1 -l "${KLIPPER_LOGS}" -s -L -S "${obico_server_url}" j=$(( j + 1 )) @@ -250,7 +250,7 @@ function moonraker_obico_setup_dialog() { for name in "${not_linked_instances[@]}"; do status_msg "Link moonraker-obico-${name} to the Obico Server..." - moonraker_obico_cfg="$(cfg_dir ${name})/moonraker-obico.cfg" + moonraker_obico_cfg="$(cfg_dir "${name}")/moonraker-obico.cfg" if (( moonraker_count == 1 )); then "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "${moonraker_obico_cfg}" else @@ -265,7 +265,7 @@ function clone_or_update_moonraker_obico() { if [[ -d ${MOONRAKER_OBICO_DIR} ]]; then status_msg "Updating ${MOONRAKER_OBICO_DIR} ..." - cd ${MOONRAKER_OBICO_DIR} && git pull + cd "${MOONRAKER_OBICO_DIR}" && git pull else status_msg "Cloning Moonraker-obico from ${repo} ..." cd "${HOME}" || exit 1 From 78cc8bbffb30f7cd32d34ff7792da5eac405d63d Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:16:07 +0200 Subject: [PATCH 02/12] shellcheck: fix SC2155 Signed-off-by: Dominik Willner --- scripts/obico.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 3f18483..b24158e 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -201,10 +201,11 @@ function moonraker_obico_setup_dialog() { fi ### Step 7: Link to the Obico server if necessary + local instance_name local not_linked_instances=() # Refetch systemd service again since additional services may have been newly installed for service in $(moonraker_obico_systemd); do - local instance_name="$(get_instance_name "${service}" moonraker-obico)" + instance_name="$(get_instance_name "${service}" moonraker-obico)" if ! is_moonraker_obico_linked "${instance_name}"; then not_linked_instances+=( "${instance_name}" ) fi From 910c5867a67977a5792f7a1d531b8a1d6e1834d4 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:23:09 +0200 Subject: [PATCH 03/12] shellcheck: fix SC2068 Signed-off-by: Dominik Willner --- scripts/obico.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index b24158e..979b189 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -278,7 +278,7 @@ function clone_or_update_moonraker_obico() { } function moonraker_obico_install() { - "${MOONRAKER_OBICO_DIR}/install.sh" $@ + "${MOONRAKER_OBICO_DIR}/install.sh" "$@" } #===================================================# From 99fa596183688cb6a4455195a2a175d66e0dbe35 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:33:21 +0200 Subject: [PATCH 04/12] shellcheck: fix SC2155 Signed-off-by: Dominik Willner --- scripts/obico.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 979b189..31c40a0 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -85,7 +85,9 @@ function moonraker_obico_setup_dialog() { print_error "${error}" && return fi - local moonraker_obico_services moonraker_obico_names=() + local moonraker_obico_services + local moonraker_obico_names=() + local existing_moonraker_obico_count moonraker_obico_services=$(moonraker_obico_systemd) existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w ) for service in ${moonraker_obico_services}; do From fa2b7280376b08798449db12c0808daa8ea6a4ac Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 12:03:14 +0200 Subject: [PATCH 05/12] style: fix whitespace Signed-off-by: Dominik Willner --- scripts/obico.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 31c40a0..012079b 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -104,13 +104,13 @@ function moonraker_obico_setup_dialog() { top_border printf "|${green}%-55s${white}|\n" " ${moonraker_count} Moonraker instances found!" for name in "${moonraker_names[@]}"; do - printf "|${cyan}%-57s${white}|\n" " ●moonraker-${name}" + printf "|${cyan}%-57s${white}|\n" " ● moonraker-${name}" done blank_line if (( existing_moonraker_obico_count > 0 )); then printf "|${green}%-55s${white}|\n" " ${existing_moonraker_obico_count} Moonraker-obico instances already installed!" for name in "${moonraker_obico_names[@]}"; do - printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obico-${name}" done fi blank_line @@ -219,7 +219,7 @@ function moonraker_obico_setup_dialog() { else printf "|${green}%-55s${white}|\n" " ${#not_linked_instances[@]} Moonraker-obico instances not linked to the server!" for name in "${not_linked_instances[@]}"; do - printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obico-${name}" done fi blank_line From a9332a2b6efac1c0804cd8e4d9b4189cf5df734c Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 13:39:50 +0200 Subject: [PATCH 06/12] refactor: factor the menu title definition out into a separate function Signed-off-by: Dominik Willner --- scripts/obico.sh | 15 +++++++++++++++ scripts/ui/install_menu.sh | 25 ++++++++++--------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 012079b..cd4b585 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -421,3 +421,18 @@ function compare_moonraker_obico_versions() { echo "${versions}" } + +### +# it is possible, that moonraker_obico is installed in a so called +# "non-linked" state. the linking can be achieved by running the +# installation script again. this function will check the obico +# installation status and returns the correctly formulated menu title +# +function obico_install_title() { + if [[ $(get_moonraker_obico_status) == "Not linked!" ]]; then + echo "[Link to Obico Server]" + else + echo "[Obico for Klipper] " + fi +} + diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 5cbda6e..04f56c6 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -12,11 +12,6 @@ set -e function install_ui() { - local moonraker_obico_status=$(print_status "moonraker_obico") moonraker_obico_menu="[Obico for Klipper] " - if echo ${moonraker_obico_status} | grep -q "Not linked!"; then - moonraker_obico_menu="[Link to Obico Server]" - fi - top_border echo -e "| ${green}~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~${white} |" hr @@ -24,16 +19,16 @@ function install_ui() { echo -e "| all necessary dependencies for the various |" echo -e "| functions on a completely fresh system. |" hr - echo -e "| Firmware & API: | 3rd Party Webinterface: |" - echo -e "| 1) [Klipper] | 6) [OctoPrint] |" - echo -e "| 2) [Moonraker] | |" - echo -e "| | Other: |" - echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |" - echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" - echo -e "| 4) [Fluidd] | 9) ${moonraker_obico_menu}|" - echo -e "| | |" - echo -e "| Touchscreen GUI: | Webcam Streamer: |" - echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |" + echo -e "| Firmware & API: | 3rd Party Webinterface: |" + echo -e "| 1) [Klipper] | 6) [OctoPrint] |" + echo -e "| 2) [Moonraker] | |" + echo -e "| | Other: |" + echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |" + echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" + echo -e "| 4) [Fluidd] | 9) $(obico_install_title) |" + echo -e "| | |" + echo -e "| Touchscreen GUI: | Webcam Streamer: |" + echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |" back_footer } From 53e03254e7d4f75986b15c29de6aedbb0a8d857e Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 13:51:45 +0200 Subject: [PATCH 07/12] fix: make correct use of `do_action_service` function Signed-off-by: Dominik Willner --- scripts/obico.sh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index cd4b585..27fed30 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -349,16 +349,12 @@ function remove_moonraker_obico() { #===================================================# function update_moonraker_obico() { - for service in $(moonraker_obico_systemd | cut -d"/" -f5); do - do_action_service "stop" "${service}" - done + do_action_service "stop" "moonraker-obico" clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}" ok_msg "Update complete!" - for service in $(moonraker_obico_systemd | cut -d"/" -f5); do - do_action_service "restart" "${service}" - done + do_action_service "restart" "moonraker-obico" } #===================================================# From cb53982cac488abc1231929cb87c3bf57eaa75ae Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 14:03:47 +0200 Subject: [PATCH 08/12] refactor: refactor clone_and_update function into two separate ones Signed-off-by: Dominik Willner --- scripts/obico.sh | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 27fed30..9113e62 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -181,7 +181,7 @@ function moonraker_obico_setup_dialog() { dependency_check "${dep[@]}" ### Step 5: Clone the moonraker-obico repo - clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}" + clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" ### step 6: call moonrake-obico/install.sh with the correct params local port=7125 moonraker_cfg @@ -263,19 +263,17 @@ function moonraker_obico_setup_dialog() { } -function clone_or_update_moonraker_obico() { +function clone_moonraker_obico() { local repo=${1} - if [[ -d ${MOONRAKER_OBICO_DIR} ]]; then - status_msg "Updating ${MOONRAKER_OBICO_DIR} ..." - cd "${MOONRAKER_OBICO_DIR}" && git pull - else - status_msg "Cloning Moonraker-obico from ${repo} ..." - cd "${HOME}" || exit 1 - if ! git clone "${MOONRAKER_OBICO_REPO}" "${MOONRAKER_OBICO_DIR}"; then - print_error "Cloning Moonraker-obico from\n ${repo}\n failed!" - exit 1 - fi + status_msg "Cloning Moonraker-obico from ${repo} ..." + ### force remove existing Moonraker-obico dir + [[ -d ${repo} ]] && rm -rf "${MOONRAKER_OBICO_DIR}" + + cd "${HOME}" || exit 1 + if ! git clone "${repo}" "${MOONRAKER_OBICO_DIR}"; then + print_error "Cloning Moonraker-obico from\n ${repo}\n failed!" + exit 1 fi } @@ -351,9 +349,14 @@ function remove_moonraker_obico() { function update_moonraker_obico() { do_action_service "stop" "moonraker-obico" - clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}" - ok_msg "Update complete!" + if [[ ! -d ${MOONRAKER_OBICO_DIR} ]]; then + clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" + else + status_msg "Updating Moonraker-obico ..." + cd "${MOONRAKER_OBICO_DIR}" && git pull + fi + ok_msg "Update complete!" do_action_service "restart" "moonraker-obico" } From d11323d55db4bb362e15b98af09d5515bae2bf82 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 14:25:55 +0200 Subject: [PATCH 09/12] refactor(moonraker.sh): allow to ignore moonraker clients which have "moonraker" in their own name Signed-off-by: Dominik Willner --- scripts/moonraker.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 45af1be..0e8cfe1 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -15,9 +15,24 @@ set -e #================ INSTALL MOONRAKER ================# #===================================================# +### +# this function detects all installed moonraker +# systemd instances and returns their absolute path function moonraker_systemd() { local services - services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker(-[0-9a-zA-Z]+)?.service" | grep -v "moonraker-obico" | sort) + local blacklist + local ignore + local match + + ### + # any moonraker client that uses "moonraker" in its own name must be blacklisted using + # this variable, otherwise they will be falsely recognized as moonraker instances + blacklist="obico" + + ignore="${SYSTEMD}/moonraker-(${blacklist}).service" + match="${SYSTEMD}/moonraker(-[0-9a-zA-Z]+)?.service" + + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype awk ! -regex "${ignore}" -regex "${match}" | sort) echo "${services}" } From 7d5b4e7242eca2065d10ad32491a50da49c0a3f1 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 16:01:18 +0200 Subject: [PATCH 10/12] refactor(obico.sh): refactor obico status initializes `is_linked` as true. switches it to false as soon as the first non linked instance is detected. status is then printed as non linked in the main menu. Signed-off-by: Dominik Willner --- scripts/obico.sh | 30 ++++++++++++++++++++---------- scripts/ui/main_menu.sh | 5 ++++- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 9113e62..f212345 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -365,23 +365,33 @@ function update_moonraker_obico() { #===================================================# function get_moonraker_obico_status() { - local moonraker_obico_services sf_count status - moonraker_obico_services=$(moonraker_obico_systemd) - sf_count=$(echo "${moonraker_obico_services}" | wc -w ) + local status + local service_count + local is_linked + local moonraker_obico_services - if (( sf_count == 0 )); then - status="Not installed!" - elif [[ ! -e "${MOONRAKER_OBICO_DIR}" ]]; then - status="Incomplete!" - else - status="Installed!" + moonraker_obico_services=$(moonraker_obico_systemd) + service_count=$(echo "${moonraker_obico_services}" | wc -w ) + + is_linked="true" + if [[ -n ${moonraker_obico_services} ]]; then for service in ${moonraker_obico_services}; do if ! is_moonraker_obico_linked "$(get_instance_name "${service}" moonraker-obico)"; then - status="Not linked!" + is_linked="false" fi done fi + if (( service_count == 0 )); then + status="Not installed!" + elif [[ ! -d "${MOONRAKER_OBICO_DIR}" ]]; then + status="Incomplete!" + elif [[ ${is_linked} == "false" ]]; then + status="Not linked!" + else + status="Installed!" + fi + echo "${status}" } diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index 91092cc..925fca6 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -49,7 +49,10 @@ function print_status() { if [[ ${status} == "Not installed!" ]]; then status="${red}${status}${white}" - elif [[ ${status} == "Incomplete!" ]] || [[ ${status} == "Not linked!" ]]; then + elif [[ ${status} == "Incomplete!" ]]; then + status="${yellow}${status}${white}" + elif [[ ${status} == "Not linked!" ]]; then + ### "Not linked!" is only required for Moonraker-obico status="${yellow}${status}${white}" else status="${green}${status}${white}" From 23c39d225ab727302819fd684351b7a1c59270b5 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 16:19:34 +0200 Subject: [PATCH 11/12] fix(obico.sh): fix regex for logfiles Signed-off-by: Dominik Willner --- scripts/obico.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index f212345..cd73446 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -304,7 +304,7 @@ function remove_moonraker_obico_systemd() { } function remove_moonraker_obico_logs() { - local files regex="moonraker-obico(-[0-9a-zA-Z]+)?\.log([.-0-9]+)?" + local files regex="moonraker-obico(-[0-9a-zA-Z]+)?\.log(.*)?" files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" 2> /dev/null | sort) if [[ -n ${files} ]]; then From d79856b6410256fd0718527e3adbff534b74aae0 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 18:12:52 +0200 Subject: [PATCH 12/12] feat: add helper function to get all config directory names Signed-off-by: Dominik Willner --- scripts/utilities.sh | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index ca22069..b70f421 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -674,6 +674,10 @@ function set_hostname() { ok_msg "Remember to reboot for the changes to take effect!" } +#================================================# +#============ INSTANCE MANAGEMENT ===============# +#================================================# + ### this function takes in the full path of a systemd service file and returns ### either the instance index or the custom name ### input: /etc/systemd/system/klipper-name.service @@ -708,8 +712,7 @@ function get_klipper_instance_name() { } ### -# combines and saves each instance name/identifier -# to the kiauh.ini file in a comma separated format +# save all instance names in a comma separated format to the kiauh.ini # function add_to_multi_instance_names() { read_kiauh_ini "${FUNCNAME[0]}" @@ -734,3 +737,42 @@ function fetch_multi_instance_names() { add_to_multi_instance_names "${name}" done } + +### +# helper function that returns all possibly available absolute +# klipper config directory paths based on their instance name. +# +# => returns an empty string if klipper is not installed +# => returns a space separated string of absolute config directory paths +# +function get_config_folders() { + read_kiauh_ini "${FUNCNAME[0]}" + + local instance_names=() + local cfg_dirs=() + + ### + # convert the comma separates string from the .kiauh.ini into + # an array of instance names. a single instance installation + # results in an empty instance_names array + IFS=',' read -r -a instance_names <<< "${multi_instance_names}" + + if (( ${#instance_names[@]} > 0 )); then + for name in "${instance_names[@]}"; do + ### + # by KIAUH convention, all instance names of only numbers + # need to be prefixed with 'printer_' + if [[ ${name} =~ ^[0-9]+$ ]]; then + cfg_dirs+=("${KLIPPER_CONFIG}/printer_${name}") + else + cfg_dirs+=("${KLIPPER_CONFIG}/${name}") + fi + done + elif (( ${#instance_names[@]} == 0 && $(klipper_systemd | wc -w) > 0 )); then + cfg_dirs+=("${KLIPPER_CONFIG}") + else + cfg_dirs=() + fi + + echo "${cfg_dirs[@]}" +}