From 98866caefa989eb3e1ecbbf8cd439e9a43f39b01 Mon Sep 17 00:00:00 2001 From: Patrick Schmidt Date: Sat, 17 Jun 2023 19:34:26 +0200 Subject: [PATCH] feat: add Mobileraker (#343) Co-authored-by: th33xitus --- README.md | 7 +- scripts/globals.sh | 5 + scripts/mobileraker.sh | 247 +++++++++++++++++++++++++++++++++++++ scripts/ui/install_menu.sh | 5 +- scripts/ui/main_menu.sh | 1 + scripts/ui/remove_menu.sh | 5 +- scripts/ui/update_menu.sh | 14 ++- 7 files changed, 275 insertions(+), 9 deletions(-) create mode 100644 scripts/mobileraker.sh diff --git a/README.md b/README.md index 2cabb75..9a68ccb 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 +
diff --git a/scripts/globals.sh b/scripts/globals.sh index 24f2a22..b80011b 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_companion.git" + } diff --git a/scripts/mobileraker.sh b/scripts/mobileraker.sh new file mode 100644 index 0000000..45b8055 --- /dev/null +++ b/scripts/mobileraker.sh @@ -0,0 +1,247 @@ +#!/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 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..." + "${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/main --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 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 + [[ $(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 842c367..35cfe92 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] |" echo -e "| Touchscreen GUI: | |" echo -e "| 5) [KlipperScreen] | Webcam Streamer: |" - echo -e "| | 11) [Crowsnest] |" + echo -e "| | 12) [Crowsnest] |" back_footer } @@ -69,6 +70,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..7cf8898 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: $(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..b6c5581 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] |" + 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 169d9b3..caa5e64 100755 --- a/scripts/ui/update_menu.sh +++ b/scripts/ui/update_menu.sh @@ -11,7 +11,7 @@ set -e -function update_ui() { +function update_ui() { top_border echo -e "| ${green}~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~${white} |" hr @@ -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] |$(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 } @@ -68,8 +69,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 "upgrade_system_packages" "update_ui";; a) do_action "update_all" "update_ui";; @@ -124,6 +127,9 @@ function update_all() { [[ "${update_arr[*]}" =~ "octoeverywhere" ]] && \ echo -e "| ${cyan}● OctoEverywhere${white} |" + [[ "${update_arr[*]}" =~ "mobileraker" ]] && \ + echo -e "| ${cyan}● Mobileraker${white} |" + [[ "${update_arr[*]}" =~ "system" ]] && \ echo -e "| ${cyan}● System${white} |"