Merge pull request #1 from TheSpaghettiDetective/patch-th33xitus-moonraker_obico

refactor: finalize obico integration
This commit is contained in:
Kenneth Jiang
2022-08-13 10:50:14 -07:00
committed by GitHub
5 changed files with 142 additions and 60 deletions

View File

@@ -15,9 +15,24 @@ set -e
#================ INSTALL MOONRAKER ================# #================ INSTALL MOONRAKER ================#
#===================================================# #===================================================#
###
# this function detects all installed moonraker
# systemd instances and returns their absolute path
function moonraker_systemd() { function moonraker_systemd() {
local services 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}" echo "${services}"
} }

View File

@@ -38,7 +38,7 @@ function cfg_dir() {
function is_moonraker_obico_linked() { function is_moonraker_obico_linked() {
local name=${1} 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' grep -s -E "^[^#]" "${moonraker_obico_cfg}" | grep -q 'auth_token'
return $? return $?
} }
@@ -85,7 +85,9 @@ function moonraker_obico_setup_dialog() {
print_error "${error}" && return print_error "${error}" && return
fi 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) moonraker_obico_services=$(moonraker_obico_systemd)
existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w ) existing_moonraker_obico_count=$(echo "${moonraker_obico_services}" | wc -w )
for service in ${moonraker_obico_services}; do for service in ${moonraker_obico_services}; do
@@ -102,13 +104,13 @@ function moonraker_obico_setup_dialog() {
top_border top_border
printf "|${green}%-55s${white}|\n" " ${moonraker_count} Moonraker instances found!" printf "|${green}%-55s${white}|\n" " ${moonraker_count} Moonraker instances found!"
for name in "${moonraker_names[@]}"; do for name in "${moonraker_names[@]}"; do
printf "|${cyan}%-57s${white}|\n" " ●moonraker-${name}" printf "|${cyan}%-57s${white}|\n" " moonraker-${name}"
done done
blank_line blank_line
if (( existing_moonraker_obico_count > 0 )); then if (( existing_moonraker_obico_count > 0 )); then
printf "|${green}%-55s${white}|\n" " ${existing_moonraker_obico_count} Moonraker-obico instances already installed!" printf "|${green}%-55s${white}|\n" " ${existing_moonraker_obico_count} Moonraker-obico instances already installed!"
for name in "${moonraker_obico_names[@]}"; do for name in "${moonraker_obico_names[@]}"; do
printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" printf "|${cyan}%-57s${white}|\n" " moonraker-obico-${name}"
done done
fi fi
blank_line blank_line
@@ -179,20 +181,20 @@ function moonraker_obico_setup_dialog() {
dependency_check "${dep[@]}" dependency_check "${dep[@]}"
### Step 5: Clone the moonraker-obico repo ### 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 ### step 6: call moonrake-obico/install.sh with the correct params
local port=7125 moonraker_cfg local port=7125 moonraker_cfg
if (( moonraker_count == 1 )); then if (( moonraker_count == 1 )); then
moonraker_cfg="$(cfg_dir '')/moonraker.conf" 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 elif (( moonraker_count > 1 )); then
local j=${existing_moonraker_obico_count} local j=${existing_moonraker_obico_count}
for (( i=1; i <= new_moonraker_obico_count; i++ )); do for (( i=1; i <= new_moonraker_obico_count; i++ )); do
local name=${moonraker_names[${j}]} 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}" "${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 )) j=$(( j + 1 ))
@@ -201,10 +203,11 @@ function moonraker_obico_setup_dialog() {
fi fi
### Step 7: Link to the Obico server if necessary ### Step 7: Link to the Obico server if necessary
local instance_name
local not_linked_instances=() local not_linked_instances=()
# Refetch systemd service again since additional services may have been newly installed # Refetch systemd service again since additional services may have been newly installed
for service in $(moonraker_obico_systemd); do 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 if ! is_moonraker_obico_linked "${instance_name}"; then
not_linked_instances+=( "${instance_name}" ) not_linked_instances+=( "${instance_name}" )
fi fi
@@ -216,7 +219,7 @@ function moonraker_obico_setup_dialog() {
else else
printf "|${green}%-55s${white}|\n" " ${#not_linked_instances[@]} Moonraker-obico instances not linked to the server!" printf "|${green}%-55s${white}|\n" " ${#not_linked_instances[@]} Moonraker-obico instances not linked to the server!"
for name in "${not_linked_instances[@]}"; do for name in "${not_linked_instances[@]}"; do
printf "|${cyan}%-57s${white}|\n" " ●moonraker-obico-${name}" printf "|${cyan}%-57s${white}|\n" " moonraker-obico-${name}"
done done
fi fi
blank_line blank_line
@@ -250,7 +253,7 @@ function moonraker_obico_setup_dialog() {
for name in "${not_linked_instances[@]}"; do for name in "${not_linked_instances[@]}"; do
status_msg "Link moonraker-obico-${name} to the Obico Server..." 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 if (( moonraker_count == 1 )); then
"${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "${moonraker_obico_cfg}" "${MOONRAKER_OBICO_DIR}/scripts/link.sh" -q -c "${moonraker_obico_cfg}"
else else
@@ -260,24 +263,22 @@ function moonraker_obico_setup_dialog() {
} }
function clone_or_update_moonraker_obico() { function clone_moonraker_obico() {
local repo=${1} local repo=${1}
if [[ -d ${MOONRAKER_OBICO_DIR} ]]; then status_msg "Cloning Moonraker-obico from ${repo} ..."
status_msg "Updating ${MOONRAKER_OBICO_DIR} ..." ### force remove existing Moonraker-obico dir
cd ${MOONRAKER_OBICO_DIR} && git pull [[ -d ${repo} ]] && rm -rf "${MOONRAKER_OBICO_DIR}"
else
status_msg "Cloning Moonraker-obico from ${repo} ..." cd "${HOME}" || exit 1
cd "${HOME}" || exit 1 if ! git clone "${repo}" "${MOONRAKER_OBICO_DIR}"; then
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
exit 1
fi
fi fi
} }
function moonraker_obico_install() { function moonraker_obico_install() {
"${MOONRAKER_OBICO_DIR}/install.sh" $@ "${MOONRAKER_OBICO_DIR}/install.sh" "$@"
} }
#===================================================# #===================================================#
@@ -303,7 +304,7 @@ function remove_moonraker_obico_systemd() {
} }
function remove_moonraker_obico_logs() { 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) files=$(find "${KLIPPER_LOGS}" -maxdepth 1 -regextype posix-extended -regex "${KLIPPER_LOGS}/${regex}" 2> /dev/null | sort)
if [[ -n ${files} ]]; then if [[ -n ${files} ]]; then
@@ -346,16 +347,17 @@ function remove_moonraker_obico() {
#===================================================# #===================================================#
function update_moonraker_obico() { function update_moonraker_obico() {
for service in $(moonraker_obico_systemd | cut -d"/" -f5); do do_action_service "stop" "moonraker-obico"
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
fi
clone_or_update_moonraker_obico "${MOONRAKER_OBICO_REPO}"
ok_msg "Update complete!" ok_msg "Update complete!"
do_action_service "restart" "moonraker-obico"
for service in $(moonraker_obico_systemd | cut -d"/" -f5); do
do_action_service "restart" "${service}"
done
} }
#===================================================# #===================================================#
@@ -363,23 +365,33 @@ function update_moonraker_obico() {
#===================================================# #===================================================#
function get_moonraker_obico_status() { function get_moonraker_obico_status() {
local moonraker_obico_services sf_count status local status
moonraker_obico_services=$(moonraker_obico_systemd) local service_count
sf_count=$(echo "${moonraker_obico_services}" | wc -w ) local is_linked
local moonraker_obico_services
if (( sf_count == 0 )); then moonraker_obico_services=$(moonraker_obico_systemd)
status="Not installed!" service_count=$(echo "${moonraker_obico_services}" | wc -w )
elif [[ ! -e "${MOONRAKER_OBICO_DIR}" ]]; then
status="Incomplete!" is_linked="true"
else if [[ -n ${moonraker_obico_services} ]]; then
status="Installed!"
for service in ${moonraker_obico_services}; do 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!" is_linked="false"
fi fi
done done
fi 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}" echo "${status}"
} }
@@ -418,3 +430,18 @@ function compare_moonraker_obico_versions() {
echo "${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
}

View File

@@ -12,11 +12,6 @@
set -e set -e
function install_ui() { 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 top_border
echo -e "| ${green}~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~${white} |" echo -e "| ${green}~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~${white} |"
hr hr
@@ -24,16 +19,16 @@ function install_ui() {
echo -e "| all necessary dependencies for the various |" echo -e "| all necessary dependencies for the various |"
echo -e "| functions on a completely fresh system. |" echo -e "| functions on a completely fresh system. |"
hr hr
echo -e "| Firmware & API: | 3rd Party Webinterface: |" echo -e "| Firmware & API: | 3rd Party Webinterface: |"
echo -e "| 1) [Klipper] | 6) [OctoPrint] |" echo -e "| 1) [Klipper] | 6) [OctoPrint] |"
echo -e "| 2) [Moonraker] | |" echo -e "| 2) [Moonraker] | |"
echo -e "| | Other: |" echo -e "| | Other: |"
echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |" echo -e "| Klipper Webinterface: | 7) [PrettyGCode] |"
echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |" echo -e "| 3) [Mainsail] | 8) [Telegram Bot] |"
echo -e "| 4) [Fluidd] | 9) ${moonraker_obico_menu}|" echo -e "| 4) [Fluidd] | 9) $(obico_install_title) |"
echo -e "| | |" echo -e "| | |"
echo -e "| Touchscreen GUI: | Webcam Streamer: |" echo -e "| Touchscreen GUI: | Webcam Streamer: |"
echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |" echo -e "| 5) [KlipperScreen] | 10) [MJPG-Streamer] |"
back_footer back_footer
} }

