script: simplify service action methods

This commit is contained in:
th33xitus
2021-06-18 18:03:47 +02:00
parent 806c6fd275
commit 40875dfe49
6 changed files with 54 additions and 136 deletions

View File

@@ -82,7 +82,8 @@ change_klipper_cfg_path(){
set_klipper_cfg_path(){ set_klipper_cfg_path(){
### stop services ### stop services
klipper_service "stop" && moonraker_service "stop" do_action_service "stop" "klipper"
do_action_service "stop" "moonraker"
### copy config files to new klipper config folder ### copy config files to new klipper config folder
if [ ! -z "$old_klipper_cfg_loc" ] && [ -d "$old_klipper_cfg_loc" ]; then if [ ! -z "$old_klipper_cfg_loc" ] && [ -d "$old_klipper_cfg_loc" ]; then
@@ -137,77 +138,17 @@ set_klipper_cfg_path(){
sudo systemctl daemon-reload sudo systemctl daemon-reload
### restart services ### restart services
klipper_service "restart" && moonraker_service "restart" do_action_service "restart" "klipper"
do_action_service "restart" "moonraker"
} }
source_kiauh_ini(){ source_kiauh_ini(){
source $INI_FILE source $INI_FILE
} }
klipper_service(){ do_action_service(){
### set a variable for the ok and status messages shopt -s extglob # enable extended globbing
[ "$1" == "start" ] && ACTION1="started" && ACTION2="Starting" SERVICES="$SYSTEMDDIR/$2?(-*([0-9])).service"
[ "$1" == "stop" ] && ACTION1="stopped" && ACTION2="Stopping"
[ "$1" == "restart" ] && ACTION1="restarted" && ACTION2="Restarting"
if ls /etc/systemd/system/klipper-*.service 2>/dev/null 1>&2; then
INSTANCE_COUNT=$(systemctl list-units --full -all -t service --no-legend | grep -E "klipper-[[:digit:]].service" | wc -l)
INSTANCE=1
status_msg "$ACTION2 $INSTANCE_COUNT Klipper Services ..."
while [ $INSTANCE -le $INSTANCE_COUNT ]; do
sudo systemctl $1 klipper-$INSTANCE && ok_msg "Klipper Service #$INSTANCE $ACTION1!"
### instance counter +1
INSTANCE=$(expr $INSTANCE + 1)
done
elif [ "$(systemctl list-units --full -all -t service --no-legend | grep -E "klipper.service")" ]; then
status_msg "$ACTION2 Klipper Service ..."
sudo systemctl $1 klipper && ok_msg "Klipper Service $ACTION1!"
fi
}
moonraker_service(){
### set a variable for the ok and status messages
[ "$1" == "start" ] && ACTION1="started" && ACTION2="Starting"
[ "$1" == "stop" ] && ACTION1="stopped" && ACTION2="Stopping"
[ "$1" == "restart" ] && ACTION1="restarted" && ACTION2="Restarting"
if ls /etc/systemd/system/moonraker-*.service 2>/dev/null 1>&2; then
INSTANCE_COUNT=$(systemctl list-units --full -all -t service --no-legend | grep -E "moonraker-[[:digit:]].service" | wc -l)
INSTANCE=1
status_msg "$ACTION2 $INSTANCE_COUNT Moonraker Services ..."
while [ $INSTANCE -le $INSTANCE_COUNT ]; do
sudo systemctl $1 moonraker-$INSTANCE && ok_msg "Moonraker Service #$INSTANCE $ACTION1!"
### instance counter +1
INSTANCE=$(expr $INSTANCE + 1)
done
elif [ "$(systemctl list-units --full -all -t service --no-legend | grep -E "moonraker.service")" ]; then
status_msg "$ACTION2 Moonraker Service ..."
sudo systemctl $1 moonraker && ok_msg "Moonraker Service $ACTION1!"
fi
}
dwc_service(){
### set a variable for the ok and status messages
[ "$1" == "start" ] && ACTION1="started" && ACTION2="Starting"
[ "$1" == "stop" ] && ACTION1="stopped" && ACTION2="Stopping"
[ "$1" == "restart" ] && ACTION1="restarted" && ACTION2="Restarting"
if ls /etc/systemd/system/dwc-*.service 2>/dev/null 1>&2; then
INSTANCE_COUNT=$(systemctl list-units --full -all -t service --no-legend | grep -E "dwc-[[:digit:]].service" | wc -l)
INSTANCE=1
status_msg "$ACTION2 $INSTANCE_COUNT DWC-for-Klipper-Socket Services ..."
while [ $INSTANCE -le $INSTANCE_COUNT ]; do
sudo systemctl $1 dwc-$INSTANCE && ok_msg "DWC-for-Klipper-Socket Service #$INSTANCE $ACTION1!"
### instance counter +1
INSTANCE=$(expr $INSTANCE + 1)
done
elif [ "$(systemctl list-units --full -all -t service --no-legend | grep -E "dwc.service")" ]; then
status_msg "$ACTION2 DWC-for-Klipper-Socket Service ..."
sudo systemctl $1 dwc && ok_msg "DWC-for-Klipper-Socket Service $ACTION1!"
fi
}
octoprint_service(){
### set a variable for the ok and status messages ### set a variable for the ok and status messages
[ "$1" == "start" ] && ACTION1="started" && ACTION2="Starting" [ "$1" == "start" ] && ACTION1="started" && ACTION2="Starting"
[ "$1" == "stop" ] && ACTION1="stopped" && ACTION2="Stopping" [ "$1" == "stop" ] && ACTION1="stopped" && ACTION2="Stopping"
@@ -215,30 +156,27 @@ octoprint_service(){
[ "$1" == "enable" ] && ACTION1="enabled" && ACTION2="Enabling" [ "$1" == "enable" ] && ACTION1="enabled" && ACTION2="Enabling"
[ "$1" == "disable" ] && ACTION1="disabled" && ACTION2="Disabling" [ "$1" == "disable" ] && ACTION1="disabled" && ACTION2="Disabling"
if ls /etc/systemd/system/octoprint-*.service 2>/dev/null 1>&2; then if ls $SERVICES 2>/dev/null 1>&2; then
INSTANCE=1 for service in $(ls $SERVICES | rev | cut -d"/" -f1 | rev); do
INSTANCE_COUNT=$(systemctl list-unit-files | grep -E "octoprint.*" | wc -l) status_msg "$ACTION2 $service ..."
status_msg "$ACTION2 $INSTANCE_COUNT OctoPrint Services ..." sudo systemctl $1 "$service"
while [ $INSTANCE -le $INSTANCE_COUNT ]; do ok_msg "$service $ACTION1!"
sudo systemctl $1 octoprint-$INSTANCE && ok_msg "OctoPrint Service #$INSTANCE $ACTION1!"
### instance counter +1
INSTANCE=$(expr $INSTANCE + 1)
done done
elif [ "$(systemctl list-unit-files | grep -E "octoprint.*")" ]; then else
status_msg "$ACTION2 OctoPrint Service ..." ERROR_MSG="Service '$2' not found!" && print_msg && clear_msg
sudo systemctl $1 octoprint && ok_msg "OctoPrint Service $ACTION1!"
fi fi
shopt -u extglob # disable extended globbing
} }
toggle_octoprint_service(){ toggle_octoprint_service(){
if systemctl list-unit-files | grep -E "octoprint.*" | grep "enabled" &>/dev/null; then if systemctl list-unit-files | grep -E "octoprint.*" | grep "enabled" &>/dev/null; then
octoprint_service "stop" do_action_service "stop" "octoprint"
octoprint_service "disable" do_action_service "disable" "octoprint"
sleep 2 sleep 2
CONFIRM_MSG=" OctoPrint Service is now >>> DISABLED <<< !" CONFIRM_MSG=" OctoPrint Service is now >>> DISABLED <<< !"
elif systemctl list-unit-files | grep -E "octoprint.*" | grep "disabled" &>/dev/null; then elif systemctl list-unit-files | grep -E "octoprint.*" | grep "disabled" &>/dev/null; then
octoprint_service "enable" do_action_service "enable" "octoprint"
octoprint_service "start" do_action_service "start" "octoprint"
sleep 2 sleep 2
CONFIRM_MSG=" OctoPrint Service is now >>> ENABLED <<< !" CONFIRM_MSG=" OctoPrint Service is now >>> ENABLED <<< !"
else else
@@ -370,7 +308,7 @@ setup_gcode_shell_command(){
} }
install_gcode_shell_command(){ install_gcode_shell_command(){
klipper_service "stop" do_action_service "stop" "klipper"
status_msg "Copy 'gcode_shell_command.py' to '$KLIPPER_DIR/klippy/extras' ..." status_msg "Copy 'gcode_shell_command.py' to '$KLIPPER_DIR/klippy/extras' ..."
cp ${SRCDIR}/kiauh/resources/gcode_shell_command.py $KLIPPER_DIR/klippy/extras cp ${SRCDIR}/kiauh/resources/gcode_shell_command.py $KLIPPER_DIR/klippy/extras
while true; do while true; do
@@ -412,7 +350,7 @@ install_gcode_shell_command(){
esac esac
done done
ok_msg "Shell command extension installed!" ok_msg "Shell command extension installed!"
klipper_service "restart" do_action_service "restart" "klipper"
} }
create_minimal_cfg(){ create_minimal_cfg(){

View File

@@ -286,18 +286,18 @@ select_mcu_id(){
} }
flash_mcu(){ flash_mcu(){
klipper_service "stop" do_action_service "stop" "klipper"
if ! make flash FLASH_DEVICE="${mcu_list[$mcu_index]}" ; then if ! make flash FLASH_DEVICE="${mcu_list[$mcu_index]}" ; then
warn_msg "Flashing failed!" warn_msg "Flashing failed!"
warn_msg "Please read the console output above!" warn_msg "Please read the console output above!"
else else
ok_msg "Flashing successfull!" ok_msg "Flashing successfull!"
fi fi
klipper_service "start" do_action_service "start" "klipper"
} }
flash_mcu_sd(){ flash_mcu_sd(){
klipper_service "stop" do_action_service "stop" "klipper"
### write each supported board to the array to make it selectable ### write each supported board to the array to make it selectable
board_list=() board_list=()
@@ -362,7 +362,7 @@ flash_mcu_sd(){
ok_msg "Flashing successfull!" ok_msg "Flashing successfull!"
fi fi
klipper_service "start" do_action_service "start" "klipper"
} }
build_fw(){ build_fw(){

View File

@@ -163,7 +163,7 @@ install_kiauh_macros(){
ok_msg "$klipper_cfg_loc/kiauh_macros.cfg created!" ok_msg "$klipper_cfg_loc/kiauh_macros.cfg created!"
fi fi
### restart klipper service to parse the modified printer.cfg ### restart klipper service to parse the modified printer.cfg
klipper_service "restart" do_action_service "restart" "klipper"
fi fi
} }

View File

@@ -352,9 +352,9 @@ process_octoprint_dialog(){
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" echo -e "###### > Yes"
status_msg "Stopping OctoPrint ..." status_msg "Stopping OctoPrint ..."
octoprint_service "stop" && ok_msg "OctoPrint service stopped!" do_action_service "stop" "octoprint" && ok_msg "OctoPrint service stopped!"
status_msg "Disabling OctoPrint ..." status_msg "Disabling OctoPrint ..."
octoprint_service "disable" && ok_msg "OctoPrint service disabled!" do_action_service "disable" "octoprint" && ok_msg "OctoPrint service disabled!"
break;; break;;
N|n|No|no) N|n|No|no)
echo -e "###### > No" echo -e "###### > No"

View File

@@ -59,46 +59,26 @@ main_menu(){
while true; do while true; do
read -p "${cyan}Perform action:${default} " action; echo read -p "${cyan}Perform action:${default} " action; echo
case "$action" in case "$action" in
"start klipper") "start klipper") do_action_service "start" "klipper"; main_ui;;
do_action "klipper_service start" "main_ui";; "stop klipper") do_action_service "stop" "klipper"; main_ui;;
"stop klipper") "restart klipper") do_action_service "restart" "klipper"; main_ui;;
do_action "klipper_service stop" "main_ui";; "start moonraker") do_action_service "start" "moonraker"; main_ui;;
"restart klipper") "stop moonraker") do_action_service "stop" "moonraker"; main_ui;;
do_action "klipper_service restart" "main_ui";; "restart moonraker")do_action_service "restart" "moonraker"; main_ui;;
"start moonraker") "start dwc") do_action_service "start" "dwc"; main_ui;;
do_action "moonraker_service start" "main_ui";; "stop dwc") do_action_service "stop" "dwc"; main_ui;;
"stop moonraker") "restart dwc") do_action_service "restart" "dwc"; main_ui;;
do_action "moonraker_service stop" "main_ui";; "start octoprint") do_action_service "start" "octoprint"; main_ui;;
"restart moonraker") "stop octoprint") do_action_service "stop" "octoprint"; main_ui;;
do_action "moonraker_service restart" "main_ui";; "restart octoprint") do_action_service "restart" "octoprint"; main_ui;;
"start dwc") update) do_action "update_kiauh" "main_ui";;
do_action "dwc_service start" "main_ui";; 0) do_action "upload_selection" "main_ui";;
"stop dwc") 1) clear && install_menu && break;;
do_action "dwc_service stop" "main_ui";; 2) clear && update_menu && break;;
"restart dwc") 3) clear && remove_menu && break;;
do_action "dwc_service restart" "main_ui";; 4) clear && advanced_menu && break;;
"start octoprint") 5) clear && backup_menu && break;;
do_action "octoprint_service start" "main_ui";; 6) clear && settings_menu && break;;
"stop octoprint")
do_action "octoprint_service stop" "main_ui";;
"restart octoprint")
do_action "octoprint_service restart" "main_ui";;
update)
do_action "update_kiauh" "main_ui";;
0)
do_action "upload_selection" "main_ui";;
1)
clear && install_menu && break;;
2)
clear && update_menu && break;;
3)
clear && remove_menu && break;;
4)
clear && advanced_menu && break;;
5)
clear && backup_menu && break;;
6)
clear && settings_menu && break;;
Q|q) Q|q)
echo -e "${green}###### Happy printing! ######${default}"; echo echo -e "${green}###### Happy printing! ######${default}"; echo
exit -1;; exit -1;;

