From 430cf97e97ff93e84ad51f4e55b79c9e89f537fa Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 27 May 2023 17:22:38 +0100 Subject: [PATCH 1/7] feat: Add Mobileraker_Companion --- scripts/globals.sh | 5 + scripts/mobileraker.sh | 246 +++++++++++++++++++++++++++++++++++++ scripts/ui/install_menu.sh | 5 +- scripts/ui/main_menu.sh | 1 + scripts/ui/remove_menu.sh | 5 +- scripts/ui/update_menu.sh | 12 +- 6 files changed, 269 insertions(+), 5 deletions(-) create mode 100644 scripts/mobileraker.sh diff --git a/scripts/globals.sh b/scripts/globals.sh index 24f2a22..f3faacd 100644 --- a/scripts/globals.sh +++ b/scripts/globals.sh @@ -78,4 +78,9 @@ function set_globals() { CROWSNEST_DIR="${HOME}/crowsnest" CROWSNEST_REPO="https://github.com/mainsail-crew/crowsnest.git" + #=============== Mobileraker ================# + MOBILERAKER_ENV="${HOME}/mobileraker-env" + MOBILERAKER_DIR="${HOME}/mobileraker_companion" + MOBILERAKER_REPO="https://github.com/Clon1998/mobileraker.git" + } diff --git a/scripts/mobileraker.sh b/scripts/mobileraker.sh new file mode 100644 index 0000000..83d7eac --- /dev/null +++ b/scripts/mobileraker.sh @@ -0,0 +1,246 @@ +#!/usr/bin/env bash + +#=======================================================================# +# Copyright (C) 2020 - 2023 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 # +#=======================================================================# + +# +# This file is written and maintained by Patrick Schmidt author of Mobileraker +# It is based of the kliperscreen.sh install script! + + +set -e + +#===================================================# +#========== INSTALL MOBILERAKER COMPANION ==========# +#===================================================# + +function mobileraker_systemd() { + local services + services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/mobileraker.service") + echo "${services}" +} + +function install_mobileraker() { + ### 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 + + ### first, we create a backup of the full klipper_config dir - safety first! + backup_klipper_config_dir + + ### install Mobileraker's Companion + mobileraker_setup + + ### add Mobileraker's Companion to the update manager in moonraker.conf + patch_mobileraker_update_manager + + do_action_service "restart" "mobileraker" +} + +function mobileraker_setup() { + local dep=(wget curl unzip dfu-util) + dependency_check "${dep[@]}" + status_msg "Cloning Mobileraker's companion from ${MOBILERAKER_REPO} ..." + + # force remove existing Mobileraker's companion dir + [[ -d ${MOBILERAKER_DIR} ]] && rm -rf "${MOBILERAKER_DIR}" + + # clone into fresh Mobileraker's companion dir + cd "${HOME}" || exit 1 + if ! git clone "${MOBILERAKER_REPO}" "${MOBILERAKER_DIR}"; then + print_error "Cloning mobileraker's companion from\n ${MOBILERAKER_REPO}\n failed!" + exit 1 + fi + + status_msg "Installing Mobileraker's companion ..." + if "${MOBILERAKER_DIR}"/scripts/install-mobileraker-companion.sh; then + ok_msg "Mobileraker's companion successfully installed!" + else + print_error "Mobileraker's companion installation failed!" + exit 1 + fi +} + +#===================================================# +#=========== REMOVE MOBILERAKER COMPANION ==========# +#===================================================# + +function remove_mobileraker() { + ### remove Mobileraker's companion dir + if [[ -d ${MOBILERAKER_DIR} ]]; then + status_msg "Removing Mobileraker's companion directory ..." + rm -rf "${MOBILERAKER_DIR}" && ok_msg "Directory removed!" + fi + + ### remove Mobileraker's companion VENV dir + if [[ -d ${MOBILERAKER_ENV} ]]; then + status_msg "Removing Mobileraker's companion VENV directory ..." + rm -rf "${MOBILERAKER_ENV}" && ok_msg "Directory removed!" + fi + + ### remove Mobileraker's companion service + if [[ -e "${SYSTEMD}/mobileraker.service" ]]; then + status_msg "Removing mobileraker service ..." + do_action_service "stop" "mobileraker" + do_action_service "disable" "mobileraker" + sudo rm -f "${SYSTEMD}/mobileraker.service" + + ###reloading units + sudo systemctl daemon-reload + sudo systemctl reset-failed + ok_msg "Mobileraker's companion Service removed!" + fi + + + remove_mobileraker_logs + + print_confirm "Mobileraker's companion successfully removed!" +} + +function remove_mobileraker_logs() { + local files regex="${HOME//\//\\/}\/([A-Za-z0-9_]+)\/logs\/mobileraker\.log.*" + files=$(find "${HOME}" -maxdepth 3 -regextype posix-extended -regex "${regex}" | sort) + + if [[ -n ${files} ]]; then + for file in ${files}; do + status_msg "Removing ${file} ..." + rm -f "${file}" + ok_msg "${file} removed!" + done + fi +} + +#===================================================# +#=========== UPDATE MOBILERAKER COMPANION ==========# +#===================================================# + +function update_mobileraker() { + local old_md5 + old_md5=$(md5sum "${MOBILERAKER_DIR}/scripts/mobileraker-requirements.txt" | cut -d " " -f1) + + do_action_service "stop" "mobileraker" + cd "${MOBILERAKER_DIR}" + git pull origin master -q && ok_msg "Fetch successfull!" + git checkout -f master && ok_msg "Checkout successfull" + + if [[ $(md5sum "${MOBILERAKER_DIR}/scripts/mobileraker-requirements.txt" | cut -d " " -f1) != "${old_md5}" ]]; then + status_msg "New dependecies detected..." + "${MOBILERAKER_ENV}"/bin/pip install -r "${MOBILERAKER_DIR}/scripts/mobileraker-requirements.txt" + ok_msg "Dependencies have been installed!" + fi + + ok_msg "Update complete!" + do_action_service "start" "mobileraker" +} + +#===================================================# +#=========== MOBILERAKER COMPANION STATUS ==========# +#===================================================# + +function get_mobileraker_status() { + local sf_count status + sf_count="$(mobileraker_systemd | wc -w)" + + ### remove the "SERVICE" entry from the data array if a moonraker service is installed + local data_arr=(SERVICE "${MOBILERAKER_DIR}" "${MOBILERAKER_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_mobileraker_commit() { + [[ ! -d ${MOBILERAKER_DIR} || ! -d "${MOBILERAKER_DIR}/.git" ]] && return + + local commit + cd "${MOBILERAKER_DIR}" + commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)" + echo "${commit}" +} + +function get_remote_mobileraker_commit() { + [[ ! -d ${MOBILERAKER_DIR} || ! -d "${MOBILERAKER_DIR}/.git" ]] && return + + local commit + cd "${MOBILERAKER_DIR}" && git fetch origin -q + commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) + echo "${commit}" +} + +function compare_mobileraker_versions() { + local versions local_ver remote_ver + local_ver="$(get_local_mobileraker_commit)" + remote_ver="$(get_remote_mobileraker_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 "mobileraker" + else + versions="${green}$(printf " %-14s" "${local_ver}")${white}" + versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" + fi + + echo "${versions}" +} + +#================================================# +#=================== HELPERS ====================# +#================================================# + +function patch_mobileraker_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 mobileraker\]\s*$" "${conf}"; then + ### add new line to conf if it doesn't end with one + [[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}" + + ### add Mobileraker's Companion update manager section to moonraker.conf + status_msg "Adding Mobileraker's Companion to update manager in file:\n ${conf}" + /bin/sh -c "cat >> ${conf}" << MOONRAKER_CONF + +[update_manager mobileraker] +type: git_repo +path: ${HOME}/mobileraker_companion +origin: https://github.com/Clon1998/mobileraker_companion.git +primary_branch:main +managed_services: mobileraker +env: ${HOME}/mobileraker-env/bin/python +requirements: scripts/mobileraker-requirements.txt +install_script: scripts/install-mobileraker-companion.sh +MOONRAKER_CONF + + fi + + patched="true" + done + + if [[ ${patched} == "true" ]]; then + do_action_service "restart" "moonraker" + fi +} diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index 2ebcc00..eb687c5 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -27,9 +27,10 @@ function install_ui() { echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" echo -e "| 4) [Fluidd] | 9) $(obico_install_title) |" echo -e "| | 10) [OctoEverywhere] |" + echo -e "| | 11) [Mobileraker_Companion]|" echo -e "| Touchscreen GUI: | |" echo -e "| 5) [KlipperScreen] | Webcam Streamer: |" - echo -e "| | 11) [Crowsnest] |" + echo -e "| | 12) [Crowsnest] |" back_footer } @@ -68,6 +69,8 @@ function install_menu() { 10) do_action "octoeverywhere_setup_dialog" "install_ui";; 11) + do_action "install_mobileraker" "install_ui";; + 12) do_action "install_crowsnest" "install_ui";; B|b) clear; main_menu; break;; diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index b04a090..a37a621 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -32,6 +32,7 @@ function main_ui() { echo -e "| | Crowsnest: $(print_status "crowsnest")|" echo -e "| | Obico: $(print_status "moonraker_obico")|" echo -e "| | OctoEverywhere: $(print_status "octoeverywhere")|" + echo -e "| | Mobileraker_Companion: $(print_status "mobileraker")|" echo -e "| | |" echo -e "| $(print_kiauh_version)| Octoprint: $(print_status "octoprint")|" quit_footer diff --git a/scripts/ui/remove_menu.sh b/scripts/ui/remove_menu.sh index 0749921..f560601 100755 --- a/scripts/ui/remove_menu.sh +++ b/scripts/ui/remove_menu.sh @@ -29,7 +29,8 @@ function remove_ui() { echo -e "| | 12) [Telegram Bot] |" echo -e "| Touchscreen GUI: | 13) [Obico for Klipper] |" echo -e "| 7) [KlipperScreen] | 14) [OctoEverywhere] |" - echo -e "| | 15) [NGINX] |" + echo -e "| | 15) [Mobileraker_Companion]|" + echo -e "| | 16) [NGINX] |" back_footer } @@ -69,6 +70,8 @@ function remove_menu() { 14) do_action "remove_octoeverywhere" "remove_ui";; 15) + do_action "remove_mobileraker" "remove_ui";; + 16) 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 61e61ee..1c05e73 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -33,9 +33,10 @@ function update_ui() { echo -e "| 7) [Telegram Bot] |$(compare_telegram_bot_versions)|" echo -e "| 8) [Obico for Klipper]|$(compare_moonraker_obico_versions)|" echo -e "| 9) [OctoEverywhere] |$(compare_octoeverywhere_versions)|" - echo -e "| 10) [Crowsnest] |$(compare_crowsnest_versions)|" + echo -e "| 10) [Mobileraker_Companion] |$(compare_mobileraker_versions)|" + echo -e "| 11) [Crowsnest] |$(compare_crowsnest_versions)|" echo -e "| |------------------------------|" - echo -e "| 11) [System] | $(check_system_updates) |" + echo -e "| 12) [System] | $(check_system_updates) |" back_footer } @@ -67,8 +68,10 @@ function update_menu() { 9) do_action "update_octoeverywhere" "update_ui";; 10) - do_action "update_crowsnest" "update_ui";; + do_action "update_mobileraker" "update_ui";; 11) + do_action "update_crowsnest" "update_ui";; + 12) do_action "update_system" "update_ui";; a) do_action "update_all" "update_ui";; @@ -123,6 +126,9 @@ function update_all() { [[ "${update_arr[*]}" =~ "octoeverywhere" ]] && \ echo -e "| ${cyan}● OctoEverywhere${white} |" + [[ "${update_arr[*]}" =~ "mobileraker" ]] && \ + echo -e "| ${cyan}● Mobileraker_Companion${white} |" + [[ "${update_arr[*]}" =~ "system" ]] && \ echo -e "| ${cyan}● System${white} |" -- 2.39.5 From 07d402054fd0c94707c4e4c9408f5739635c1e91 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 27 May 2023 17:29:35 +0100 Subject: [PATCH 2/7] Updated name to only [Mobileraker] --- scripts/ui/install_menu.sh | 2 +- scripts/ui/remove_menu.sh | 2 +- scripts/ui/update_menu.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh index eb687c5..d9d26d0 100755 --- a/scripts/ui/install_menu.sh +++ b/scripts/ui/install_menu.sh @@ -27,7 +27,7 @@ function install_ui() { echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" echo -e "| 4) [Fluidd] | 9) $(obico_install_title) |" echo -e "| | 10) [OctoEverywhere] |" - echo -e "| | 11) [Mobileraker_Companion]|" + echo -e "| | 11) [Mobileraker] |" echo -e "| Touchscreen GUI: | |" echo -e "| 5) [KlipperScreen] | Webcam Streamer: |" echo -e "| | 12) [Crowsnest] |" diff --git a/scripts/ui/remove_menu.sh b/scripts/ui/remove_menu.sh index f560601..b6c5581 100755 --- a/scripts/ui/remove_menu.sh +++ b/scripts/ui/remove_menu.sh @@ -29,7 +29,7 @@ function remove_ui() { echo -e "| | 12) [Telegram Bot] |" echo -e "| Touchscreen GUI: | 13) [Obico for Klipper] |" echo -e "| 7) [KlipperScreen] | 14) [OctoEverywhere] |" - echo -e "| | 15) [Mobileraker_Companion]|" + echo -e "| | 15) [Mobileraker] |" echo -e "| | 16) [NGINX] |" back_footer } diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index 1c05e73..f1d2b08 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -33,7 +33,7 @@ function update_ui() { echo -e "| 7) [Telegram Bot] |$(compare_telegram_bot_versions)|" echo -e "| 8) [Obico for Klipper]|$(compare_moonraker_obico_versions)|" echo -e "| 9) [OctoEverywhere] |$(compare_octoeverywhere_versions)|" - echo -e "| 10) [Mobileraker_Companion] |$(compare_mobileraker_versions)|" + echo -e "| 10) [Mobileraker] |$(compare_mobileraker_versions)|" echo -e "| 11) [Crowsnest] |$(compare_crowsnest_versions)|" echo -e "| |------------------------------|" echo -e "| 12) [System] | $(check_system_updates) |" -- 2.39.5 From 73daee91a75230156af0acba5cc8f821c963a388 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 27 May 2023 17:31:11 +0100 Subject: [PATCH 3/7] Mobileraker_Companion renamed in main and update menu --- scripts/ui/main_menu.sh | 2 +- scripts/ui/update_menu.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh index a37a621..7cf8898 100755 --- a/scripts/ui/main_menu.sh +++ b/scripts/ui/main_menu.sh @@ -32,7 +32,7 @@ function main_ui() { echo -e "| | Crowsnest: $(print_status "crowsnest")|" echo -e "| | Obico: $(print_status "moonraker_obico")|" echo -e "| | OctoEverywhere: $(print_status "octoeverywhere")|" - echo -e "| | Mobileraker_Companion: $(print_status "mobileraker")|" + echo -e "| | Mobileraker: $(print_status "mobileraker")|" echo -e "| | |" echo -e "| $(print_kiauh_version)| Octoprint: $(print_status "octoprint")|" quit_footer diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh index f1d2b08..6700016 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -127,7 +127,7 @@ function update_all() { echo -e "| ${cyan}● OctoEverywhere${white} |" [[ "${update_arr[*]}" =~ "mobileraker" ]] && \ - echo -e "| ${cyan}● Mobileraker_Companion${white} |" + echo -e "| ${cyan}● Mobileraker${white} |" [[ "${update_arr[*]}" =~ "system" ]] && \ echo -e "| ${cyan}● System${white} |" -- 2.39.5 From f33a5b74a1cf3cb54dd4e224da8df51afe4b2e1c Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 27 May 2023 17:32:29 +0100 Subject: [PATCH 4/7] Corrected GitRepo --- scripts/globals.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/globals.sh b/scripts/globals.sh index f3faacd..b80011b 100644 --- a/scripts/globals.sh +++ b/scripts/globals.sh @@ -81,6 +81,6 @@ function set_globals() { #=============== Mobileraker ================# MOBILERAKER_ENV="${HOME}/mobileraker-env" MOBILERAKER_DIR="${HOME}/mobileraker_companion" - MOBILERAKER_REPO="https://github.com/Clon1998/mobileraker.git" + MOBILERAKER_REPO="https://github.com/Clon1998/mobileraker_companion.git" } -- 2.39.5 From c4e0a9d7319859bb5b5a4f5fcf5f2c806754df37 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 27 May 2023 17:43:31 +0100 Subject: [PATCH 5/7] Fixed master -> main --- scripts/mobileraker.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/mobileraker.sh b/scripts/mobileraker.sh index 83d7eac..ebcfc96 100644 --- a/scripts/mobileraker.sh +++ b/scripts/mobileraker.sh @@ -129,8 +129,8 @@ function update_mobileraker() { do_action_service "stop" "mobileraker" cd "${MOBILERAKER_DIR}" - git pull origin master -q && ok_msg "Fetch successfull!" - git checkout -f master && ok_msg "Checkout successfull" + git pull origin main -q && ok_msg "Fetch successfull!" + git checkout -f main && ok_msg "Checkout successfull" if [[ $(md5sum "${MOBILERAKER_DIR}/scripts/mobileraker-requirements.txt" | cut -d " " -f1) != "${old_md5}" ]]; then status_msg "New dependecies detected..." @@ -184,7 +184,7 @@ function get_remote_mobileraker_commit() { local commit cd "${MOBILERAKER_DIR}" && git fetch origin -q - commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2) + commit=$(git describe origin/main --always --tags | cut -d "-" -f 1,2) echo "${commit}" } -- 2.39.5 From ee27ccbf7b0d3d6ddc58d6e089490ad94c4eaa49 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 3 Jun 2023 16:45:57 +0100 Subject: [PATCH 6/7] Added Mobileraker to the Readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d662268..74817f0 100644 --- a/README.md +++ b/README.md @@ -152,23 +152,24 @@ prompt and confirm by hitting ENTER. -

+

Mobileraker's Companion

OctoEverywhere For Klipper

- +OctoEverywhere Logo OctoEverywhere Logo - +by Patrick Schmidt by Quinn Damerell +
-- 2.39.5 From c5dea3f36e72fddbe4e04f2dddd6ec8e9858cda9 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Sat, 17 Jun 2023 19:25:43 +0200 Subject: [PATCH 7/7] fix: make patch_mobileraker_update_manager function work Signed-off-by: Dominik Willner --- scripts/mobileraker.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/mobileraker.sh b/scripts/mobileraker.sh index ebcfc96..45b8055 100644 --- a/scripts/mobileraker.sh +++ b/scripts/mobileraker.sh @@ -211,10 +211,11 @@ function compare_mobileraker_versions() { #================================================# function patch_mobileraker_update_manager() { - local patched="false" - local moonraker_configs - moonraker_configs=$(find "${KLIPPER_CONFIG}" -type f -name "moonraker.conf" | sort) + local patched moonraker_configs regex + regex="${HOME//\//\\/}\/([A-Za-z0-9_]+)\/config\/moonraker\.conf" + moonraker_configs=$(find "${HOME}" -maxdepth 3 -type f -regextype posix-extended -regex "${regex}" | sort) + patched="false" for conf in ${moonraker_configs}; do if ! grep -Eq "^\[update_manager mobileraker\]\s*$" "${conf}"; then ### add new line to conf if it doesn't end with one -- 2.39.5