View File

@@ -49,7 +49,10 @@ function print_status() {
if [[ ${status} == "Not installed!" ]]; then if [[ ${status} == "Not installed!" ]]; then
status="${red}${status}${white}" 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}" status="${yellow}${status}${white}"
else else
status="${green}${status}${white}" status="${green}${status}${white}"

View File

@@ -674,6 +674,10 @@ function set_hostname() {
ok_msg "Remember to reboot for the changes to take effect!" 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 ### this function takes in the full path of a systemd service file and returns
### either the instance index or the custom name ### either the instance index or the custom name
### input: /etc/systemd/system/klipper-name.service ### input: /etc/systemd/system/klipper-name.service
@@ -708,8 +712,7 @@ function get_klipper_instance_name() {
} }
### ###
# combines and saves each instance name/identifier # save all instance names in a comma separated format to the kiauh.ini
# to the kiauh.ini file in a comma separated format
# #
function add_to_multi_instance_names() { function add_to_multi_instance_names() {
read_kiauh_ini "${FUNCNAME[0]}" read_kiauh_ini "${FUNCNAME[0]}"
@@ -734,3 +737,42 @@ function fetch_multi_instance_names() {
add_to_multi_instance_names "${name}" add_to_multi_instance_names "${name}"
done 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[@]}"
}