View File

@@ -202,7 +202,7 @@ migrate_custompios(){
} }
update_klipper(){ update_klipper(){
klipper_service "stop" do_action_service "stop" "klipper"
if [ ! -d $KLIPPER_DIR ]; then if [ ! -d $KLIPPER_DIR ]; then
cd ${HOME} && git clone $KLIPPER_REPO cd ${HOME} && git clone $KLIPPER_REPO
else else
@@ -240,18 +240,18 @@ update_klipper(){
fi fi
update_log_paths "klipper" update_log_paths "klipper"
ok_msg "Update complete!" ok_msg "Update complete!"
klipper_service "restart" do_action_service "restart" "klipper"
} }
update_dwc2fk(){ update_dwc2fk(){
dwc_service "stop" do_action_service "stop" "dwc"
bb4u "dwc2" bb4u "dwc2"
if [ ! -d $DWC2FK_DIR ]; then if [ ! -d $DWC2FK_DIR ]; then
cd ${HOME} && git clone $DWC2FK_REPO cd ${HOME} && git clone $DWC2FK_REPO
else else
cd $DWC2FK_DIR && git pull cd $DWC2FK_DIR && git pull
fi fi
dwc_service "start" do_action_service "start" "dwc"
} }
update_dwc2(){ update_dwc2(){
@@ -274,7 +274,7 @@ update_fluidd(){
} }
update_moonraker(){ update_moonraker(){
moonraker_service "stop" do_action_service "stop" "moonraker"
bb4u "moonraker" bb4u "moonraker"
status_msg "Updating Moonraker ..." status_msg "Updating Moonraker ..."
cd $MOONRAKER_DIR cd $MOONRAKER_DIR
@@ -301,7 +301,7 @@ update_moonraker(){
fi fi
update_log_paths "moonraker" update_log_paths "moonraker"
ok_msg "Update complete!" ok_msg "Update complete!"
moonraker_service "restart" do_action_service "restart" "moonraker"
} }
update_klipperscreen(){ update_klipperscreen(){