refactor: full rework of klipper related functions

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-03-19 01:16:04 +01:00
parent cfaa2ddafc
commit 61b5ccde00
3 changed files with 263 additions and 349 deletions

View File

@@ -1,15 +1,22 @@
#Systemd service file for klipper
#Systemd Klipper Service
[Unit]
Description=Starts klipper instance %INST% on startup
Description=Starts Klipper instance %INST% on startup
Documentation=https://www.klipper3d.org/
After=network.target
Wants=udev.target
[Install]
WantedBy=multi-user.target
[Service]
Environment=KLIPPER_CONFIG=%CFG%
Environment=KLIPPER_LOG=%LOG%
Environment=KLIPPER_SOCKET=%UDS%
Environment=KLIPPER_PRINTER=%PRINTER%
Type=simple
User=%USER%
RemainAfterExit=yes
ExecStart=%KL_ENV%/bin/python %KL_DIR%/klippy/klippy.py %P_CFG% -I %P_TMP% -l %KL_LOG% -a %KL_UDS%
ExecStart=%ENV%/bin/python %DIR%/klippy/klippy.py ${KLIPPER_CONFIG} -I ${KLIPPER_PRINTER} -l ${KLIPPER_LOG} -a ${KLIPPER_SOCKET}
Restart=always
RestartSec=10

View File

