Streamline the Obico linking process in KIAUH

This commit is contained in:
Kenneth Jiang
2022-08-01 06:46:06 -07:00
parent 121dde5a95
commit 767f8e14f8
3 changed files with 253 additions and 180 deletions

View File

@@ -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}"
}

View File

@@ -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] |"

View File

@@ -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}"