mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-14 11:04:29 +05:00
refactor(klipper): implement blacklisted service names (#304)
This commit is contained in:
@@ -19,8 +19,30 @@ set -e
|
|||||||
#================ INSTALL KLIPPER ================#
|
#================ INSTALL KLIPPER ================#
|
||||||
#=================================================#
|
#=================================================#
|
||||||
|
|
||||||
|
###
|
||||||
|
# this function detects all installed klipper
|
||||||
|
# systemd instances and returns their absolute path
|
||||||
|
function klipper_systemd() {
|
||||||
|
local services
|
||||||
|
local blacklist
|
||||||
|
local ignore
|
||||||
|
local match
|
||||||
|
|
||||||
|
###
|
||||||
|
# any service that uses "klipper" in its own name but isn't a full klipper service must be blacklisted using
|
||||||
|
# this variable, otherwise they will be falsely recognized as klipper instances. E.g. "klipper-mcu.service"
|
||||||
|
# is not a klipper service, but related to klippers linux mcu, which also requires its own service file, hence
|
||||||
|
# it must be blacklisted.
|
||||||
|
blacklist="mcu"
|
||||||
|
|
||||||
|
ignore="${SYSTEMD}/klipper-(${blacklist}).service"
|
||||||
|
match="${SYSTEMD}/klipper(-[0-9a-zA-Z]+)?.service"
|
||||||
|
|
||||||
|
services=$(find "${SYSTEMD}" -maxdepth 1 -regextype awk ! -regex "${ignore}" -regex "${match}" | sort)
|
||||||
|
echo "${services}"
|
||||||
|
}
|
||||||
|
|
||||||
function start_klipper_setup() {
|
function start_klipper_setup() {
|
||||||
local klipper_initd_service
|
|
||||||
local klipper_systemd_services
|
local klipper_systemd_services
|
||||||
local python_version
|
local python_version
|
||||||
local instance_count
|
local instance_count
|
||||||
@@ -28,20 +50,15 @@ function start_klipper_setup() {
|
|||||||
local use_custom_names
|
local use_custom_names
|
||||||
local input
|
local input
|
||||||
local regex
|
local regex
|
||||||
|
local blacklist
|
||||||
local error
|
local error
|
||||||
|
|
||||||
status_msg "Initializing Klipper installation ...\n"
|
status_msg "Initializing Klipper installation ...\n"
|
||||||
|
|
||||||
### return early if klipper already exists
|
### return early if klipper already exists
|
||||||
klipper_initd_service=$(find_klipper_initd)
|
klipper_systemd_services=$(klipper_systemd)
|
||||||
klipper_systemd_services=$(find_klipper_systemd)
|
|
||||||
|
|
||||||
if [[ -n ${klipper_initd_service} ]]; then
|
if [[ -n ${klipper_systemd_services} ]]; then
|
||||||
error="Unsupported Klipper SysVinit service detected:"
|
|
||||||
error="${error}\n ➔ ${klipper_initd_service}"
|
|
||||||
error="${error}\n Please re-install Klipper with KIAUH!"
|
|
||||||
log_info "Unsupported Klipper SysVinit service detected: ${klipper_initd_service}"
|
|
||||||
elif [[ -n ${klipper_systemd_services} ]]; then
|
|
||||||
error="At least one Klipper service is already installed:"
|
error="At least one Klipper service is already installed:"
|
||||||
|
|
||||||
for s in ${klipper_systemd_services}; do
|
for s in ${klipper_systemd_services}; do
|
||||||
@@ -113,15 +130,19 @@ function start_klipper_setup() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
### user selection for setting the actual custom names
|
### user selection for setting the actual custom names
|
||||||
|
shopt -s nocasematch
|
||||||
if (( instance_count > 1 )) && [[ ${use_custom_names} == "true" ]]; then
|
if (( instance_count > 1 )) && [[ ${use_custom_names} == "true" ]]; then
|
||||||
local i
|
local i
|
||||||
|
|
||||||
i=1
|
i=1
|
||||||
regex="^[0-9a-zA-Z]+$"
|
regex="^[0-9a-zA-Z]+$"
|
||||||
while [[ ! ${input} =~ ${regex} || ${i} -le ${instance_count} ]]; do
|
blacklist="mcu"
|
||||||
|
while [[ ! ${input} =~ ${regex} || ${input} =~ ${blacklist} || ${i} -le ${instance_count} ]]; do
|
||||||
read -p "${cyan}###### Name for instance #${i}:${white} " input
|
read -p "${cyan}###### Name for instance #${i}:${white} " input
|
||||||
|
|
||||||
if [[ ${input} =~ ${regex} ]]; then
|
if [[ ${input} =~ ${blacklist} ]]; then
|
||||||
|
error_msg "Name not allowed! You are trying to use a reserved name."
|
||||||
|
elif [[ ${input} =~ ${regex} && ! ${input} =~ ${blacklist} ]]; then
|
||||||
select_msg "Name: ${input}\n"
|
select_msg "Name: ${input}\n"
|
||||||
if [[ ${input} =~ ^[0-9]+$ ]]; then
|
if [[ ${input} =~ ^[0-9]+$ ]]; then
|
||||||
instance_names+=("printer_${input}")
|
instance_names+=("printer_${input}")
|
||||||
@@ -138,6 +159,7 @@ function start_klipper_setup() {
|
|||||||
instance_names+=("printer_${i}")
|
instance_names+=("printer_${i}")
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
shopt -u nocasematch
|
||||||
|
|
||||||
(( instance_count > 1 )) && status_msg "Installing ${instance_count} Klipper instances ..."
|
(( instance_count > 1 )) && status_msg "Installing ${instance_count} Klipper instances ..."
|
||||||
(( instance_count == 1 )) && status_msg "Installing single Klipper instance ..."
|
(( instance_count == 1 )) && status_msg "Installing single Klipper instance ..."
|
||||||
@@ -386,18 +408,11 @@ function write_example_printer_cfg() {
|
|||||||
#================================================#
|
#================================================#
|
||||||
|
|
||||||
function remove_klipper_service() {
|
function remove_klipper_service() {
|
||||||
if [[ ! -e "${INITD}/klipper" ]] && [[ -z $(find_klipper_systemd) ]]; then
|
[[ -z $(klipper_systemd) ]] && return
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
status_msg "Removing Klipper services ..."
|
status_msg "Removing Klipper services ..."
|
||||||
|
|
||||||
if [[ -e "${INITD}/klipper" ]]; then
|
for service in $(klipper_systemd | cut -d"/" -f5); do
|
||||||
sudo systemctl stop klipper
|
|
||||||
sudo update-rc.d -f klipper remove
|
|
||||||
sudo rm -f "${INITD}/klipper" "${ETCDEF}/klipper"
|
|
||||||
else
|
|
||||||
for service in $(find_klipper_systemd | cut -d"/" -f5); do
|
|
||||||
status_msg "Removing ${service} ..."
|
status_msg "Removing ${service} ..."
|
||||||
sudo systemctl stop "${service}"
|
sudo systemctl stop "${service}"
|
||||||
sudo systemctl disable "${service}"
|
sudo systemctl disable "${service}"
|
||||||
@@ -405,7 +420,6 @@ function remove_klipper_service() {
|
|||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl reset-failed
|
sudo systemctl reset-failed
|
||||||
done
|
done
|
||||||
fi
|
|
||||||
|
|
||||||
ok_msg "All Klipper services removed!"
|
ok_msg "All Klipper services removed!"
|
||||||
}
|
}
|
||||||
@@ -534,13 +548,7 @@ function update_klipper() {
|
|||||||
|
|
||||||
function get_klipper_status() {
|
function get_klipper_status() {
|
||||||
local sf_count status py_ver
|
local sf_count status py_ver
|
||||||
sf_count="$(find_klipper_systemd | wc -w)"
|
sf_count="$(klipper_systemd | wc -w)"
|
||||||
|
|
||||||
### detect an existing "legacy" klipper init.d installation
|
|
||||||
if [[ $(find_klipper_systemd | wc -w) -eq 0 ]] \
|
|
||||||
&& [[ $(find_klipper_initd | wc -w) -ge 1 ]]; then
|
|
||||||
sf_count=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
py_ver=$(get_klipper_python_ver)
|
py_ver=$(get_klipper_python_ver)
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ function moonraker_setup_dialog() {
|
|||||||
|
|
||||||
### return early if klipper is not installed
|
### return early if klipper is not installed
|
||||||
local klipper_services
|
local klipper_services
|
||||||
klipper_services=$(find_klipper_systemd)
|
klipper_services=$(klipper_systemd)
|
||||||
if [[ -z ${klipper_services} ]]; then
|
if [[ -z ${klipper_services} ]]; then
|
||||||
local error="Klipper not installed! Please install Klipper first!"
|
local error="Klipper not installed! Please install Klipper first!"
|
||||||
log_error "Moonraker setup started without Klipper being installed. Aborting setup."
|
log_error "Moonraker setup started without Klipper being installed. Aborting setup."
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ function octoprint_setup_dialog() {
|
|||||||
status_msg "Initializing OctoPrint installation ..."
|
status_msg "Initializing OctoPrint installation ..."
|
||||||
|
|
||||||
local klipper_services
|
local klipper_services
|
||||||
klipper_services=$(find_klipper_systemd)
|
klipper_services=$(klipper_systemd)
|
||||||
if [[ -z ${klipper_services} ]]; then
|
if [[ -z ${klipper_services} ]]; then
|
||||||
local error="Klipper not installed! Please install Klipper first!"
|
local error="Klipper not installed! Please install Klipper first!"
|
||||||
log_error "OctoPrint setup started without Klipper being installed. Aborting setup."
|
log_error "OctoPrint setup started without Klipper being installed. Aborting setup."
|
||||||
|
|||||||
@@ -338,18 +338,6 @@ function fetch_webui_ports() {
|
|||||||
#=================== SYSTEM =====================#
|
#=================== SYSTEM =====================#
|
||||||
#================================================#
|
#================================================#
|
||||||
|
|
||||||
function find_klipper_initd() {
|
|
||||||
local services
|
|
||||||
services=$(find "${INITD}" -maxdepth 1 -regextype posix-extended -regex "${INITD}/klipper(-[^0])?[0-9]*" | sort)
|
|
||||||
echo "${services}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function find_klipper_systemd() {
|
|
||||||
local services
|
|
||||||
services=$(find "${SYSTEMD}" -maxdepth 1 -regextype posix-extended -regex "${SYSTEMD}/klipper(-[0-9a-zA-Z]+)?.service" | sort)
|
|
||||||
echo "${services}"
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_required_folders() {
|
function create_required_folders() {
|
||||||
local printer_data=${1} folders
|
local printer_data=${1} folders
|
||||||
folders=("backup" "certs" "config" "database" "gcodes" "comms" "logs" "systemd")
|
folders=("backup" "certs" "config" "database" "gcodes" "comms" "logs" "systemd")
|
||||||
@@ -599,7 +587,7 @@ function set_multi_instance_names() {
|
|||||||
local names=""
|
local names=""
|
||||||
local services
|
local services
|
||||||
|
|
||||||
services=$(find_klipper_systemd)
|
services=$(klipper_systemd)
|
||||||
|
|
||||||
###
|
###
|
||||||
# if value of 'multi_instance_names' is not an empty
|
# if value of 'multi_instance_names' is not an empty
|
||||||
@@ -664,7 +652,7 @@ function get_config_folders() {
|
|||||||
cfg_dirs+=("${HOME}/${name}_data/config")
|
cfg_dirs+=("${HOME}/${name}_data/config")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
elif [[ -z ${instance_names} && $(find_klipper_systemd | wc -w) -gt 0 ]]; then
|
elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) -gt 0 ]]; then
|
||||||
cfg_dirs+=("${HOME}/printer_data/config")
|
cfg_dirs+=("${HOME}/printer_data/config")
|
||||||
else
|
else
|
||||||
cfg_dirs=()
|
cfg_dirs=()
|
||||||
@@ -707,7 +695,7 @@ function get_instance_folder_path() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
elif [[ -z ${instance_names} && $(find_klipper_systemd | wc -w) -gt 0 ]]; then
|
elif [[ -z ${instance_names} && $(klipper_systemd | wc -w) -gt 0 ]]; then
|
||||||
path="${HOME}/printer_data/${folder_name}"
|
path="${HOME}/printer_data/${folder_name}"
|
||||||
if [[ -d ${path} ]]; then
|
if [[ -d ${path} ]]; then
|
||||||
folder_paths+=("${path}")
|
folder_paths+=("${path}")
|
||||||
|
|||||||
Reference in New Issue
Block a user