@@ -1,11 +1,15 @@
#!/usr/bin/env bash
#
# KIAUH - Klipper Installation And Update Helper
# https://github.com/th33xitus/kiauh
#
# Copyright (C) 2020 - 2022 Dominik Willner <th33xitus@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license
#=======================================================================#
# Copyright (C) 2020 - 2022 Dominik Willner <th33xitus@gmail.com> #
# #
# 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
### global variables
SYSTEMD="/etc/systemd/system"
@@ -14,42 +18,44 @@ ETCDEF="/etc/default"
KLIPPY_ENV="${HOME}/klippy-env"
KLIPPER_DIR="${HOME}/klipper"
KLIPPER_REPO="https://github.com/Klipper3d/klipper.git"
KLIPPER_CONFIG="${HOME}/klipper_config"
#=================================================#
#================ INSTALL KLIPPER ================#
#=================================================#
### check for existing klipper service installations
function check_klipper_exists() {
local SERVICE_FILES
local INITD_SF
local SYSTEMD_SF
function klipper_initd() {
local services
services=$(find "${INITD}" -maxdepth 1 -regextype posix-extended -regex "${INITD}/klipper(-[^0])?[0-9]*")
echo "${services}"
}
INITD_SF=$(find "${INITD}" -regextype posix-extended -regex "${INITD}/klipper(-[^0])?[0-9]*")
SYSTEMD_SF=$(find "${SYSTEMD}" -regextype posix-extended -regex "${SYSTEMD}/klipper(-[^0])?[0-9]*.service")
function klipper_systemd() {
local services
services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/klipper(-[^0])?[0-9]*.service")
echo "${services}"
}
if [ -n "${INITD_SF}" ]; then
SERVICE_FILES+="${INITD_SF}"
fi
if [ -n "${SYSTEMD_SF}" ]; then
SERVICE_FILES+=" ${SYSTEMD_SF}"
fi
if [ -n "${SERVICE_FILES}" ]; then
ERROR_MSG="At least one Klipper service is already installed:"
for service in $SERVICE_FILES; do
ERROR_MSG="${ERROR_MSG}\n ➔ ${service}"
done && return
fi
klipper_setup_dialog
function klipper_exists() {
local services
[ -n "$(klipper_initd)" ] && services+="$(klipper_initd) "
[ -n "$(klipper_systemd)" ] && services+="$(klipper_systemd)"
echo "${services}"
}
function klipper_setup_dialog(){
status_msg "Initializing Klipper installation ..."
### initial printer.cfg path check
check_klipper_cfg_path
### return early if klipper already exists
if [ -n "$(klipper_exists)" ]; then
ERROR_MSG="At least one Klipper service is already installed:"
for s in $(klipper_exists); do
ERROR_MSG="${ERROR_MSG}\n ➔ ${s}"
done
export ERROR_MSG
print_error && return
fi
### ask for amount of instances to create
top_border
@@ -57,168 +63,154 @@ function klipper_setup_dialog(){
echo -e "| up. The number of Klipper instances will determine |"
echo -e "| the amount of printers you can run from this machine. |"
blank_line
echo -e "| ${yellow}WARNING: There is no limit on the number of instances${default} |"
echo -e "| ${yellow}you can set up with this script.${default} |"
echo -e "| ${yellow}WARNING: There is no limit on the number of instances${white} |"
echo -e "| ${yellow}you can set up with this script. Setting up too many${white} |"
echo -e "| ${yellow}Klipper instances can potentially crash your system.${white} |"
bottom_border
local count
while [[ ! ($count =~ ^[1-9]+((0)+)?$) ]]; do
read -p "${cyan}###### Number of Klipper instances to set up:${default} " count
if [[ ! ($count =~ ^[1-9]+((0)+)?$) ]]; then
warn_msg "Invalid Input!\n"
while [[ ! (${count} =~ ^[1-9]+((0)+)?$) ]]; do
read -p "${cyan}###### Number of Klipper instances to set up:${white} " count
if [[ ! (${count} =~ ^[1-9]+((0)+)?$) ]]; then
error_msg "Invalid input!\n"
else
echo
read -p "${cyan}###### Install $count instance(s)? (Y/n):${default} " yn
case "$yn" in
read -p "${cyan}###### Install ${count} instance(s)? (Y/n):${white} " yn
case "${yn}" in
Y|y|Yes|yes|"")
echo -e "###### > Yes"
status_msg "Installing $count Klipper instance(s) ..."
klipper_setup "$count"
select_msg "Yes"
status_msg "Installing ${count} Klipper instance(s) ..."
klipper_setup "${count}"
break;;
N|n|No|no)
echo -e "###### > No"
warn_msg "Exiting Klipper setup ...\n"
select_msg "No"
abort_msg "Exiting Klipper setup ...\n"
break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
invalid_option && print_error
;;
esac
fi
done
}
function install_klipper_packages(){
### read PKGLIST from official install script
status_msg "Reading dependencies..."
install_script="${HOME}/klipper/scripts/install-octopi.sh"
#PKGLIST=$(grep "PKGLIST=" $install_script | sed 's/PKGLIST//g; s/[$={}\n"]//g')
PKGLIST=$(grep "PKGLIST=" "$install_script" | sed 's/PKGLIST//g; s/[$"{}=]//g; s/\s\s*/ /g' | tr -d '\n')
### add dbus requirement for DietPi distro
[ -e "/boot/dietpi/.version" ] && PKGLIST+=" dbus"
local packages
local install_script="${HOME}/klipper/scripts/install-octopi.sh"
for pkg in $PKGLIST; do
echo "${cyan}${pkg}${default}"
done
read -r -a PKGLIST <<< "$PKGLIST"
status_msg "Reading dependencies..."
# shellcheck disable=SC2016
packages="$(grep "PKGLIST=" "${install_script}" | cut -d'"' -f2 | sed 's/\${PKGLIST}//g' | tr -d '\n')"
### add dbus requirement for DietPi distro
[ -e "/boot/dietpi/.version" ] && packages+=" dbus"
echo "${cyan}${packages}${white}" | tr '[:space:]' '\n'
read -r -a packages <<< "${packages}"
### Update system package info
status_msg "Running apt-get update..."
status_msg "Updating lists of packages..."
sudo apt-get update --allow-releaseinfo-change
### Install desired packages
status_msg "Installing packages..."
sudo apt-get install --yes "${PKGLIST[@]}"
sudo apt-get install --yes "${packages[@]}"
}
function create_klipper_virtualenv(){
status_msg "Installing python virtual environment..."
# Create virtualenv if it doesn't already exist
[ ! -d "${KLIPPY_ENV}" ] && virtualenv -p python2 "${KLIPPY_ENV}"
# Install/update dependencies
"${KLIPPY_ENV}"/bin/pip install -r "${KLIPPER_DIR}"/scripts/klippy-requirements.txt
}
function klipper_setup(){
INSTANCE_COUNT=$1
local instances=${1}
### checking dependencies
dep=(git)
dependency_check
local dep=(git)
dependency_check "${dep[@]}"
### step 1: clone klipper
status_msg "Downloading Klipper ..."
### force remove existing klipper dir and clone into fresh klipper dir
[ -d "${KLIPPER_DIR}" ] && rm -rf "${KLIPPER_DIR}"
cd "${HOME}" && git clone "${KLIPPER_REPO}"
status_msg "Download complete!"
### step 2: install klipper dependencies and create python virtualenv
status_msg "Installing dependencies ..."
install_klipper_packages
create_klipper_virtualenv
### step 3: create shared gcode_files and logs folder
### step 3: create gcode_files and logs folder
[ ! -d "${HOME}/gcode_files" ] && mkdir -p "${HOME}/gcode_files"
[ ! -d "${HOME}/klipper_logs" ] && mkdir -p "${HOME}/klipper_logs"
### step 4: create klipper instances
create_klipper_service
create_klipper_service "${instances}"
### step 5: enable and start all instances
do_action_service "enable" "klipper"
do_action_service "start" "klipper"
### confirm message
if [[ ${INSTANCE_COUNT} -eq 1 ]]; then
if [[ ${instances} -eq 1 ]]; then
CONFIRM_MSG="Klipper has been set up!"
elif [[ ${INSTANCE_COUNT} -gt 1 ]]; then
CONFIRM_MSG="${INSTANCE_COUNT} Klipper instances have been set up!"
fi && print_msg && clear_msg
elif [[ ${instances} -gt 1 ]]; then
CONFIRM_MSG="${instances} Klipper instances have been set up!"
fi
export CONFIRM_MSG
print_confirm && return
}
function write_klipper_service(){
local i=${1} cfg_dir=${2} cfg=${3} log=${4} printer=${5} uds=${6} service=${7}
local service_template="${SRCDIR}/kiauh/resources/klipper.service"
local cfg_template="${SRCDIR}/kiauh/resources/printer.cfg"
### create a config directory if it doesn't exist
[ ! -d "${cfg_dir}" ] && mkdir -p "${cfg_dir}"
### create a minimal config if there is no printer.cfg
[ ! -f "${cfg}" ] && cp "${cfg_template}" "${cfg}"
### replace all placeholders
if [ ! -f "${service}" ]; then
status_msg "Creating Klipper Service ${i} ..."
sudo cp "${service_template}" "${service}"
[ -z "${i}" ] && sudo sed -i "s|instance %INST% ||" "${service}"
[ -n "${i}" ] && sudo sed -i "s|%INST%|${i}|" "${service}"
sudo sed -i "s|%USER%|${USER}|; s|%ENV%|${KLIPPY_ENV}|; s|%DIR%|${KLIPPER_DIR}|" "${service}"
sudo sed -i "s|%LOG%|${log}|; s|%CFG%|${cfg}|; s|%PRINTER%|${printer}|; s|%UDS%|${uds}|" "${service}"
fi
}
function create_klipper_service(){
### get config directory
source_kiauh_ini
### set up default values
SINGLE_INST=1
CFG_PATH="$klipper_cfg_loc"
KL_ENV=$KLIPPY_ENV
KL_DIR=$KLIPPER_DIR
KL_LOG="${HOME}/klipper_logs/klippy.log"
KL_UDS="/tmp/klippy_uds"
P_TMP="/tmp/printer"
P_CFG="$CFG_PATH/printer.cfg"
P_CFG_SRC="${SRCDIR}/kiauh/resources/printer.cfg"
KL_SERV_SRC="${SRCDIR}/kiauh/resources/klipper.service"
KL_SERV_TARGET="${SYSTEMD}/klipper.service"
write_kl_service(){
[ ! -d "$CFG_PATH" ] && mkdir -p "$CFG_PATH"
### create a minimal config if there is no printer.cfg
[ ! -f "$P_CFG" ] && cp "$P_CFG_SRC" "$P_CFG"
### replace placeholder
if [ ! -f $KL_SERV_TARGET ]; then
status_msg "Creating Klipper Service $i ..."
sudo cp "$KL_SERV_SRC" $KL_SERV_TARGET
sudo sed -i "s|%INST%|$i|" $KL_SERV_TARGET
sudo sed -i "s|%USER%|${USER}|" $KL_SERV_TARGET
sudo sed -i "s|%KL_ENV%|$KL_ENV|" $KL_SERV_TARGET
sudo sed -i "s|%KL_DIR%|$KL_DIR|" $KL_SERV_TARGET
sudo sed -i "s|%KL_LOG%|$KL_LOG|" $KL_SERV_TARGET
sudo sed -i "s|%P_CFG%|$P_CFG|" $KL_SERV_TARGET
sudo sed -i "s|%P_TMP%|$P_TMP|" $KL_SERV_TARGET
sudo sed -i "s|%KL_UDS%|$KL_UDS|" $KL_SERV_TARGET
fi
}
if [[ $SINGLE_INST -eq $INSTANCE_COUNT ]]; then
local instances=${1}
if [ "${instances}" -eq 1 ]; then
local i=""
local cfg_dir="${KLIPPER_CONFIG}"
local cfg="${cfg_dir}/printer.cfg"
local log="${HOME}/klipper_logs/klippy.log"
local printer="/tmp/printer"
local uds="/tmp/klippy_uds"
local service="${SYSTEMD}/klipper.service"
### write single instance service
write_kl_service
### enable instance
sudo systemctl enable klipper.service
write_klipper_service "${i}" "${cfg_dir}" "${cfg}" "${log}" "${printer}" "${uds}" "${service}"
ok_msg "Single Klipper instance created!"
### launching instance
status_msg "Launching Klipper instance ..."
sudo systemctl start klipper
else
i=1
while [[ $i -le $INSTANCE_COUNT ]]; do
### rewrite default variables for multi instance cases
CFG_PATH="$klipper_cfg_loc/printer_$i"
KL_SERV_TARGET="${SYSTEMD}/klipper-$i.service"
P_TMP="/tmp/printer-$i"
P_CFG="$CFG_PATH/printer.cfg"
KL_LOG="${HOME}/klipper_logs/klippy-$i.log"
KL_UDS="/tmp/klippy_uds-$i"
elif [ "${instances}" -gt 1 ]; then
local i=1
while [[ "${i}" -le "${instances}" ]]; do
local cfg_dir="${KLIPPER_CONFIG}/printer_${i}"
local cfg="${cfg_dir}/printer.cfg"
local log="${HOME}/klipper_logs/klippy-${i}.log"
local printer="/tmp/printer-${i}"
local uds="/tmp/klippy_uds-${i}"
local service="${SYSTEMD}/klipper-${i}.service"
### write multi instance service
write_kl_service
### enable instance
sudo systemctl enable klipper-$i.service
ok_msg "Klipper instance #$i created!"
### launching instance
status_msg "Launching Klipper instance #$i ..."
sudo systemctl start klipper-$i
### raise values by 1
write_klipper_service "${i}" "${cfg_dir}" "${cfg}" "${log}" "${printer}" "${uds}" "${service}"
ok_msg "Klipper instance #${i} created!"
i=$((i+1))
done
unset i
done && unset i
else
return 1
fi
}
@@ -226,117 +218,93 @@ function create_klipper_service(){
#================ REMOVE KLIPPER ================#
#================================================#
function remove_klipper_sysvinit() {
[ ! -e "${INITD}/klipper" ] && return
status_msg "Removing Klipper SysVinit service ..."
sudo systemctl stop klipper
sudo update-rc.d -f klipper remove
sudo rm -f "${INITD}/klipper" "${ETCDEF}/klipper"
ok_msg "Klipper SysVinit service removed!"
}
function remove_klipper_systemd() {
[ -z "$(klipper_systemd)" ] && return
status_msg "Removing Klipper Systemd Services ..."
for service in $(klipper_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 "Klipper Service removed!"
}
function remove_klipper_logs() {
local files
files=$(find "${HOME}/klipper_logs" -maxdepth 1 -regextype posix-extended -regex "${HOME}/klipper_logs/klippy(-[^0])?[0-9]*\.log(.*)?")
if [ -n "${files}" ]; then
for file in ${files}; do
status_msg "Removing ${file} ..."
rm -f "${file}"
ok_msg "${file} removed!"
done
fi
}
function remove_klipper_uds() {
local files
files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/klippy_uds(-[^0])?[0-9]*")
if [ -n "${files}" ]; then
for file in ${files}; do
status_msg "Removing ${file} ..."
rm -f "${file}"
ok_msg "${file} removed!"
done
fi
}
function remove_klipper_printer() {
local files
files=$(find /tmp -maxdepth 1 -regextype posix-extended -regex "/tmp/printer(-[^0])?[0-9]*")
if [ -n "${files}" ]; then
for file in ${files}; do
status_msg "Removing ${file} ..."
rm -f "${file}"
ok_msg "${file} removed!"
done
fi
}
function remove_klipper_dir() {
[ ! -d "${KLIPPER_DIR}" ] && return
status_msg "Removing Klipper directory ..."
rm -rf "${KLIPPER_DIR}"
ok_msg "Directory removed!"
}
function remove_klipper_env() {
[ ! -d "${KLIPPY_ENV}" ] && return
status_msg "Removing klippy-env directory ..."
rm -rf "${KLIPPY_ENV}"
ok_msg "Directory removed!"
}
function remove_klipper(){
shopt -s extglob # enable extended globbing
### ask the user if he wants to uninstall moonraker too.
###? currently usefull if the user wants to switch from single-instance to multi-instance
FILE="${SYSTEMD}/moonraker?(-*([0-9])).service"
if ls $FILE 2>/dev/null 1>&2; then
while true; do
unset REM_MR
top_border
echo -e "| Do you want to remove Moonraker afterwards? |"
echo -e "| |"
echo -e "| This is useful in case you want to switch from a |"
echo -e "| single-instance to a multi-instance installation, |"
echo -e "| which makes a re-installation of Moonraker necessary. |"
echo -e "| |"
echo -e "| If for any other reason you only want to uninstall |"
echo -e "| Klipper, please select 'No' and continue. |"
bottom_border
read -p "${cyan}###### Remove Moonraker afterwards? (y/N):${default} " yn
case "$yn" in
Y|y|Yes|yes)
echo -e "###### > Yes"
REM_MR="true"
break;;
N|n|No|no|"")
echo -e "###### > No"
REM_MR="false"
break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
done
fi
remove_klipper_sysvinit
remove_klipper_systemd
remove_klipper_logs
remove_klipper_uds
remove_klipper_printer
remove_klipper_dir
remove_klipper_env
### remove "legacy" klipper SysVinit service
if [ -e "${INITD}/klipper" ]; then
status_msg "Removing Klipper Service ..."
sudo systemctl stop klipper
sudo update-rc.d -f klipper remove
sudo rm -f "${INITD}/klipper"
sudo rm -f "${ETCDEF}/klipper"
ok_msg "Klipper Service removed!"
fi
### remove all klipper services
FILE="${SYSTEMD}/klipper?(-*([0-9])).service"
if ls "${FILE}" 2>/dev/null 1>&2; then
status_msg "Removing Klipper Services ..."
for service in $(ls "${FILE}" | 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 "Klipper Service removed!"
fi
### remove all logfiles
FILE="${HOME}/klipper_logs/klippy?(-*([0-9])).log"
if ls "${FILE}" 2>/dev/null 1>&2; then
for log in $(ls "${FILE}"); do
status_msg "Removing ${log} ..."
rm -f "${log}"
ok_msg "${log} removed!"
done
fi
### remove all UDS
FILE="/tmp/klippy_uds?(-*([0-9]))"
if ls "${FILE}" 2>/dev/null 1>&2; then
for uds in $(ls "${FILE}"); do
status_msg "Removing ${uds} ..."
rm -f "${uds}"
ok_msg "${uds} removed!"
done
fi
### remove all tmp-printer
FILE="/tmp/printer?(-*([0-9]))"
if ls "${FILE}" 2>/dev/null 1>&2; then
for tmp_printer in $(ls "${FILE}"); do
status_msg "Removing ${tmp_printer} ..."
rm -f "${tmp_printer}"
ok_msg "${tmp_printer} removed!"
done
fi
### removing klipper and klippy-env folders
if [ -d "${KLIPPER_DIR}" ]; then
status_msg "Removing Klipper directory ..."
rm -rf "${KLIPPER_DIR}" && ok_msg "Directory removed!"
fi
if [ -d "${KLIPPY_ENV}" ]; then
status_msg "Removing klippy-env directory ..."
rm -rf "${KLIPPY_ENV}" && ok_msg "Directory removed!"
fi
CONFIRM_MSG=" Klipper was successfully removed!" && print_msg && clear_msg
CONFIRM_MSG=" Klipper was successfully removed!" && print_confirm
export CONFIRM_MSG
shopt -u extglob # enable extended globbing
if [ "${REM_MR}" == "true" ]; then
remove_moonraker && unset REM_MR
fi
}
#================================================#
@@ -349,21 +317,14 @@ function update_klipper(){
cd "${HOME}" && git clone "${KLIPPER_REPO}"
else
bb4u "klipper"
read_branch
save_klipper_state
status_msg "Updating ${GET_BRANCH}"
status_msg "Updating Klipper ..."
cd "${KLIPPER_DIR}"
if [ "$DETACHED_HEAD" == "true" ]; then
git checkout "${GET_BRANCH}"
unset DETACHED_HEAD
fi
### pull latest files from github
git pull
### read PKGLIST and install possible new dependencies
install_klipper_packages
### install possible new python dependencies
KLIPPER_REQ_TXT="${KLIPPER_DIR}/scripts/klippy-requirements.txt"
"${KLIPPY_ENV}"/bin/pip install -r "${KLIPPER_REQ_TXT}"
"${KLIPPY_ENV}"/bin/pip install -r "${KLIPPER_DIR}/scripts/klippy-requirements.txt"
fi
update_log_paths "klipper"
ok_msg "Update complete!"
@@ -374,121 +335,67 @@ function update_klipper(){
#================ KLIPPER STATUS ================#
#================================================#
function klipper_status(){
kcount=0
klipper_data=(
SERVICE
"${KLIPPER_DIR}"
"${KLIPPY_ENV_DIR}"
)
### count amount of klipper service files in /etc/systemd/system
SERVICE_FILE_COUNT=$(ls "${SYSTEMD}" | grep -E "^klipper(\-[[:digit:]]+)?\.service$" | wc -l)
### a fix to detect an existing "legacy" klipper init.d installation
if [ -f "${INITD}/klipper" ]; then
SERVICE_FILE_COUNT=1
function get_klipper_status(){
local sf_count status
sf_count="$(klipper_systemd | wc -w)"
### detect an existing "legacy" klipper init.d installation
if [ "$(klipper_systemd | wc -w)" -eq 0 ] \
&& [ "$(klipper_initd | wc -w)" -ge 1 ]; then
sf_count=1
fi
### remove the "SERVICE" entry from the klipper_data array if a klipper service is installed
[ $SERVICE_FILE_COUNT -gt 0 ] && unset klipper_data[0]
local data_arr=(SERVICE "${KLIPPER_DIR}" "${KLIPPY_ENV}")
[ "${sf_count}" -gt 0 ] && unset "data_arr[0]"
### count+1 for each found data-item from array
for kd in "${klipper_data[@]}"
local filecount=0
for data in "${data_arr[@]}"
do
if [ -e "${kd}" ]; then
kcount=$(expr ${kcount} + 1)
fi
[ -e "${data}" ] && filecount=$(("${filecount}" + 1))
done
### display status
if [ "$kcount" == "${#klipper_data[*]}" ]; then
KLIPPER_STATUS="$(printf "${green}Installed: %-5s${default}" ${SERVICE_FILE_COUNT})"
elif [ "$kcount" == 0 ]; then
KLIPPER_STATUS="${red}Not installed!${default} "
if [ "${filecount}" == "${#data_arr[*]}" ]; then
status="$(printf "${green}Installed: %-5s${white}" "${sf_count}")"
elif [ "${filecount}" == 0 ]; then
status="${red}Not installed!${white} "
else
KLIPPER_STATUS="${yellow}Incomplete!${default} "
status="${yellow}Incomplete!${white} "
fi
echo "${status}"
}
### reading the klipper branch the user is currently on
read_branch(){
if [ -d "${KLIPPER_DIR}/.git" ]; then
cd "${KLIPPER_DIR}"
GET_BRANCH="$(git branch | grep "*" | cut -d"*" -f2 | cut -d" " -f2)"
### try to fix a detached HEAD state and read the correct branch from the output you get
if [ "$(echo "${GET_BRANCH}" | grep "HEAD" )" ]; then
DETACHED_HEAD="true"
GET_BRANCH=$(git branch | grep "HEAD" | rev | cut -d" " -f1 | rev | cut -d")" -f1 | cut -d"/" -f2)
### try to identify the branch when the HEAD was detached at a single commit
### will only work if its either master, scurve-shaping or scurve-smoothing branch
if [[ ${GET_BRANCH} =~ [[:alnum:]] ]]; then
if [ "$(git branch -r --contains "${GET_BRANCH}" | grep "master")" ]; then
GET_BRANCH="master"
elif [ "$(git branch -r --contains "${GET_BRANCH}" | grep "scurve-shaping")" ]; then
GET_BRANCH="scurve-shaping"
elif [ "$(git branch -r --contains "${GET_BRANCH}" | grep "scurve-smoothing")" ]; then
GET_BRANCH="scurve-smoothing"
fi
fi
fi
else
GET_BRANCH=""
fi
}
#prints the current klipper branch in the main menu
print_branch(){
read_branch
if [ -n "${GET_BRANCH}" ]; then
PRINT_BRANCH="$(printf "%-16s" "${GET_BRANCH}")"
else
PRINT_BRANCH="${red}--------------${default} "
fi
}
read_local_klipper_commit(){
function get_local_klipper_commit(){
if [ -d "${KLIPPER_DIR}" ] && [ -d "${KLIPPER_DIR}"/.git ]; then
cd "${KLIPPER_DIR}"
LOCAL_COMMIT=$(git describe HEAD --always --tags | cut -d "-" -f 1,2)
commit="$(git describe HEAD --always --tags | cut -d "-" -f 1,2)"
else
LOCAL_COMMIT="${NONE}"
commit="${NONE}"
fi
echo "${commit}"
}
read_remote_klipper_commit(){
read_branch
if [ -n "${GET_BRANCH}" ];then
if [ "${GET_BRANCH}" = "origin/master" ] || [ "${GET_BRANCH}" = "master" ]; then
git fetch origin -q
REMOTE_COMMIT=$(git describe origin/master --always --tags | cut -d "-" -f 1,2)
elif [ "${GET_BRANCH}" = "scurve-shaping" ]; then
git fetch dmbutyugin scurve-shaping -q
REMOTE_COMMIT=$(git describe dmbutyugin/scurve-shaping --always --tags | cut -d "-" -f 1,2)
elif [ "${GET_BRANCH}" = "scurve-smoothing" ]; then
git fetch dmbutyugin scurve-smoothing -q
REMOTE_COMMIT=$(git describe dmbutyugin/scurve-smoothing --always --tags | cut -d "-" -f 1,2)
fi
function get_remote_klipper_commit(){
if [ -d "${KLIPPER_DIR}" ] && [ -d "${KLIPPER_DIR}"/.git ]; then
cd "${KLIPPER_DIR}"
git fetch origin -q
commit=$(git describe origin/master --always --tags | cut -d "-" -f 1,2)
else
REMOTE_COMMIT="${NONE}"
commit="${NONE}"
fi
echo "${commit}"
}
compare_klipper_versions(){
function compare_klipper_versions(){
unset KLIPPER_UPDATE_AVAIL
read_local_klipper_commit && read_remote_klipper_commit
if [ "${LOCAL_COMMIT}" != "${REMOTE_COMMIT}" ]; then
LOCAL_COMMIT="${yellow}$(printf "%-12s" "${LOCAL_COMMIT}")${default}"
REMOTE_COMMIT="${green}$(printf "%-12s" "${REMOTE_COMMIT}")${default}"
if [ "$(get_local_klipper_commit)" != "$(get_remote_klipper_commit)" ]; then
LOCAL_COMMIT="${yellow}$(printf "%-12s" "$(get_local_klipper_commit)")${default}"
REMOTE_COMMIT="${green}$(printf "%-12s" "$(get_remote_klipper_commit)")${default}"
# add klipper to the update all array for the update all function in the updater
KLIPPER_UPDATE_AVAIL="true" && update_arr+=(update_klipper)
else
LOCAL_COMMIT="${green}$(printf "%-12s" "${LOCAL_COMMIT}")${default}"
REMOTE_COMMIT="${green}$(printf "%-12s" "${REMOTE_COMMIT}")${default}"
LOCAL_COMMIT="${green}$(printf "%-12s" "$(get_remote_klipper_commit)")${default}"
REMOTE_COMMIT="${green}$(printf "%-12s" "$(get_remote_klipper_commit)")${default}"
KLIPPER_UPDATE_AVAIL="false"
fi
#if detached head was found, force the user with warn message to update klipper
if [ "${DETACHED_HEAD}" == "true" ]; then
LOCAL_COMMIT="${red}$(printf "%-12s" "Need update!")${default}"
fi
}

View File

@@ -3,9 +3,9 @@ main_ui(){
top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~~~ [ Main Menu ] ~~~~~~~~~~~~~~~") |"
hr
echo -e "| 0) [Upload Log] | Klipper: $KLIPPER_STATUS|"
echo -e "| | Branch: ${cyan}$PRINT_BRANCH${default}|"
echo -e "| 1) [Install] | |"
echo -e "| 0) [Upload Log] | Klipper: $(get_klipper_status)|"
echo -e "| | |"
echo -e "| 1) [Install] |--------------------------------|"
echo -e "| 2) [Update] | Moonraker: $MOONRAKER_STATUS|"
echo -e "| 3) [Remove] | |"
echo -e "| 4) [Advanced] | Mainsail: $MAINSAIL_STATUS|"
@@ -47,7 +47,7 @@ main_menu(){
fi
#check install status
print_kiauh_version
klipper_status
# klipper_status
moonraker_status
dwc2_status
fluidd_status
@@ -55,7 +55,7 @@ main_menu(){
octoprint_status
klipperscreen_status
MoonrakerTelegramBot_status
print_branch
# print_branch
print_msg && clear_msg
main_ui
while true; do
@@ -83,7 +83,7 @@ main_menu(){
6) clear && settings_menu && break;;
Q|q)
echo -e "${green}###### Happy printing! ######${default}"; echo
exit -1;;
exit 0;;
*)
deny_action "main_ui";;
esac