From 023103709d7888435e5d3ccf4c96c6fb3a4730f1 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sun, 24 Jul 2022 21:40:15 -0700 Subject: [PATCH 01/35] Status and installation worked. --- scripts/globals.sh | 6 +- scripts/obico.sh | 365 ++++++++++++++++++++++++++++++++++++++++ scripts/ui/main_menu.sh | 1 + 3 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 scripts/obico.sh diff --git a/scripts/globals.sh b/scripts/globals.sh index 4b1ca67..5eb327b 100644 --- a/scripts/globals.sh +++ b/scripts/globals.sh @@ -70,4 +70,8 @@ function set_globals() { NGINX_SA="/etc/nginx/sites-available" NGINX_SE="/etc/nginx/sites-enabled" NGINX_CONFD="/etc/nginx/conf.d" -} \ No newline at end of file + + #=============== MOONRAKER-OBICO ================# + MOONRAKER_OBICO_DIR="${HOME}/moonraker-obico" + MOONRAKER_OBICO_REPO="https://github.com/TheSpaghettiDetective/moonraker-obico.git" +} diff --git a/scripts/obico.sh b/scripts/obico.sh new file mode 100644 index 0000000..447e3a8 --- /dev/null +++ b/scripts/obico.sh @@ -0,0 +1,365 @@ +#!/usr/bin/env bash + +#=======================================================================# +# Copyright (C) 2020 - 2022 Dominik Willner # +# # +# This file is part of KIAUH - Klipper Installation And Update Helper # +# https://github.com/th33xitus/kiauh # +# # +# This file may be distributed under the terms of the GNU GPLv3 license # +#=======================================================================# + +set -e + +#===================================================# +#============== INSTALL MOONRAKER-OBICO ============# +#===================================================# + +function moonraker_obico_systemd() { + local services + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker-obico.*.service") + echo "${services}" +} + +function moonraker_obico_setup_dialog() { + status_msg "Initializing moonraker-obico installation ..." + + ### return early if python version check fails + if [[ $(python3_check) == "false" ]]; then + local error="Versioncheck failed! Python 3.7 or newer required!\n" + error="${error} Please upgrade Python." + print_error "${error}" && return + fi + + ### return early if moonraker_obico already exists + local moonraker_obico_services + moonraker_obico_services=$(moonraker_obico_systemd) + if [[ -n ${moonraker_obico_services} ]]; then + local error="At least one moonraker-obico service is already installed:" + for s in ${moonraker_obico_services}; do + log_info "Found moonraker-obico service: ${s}" + error="${error}\n ➔ ${s}" + done + print_error "${error}" && return + fi + + ### return early if moonraker is not installed + local moonraker_services + moonraker_services=$(moonraker_systemd) + if [[ -z ${moonraker_services} ]]; then + local error="Moonraker not installed! Please install Moonraker first!" + log_error "Moonraker-obico setup started without Moonraker being installed. Aborting setup." + print_error "${error}" && return + fi + + local moonraker_count user_input=() moonraker_names=() + moonraker_count=$(echo "${moonraker_services}" | wc -w ) + for service in ${moonraker_services}; do + moonraker_names+=( "$(get_instance_name "${service}")" ) + done + + local moonraker_obico_count + if (( moonraker_count == 1 )); then + ok_msg "Moonraker installation found!\n" + moonraker_obico_count=1 + elif (( moonraker_count > 1 )); then + 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}" + done + blank_line + echo -e "| The setup will apply the same names to |" + echo -e "| moonraker-obico! |" + blank_line + echo -e "| Please select the number of moonraker-obico instances |" + echo -e "| to install. Usually one moonraker-obico instance per |" + echo -e "| Moonraker instance is required, but you may not |" + echo -e "| install more moonraker-obico instances than available |" + echo -e "| Moonraker instances. |" + bottom_border + + ### ask for amount of instances + local re="^[1-9][0-9]*$" + while [[ ! ${moonraker_obico_count} =~ ${re} || ${moonraker_obico_count} -gt ${moonraker_count} ]]; do + read -p "${cyan}###### Number of moonraker-obico instances to set up:${white} " -i "${moonraker_count}" -e moonraker_obico_count + ### break if input is valid + [[ ${moonraker_obico_count} =~ ${re} && ${moonraker_obico_count} -le ${moonraker_count} ]] && break + ### conditional error messages + [[ ! ${moonraker_obico_count} =~ ${re} ]] && error_msg "Input not a number" + (( moonraker_obico_count > moonraker_count )) && error_msg "Number of moonraker-obico instances larger than installed Moonraker instances" + done && select_msg "${moonraker_obico_count}" + else + log_error "Internal error. moonraker_count of '${moonraker_count}' not equal or grather than one!" + return 1 + fi + + user_input+=("${moonraker_obico_count}") + + ### confirm instance amount + local yn + while true; do + (( moonraker_obico_count == 1 )) && local question="Install moonraker-obico?" + (( moonraker_obico_count > 1 )) && local question="Install ${moonraker_obico_count} moonraker-obico instances?" + read -p "${cyan}###### ${question} (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + break;; + N|n|No|no) + select_msg "No" + abort_msg "Exiting moonraker-obico setup ...\n" + return;; + *) + error_msg "Invalid Input!";; + esac + done + + ### write existing moonraker names into user_input array to use them as names for moonraker-obico + if (( moonraker_count > 1 )); then + for name in "${moonraker_names[@]}"; do + user_input+=("${name}") + done + fi + + (( moonraker_obico_count > 1 )) && status_msg "Installing ${moonraker_count} moonraker-obico instances ..." + (( moonraker_obico_count == 1 )) && status_msg "Installing moonraker-obico ..." + moonraker_obico_setup "${user_input[@]}" + +} + +function moonraker_obico_setup() { + + ### checking dependencies + local dep=(git dfu-util virtualenv python3 python3-pip python3-venv ffmpeg) + dependency_check "${dep[@]}" + + ### step 1: clone moonraker-obico + clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" + + ### step 2: call moonrake-obico/install.sh with the correct params + local input=("${@}") + local moonraker_obico_count=${input[0]} && unset "input[0]" + local names=("${input[@]}") && unset "input[@]" + local log="${KLIPPER_LOGS}" + local port=7125 cfg_dir moonraker_cfg + + if (( moonraker_obico_count == 1 )); then + cfg_dir="${KLIPPER_CONFIG}" + moonraker_cfg="${cfg_dir}/moonraker.conf" + + # Invoke moonrake-obico/install.sh + moonraker_obico_install -c "${moonraker_cfg}" -p ${port} -H 127.0.0.1 -l "${KLIPPER_LOGS}" + + elif (( moonraker_count > 1 )); then + local j=0 re="^[1-9][0-9]*$" + + for (( i=1; i <= moonraker_count; i++ )); do + ### overwrite config folder if name is only a number + if [[ ${names[j]} =~ ${re} ]]; then + cfg_dir="${KLIPPER_CONFIG}/printer_${names[${j}]}" + else + cfg_dir="${KLIPPER_CONFIG}/${names[${j}]}" + fi + + moonraker_cfg="${cfg_dir}/moonraker.conf" + + # Invoke moonrake-obico/install.sh + moonraker_obico_install -c "${moonraker_cfg}" -p ${port} -H 127.0.0.1 -l "${KLIPPER_LOGS}" + + port=$(( port + 1 )) + j=$(( j + 1 )) + done && unset j + + else + return 1 + fi +} + +function clone_moonraker_obico() { + local repo=${1} + + status_msg "Cloning moonraker-obico from ${repo} ..." + + ### force remove existing moonraker-obico dir and clone into fresh moonraker-obico dir + [[ -d ${MOONRAKER_OBICO_DIR} ]] && rm -rf "${MOONRAKER_OBICO_DIR}" + + 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 +} + +function moonraker_obico_install() { + echo "${MOONRAKER_OBICO_DIR}/install.sh" $* +} + +#===================================================# +#============= REMOVE MOONRAKER-OBICO ==============# +#===================================================# + +function remove_klipperscreen() { + ### remove KlipperScreen dir + if [[ -d ${KLIPPERSCREEN_DIR} ]]; then + status_msg "Removing KlipperScreen directory ..." + rm -rf "${KLIPPERSCREEN_DIR}" && ok_msg "Directory removed!" + fi + + ### remove KlipperScreen VENV dir + if [[ -d ${KLIPPERSCREEN_ENV} ]]; then + status_msg "Removing KlipperScreen VENV directory ..." + rm -rf "${KLIPPERSCREEN_ENV}" && ok_msg "Directory removed!" + fi + + ### remove KlipperScreen service + if [[ -e "${SYSTEMD}/KlipperScreen.service" ]]; then + status_msg "Removing KlipperScreen service ..." + do_action_service "stop" "KlipperScreen" + do_action_service "disable" "KlipperScreen" + sudo rm -f "${SYSTEMD}/KlipperScreen.service" + + ###reloading units + sudo systemctl daemon-reload + sudo systemctl reset-failed + ok_msg "KlipperScreen Service removed!" + fi + + ### remove KlipperScreen log + if [[ -e "/tmp/KlipperScreen.log" ]]; then + status_msg "Removing KlipperScreen log file ..." + rm -f "/tmp/KlipperScreen.log" && ok_msg "File removed!" + fi + + ### remove KlipperScreen log symlink in config dir + if [[ -e "${KLIPPER_CONFIG}/KlipperScreen.log" ]]; then + status_msg "Removing KlipperScreen log symlink ..." + rm -f "${KLIPPER_CONFIG}/KlipperScreen.log" && ok_msg "File removed!" + fi + + print_confirm "KlipperScreen successfully removed!" +} + +#===================================================# +#============= UPDATE MOONRAKER-OBICO ==============# +#===================================================# + +function update_klipperscreen() { + local old_md5 + old_md5=$(md5sum "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" | cut -d " " -f1) + + do_action_service "stop" "KlipperScreen" + cd "${KLIPPERSCREEN_DIR}" + git pull origin master -q && ok_msg "Fetch successfull!" + git checkout -f master && ok_msg "Checkout successfull" + + if [[ $(md5sum "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" | cut -d " " -f1) != "${old_md5}" ]]; then + status_msg "New dependecies detected..." + "${KLIPPERSCREEN_ENV}"/bin/pip install -r "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" + ok_msg "Dependencies have been installed!" + fi + + ok_msg "Update complete!" + do_action_service "start" "KlipperScreen" +} + +#===================================================# +#============= MOONRAKER-OBICO STATUS ==============# +#===================================================# + +function get_obico_status() { + local sf_count status + sf_count="$(klipperscreen_systemd | wc -w)" + + ### remove the "SERVICE" entry from the data array if a moonraker service is installed + local data_arr=(SERVICE "${KLIPPERSCREEN_DIR}" "${KLIPPERSCREEN_ENV}") + (( sf_count > 0 )) && unset "data_arr[0]" + + ### count+1 for each found data-item from array + local filecount=0 + for data in "${data_arr[@]}"; do + [[ -e ${data} ]] && filecount=$(( filecount + 1 )) + done + + if (( filecount == ${#data_arr[*]} )); then + status="Installed!" + elif (( filecount == 0 )); then + status="Not installed!" + else + status="Incomplete!" + fi + echo "${status}" +} + +function get_local_klipperscreen_commit() { + [[ ! -d ${KLIPPERSCREEN_DIR} || ! -d "${KLIPPERSCREEN_DIR}/.git" ]] && return + + local commit + cd "${KLIPPERSCREEN_DIR}" + commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)" + echo "${commit}" +} + +function get_remote_klipperscreen_commit() { + [[ ! -d ${KLIPPERSCREEN_DIR} || ! -d "${KLIPPERSCREEN_DIR}/.git" ]] && return + + local commit + cd "${KLIPPERSCREEN_DIR}" && git fetch origin -q + commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) + echo "${commit}" +} + +function compare_klipperscreen_versions() { + local versions local_ver remote_ver + local_ver="$(get_local_klipperscreen_commit)" + remote_ver="$(get_remote_klipperscreen_commit)" + + if [[ ${local_ver} != "${remote_ver}" ]]; then + versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" + versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" + # add moonraker to application_updates_available in kiauh.ini + add_to_application_updates "klipperscreen" + else + versions="${green}$(printf " %-14s" "${local_ver}")${white}" + versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" + fi + + echo "${versions}" +} + +#================================================# +#=================== HELPERS ====================# +#================================================# + +function patch_klipperscreen_update_manager() { + local patched="false" + local moonraker_configs + moonraker_configs=$(find "${KLIPPER_CONFIG}" -type f -name "moonraker.conf" | sort) + + for conf in ${moonraker_configs}; do + if ! grep -Eq "^\[update_manager KlipperScreen\]$" "${conf}"; then + ### add new line to conf if it doesn't end with one + [[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}" + + ### add KlipperScreens update manager section to moonraker.conf + status_msg "Adding KlipperScreen to update manager in file:\n ${conf}" + /bin/sh -c "cat >> ${conf}" << MOONRAKER_CONF + +[update_manager KlipperScreen] +type: git_repo +path: ${HOME}/KlipperScreen +origin: https://github.com/jordanruthe/KlipperScreen.git +env: ${HOME}/.KlipperScreen-env/bin/python +requirements: scripts/KlipperScreen-requirements.txt +install_script: scripts/KlipperScreen-install.sh +MOONRAKER_CONF + + fi + + patched="true" + done + + if [[ ${patched} == "true" ]]; then + do_action_service "restart" "moonraker" + fi +} diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index f4e02a1..90aa4db 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -24,6 +24,7 @@ function main_ui() { echo -e "| 5) [Backup] | Fluidd: $(print_status "fluidd")|" echo -e "| | KlipperScreen: $(print_status "klipperscreen")|" echo -e "| 6) [Settings] | Telegram Bot: $(print_status "telegram_bot")|" + echo -e "| | Obico: $(print_status "obico")|" echo -e "| | |" echo -e "| $(print_kiauh_version)| Octoprint: $(print_status "octoprint")|" quit_footer -- 2.39.5 From 64058e96e28a044e57fb1e47db78c0aec4e7253f Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 25 Jul 2022 14:04:03 -0700 Subject: [PATCH 02/35] Uninstall and update works --- scripts/obico.sh | 213 +++++++++++++++++------------------------------ 1 file changed, 78 insertions(+), 135 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 447e3a8..f0396b5 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -22,7 +22,7 @@ function moonraker_obico_systemd() { } function moonraker_obico_setup_dialog() { - status_msg "Initializing moonraker-obico installation ..." + status_msg "Initializing Moonraker-obico installation ..." ### return early if python version check fails if [[ $(python3_check) == "false" ]]; then @@ -35,9 +35,9 @@ function moonraker_obico_setup_dialog() { local moonraker_obico_services moonraker_obico_services=$(moonraker_obico_systemd) if [[ -n ${moonraker_obico_services} ]]; then - local error="At least one moonraker-obico service is already installed:" + local error="At least one Moonraker-obico service is already installed:" for s in ${moonraker_obico_services}; do - log_info "Found moonraker-obico service: ${s}" + log_info "Found Moonraker-obico service: ${s}" error="${error}\n ➔ ${s}" done print_error "${error}" && return @@ -70,24 +70,24 @@ function moonraker_obico_setup_dialog() { done blank_line echo -e "| The setup will apply the same names to |" - echo -e "| moonraker-obico! |" + echo -e "| Moonraker-obico! |" blank_line - echo -e "| Please select the number of moonraker-obico instances |" - echo -e "| to install. Usually one moonraker-obico instance per |" + echo -e "| Please select the number of Moonraker-obico instances |" + echo -e "| to install. Usually one Moonraker-obico instance per |" echo -e "| Moonraker instance is required, but you may not |" - echo -e "| install more moonraker-obico instances than available |" + echo -e "| install more Moonraker-obico instances than available |" echo -e "| Moonraker instances. |" bottom_border ### ask for amount of instances local re="^[1-9][0-9]*$" while [[ ! ${moonraker_obico_count} =~ ${re} || ${moonraker_obico_count} -gt ${moonraker_count} ]]; do - read -p "${cyan}###### Number of moonraker-obico instances to set up:${white} " -i "${moonraker_count}" -e moonraker_obico_count + read -p "${cyan}###### Number of Moonraker-obico instances to set up:${white} " -i "${moonraker_count}" -e moonraker_obico_count ### break if input is valid [[ ${moonraker_obico_count} =~ ${re} && ${moonraker_obico_count} -le ${moonraker_count} ]] && break ### conditional error messages [[ ! ${moonraker_obico_count} =~ ${re} ]] && error_msg "Input not a number" - (( moonraker_obico_count > moonraker_count )) && error_msg "Number of moonraker-obico instances larger than installed Moonraker instances" + (( moonraker_obico_count > moonraker_count )) && error_msg "Number of Moonraker-obico instances larger than installed Moonraker instances" done && select_msg "${moonraker_obico_count}" else log_error "Internal error. moonraker_count of '${moonraker_count}' not equal or grather than one!" @@ -99,8 +99,8 @@ function moonraker_obico_setup_dialog() { ### confirm instance amount local yn while true; do - (( moonraker_obico_count == 1 )) && local question="Install moonraker-obico?" - (( moonraker_obico_count > 1 )) && local question="Install ${moonraker_obico_count} moonraker-obico instances?" + (( moonraker_obico_count == 1 )) && local question="Install Moonraker-obico?" + (( moonraker_obico_count > 1 )) && local question="Install ${moonraker_obico_count} Moonraker-obico instances?" read -p "${cyan}###### ${question} (Y/n):${white} " yn case "${yn}" in Y|y|Yes|yes|"") @@ -108,7 +108,7 @@ function moonraker_obico_setup_dialog() { break;; N|n|No|no) select_msg "No" - abort_msg "Exiting moonraker-obico setup ...\n" + abort_msg "Exiting Moonraker-obico setup ...\n" return;; *) error_msg "Invalid Input!";; @@ -122,8 +122,8 @@ function moonraker_obico_setup_dialog() { done fi - (( moonraker_obico_count > 1 )) && status_msg "Installing ${moonraker_count} moonraker-obico instances ..." - (( moonraker_obico_count == 1 )) && status_msg "Installing moonraker-obico ..." + (( moonraker_obico_count > 1 )) && status_msg "Installing ${moonraker_count} Moonraker-obico instances ..." + (( moonraker_obico_count == 1 )) && status_msg "Installing Moonraker-obico ..." moonraker_obico_setup "${user_input[@]}" } @@ -179,88 +179,104 @@ function moonraker_obico_setup() { function clone_moonraker_obico() { local repo=${1} - status_msg "Cloning moonraker-obico from ${repo} ..." + status_msg "Cloning Moonraker-obico from ${repo} ..." ### force remove existing moonraker-obico dir and clone into fresh moonraker-obico dir [[ -d ${MOONRAKER_OBICO_DIR} ]] && rm -rf "${MOONRAKER_OBICO_DIR}" cd "${HOME}" || exit 1 if ! git clone "${MOONRAKER_OBICO_REPO}" "${MOONRAKER_OBICO_DIR}"; then - print_error "Cloning moonraker-obico from\n ${repo}\n failed!" + print_error "Cloning Moonraker-obico from\n ${repo}\n failed!" exit 1 fi } function moonraker_obico_install() { - echo "${MOONRAKER_OBICO_DIR}/install.sh" $* + "${MOONRAKER_OBICO_DIR}/install.sh" $@ } #===================================================# #============= REMOVE MOONRAKER-OBICO ==============# #===================================================# -function remove_klipperscreen() { - ### remove KlipperScreen dir - if [[ -d ${KLIPPERSCREEN_DIR} ]]; then - status_msg "Removing KlipperScreen directory ..." - rm -rf "${KLIPPERSCREEN_DIR}" && ok_msg "Directory removed!" +function remove_moonraker_obico_systemd() { + [[ -z $(moonraker_obico_systemd) ]] && return + status_msg "Removing Moonraker-obico Systemd Services ..." + + for service in $(moonraker_obico_systemd | cut -d"/" -f5); do + status_msg "Removing ${service} ..." + sudo systemctl stop "${service}" + sudo systemctl disable "${service}" + sudo rm -f "${SYSTEMD}/${service}" + ok_msg "Done!" + done + + ### reloading units + sudo systemctl daemon-reload + sudo systemctl reset-failed + ok_msg "Moonraker-obico Services removed!" +} + +function remove_moonraker_obico_logs() { + 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 + for file in ${files}; do + status_msg "Removing ${file} ..." + rm -f "${file}" + ok_msg "${file} removed!" + done fi +} - ### remove KlipperScreen VENV dir - if [[ -d ${KLIPPERSCREEN_ENV} ]]; then - status_msg "Removing KlipperScreen VENV directory ..." - rm -rf "${KLIPPERSCREEN_ENV}" && ok_msg "Directory removed!" - fi +function remove_moonraker_obico_dir() { + [[ ! -d ${MOONRAKER_OBICO_DIR} ]] && return - ### remove KlipperScreen service - if [[ -e "${SYSTEMD}/KlipperScreen.service" ]]; then - status_msg "Removing KlipperScreen service ..." - do_action_service "stop" "KlipperScreen" - do_action_service "disable" "KlipperScreen" - sudo rm -f "${SYSTEMD}/KlipperScreen.service" + status_msg "Removing Moonraker-obico directory ..." + rm -rf "${MOONRAKER_OBICO_DIR}" + ok_msg "Directory removed!" +} - ###reloading units - sudo systemctl daemon-reload - sudo systemctl reset-failed - ok_msg "KlipperScreen Service removed!" - fi +function remove_moonraker_obico_env() { + [[ ! -d "${HOME}/moonraker-env" ]] && return - ### remove KlipperScreen log - if [[ -e "/tmp/KlipperScreen.log" ]]; then - status_msg "Removing KlipperScreen log file ..." - rm -f "/tmp/KlipperScreen.log" && ok_msg "File removed!" - fi + status_msg "Removing moonraker-env directory ..." + rm -rf "${HOME}/moonraker-env" + ok_msg "Directory removed!" +} - ### remove KlipperScreen log symlink in config dir - if [[ -e "${KLIPPER_CONFIG}/KlipperScreen.log" ]]; then - status_msg "Removing KlipperScreen log symlink ..." - rm -f "${KLIPPER_CONFIG}/KlipperScreen.log" && ok_msg "File removed!" - fi +function remove_moonraker_obico() { + remove_moonraker_obico_systemd + remove_moonraker_obico_logs + remove_moonraker_obico_dir + remove_moonraker_obico_env - print_confirm "KlipperScreen successfully removed!" + print_confirm "Moonraker-obico was successfully removed!" + return } #===================================================# #============= UPDATE MOONRAKER-OBICO ==============# #===================================================# -function update_klipperscreen() { - local old_md5 - old_md5=$(md5sum "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" | cut -d " " -f1) +function update_moonraker() { + for service in $(moonraker_obico_systemd | cut -d"/" -f5); do + do_action_service "stop" "${service}" + done - do_action_service "stop" "KlipperScreen" - cd "${KLIPPERSCREEN_DIR}" - git pull origin master -q && ok_msg "Fetch successfull!" - git checkout -f master && ok_msg "Checkout successfull" - - if [[ $(md5sum "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" | cut -d " " -f1) != "${old_md5}" ]]; then - status_msg "New dependecies detected..." - "${KLIPPERSCREEN_ENV}"/bin/pip install -r "${KLIPPERSCREEN_DIR}/scripts/KlipperScreen-requirements.txt" - ok_msg "Dependencies have been installed!" + if [[ ! -d ${MOONRAKER_OBICO_DIR} ]]; then + clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" + else + status_msg "Updating Moonraker-obico ..." + cd "${MOONRAKER_OBICO_DIR}" && git pull + "${MOONRAKER_OBICO_DIR}/install.sh" -u fi ok_msg "Update complete!" - do_action_service "start" "KlipperScreen" + for service in $(moonraker_obico_systemd | cut -d"/" -f5); do + do_action_service "restart" "${service}" + done } #===================================================# @@ -290,76 +306,3 @@ function get_obico_status() { fi echo "${status}" } - -function get_local_klipperscreen_commit() { - [[ ! -d ${KLIPPERSCREEN_DIR} || ! -d "${KLIPPERSCREEN_DIR}/.git" ]] && return - - local commit - cd "${KLIPPERSCREEN_DIR}" - commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)" - echo "${commit}" -} - -function get_remote_klipperscreen_commit() { - [[ ! -d ${KLIPPERSCREEN_DIR} || ! -d "${KLIPPERSCREEN_DIR}/.git" ]] && return - - local commit - cd "${KLIPPERSCREEN_DIR}" && git fetch origin -q - commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) - echo "${commit}" -} - -function compare_klipperscreen_versions() { - local versions local_ver remote_ver - local_ver="$(get_local_klipperscreen_commit)" - remote_ver="$(get_remote_klipperscreen_commit)" - - if [[ ${local_ver} != "${remote_ver}" ]]; then - versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" - versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" - # add moonraker to application_updates_available in kiauh.ini - add_to_application_updates "klipperscreen" - else - versions="${green}$(printf " %-14s" "${local_ver}")${white}" - versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" - fi - - echo "${versions}" -} - -#================================================# -#=================== HELPERS ====================# -#================================================# - -function patch_klipperscreen_update_manager() { - local patched="false" - local moonraker_configs - moonraker_configs=$(find "${KLIPPER_CONFIG}" -type f -name "moonraker.conf" | sort) - - for conf in ${moonraker_configs}; do - if ! grep -Eq "^\[update_manager KlipperScreen\]$" "${conf}"; then - ### add new line to conf if it doesn't end with one - [[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}" - - ### add KlipperScreens update manager section to moonraker.conf - status_msg "Adding KlipperScreen to update manager in file:\n ${conf}" - /bin/sh -c "cat >> ${conf}" << MOONRAKER_CONF - -[update_manager KlipperScreen] -type: git_repo -path: ${HOME}/KlipperScreen -origin: https://github.com/jordanruthe/KlipperScreen.git -env: ${HOME}/.KlipperScreen-env/bin/python -requirements: scripts/KlipperScreen-requirements.txt -install_script: scripts/KlipperScreen-install.sh -MOONRAKER_CONF - - fi - - patched="true" - done - - if [[ ${patched} == "true" ]]; then - do_action_service "restart" "moonraker" - fi -} -- 2.39.5 From efdaae436adc86a50cca385d4671a80ec01a91de Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 25 Jul 2022 15:09:35 -0700 Subject: [PATCH 03/35] Compare version for update --- scripts/obico.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scripts/obico.sh b/scripts/obico.sh index f0396b5..fb065d5 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -306,3 +306,39 @@ function get_obico_status() { fi echo "${status}" } + +function get_local_moonraker_obico_commit() { + [[ ! -d ${MOONRAKER_OBICO_DIR} || ! -d "${MOONRAKER_OBICO_DIR}/.git" ]] && return + + local commit + cd "${MOONRAKER_OBICO_DIR}" + commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)" + echo "${commit}" +} + +function get_remote_moonraker_obico_commit() { + [[ ! -d ${MOONRAKER_OBICO_DIR} || ! -d "${MOONRAKER_OBICO_DIR}/.git" ]] && return + + local commit + cd "${MOONRAKER_OBICO_DIR}" && git fetch origin -q + commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) + echo "${commit}" +} + +function compare_moonraker_obico_versions() { + local versions local_ver remote_ver + local_ver="$(get_local_moonraker_obico_commit)" + remote_ver="$(get_remote_moonraker_obico_commit)" + + if [[ ${local_ver} != "${remote_ver}" ]]; then + versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" + versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" + # add moonraker to application_updates_available in kiauh.ini + add_to_application_updates "moonraker_obico" + else + versions="${green}$(printf " %-14s" "${local_ver}")${white}" + versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" + fi + + echo "${versions}" +} -- 2.39.5 From 08842465b2f35d4694c8f87eacf9d626d409c7e9 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 25 Jul 2022 15:10:06 -0700 Subject: [PATCH 04/35] Changes to the KIAUH menu --- scripts/ui/install_menu.sh | 10 ++++++---- scripts/ui/remove_menu.sh | 5 ++++- scripts/ui/update_menu.sh | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 4fdb771..0df0d3e 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -25,10 +25,10 @@ function install_ui() { echo -e "| | Other: |" echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |" echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" - echo -e "| 4) [Fluidd] | |" - echo -e "| | Webcam Streamer: |" - echo -e "| Touchscreen GUI: | 9) [MJPG-Streamer] |" - echo -e "| 5) [KlipperScreen] | |" + echo -e "| 4) [Fluidd] | 9) [Obico for Klipper] |" + echo -e "| | |" + echo -e "| Touchscreen GUI: | Webcam Streamer: |" + echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |" back_footer } @@ -57,6 +57,8 @@ function install_menu() { 8) do_action "telegram_bot_setup_dialog" "install_ui";; 9) + do_action "moonraker_obico_setup_dialog" "install_ui";; + 10) do_action "install_mjpg-streamer" "install_ui";; B|b) clear; main_menu; break;; diff --git a/scripts/ui/remove_menu.sh b/scripts/ui/remove_menu.sh index b58d2f9..4cf743d 100755 --- a/scripts/ui/remove_menu.sh +++ b/scripts/ui/remove_menu.sh @@ -26,7 +26,8 @@ function remove_ui() { echo -e "| 4) [Fluidd] | Other: |" echo -e "| | 8) [PrettyGCode] |" echo -e "| Touchscreen GUI: | 9) [Telegram Bot] |" - echo -e "| 5) [KlipperScreen] | 10) [NGINX] |" + echo -e "| 5) [KlipperScreen] | 10) [Obico for Klipper] |" + echo -e "| | 11) [NGINX] |" back_footer } @@ -56,6 +57,8 @@ function remove_menu() { 9) do_action "remove_telegram_bot" "remove_ui";; 10) + do_action "remove_moonraker_obico" "remove_ui";; + 11) do_action "remove_nginx" "remove_ui";; B|b) clear; main_menu; break;; diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index 3d6338d..ea48024 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -31,8 +31,9 @@ function update_ui() { echo -e "| Other: |---------------|--------------|" echo -e "| 6) [PrettyGCode] |$(compare_prettygcode_versions)|" echo -e "| 7) [Telegram Bot] |$(compare_telegram_bot_versions)|" + echo -e "| 8) [Obico for Klipper]|$(compare_moonraker_obico_versions)|" echo -e "| |------------------------------|" - echo -e "| 8) [System] | $(check_system_updates) |" + echo -e "| 9) [System] | $(check_system_updates) |" back_footer } -- 2.39.5 From 121dde5a951dbbeb39fd0ebd0b734063c85b97c7 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sat, 30 Jul 2022 07:38:10 -0700 Subject: [PATCH 05/35] The name extraction should work in case of service-name-with-hyphon-name.service --- scripts/utilities.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 8d4d385..94a4a13 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -680,7 +680,7 @@ function set_hostname() { ### returns: name function get_instance_name() { local instance=${1} name - name=$(echo "${instance}" | rev | cut -d"/" -f1 | rev | cut -d"-" -f2 | cut -d"." -f1) + name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"-" -f1 | rev | cut -d"." -f1) echo "${name}" } -- 2.39.5 From 767f8e14f81ee27c1ef88078e98f5f0d56eae1f6 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 06:46:06 -0700 Subject: [PATCH 06/35] Streamline the Obico linking process in KIAUH --- scripts/obico.sh | 422 +++++++++++++++++++++---------------- scripts/ui/install_menu.sh | 7 +- scripts/ui/main_menu.sh | 4 +- 3 files changed, 253 insertions(+), 180 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index fb065d5..199129d 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -17,177 +17,254 @@ set -e function moonraker_obico_systemd() { local services - services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker-obico.*.service") + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker-obico(-[0-9a-zA-Z]+)?.service") echo "${services}" } +function cfg_dir() { + local name=${1} + if [[ -z ${name} ]]; then + echo "${KLIPPER_CONFIG}" + else + local re="^[1-9][0-9]*$" + ### overwrite config folder if name is only a number + if [[ ${name} =~ ${re} ]]; then + echo "${KLIPPER_CONFIG}/printer_${name}" + else + echo "${KLIPPER_CONFIG}/${name}" + fi + fi +} + +function is_moonraker_obico_linked() { + local name=${1} + moonraker_obico_cfg="$(cfg_dir ${name})/moonraker-obico.cfg" + grep -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token' + return $? +} + +function get_moonraker_names() { + local moonraker_services + moonraker_services=$(moonraker_systemd) + if [[ -z ${moonraker_services} ]]; then + echo '' && return + fi + + for service in ${moonraker_services}; do + get_instance_name "${service}" + done +} + +function obico_server_url_prompt() { + top_border + printf "|${green}%-55s${white}|\n" " Obico Server URL" + blank_line + echo -e "| You can use a self-hosted Obico Server or the Obico |" + echo -e "| Cloud. For more information, please visit: |" + echo -e "| https://obico.io. |" + blank_line + echo -e "| For the Obico Cloud, leave it as the default: |" + printf "|${cyan}%-55s${white}|\n" " https://app.obico.io" + blank_line + echo -e "| For self-hosted server, specify: |" + printf "|${cyan}%-55s${white}|\n" " http://server_ip:port" + echo -e "| For instance, \`http://192.168.0.5:3334\`. |" + bottom_border +} + function moonraker_obico_setup_dialog() { status_msg "Initializing Moonraker-obico installation ..." - ### return early if python version check fails - if [[ $(python3_check) == "false" ]]; then - local error="Versioncheck failed! Python 3.7 or newer required!\n" - error="${error} Please upgrade Python." - print_error "${error}" && return - fi - - ### return early if moonraker_obico already exists - local moonraker_obico_services - moonraker_obico_services=$(moonraker_obico_systemd) - if [[ -n ${moonraker_obico_services} ]]; then - local error="At least one Moonraker-obico service is already installed:" - for s in ${moonraker_obico_services}; do - log_info "Found Moonraker-obico service: ${s}" - error="${error}\n ➔ ${s}" - done - print_error "${error}" && return - fi - ### return early if moonraker is not installed - local moonraker_services - moonraker_services=$(moonraker_systemd) - if [[ -z ${moonraker_services} ]]; then + local moonraker_count moonraker_names + moonraker_names=($(get_moonraker_names)) + moonraker_count=${#moonraker_names[@]} + if (( moonraker_count == 0 )); then local error="Moonraker not installed! Please install Moonraker first!" log_error "Moonraker-obico setup started without Moonraker being installed. Aborting setup." print_error "${error}" && return fi - local moonraker_count user_input=() moonraker_names=() - moonraker_count=$(echo "${moonraker_services}" | wc -w ) - for service in ${moonraker_services}; do - moonraker_names+=( "$(get_instance_name "${service}")" ) + local moonraker_obico_services moonraker_obico_names=() + moonraker_obico_services=$(moonraker_obico_systemd) + existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w ) + for service in ${moonraker_obico_services}; do + moonraker_obico_names+=( "$(get_instance_name "${service}")" ) done - local moonraker_obico_count - if (( moonraker_count == 1 )); then - ok_msg "Moonraker installation found!\n" - moonraker_obico_count=1 - elif (( moonraker_count > 1 )); then - 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}" - done - blank_line - echo -e "| The setup will apply the same names to |" - echo -e "| Moonraker-obico! |" - blank_line - echo -e "| Please select the number of Moonraker-obico instances |" - echo -e "| to install. Usually one Moonraker-obico instance per |" - echo -e "| Moonraker instance is required, but you may not |" - echo -e "| install more Moonraker-obico instances than available |" - echo -e "| Moonraker instances. |" - bottom_border + local allowed_moonraker_obico_count=$(( moonraker_count - existing_moonraker_obico_count )) + if (( allowed_moonraker_obico_count > 0 )); then - ### ask for amount of instances - local re="^[1-9][0-9]*$" - while [[ ! ${moonraker_obico_count} =~ ${re} || ${moonraker_obico_count} -gt ${moonraker_count} ]]; do - read -p "${cyan}###### Number of Moonraker-obico instances to set up:${white} " -i "${moonraker_count}" -e moonraker_obico_count - ### break if input is valid - [[ ${moonraker_obico_count} =~ ${re} && ${moonraker_obico_count} -le ${moonraker_count} ]] && break - ### conditional error messages - [[ ! ${moonraker_obico_count} =~ ${re} ]] && error_msg "Input not a number" - (( moonraker_obico_count > moonraker_count )) && error_msg "Number of Moonraker-obico instances larger than installed Moonraker instances" - done && select_msg "${moonraker_obico_count}" - else - log_error "Internal error. moonraker_count of '${moonraker_count}' not equal or grather than one!" - return 1 - fi - - user_input+=("${moonraker_obico_count}") - - ### confirm instance amount - local yn - while true; do - (( moonraker_obico_count == 1 )) && local question="Install Moonraker-obico?" - (( moonraker_obico_count > 1 )) && local question="Install ${moonraker_obico_count} Moonraker-obico instances?" - read -p "${cyan}###### ${question} (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - select_msg "Yes" - break;; - N|n|No|no) - select_msg "No" - abort_msg "Exiting Moonraker-obico setup ...\n" - return;; - *) - error_msg "Invalid Input!";; - esac - done - - ### write existing moonraker names into user_input array to use them as names for moonraker-obico - if (( moonraker_count > 1 )); then - for name in "${moonraker_names[@]}"; do - user_input+=("${name}") - done - fi - - (( moonraker_obico_count > 1 )) && status_msg "Installing ${moonraker_count} Moonraker-obico instances ..." - (( moonraker_obico_count == 1 )) && status_msg "Installing Moonraker-obico ..." - moonraker_obico_setup "${user_input[@]}" - -} - -function moonraker_obico_setup() { - - ### checking dependencies - local dep=(git dfu-util virtualenv python3 python3-pip python3-venv ffmpeg) - dependency_check "${dep[@]}" - - ### step 1: clone moonraker-obico - clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" - - ### step 2: call moonrake-obico/install.sh with the correct params - local input=("${@}") - local moonraker_obico_count=${input[0]} && unset "input[0]" - local names=("${input[@]}") && unset "input[@]" - local log="${KLIPPER_LOGS}" - local port=7125 cfg_dir moonraker_cfg - - if (( moonraker_obico_count == 1 )); then - cfg_dir="${KLIPPER_CONFIG}" - moonraker_cfg="${cfg_dir}/moonraker.conf" - - # Invoke moonrake-obico/install.sh - moonraker_obico_install -c "${moonraker_cfg}" -p ${port} -H 127.0.0.1 -l "${KLIPPER_LOGS}" - - elif (( moonraker_count > 1 )); then - local j=0 re="^[1-9][0-9]*$" - - for (( i=1; i <= moonraker_count; i++ )); do - ### overwrite config folder if name is only a number - if [[ ${names[j]} =~ ${re} ]]; then - cfg_dir="${KLIPPER_CONFIG}/printer_${names[${j}]}" - else - cfg_dir="${KLIPPER_CONFIG}/${names[${j}]}" + ### Step 1: Ask for the number of moonraker-obico instances to install + if (( moonraker_count == 1 )); then + ok_msg "Moonraker installation found!\n" + new_moonraker_obico_count=1 + elif (( moonraker_count > 1 )); then + 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}" + 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" " ●moonrakerobico-${name}" + done fi + blank_line + echo -e "| The setup will apply the same names to |" + echo -e "| Moonraker-obico! |" + blank_line + echo -e "| Please select the number of Moonraker-obico instances |" + echo -e "| to install. Usually one Moonraker-obico instance per |" + echo -e "| Moonraker instance is required, but you may not |" + echo -e "| install more Moonraker-obico instances than available |" + echo -e "| Moonraker instances. |" + bottom_border - moonraker_cfg="${cfg_dir}/moonraker.conf" + ### ask for amount of instances + local new_moonraker_obico_count re="^[1-9][0-9]*$" + while [[ ! ${new_moonraker_obico_count} =~ ${re} || ${new_moonraker_obico_count} -gt ${allowed_moonraker_obico_count} ]]; do + read -p "${cyan}###### Number of new Moonraker-obico instances to set up:${white} " -i "${allowed_moonraker_obico_count}" -e new_moonraker_obico_count + ### break if input is valid + [[ ${new_moonraker_obico_count} =~ ${re} && ${new_moonraker_obico_count} -le ${allowed_moonraker_obico_count} ]] && break + ### conditional error messages + [[ ! ${new_moonraker_obico_count} =~ ${re} ]] && error_msg "Input not a number" + (( new_moonraker_obico_count > allowed_moonraker_obico_count )) && error_msg "Number of Moonraker-obico instances larger than installed Moonraker instances" + done && select_msg "${new_moonraker_obico_count}" + else + log_error "Internal error. new_moonraker_obico_count of '${new_moonraker_obico_count}' not equal or grather than one!" + return 1 + fi - # Invoke moonrake-obico/install.sh - moonraker_obico_install -c "${moonraker_cfg}" -p ${port} -H 127.0.0.1 -l "${KLIPPER_LOGS}" - - port=$(( port + 1 )) - j=$(( j + 1 )) - done && unset j - - else - return 1 + ### Step 2: Confirm instance amount + local yn + while true; do + (( new_moonraker_obico_count == 1 )) && local question="Install Moonraker-obico?" + (( new_moonraker_obico_count > 1 )) && local question="Install ${new_moonraker_obico_count} Moonraker-obico instances?" + read -p "${cyan}###### ${question} (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + break;; + N|n|No|no) + select_msg "No" + abort_msg "Exiting Moonraker-obico setup ...\n" + return;; + *) + error_msg "Invalid Input!";; + esac + done fi + + if (( new_moonraker_obico_count > 0 )); then + + ### Step 3: Ask for the Obico server URL + obico_server_url_prompt + local obico_server_url + while true; do + read -p "${cyan}###### Obico Server URL:${white} " -i "https://app.obico.io" -e obico_server_url + if echo "${obico_server_url}" | grep -qE "^(http|https)://[a-zA-Z0-9./?=_%:-]*"; then + break + else + error_msg "Invalid server URL!" + fi + done + + (( new_moonraker_obico_count > 1 )) && status_msg "Installing ${new_moonraker_obico_count} Moonraker-obico instances ..." + (( new_moonraker_obico_count == 1 )) && status_msg "Installing Moonraker-obico ..." + + ### Step 4: Install dependencies + local dep=(git dfu-util virtualenv python3 python3-pip python3-venv ffmpeg) + dependency_check "${dep[@]}" + + ### Step 5: Clone the moonraker-obico repo + clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}" + + ### step 6: call moonrake-obico/install.sh with the correct params + local log="${KLIPPER_LOGS}" + local port=7125 moonraker_cfg + + 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}" + 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_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 )) + done && unset j + fi + fi + + ### Step 7: Link to the Obico server if necessary + if [[ -n ${moonraker_obico_services} ]]; then + for service in ${moonraker_obico_services}; do + local instance_name="$(get_instance_name ${service})" + if ! is_moonraker_obico_linked "${instance_name}"; then + not_linked_instances+=( "${instance_name}" ) + fi + done + if (( ${#not_linked_instances[@]} > 0 )); then + top_border + 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" " ●moonrakerobico-${name}" + done + blank_line + echo -e "| If you don't want to link the printer now, you can |" + echo -e "| restart the linking process later by: |" + echo -e "| 1. \`cd ~/kiauh && ./kiauh.sh\` to launch KIAUH. |" + echo -e "| 2. Select ${green}[Install]${white} |" + echo -e "| 3. Select ${green}[Link to Obico Server]${white} |" + blank_line + echo -e "| For more information, visit: |" + echo -e "| https://www.obico.io/docs/user-guides/klipper-setup/ |" + bottom_border + fi + + while true; do + read -p "${cyan}###### Link to the Obico Server now? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + break;; + N|n|No|no) + select_msg "No" + abort_msg "Exiting Moonraker-obico setup ...\n" + return;; + *) + error_msg "Invalid Input!";; + esac + done + + 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_DIR}/scripts/link.sh" -n "${name}" -c "${moonraker_obico_cfg}" + done + fi + } -function clone_moonraker_obico() { +function clone_or_update_moonraker_obico() { local repo=${1} - status_msg "Cloning Moonraker-obico from ${repo} ..." - - ### force remove existing moonraker-obico dir and clone into fresh moonraker-obico dir - [[ -d ${MOONRAKER_OBICO_DIR} ]] && rm -rf "${MOONRAKER_OBICO_DIR}" - - 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 + 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 fi } @@ -218,7 +295,7 @@ function remove_moonraker_obico_systemd() { } function remove_moonraker_obico_logs() { - local files regex="moonraker-obico(-[0-9a-zA-Z]+)?\.log(.*)?" + local files regex="moonraker-obico(-[0-9a-zA-Z]+)?\.log([.-0-9]+)?" files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" 2> /dev/null | sort) if [[ -n ${files} ]]; then @@ -239,10 +316,10 @@ function remove_moonraker_obico_dir() { } function remove_moonraker_obico_env() { - [[ ! -d "${HOME}/moonraker-env" ]] && return + [[ ! -d "${HOME}/moonraker-obico-env" ]] && return - status_msg "Removing moonraker-env directory ..." - rm -rf "${HOME}/moonraker-env" + status_msg "Removing moonraker-obico-env directory ..." + rm -rf "${HOME}/moonraker-obico-env" ok_msg "Directory removed!" } @@ -265,15 +342,9 @@ function update_moonraker() { do_action_service "stop" "${service}" done - if [[ ! -d ${MOONRAKER_OBICO_DIR} ]]; then - clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" - else - status_msg "Updating Moonraker-obico ..." - cd "${MOONRAKER_OBICO_DIR}" && git pull - "${MOONRAKER_OBICO_DIR}/install.sh" -u - fi - + 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 @@ -283,27 +354,24 @@ function update_moonraker() { #============= MOONRAKER-OBICO STATUS ==============# #===================================================# -function get_obico_status() { - local sf_count status - sf_count="$(klipperscreen_systemd | wc -w)" +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 ) - ### remove the "SERVICE" entry from the data array if a moonraker service is installed - local data_arr=(SERVICE "${KLIPPERSCREEN_DIR}" "${KLIPPERSCREEN_ENV}") - (( sf_count > 0 )) && unset "data_arr[0]" - - ### count+1 for each found data-item from array - local filecount=0 - for data in "${data_arr[@]}"; do - [[ -e ${data} ]] && filecount=$(( filecount + 1 )) - done - - if (( filecount == ${#data_arr[*]} )); then - status="Installed!" - elif (( filecount == 0 )); then + if (( sf_count == 0 )); then status="Not installed!" - else + elif [[ ! -e "${MOONRAKER_OBICO_DIR}" ]]; then status="Incomplete!" + else + status="Installed!" + for service in ${moonraker_obico_services}; do + if ! is_moonraker_obico_linked "$(get_instance_name ${service})"; then + status="Not linked!" + fi + done fi + echo "${status}" } diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 0df0d3e..5cbda6e 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -12,6 +12,11 @@ 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 @@ -25,7 +30,7 @@ function install_ui() { echo -e "| | Other: |" echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |" echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" - echo -e "| 4) [Fluidd] | 9) [Obico for Klipper] |" + echo -e "| 4) [Fluidd] | 9) ${moonraker_obico_menu}|" echo -e "| | |" echo -e "| Touchscreen GUI: | Webcam Streamer: |" echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |" diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index 90aa4db..91092cc 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -24,7 +24,7 @@ function main_ui() { echo -e "| 5) [Backup] | Fluidd: $(print_status "fluidd")|" echo -e "| | KlipperScreen: $(print_status "klipperscreen")|" echo -e "| 6) [Settings] | Telegram Bot: $(print_status "telegram_bot")|" - echo -e "| | Obico: $(print_status "obico")|" + echo -e "| | Obico: $(print_status "moonraker_obico")|" echo -e "| | |" echo -e "| $(print_kiauh_version)| Octoprint: $(print_status "octoprint")|" quit_footer @@ -49,7 +49,7 @@ function print_status() { if [[ ${status} == "Not installed!" ]]; then status="${red}${status}${white}" - elif [[ ${status} == "Incomplete!" ]]; then + elif [[ ${status} == "Incomplete!" ]] || [[ ${status} == "Not linked!" ]]; then status="${yellow}${status}${white}" else status="${green}${status}${white}" -- 2.39.5 From 8f5bdf2707e047db002ad124052ec204d3ecceca Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 14:30:36 -0700 Subject: [PATCH 07/35] Make the linking step more user-friendly --- scripts/obico.sh | 87 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 199129d..0376ad2 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -203,53 +203,54 @@ function moonraker_obico_setup_dialog() { fi ### Step 7: Link to the Obico server if necessary - if [[ -n ${moonraker_obico_services} ]]; then - for service in ${moonraker_obico_services}; do - local instance_name="$(get_instance_name ${service})" - if ! is_moonraker_obico_linked "${instance_name}"; then - not_linked_instances+=( "${instance_name}" ) - fi - done - if (( ${#not_linked_instances[@]} > 0 )); then - top_border - 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" " ●moonrakerobico-${name}" - done - blank_line - echo -e "| If you don't want to link the printer now, you can |" - echo -e "| restart the linking process later by: |" - echo -e "| 1. \`cd ~/kiauh && ./kiauh.sh\` to launch KIAUH. |" - echo -e "| 2. Select ${green}[Install]${white} |" - echo -e "| 3. Select ${green}[Link to Obico Server]${white} |" - blank_line - echo -e "| For more information, visit: |" - echo -e "| https://www.obico.io/docs/user-guides/klipper-setup/ |" - bottom_border - fi - - while true; do - read -p "${cyan}###### Link to the Obico Server now? (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - select_msg "Yes" - break;; - N|n|No|no) - select_msg "No" - abort_msg "Exiting Moonraker-obico setup ...\n" - return;; - *) - error_msg "Invalid Input!";; - esac - done - + local not_linked_instances=() + for service in $(moonraker_obico_systemd); do + local instance_name="$(get_instance_name ${service})" + if ! is_moonraker_obico_linked "${instance_name}"; then + not_linked_instances+=( "${instance_name}" ) + fi + done + if (( ${#not_linked_instances[@]} > 0 )); then + top_border + printf "|${green}%-55s${white}|\n" " ${#not_linked_instances[@]} Moonraker-obico instances not linked to the server!" 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_DIR}/scripts/link.sh" -n "${name}" -c "${moonraker_obico_cfg}" + printf "|${cyan}%-57s${white}|\n" " ●moonrakerobico-${name}" done + blank_line + echo -e "| To link to your Obico Server account, you need to |" + echo -e "| obtain the 6-digit verification code in the Obico |" + echo -e "| mobile or web app. For more information, visit: |" + echo -e "| https://www.obico.io/docs/user-guides/klipper-setup/ |" + blank_line + echo -e "| If you don't want to link the printer now, you can |" + echo -e "| restart the linking process later by: |" + echo -e "| 1. \`cd ~/kiauh && ./kiauh.sh\` to launch KIAUH. |" + echo -e "| 2. Select ${green}[Install]${white} |" + echo -e "| 3. Select ${green}[Link to Obico Server]${white} |" + bottom_border fi + while true; do + read -p "${cyan}###### Link to your Obico Server account now? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + break;; + N|n|No|no) + select_msg "No" + abort_msg "Exiting Moonraker-obico setup ...\n" + return;; + *) + error_msg "Invalid Input!";; + esac + done + + 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_DIR}/scripts/link.sh" -q -n "${name}" -c "${moonraker_obico_cfg}" + done + } function clone_or_update_moonraker_obico() { -- 2.39.5 From ff9b563131ee1d3c156798557b2069876943a4ef Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 16:11:01 -0700 Subject: [PATCH 08/35] get_instance_name takes base_name now so that it won't confuse moonraker-obico as an instance of Moonraker --- scripts/moonraker-telegram-bot.sh | 2 +- scripts/moonraker.sh | 6 +++--- scripts/obico.sh | 8 ++++---- scripts/octoprint.sh | 2 +- scripts/utilities.sh | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/moonraker-telegram-bot.sh b/scripts/moonraker-telegram-bot.sh index d10c5e1..9e79ad9 100644 --- a/scripts/moonraker-telegram-bot.sh +++ b/scripts/moonraker-telegram-bot.sh @@ -38,7 +38,7 @@ function telegram_bot_setup_dialog() { local moonraker_count user_input=() moonraker_names=() moonraker_count=$(echo "${moonraker_services}" | wc -w ) for service in ${moonraker_services}; do - moonraker_names+=( "$(get_instance_name "${service}")" ) + moonraker_names+=( "$(get_instance_name "${service}" moonraker)" ) done local telegram_bot_count diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 294054c..609fe13 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -17,7 +17,7 @@ set -e function moonraker_systemd() { local services - services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker(-[0-9a-zA-Z]+)?.service" | sort) + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/moonraker(-[0-9a-zA-Z]+)?.service" | grep -v "moonraker-obico" | sort) echo "${services}" } @@ -55,7 +55,7 @@ function moonraker_setup_dialog() { local klipper_count user_input=() klipper_names=() klipper_count=$(echo "${klipper_services}" | wc -w ) for service in ${klipper_services}; do - klipper_names+=( "$(get_instance_name "${service}")" ) + klipper_names+=( "$(get_instance_name "${service}" moonraker)" ) done local moonraker_count @@ -572,4 +572,4 @@ function compare_moonraker_versions() { fi echo "${versions}" -} \ No newline at end of file +} diff --git a/scripts/obico.sh b/scripts/obico.sh index 0376ad2..0659284 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -51,7 +51,7 @@ function get_moonraker_names() { fi for service in ${moonraker_services}; do - get_instance_name "${service}" + get_instance_name "${service}" moonraker done } @@ -89,7 +89,7 @@ function moonraker_obico_setup_dialog() { moonraker_obico_services=$(moonraker_obico_systemd) existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w ) for service in ${moonraker_obico_services}; do - moonraker_obico_names+=( "$(get_instance_name "${service}")" ) + moonraker_obico_names+=( "$(get_instance_name "${service}" moonraker_obico)" ) done local allowed_moonraker_obico_count=$(( moonraker_count - existing_moonraker_obico_count )) @@ -205,7 +205,7 @@ function moonraker_obico_setup_dialog() { ### Step 7: Link to the Obico server if necessary local not_linked_instances=() for service in $(moonraker_obico_systemd); do - local instance_name="$(get_instance_name ${service})" + local instance_name="$(get_instance_name "${service}" moonraker_obico)" if ! is_moonraker_obico_linked "${instance_name}"; then not_linked_instances+=( "${instance_name}" ) fi @@ -367,7 +367,7 @@ function get_moonraker_obico_status() { else status="Installed!" for service in ${moonraker_obico_services}; do - if ! is_moonraker_obico_linked "$(get_instance_name ${service})"; then + if ! is_moonraker_obico_linked "$(get_instance_name "${service}" moonraker_obico)"; then status="Not linked!" fi done diff --git a/scripts/octoprint.sh b/scripts/octoprint.sh index 8bdd9f3..144b033 100644 --- a/scripts/octoprint.sh +++ b/scripts/octoprint.sh @@ -35,7 +35,7 @@ function octoprint_setup_dialog() { local klipper_count user_input=() klipper_names=() klipper_count=$(echo "${klipper_services}" | wc -w ) for service in ${klipper_services}; do - klipper_names+=( "$(get_instance_name "${service}")" ) + klipper_names+=( "$(get_instance_name "${service}" klipper)" ) done local octoprint_count diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 94a4a13..ca22069 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -679,8 +679,8 @@ function set_hostname() { ### input: /etc/systemd/system/klipper-name.service ### returns: name function get_instance_name() { - local instance=${1} name - name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"-" -f1 | rev | cut -d"." -f1) + local instance=${1} base_name=${2} name + name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | rev | sed -E "s/${base_name}-//") echo "${name}" } -- 2.39.5 From a619df236457f4281fdc06c6fc22c1f6a277cdf1 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 17:48:54 -0700 Subject: [PATCH 09/35] A few typos --- scripts/moonraker.sh | 2 +- scripts/obico.sh | 2 +- scripts/ui/update_menu.sh | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 609fe13..45af1be 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -55,7 +55,7 @@ function moonraker_setup_dialog() { local klipper_count user_input=() klipper_names=() klipper_count=$(echo "${klipper_services}" | wc -w ) for service in ${klipper_services}; do - klipper_names+=( "$(get_instance_name "${service}" moonraker)" ) + klipper_names+=( "$(get_instance_name "${service}" klipper)" ) done local moonraker_count diff --git a/scripts/obico.sh b/scripts/obico.sh index 0659284..efecf09 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -338,7 +338,7 @@ function remove_moonraker_obico() { #============= UPDATE MOONRAKER-OBICO ==============# #===================================================# -function update_moonraker() { +function update_moonraker_obico() { for service in $(moonraker_obico_systemd | cut -d"/" -f5); do do_action_service "stop" "${service}" done diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index ea48024..a4fb2c3 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -61,6 +61,8 @@ function update_menu() { 7) do_action "update_telegram_bot" "update_ui";; 8) + do_action "update_moonraker_obico" "update_ui";; + 9) do_action "update_system" "update_ui";; a) do_action "update_all" "update_ui";; -- 2.39.5 From db203d03218769507d0a737fc0839823e35b8aa9 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 17:49:32 -0700 Subject: [PATCH 10/35] Single-instance and multi-instance need to be treated differently --- scripts/obico.sh | 23 ++++++++++++++++------- scripts/octoprint.sh | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index efecf09..e1d5108 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -23,7 +23,7 @@ function moonraker_obico_systemd() { function cfg_dir() { local name=${1} - if [[ -z ${name} ]]; then + if [[ -z ${name} || ${name} == "moonraker" || ${name} == "moonraker-obico" ]]; then echo "${KLIPPER_CONFIG}" else local re="^[1-9][0-9]*$" @@ -39,7 +39,7 @@ function cfg_dir() { function is_moonraker_obico_linked() { local name=${1} moonraker_obico_cfg="$(cfg_dir ${name})/moonraker-obico.cfg" - grep -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token' + grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token' return $? } @@ -204,6 +204,7 @@ function moonraker_obico_setup_dialog() { ### Step 7: Link to the Obico server if necessary 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)" if ! is_moonraker_obico_linked "${instance_name}"; then @@ -212,10 +213,14 @@ function moonraker_obico_setup_dialog() { done if (( ${#not_linked_instances[@]} > 0 )); then top_border - 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" " ●moonrakerobico-${name}" - done + if (( moonraker_count == 1 )); then + printf "|${green}%-55s${white}|\n" " Moonraker-obico not linked to the server!" + 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" " ●moonrakerobico-${name}" + done + fi blank_line echo -e "| To link to your Obico Server account, you need to |" echo -e "| obtain the 6-digit verification code in the Obico |" @@ -248,7 +253,11 @@ 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_DIR}/scripts/link.sh" -q -n "${name}" -c "${moonraker_obico_cfg}" + if (( moonraker_count == 1 )); then + "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "${moonraker_obico_cfg}" + else + "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -n "${name}" -c "${moonraker_obico_cfg}" + fi done } diff --git a/scripts/octoprint.sh b/scripts/octoprint.sh index 144b033..dc3ad01 100644 --- a/scripts/octoprint.sh +++ b/scripts/octoprint.sh @@ -404,4 +404,4 @@ function get_octoprint_status() { fi echo "${status}" -} \ No newline at end of file +} -- 2.39.5 From 433be6a678548e08db906dd8e66f4e1d1cfd2569 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Mon, 1 Aug 2022 18:05:10 -0700 Subject: [PATCH 11/35] A few more typos --- scripts/obico.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index e1d5108..791fcdb 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -89,7 +89,7 @@ function moonraker_obico_setup_dialog() { moonraker_obico_services=$(moonraker_obico_systemd) existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w ) for service in ${moonraker_obico_services}; do - moonraker_obico_names+=( "$(get_instance_name "${service}" moonraker_obico)" ) + moonraker_obico_names+=( "$(get_instance_name "${service}" moonraker-obico)" ) done local allowed_moonraker_obico_count=$(( moonraker_count - existing_moonraker_obico_count )) @@ -109,7 +109,7 @@ function moonraker_obico_setup_dialog() { 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" " ●moonrakerobico-${name}" + printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" done fi blank_line @@ -206,7 +206,7 @@ function moonraker_obico_setup_dialog() { 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)" + local instance_name="$(get_instance_name "${service}" moonraker-obico)" if ! is_moonraker_obico_linked "${instance_name}"; then not_linked_instances+=( "${instance_name}" ) fi @@ -218,7 +218,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" " ●moonrakerobico-${name}" + printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" done fi blank_line @@ -376,7 +376,7 @@ function get_moonraker_obico_status() { else status="Installed!" for service in ${moonraker_obico_services}; do - if ! is_moonraker_obico_linked "$(get_instance_name "${service}" moonraker_obico)"; then + if ! is_moonraker_obico_linked "$(get_instance_name "${service}" moonraker-obico)"; then status="Not linked!" fi done -- 2.39.5 From 992df950c3decf6b79c3d364bc1d23589672944a Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sat, 13 Aug 2022 07:40:31 -0700 Subject: [PATCH 12/35] Removed unused vars --- scripts/obico.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 791fcdb..faf6e37 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -98,7 +98,6 @@ function moonraker_obico_setup_dialog() { ### Step 1: Ask for the number of moonraker-obico instances to install if (( moonraker_count == 1 )); then ok_msg "Moonraker installation found!\n" - new_moonraker_obico_count=1 elif (( moonraker_count > 1 )); then top_border printf "|${green}%-55s${white}|\n" " ${moonraker_count} Moonraker instances found!" @@ -134,7 +133,7 @@ function moonraker_obico_setup_dialog() { (( new_moonraker_obico_count > allowed_moonraker_obico_count )) && error_msg "Number of Moonraker-obico instances larger than installed Moonraker instances" done && select_msg "${new_moonraker_obico_count}" else - log_error "Internal error. new_moonraker_obico_count of '${new_moonraker_obico_count}' not equal or grather than one!" + log_error "Internal error. moonraker_count of '${moonraker_count}' not equal or grather than one!" return 1 fi @@ -183,7 +182,6 @@ function moonraker_obico_setup_dialog() { clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}" ### step 6: call moonrake-obico/install.sh with the correct params - local log="${KLIPPER_LOGS}" local port=7125 moonraker_cfg if (( moonraker_count == 1 )); then -- 2.39.5 From 181a5d965aa437cac4df364b2ea44ae7ee54fc37 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:14:33 +0200 Subject: [PATCH 13/35] 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 -- 2.39.5 From 78cc8bbffb30f7cd32d34ff7792da5eac405d63d Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:16:07 +0200 Subject: [PATCH 14/35] 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 -- 2.39.5 From 910c5867a67977a5792f7a1d531b8a1d6e1834d4 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:23:09 +0200 Subject: [PATCH 15/35] 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" "$@" } #===================================================# -- 2.39.5 From 99fa596183688cb6a4455195a2a175d66e0dbe35 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 11:33:21 +0200 Subject: [PATCH 16/35] 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 -- 2.39.5 From fa2b7280376b08798449db12c0808daa8ea6a4ac Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 12:03:14 +0200 Subject: [PATCH 17/35] 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 -- 2.39.5 From a9332a2b6efac1c0804cd8e4d9b4189cf5df734c Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 13:39:50 +0200 Subject: [PATCH 18/35] 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 } -- 2.39.5 From 53e03254e7d4f75986b15c29de6aedbb0a8d857e Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 13:51:45 +0200 Subject: [PATCH 19/35] 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" } #===================================================# -- 2.39.5 From cb53982cac488abc1231929cb87c3bf57eaa75ae Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 14:03:47 +0200 Subject: [PATCH 20/35] 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" } -- 2.39.5 From d11323d55db4bb362e15b98af09d5515bae2bf82 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 14:25:55 +0200 Subject: [PATCH 21/35] 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}" } -- 2.39.5 From 7d5b4e7242eca2065d10ad32491a50da49c0a3f1 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 16:01:18 +0200 Subject: [PATCH 22/35] 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}" -- 2.39.5 From 23c39d225ab727302819fd684351b7a1c59270b5 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 16:19:34 +0200 Subject: [PATCH 23/35] 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 -- 2.39.5 From d79856b6410256fd0718527e3adbff534b74aae0 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 13 Aug 2022 18:12:52 +0200 Subject: [PATCH 24/35] 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[@]}" +} -- 2.39.5 From 88d3daf20e5e0ed2a41d5088e19472b9be4810cc Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sat, 13 Aug 2022 17:07:04 -0700 Subject: [PATCH 25/35] Separate getting instance names from config_folders to be more flexible --- scripts/utilities.sh | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index b70f421..3e46b3a 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -739,17 +739,15 @@ function fetch_multi_instance_names() { } ### -# helper function that returns all possibly available absolute -# klipper config directory paths based on their instance name. +# Helper function that returns all configured instance names # -# => returns an empty string if klipper is not installed -# => returns a space separated string of absolute config directory paths +# => return an empty string if 0 or 1 klipper instance is installed +# => return space-separated string for names of the configured instances +# if 2 or more klipper instances are installed # -function get_config_folders() { +function get_multi_instance_names() { read_kiauh_ini "${FUNCNAME[0]}" - local instance_names=() - local cfg_dirs=() ### # convert the comma separates string from the .kiauh.ini into @@ -757,8 +755,23 @@ function get_config_folders() { # 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 + echo "${instance_names[@]}" +} + +### +# 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() { + local cfg_dirs=() + local instance_names + instance_names=$(get_multi_instance_names) + + if [[ -n ${instance_names} ]]; then + for name in "${instance_names}"; do ### # by KIAUH convention, all instance names of only numbers # need to be prefixed with 'printer_' @@ -768,7 +781,7 @@ function get_config_folders() { cfg_dirs+=("${KLIPPER_CONFIG}/${name}") fi done - elif (( ${#instance_names[@]} == 0 && $(klipper_systemd | wc -w) > 0 )); then + elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) > 0 ]]; then cfg_dirs+=("${KLIPPER_CONFIG}") else cfg_dirs=() -- 2.39.5 From 7837a78c4b6113e76dc883058118c0a21eef3410 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sat, 13 Aug 2022 17:08:24 -0700 Subject: [PATCH 26/35] Get instance names from the ini file to be consistent with the rest of the KIAUH --- scripts/obico.sh | 149 +++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 83 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index cd73446..400b391 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -21,40 +21,24 @@ function moonraker_obico_systemd() { echo "${services}" } -function cfg_dir() { - local name=${1} - if [[ -z ${name} || ${name} == "moonraker" || ${name} == "moonraker-obico" ]]; then - echo "${KLIPPER_CONFIG}" +function moonraker_obico_config() { + local moonraker_cfg_dirs=($(get_config_folders)) + if [[ -n "${moonraker_cfg_dirs}" ]]; then + echo "${moonraker_cfg_dirs[${i}]}/moonraker-obico.cfg" else - local re="^[1-9][0-9]*$" - ### overwrite config folder if name is only a number - if [[ ${name} =~ ${re} ]]; then - echo "${KLIPPER_CONFIG}/printer_${name}" - else - echo "${KLIPPER_CONFIG}/${name}" - fi + echo "" fi } -function is_moonraker_obico_linked() { - local name=${1} - moonraker_obico_cfg="$(cfg_dir "${name}")/moonraker-obico.cfg" - grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token' +function moonraker_obico_needs_linking() { + moonraker_obico_cfg=${1} + if [[ ! -f "${moonraker_obico_cfg}" ]]; then + return 1 + fi + grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -vq 'auth_token' return $? } -function get_moonraker_names() { - local moonraker_services - moonraker_services=$(moonraker_systemd) - if [[ -z ${moonraker_services} ]]; then - echo '' && return - fi - - for service in ${moonraker_services}; do - get_instance_name "${service}" moonraker - done -} - function obico_server_url_prompt() { top_border printf "|${green}%-55s${white}|\n" " Obico Server URL" @@ -75,31 +59,29 @@ function obico_server_url_prompt() { function moonraker_obico_setup_dialog() { status_msg "Initializing Moonraker-obico installation ..." - ### return early if moonraker is not installed local moonraker_count moonraker_names - moonraker_names=($(get_moonraker_names)) - moonraker_count=${#moonraker_names[@]} + moonraker_count=$(moonraker_systemd | wc -w) if (( moonraker_count == 0 )); then + ### return early if moonraker is not installed local error="Moonraker not installed! Please install Moonraker first!" log_error "Moonraker-obico setup started without Moonraker being installed. Aborting setup." print_error "${error}" && return + elif (( moonraker_count > 1 )); then + moonraker_names=($(get_multi_instance_names)) # moonraker_names is valid only in case of multi-instance fi 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 - moonraker_obico_names+=( "$(get_instance_name "${service}" moonraker-obico)" ) - done - local allowed_moonraker_obico_count=$(( moonraker_count - existing_moonraker_obico_count )) if (( allowed_moonraker_obico_count > 0 )); then + local new_moonraker_obico_count ### Step 1: Ask for the number of moonraker-obico instances to install if (( moonraker_count == 1 )); then ok_msg "Moonraker installation found!\n" + new_moonraker_obico_count=1 elif (( moonraker_count > 1 )); then top_border printf "|${green}%-55s${white}|\n" " ${moonraker_count} Moonraker instances found!" @@ -109,8 +91,8 @@ function moonraker_obico_setup_dialog() { 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}" + for svc in "${moonraker_obico_services}"; do + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${service}" moonraker-obico)" done fi blank_line @@ -125,7 +107,7 @@ function moonraker_obico_setup_dialog() { bottom_border ### ask for amount of instances - local new_moonraker_obico_count re="^[1-9][0-9]*$" + local re="^[1-9][0-9]*$" while [[ ! ${new_moonraker_obico_count} =~ ${re} || ${new_moonraker_obico_count} -gt ${allowed_moonraker_obico_count} ]]; do read -p "${cyan}###### Number of new Moonraker-obico instances to set up:${white} " -i "${allowed_moonraker_obico_count}" -e new_moonraker_obico_count ### break if input is valid @@ -137,7 +119,7 @@ function moonraker_obico_setup_dialog() { else log_error "Internal error. moonraker_count of '${moonraker_count}' not equal or grather than one!" return 1 - fi + fi # (( moonraker_count == 1 )) ### Step 2: Confirm instance amount local yn @@ -157,7 +139,7 @@ function moonraker_obico_setup_dialog() { error_msg "Invalid Input!";; esac done - fi + fi # (( allowed_moonraker_obico_count > 0 )) if (( new_moonraker_obico_count > 0 )); then @@ -184,42 +166,43 @@ function moonraker_obico_setup_dialog() { clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" ### step 6: call moonrake-obico/install.sh with the correct params - local port=7125 moonraker_cfg + local port=7125 moonraker_cfg_dirs=($(get_config_folders)) 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_dirs[0]}/moonraker.conf" -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_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}" + "${MOONRAKER_OBICO_DIR}/install.sh" -n "${moonraker_names[${j}]}" -C "${moonraker_cfg_dirs[${j}]}/moonraker.conf" -p $((port+j)) -H 127.0.0.1 -l "${KLIPPER_LOGS}" -s -L -S "${obico_server_url}" j=$(( j + 1 )) done && unset j - fi - fi + fi # (( moonraker_count == 1 )) + fi # (( new_moonraker_obico_count > 0 )) ### 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 - instance_name="$(get_instance_name "${service}" moonraker-obico)" - if ! is_moonraker_obico_linked "${instance_name}"; then - not_linked_instances+=( "${instance_name}" ) + if (( moonraker_count == 1 )); then + if moonraker_obico_needs_linking "$(moonraker_obico_config 0)"; then + not_linked_instances+=("0") + fi + elif (( moonraker_count > 1 )); then + for (( i=0; i <= moonraker_count; i++ )); do + if moonraker_obico_needs_linking "$(moonraker_obico_config ${i})"; then + not_linked_instances+=("${i}") fi - done + done + fi # (( moonraker_count == 1 )) + if (( ${#not_linked_instances[@]} > 0 )); then top_border if (( moonraker_count == 1 )); then printf "|${green}%-55s${white}|\n" " Moonraker-obico not linked to the server!" 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}" + for i in "${not_linked_instances[@]}"; do + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obico-{moonraker_names[i]}" done fi blank_line @@ -234,33 +217,33 @@ function moonraker_obico_setup_dialog() { echo -e "| 2. Select ${green}[Install]${white} |" echo -e "| 3. Select ${green}[Link to Obico Server]${white} |" bottom_border - fi - while true; do - read -p "${cyan}###### Link to your Obico Server account now? (Y/n):${white} " yn - case "${yn}" in - Y|y|Yes|yes|"") - select_msg "Yes" - break;; - N|n|No|no) - select_msg "No" - abort_msg "Exiting Moonraker-obico setup ...\n" - return;; - *) - error_msg "Invalid Input!";; - esac - done + while true; do + read -p "${cyan}###### Link to your Obico Server account now? (Y/n):${white} " yn + case "${yn}" in + Y|y|Yes|yes|"") + select_msg "Yes" + break;; + N|n|No|no) + select_msg "No" + abort_msg "Exiting Moonraker-obico setup ...\n" + return;; + *) + error_msg "Invalid Input!";; + esac + done - 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" if (( moonraker_count == 1 )); then - "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "${moonraker_obico_cfg}" - else - "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -n "${name}" -c "${moonraker_obico_cfg}" - fi - done - + status_msg "Link moonraker-obico to the Obico Server..." + "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "$(moonraker_obico_config 0)" + elif (( moonraker_count > 1 )); then + for i in "${not_linked_instances[@]}"; do + local name="${moonraker_names[i]}" + status_msg "Link moonraker-obico-${name} to the Obico Server..." + "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -n "${name}" -c "$(moonraker_obico_config ${i})" + done + fi # (( moonraker_count == 1 )) + fi # (( ${#not_linked_instances[@]} > 0 )) } function clone_moonraker_obico() { @@ -375,8 +358,8 @@ function get_moonraker_obico_status() { 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 + for cfg_dir in $(get_config_folders); do + if moonraker_obico_needs_linking "${cfg_dir}/moonraker-obico.cfg"; then is_linked="false" fi done -- 2.39.5 From 8d284179d8a21f7ef6720d5c9388a27e7fce58c5 Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sat, 13 Aug 2022 20:53:37 -0700 Subject: [PATCH 27/35] Bugs primarily caused by the confusion between space-separated vars and arrays --- scripts/obico.sh | 15 +++++++++------ scripts/utilities.sh | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 400b391..5183cbc 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -35,8 +35,11 @@ function moonraker_obico_needs_linking() { if [[ ! -f "${moonraker_obico_cfg}" ]]; then return 1 fi - grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -vq 'auth_token' - return $? + if grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token'; then + return 1 + else + return 0 + fi } function obico_server_url_prompt() { @@ -91,8 +94,8 @@ function moonraker_obico_setup_dialog() { blank_line if (( existing_moonraker_obico_count > 0 )); then printf "|${green}%-55s${white}|\n" " ${existing_moonraker_obico_count} Moonraker-obico instances already installed!" - for svc in "${moonraker_obico_services}"; do - printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${service}" moonraker-obico)" + for svc in ${moonraker_obico_services}; do + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${svc}" moonraker-obico)" done fi blank_line @@ -202,7 +205,7 @@ function moonraker_obico_setup_dialog() { else printf "|${green}%-55s${white}|\n" " ${#not_linked_instances[@]} Moonraker-obico instances not linked to the server!" for i in "${not_linked_instances[@]}"; do - printf "|${cyan}%-57s${white}|\n" " ● moonraker-obico-{moonraker_names[i]}" + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obico-${moonraker_names[${i}]}" done fi blank_line @@ -251,7 +254,7 @@ function clone_moonraker_obico() { status_msg "Cloning Moonraker-obico from ${repo} ..." ### force remove existing Moonraker-obico dir - [[ -d ${repo} ]] && rm -rf "${MOONRAKER_OBICO_DIR}" + [[ -d "${MOONRAKER_OBICO_DIR}" ]] && rm -rf "${MOONRAKER_OBICO_DIR}" cd "${HOME}" || exit 1 if ! git clone "${repo}" "${MOONRAKER_OBICO_DIR}"; then diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 3e46b3a..301cef0 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -771,7 +771,7 @@ function get_config_folders() { instance_names=$(get_multi_instance_names) if [[ -n ${instance_names} ]]; then - for name in "${instance_names}"; do + for name in ${instance_names}; do ### # by KIAUH convention, all instance names of only numbers # need to be prefixed with 'printer_' -- 2.39.5 From 7a7538bfbec3c9f482772d78f36fb25f58f2c529 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sun, 14 Aug 2022 15:16:17 +0200 Subject: [PATCH 28/35] refactor: factor out and refactor helper functions Signed-off-by: Dominik Willner --- scripts/ui/install_menu.sh | 6 +++++ scripts/utilities.sh | 50 ++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 04f56c6..9f60e7f 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -36,6 +36,12 @@ function install_menu() { clear && print_header install_ui + ### save all installed webinterface ports to the ini file + fetch_webui_ports + + ### save all klipper multi-instance names to the ini file + set_multi_instance_names + local action while true; do read -p "${cyan}####### Perform action:${white} " action diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 301cef0..1a08646 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -178,16 +178,8 @@ function init_ini() { 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}" } @@ -712,30 +704,34 @@ function get_klipper_instance_name() { } ### -# save all instance names in a comma separated format to the kiauh.ini +# loops through all installed klipper services and saves +# each instances name in a comma separated format to the kiauh.ini # -function add_to_multi_instance_names() { +function set_multi_instance_names() { read_kiauh_ini "${FUNCNAME[0]}" - local name="${1}" - local names="${multi_instance_names}" + local name + local names="" + local services=$(klipper_systemd) - if ! grep -Eq "${name}" <<< "${names}"; then - names="${names}${name}," - sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" + ### + # if value of 'multi_instance_names' is not an empty + # string, delete its value, so it can be re-written + if [[ -n ${multi_instance_names} ]]; then + sed -i "/multi_instance_names=/s/=.*/=/" "${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}" + for svc in ${services}; do + name=$(get_klipper_instance_name "${svc}") + + if ! grep -Eq "${name}" <<<"${names}"; then + names="${names}${name}," + fi + done + + # write up-to-date instance name string to kiauh.ini + sed -i "/multi_instance_names=/s/=.*/=${names}/" "${INI_FILE}" } ### @@ -762,8 +758,8 @@ function get_multi_instance_names() { # 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 +# => return an empty string if klipper is not installed +# => return space-separated string of absolute config directory paths # function get_config_folders() { local cfg_dirs=() -- 2.39.5 From 794072203f2258499decd5b3e10c077b09d22fbc Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sun, 14 Aug 2022 15:46:56 +0200 Subject: [PATCH 29/35] fix: typo Signed-off-by: Dominik Willner --- scripts/utilities.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 1a08646..fe52dd1 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -777,7 +777,7 @@ function get_config_folders() { cfg_dirs+=("${KLIPPER_CONFIG}/${name}") fi done - elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) > 0 ]]; then + elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) -gt 0 ]]; then cfg_dirs+=("${KLIPPER_CONFIG}") else cfg_dirs=() -- 2.39.5 From 276c123d3d6d0d3609e0f4b179b545d7d53f40cd Mon Sep 17 00:00:00 2001 From: Kenneth Jiang Date: Sun, 14 Aug 2022 07:13:36 -0700 Subject: [PATCH 30/35] Bug fixes based PR review feedback --- scripts/obico.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 5183cbc..2f1dc54 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -24,14 +24,14 @@ function moonraker_obico_systemd() { function moonraker_obico_config() { local moonraker_cfg_dirs=($(get_config_folders)) if [[ -n "${moonraker_cfg_dirs}" ]]; then - echo "${moonraker_cfg_dirs[${i}]}/moonraker-obico.cfg" + echo "${moonraker_cfg_dirs[${1}]}/moonraker-obico.cfg" else echo "" fi } function moonraker_obico_needs_linking() { - moonraker_obico_cfg=${1} + local moonraker_obico_cfg=${1} if [[ ! -f "${moonraker_obico_cfg}" ]]; then return 1 fi @@ -184,7 +184,6 @@ function moonraker_obico_setup_dialog() { fi # (( new_moonraker_obico_count > 0 )) ### Step 7: Link to the Obico server if necessary - local instance_name local not_linked_instances=() if (( moonraker_count == 1 )); then if moonraker_obico_needs_linking "$(moonraker_obico_config 0)"; then -- 2.39.5 From ee863329884a2c102b6adc6732a766ae8d65a4ee Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 15 Aug 2022 12:21:06 +0200 Subject: [PATCH 31/35] refactor: final polishing Signed-off-by: Dominik Willner --- scripts/obico.sh | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 2f1dc54..30b95d9 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -22,8 +22,11 @@ function moonraker_obico_systemd() { } function moonraker_obico_config() { - local moonraker_cfg_dirs=($(get_config_folders)) - if [[ -n "${moonraker_cfg_dirs}" ]]; then + local moonraker_cfg_dirs + + read -r -a moonraker_cfg_dirs <<< "$(get_config_folders)" + + if (( ${#moonraker_cfg_dirs[@]} > 0 )); then echo "${moonraker_cfg_dirs[${1}]}/moonraker-obico.cfg" else echo "" @@ -55,22 +58,27 @@ function obico_server_url_prompt() { blank_line echo -e "| For self-hosted server, specify: |" printf "|${cyan}%-55s${white}|\n" " http://server_ip:port" - echo -e "| For instance, \`http://192.168.0.5:3334\`. |" + echo -e "| For instance, 'http://192.168.0.5:3334'. |" bottom_border } function moonraker_obico_setup_dialog() { status_msg "Initializing Moonraker-obico installation ..." - local moonraker_count moonraker_names + + local moonraker_count + local moonraker_names + moonraker_count=$(moonraker_systemd | wc -w) + if (( moonraker_count == 0 )); then ### return early if moonraker is not installed local error="Moonraker not installed! Please install Moonraker first!" log_error "Moonraker-obico setup started without Moonraker being installed. Aborting setup." print_error "${error}" && return elif (( moonraker_count > 1 )); then - moonraker_names=($(get_multi_instance_names)) # moonraker_names is valid only in case of multi-instance + # moonraker_names is valid only in case of multi-instance + read -r -a moonraker_names <<< "$(get_multi_instance_names)" fi local moonraker_obico_services @@ -169,7 +177,10 @@ function moonraker_obico_setup_dialog() { clone_moonraker_obico "${MOONRAKER_OBICO_REPO}" ### step 6: call moonrake-obico/install.sh with the correct params - local port=7125 moonraker_cfg_dirs=($(get_config_folders)) + local port=7125 + local moonraker_cfg_dirs + + read -r -a moonraker_cfg_dirs <<< "$(get_config_folders)" if (( moonraker_count == 1 )); then "${MOONRAKER_OBICO_DIR}/install.sh" -C "${moonraker_cfg_dirs[0]}/moonraker.conf" -p "${port}" -H 127.0.0.1 -l "${KLIPPER_LOGS}" -s -L -S "${obico_server_url}" @@ -191,7 +202,7 @@ function moonraker_obico_setup_dialog() { fi elif (( moonraker_count > 1 )); then for (( i=0; i <= moonraker_count; i++ )); do - if moonraker_obico_needs_linking "$(moonraker_obico_config ${i})"; then + if moonraker_obico_needs_linking "$(moonraker_obico_config "${i}")"; then not_linked_instances+=("${i}") fi done @@ -215,7 +226,7 @@ function moonraker_obico_setup_dialog() { blank_line echo -e "| If you don't want to link the printer now, you can |" echo -e "| restart the linking process later by: |" - echo -e "| 1. \`cd ~/kiauh && ./kiauh.sh\` to launch KIAUH. |" + echo -e "| 1. 'cd ~/kiauh && ./kiauh.sh' to launch KIAUH. |" echo -e "| 2. Select ${green}[Install]${white} |" echo -e "| 3. Select ${green}[Link to Obico Server]${white} |" bottom_border @@ -242,7 +253,7 @@ function moonraker_obico_setup_dialog() { for i in "${not_linked_instances[@]}"; do local name="${moonraker_names[i]}" status_msg "Link moonraker-obico-${name} to the Obico Server..." - "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -n "${name}" -c "$(moonraker_obico_config ${i})" + "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -n "${name}" -c "$(moonraker_obico_config "${i}")" done fi # (( moonraker_count == 1 )) fi # (( ${#not_linked_instances[@]} > 0 )) -- 2.39.5 From 22262173b34656569dec42efcf6db8050d1e3af9 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 15 Aug 2022 12:54:05 +0200 Subject: [PATCH 32/35] refactor: revert some previously made changes to KIAUH utility function Signed-off-by: Dominik Willner --- scripts/moonraker-telegram-bot.sh | 2 +- scripts/moonraker.sh | 2 +- scripts/octoprint.sh | 2 +- scripts/utilities.sh | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/moonraker-telegram-bot.sh b/scripts/moonraker-telegram-bot.sh index 9e79ad9..d10c5e1 100644 --- a/scripts/moonraker-telegram-bot.sh +++ b/scripts/moonraker-telegram-bot.sh @@ -38,7 +38,7 @@ function telegram_bot_setup_dialog() { local moonraker_count user_input=() moonraker_names=() moonraker_count=$(echo "${moonraker_services}" | wc -w ) for service in ${moonraker_services}; do - moonraker_names+=( "$(get_instance_name "${service}" moonraker)" ) + moonraker_names+=( "$(get_instance_name "${service}")" ) done local telegram_bot_count diff --git a/scripts/moonraker.sh b/scripts/moonraker.sh index 0e8cfe1..a773dc5 100644 --- a/scripts/moonraker.sh +++ b/scripts/moonraker.sh @@ -70,7 +70,7 @@ function moonraker_setup_dialog() { local klipper_count user_input=() klipper_names=() klipper_count=$(echo "${klipper_services}" | wc -w ) for service in ${klipper_services}; do - klipper_names+=( "$(get_instance_name "${service}" klipper)" ) + klipper_names+=( "$(get_instance_name "${service}")" ) done local moonraker_count diff --git a/scripts/octoprint.sh b/scripts/octoprint.sh index dc3ad01..db118fe 100644 --- a/scripts/octoprint.sh +++ b/scripts/octoprint.sh @@ -35,7 +35,7 @@ function octoprint_setup_dialog() { local klipper_count user_input=() klipper_names=() klipper_count=$(echo "${klipper_services}" | wc -w ) for service in ${klipper_services}; do - klipper_names+=( "$(get_instance_name "${service}" klipper)" ) + klipper_names+=( "$(get_instance_name "${service}")" ) done local octoprint_count diff --git a/scripts/utilities.sh b/scripts/utilities.sh index fe52dd1..2efc20e 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -675,8 +675,8 @@ function set_hostname() { ### input: /etc/systemd/system/klipper-name.service ### returns: name function get_instance_name() { - local instance=${1} base_name=${2} name - name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | rev | sed -E "s/${base_name}-//") + local instance=${1} name + name=$(echo "${instance}" | rev | cut -d"/" -f1 | rev | cut -d"-" -f2 | cut -d"." -f1) echo "${name}" } -- 2.39.5 From 7c4b413c671a3edbbe52b4d679ebd14793de58cf Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 15 Aug 2022 13:07:25 +0200 Subject: [PATCH 33/35] refactor: improve `get_instance_name` function Signed-off-by: Dominik Willner --- scripts/obico.sh | 3 ++- scripts/utilities.sh | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/obico.sh b/scripts/obico.sh index 30b95d9..5ccca70 100644 --- a/scripts/obico.sh +++ b/scripts/obico.sh @@ -103,7 +103,8 @@ function moonraker_obico_setup_dialog() { if (( existing_moonraker_obico_count > 0 )); then printf "|${green}%-55s${white}|\n" " ${existing_moonraker_obico_count} Moonraker-obico instances already installed!" for svc in ${moonraker_obico_services}; do - printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${svc}" moonraker-obico)" +# printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${svc}" moonraker-obico)" + printf "|${cyan}%-57s${white}|\n" " ● moonraker-obco-$(get_instance_name "${svc}")" done fi blank_line diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 2efc20e..2940e90 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -676,7 +676,7 @@ function set_hostname() { ### returns: name function get_instance_name() { local instance=${1} name - name=$(echo "${instance}" | rev | cut -d"/" -f1 | rev | cut -d"-" -f2 | cut -d"." -f1) + name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | cut -d"-" -f1 | rev) echo "${name}" } -- 2.39.5 From e12eba95bb42a00f4453e1efad1134d8283a7b2b Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 15 Aug 2022 13:18:18 +0200 Subject: [PATCH 34/35] style: improve function description Signed-off-by: Dominik Willner --- scripts/utilities.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 2940e90..88d83e3 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -670,13 +670,21 @@ function set_hostname() { #============ 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 -### returns: name +### +# takes in a systemd service files full path and +# returns the sub-string with the instance name +# +# @param {string}: service file absolute path +# (e.g. '/etc/systemd/system/klipper-.service') +# +# => return sub-string containing only the part of the full string +# function get_instance_name() { - local instance=${1} name + local instance=${1} + local name + name=$(echo "${instance}" | rev | cut -d"/" -f1 | cut -d"." -f2 | cut -d"-" -f1 | rev) + echo "${name}" } -- 2.39.5 From f48136c2bb9ba3e2bb25b3639854213b5ac54c8b Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 15 Aug 2022 19:44:16 +0200 Subject: [PATCH 35/35] readme: update README.md and changelog.md Signed-off-by: Dominik Willner --- README.md | 26 ++++++++++++++++++-------- docs/changelog.md | 3 +++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 067ea80..4e24255 100644 --- a/README.md +++ b/README.md @@ -52,37 +52,47 @@ git clone https://github.com/th33xitus/kiauh.git

Klipper

Moonraker

Mainsail

-

Fluidd

Klipper Logo Arksine avatar Mainsail Logo -Fluidd Logo by KevinOConnor by Arksine by mainsail-crew -by fluidd-core +

Fluidd

KlipperScreen

-

Moonraker-Telegram-Bot

-

PrettyGCode for Klipper

OctoPrint

+Fluidd Logo jordanruthe avatar -nlef avatar -Kragrathea avatar OctoPrint Logo +by fluidd-core by jordanruthe +by OctoPrint + +

Moonraker-Telegram-Bot

+

PrettyGCode for Klipper

+

Obico for Klipper

+ + + +nlef avatar +Kragrathea avatar +Obico logo + + + by nlef by Kragrathea -by OctoPrint +by Obico diff --git a/docs/changelog.md b/docs/changelog.md index 50d7ce0..f449f3a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,9 @@ This document covers possible important changes to KIAUH. +### 2022-08-15 +Support for "Obico for Klipper" was added! Huge thanks to [kennethjiang](https://github.com/kennethjiang) for helping me with the implementation! + ### 2022-05-29 KIAUH has now reached major version 4 ! * feat: Klipper can be installed under Python3 (still considered as experimental) -- 2.39.5