diff --git a/docs/Shell Command Extension.md b/docs/Shell Command Extension.md new file mode 100644 index 0000000..b3d7bbd --- /dev/null +++ b/docs/Shell Command Extension.md @@ -0,0 +1,46 @@ +# Shell Command Extension + +### Creator of this extension is [Arksine](https://github.com/Arksine). + +This is a brief explanation of how to use the shell command extension for Klipper, which you can install with KIAUH. + +After installing the extension you can execute linux commands or even scripts from within Klipper with custom commands defined in your printer.cfg. + +#### How to configure a shell command: + +```shell +# Runs a linux command or script from within klipper. Note that sudo commands +# that require password authentication are disallowed. All executable scripts +# should include a shebang. +# [shell_command my_shell_cmd] +#command: +# The linux shell command/script to be executed. This parameter must be +# provided +#timeout: 2. +# The timeout in seconds until the command is forcably terminated. Default +# is 2 seconds. +#verbose: True +# If enabled, the command's output will be forwarded to the terminal. Its +# recommended to set this to false for commands that my run in quick +# succession. Default is True. +``` + +Once you have set up a shell command with the given parameters from above in your printer.cfg you can run the command as follows: +`RUN_SHELL_COMMAND CMD=name` + +Example: + +``` +[shell_command hello_world] +command: echo hello world +timeout: 2. +verbose: True +``` + +Execute with: +`RUN_SHELL_COMMAND CMD=hello_world` + +## Warning + +This extension may have a high potential for abuse if not used carefully! Also, depending on the command you execute, high system loads may occur and can cause system instabilities. +Use this extension at your own risk and only if you know what you are doing! diff --git a/kiauh.sh b/kiauh.sh index f4465c5..200b427 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -4,11 +4,47 @@ set -e ### set some variables ERROR_MSG="" -green="\e[92m" -yellow="\e[93m" -red="\e[91m" -cyan="\e[96m" -default="\e[39m" +green=$(echo -en "\001\033[01;32m\002") +yellow=$(echo -en "\001\033[01;33m\002") +red=$(echo -en "\001\033[01;31m\002") +cyan=$(echo -en "\001\033[01;36m\002") +default=$(echo -en "\001\033[0m\002") + +### set important directories +#klipper +KLIPPER_DIR=${HOME}/klipper +KLIPPY_ENV_DIR=${HOME}/klippy-env +KLIPPER_SERVICE1=/etc/init.d/klipper +KLIPPER_SERVICE2=/etc/default/klipper +#dwc2 +DWC2FK_DIR=${HOME}/dwc2-for-klipper +DWC2_DIR=${HOME}/sdcard/dwc2 +WEB_DWC2=${HOME}/klipper/klippy/extras/web_dwc2.py +#mainsail/moonraker +MAINSAIL_DIR=${HOME}/mainsail +MOONRAKER_DIR=${HOME}/moonraker +MOONRAKER_ENV_DIR=${HOME}/moonraker-env +MOONRAKER_SERVICE1=/etc/init.d/moonraker +MOONRAKER_SERVICE2=/etc/default/moonraker +#octoprint +OCTOPRINT_DIR=${HOME}/OctoPrint +OCTOPRINT_CFG_DIR=${HOME}/.octoprint +OCTOPRINT_SERVICE1=/etc/init.d/octoprint +OCTOPRINT_SERVICE2=/etc/default/octoprint +#misc +INI_FILE=${HOME}/kiauh/kiauh.ini +BACKUP_DIR=${HOME}/kiauh-backups + +### set github repos +KLIPPER_REPO=https://github.com/KevinOConnor/klipper.git +ARKSINE_REPO=https://github.com/Arksine/klipper.git +DMBUTYUGIN_REPO=https://github.com/dmbutyugin/klipper.git +DWC2FK_REPO=https://github.com/Stephan3/dwc2-for-klipper.git +MOONRAKER_REPO=https://github.com/Arksine/moonraker.git +#branches +BRANCH_MOONRAKER=Arksine/dev-moonraker-testing +BRANCH_SCURVE_SMOOTHING=dmbutyugin/scurve-smoothing +BRANCH_SCURVE_SHAPING=dmbutyugin/scurve-shaping ### set some messages warn_msg(){ @@ -30,45 +66,6 @@ print_unkown_cmd(){ ERROR_MSG=" Sorry i don't know that command!" } -### set important directories -#klipper -KLIPPER_DIR=${HOME}/klipper -KLIPPY_ENV_DIR=${HOME}/klippy-env -KLIPPER_SERVICE1=/etc/init.d/klipper -KLIPPER_SERVICE2=/etc/default/klipper -#dwc2 -DWC2FK_DIR=${HOME}/dwc2-for-klipper -DWC2_DIR=${HOME}/sdcard/dwc2 -WEB_DWC2=${HOME}/klipper/klippy/extras/web_dwc2.py -TORNADO_DIR1=${HOME}/klippy-env/lib/python2.7/site-packages/tornado -TORNADO_DIR2=${HOME}/klippy-env/lib/python2.7/site-packages/tornado-5.1.1.dist-info -#mainsail/moonraker -MAINSAIL_DIR=${HOME}/mainsail -MOONRAKER_DIR=${HOME}/moonraker -MOONRAKER_ENV_DIR=${HOME}/moonraker-env -MOONRAKER_SERVICE1=/etc/init.d/moonraker -MOONRAKER_SERVICE2=/etc/default/moonraker -#octoprint -OCTOPRINT_DIR=${HOME}/OctoPrint -OCTOPRINT_CFG_DIR=${HOME}/.octoprint -OCTOPRINT_SERVICE1=/etc/init.d/octoprint -OCTOPRINT_SERVICE2=/etc/default/octoprint -#misc -INI_FILE=${HOME}/kiauh/kiauh.ini -BACKUP_DIR=${HOME}/kiauh-backups -PRINTER_CFG=${HOME}/printer.cfg - -### set github repos -KLIPPER_REPO=https://github.com/KevinOConnor/klipper.git -ARKSINE_REPO=https://github.com/Arksine/klipper.git -DMBUTYUGIN_REPO=https://github.com/dmbutyugin/klipper.git -DWC2FK_REPO=https://github.com/Stephan3/dwc2-for-klipper.git -MOONRAKER_REPO=https://github.com/Arksine/moonraker.git -#branches -BRANCH_MOONRAKER=Arksine/dev-moonraker-testing -BRANCH_SCURVE_SMOOTHING=dmbutyugin/scurve-smoothing -BRANCH_SCURVE_SHAPING=dmbutyugin/scurve-shaping - print_msg(){ if [[ "$ERROR_MSG" != "" ]]; then echo -e "${red}" @@ -87,430 +84,14 @@ print_msg(){ } clear_msg(){ - CONFIRM_MSG="" && ERROR_MSG="" -} - -main_menu(){ - print_header - #print KIAUH update msg if update available - if [ "$KIAUH_UPDATE_AVAIL" = "true" ]; then - kiauh_update_msg - fi - #check install status - klipper_status - dwc2_status - mainsail_status - octoprint_status - print_branch - print_msg && clear_msg - main_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - update) - clear - print_header - update_kiauh - print_msg && clear_msg - main_ui;; - 0) - clear - print_header - ERROR_MSG="Sorry this function is not implemented yet!" - print_msg && clear_msg - 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;; - Q|q) - echo -e "${green}###### Happy printing! ######${default}"; echo - exit -1;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - main_ui;; - esac - done - clear; main_menu -} - -install_menu(){ - print_header - install_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 1) - clear - print_header - install_klipper - print_msg && clear_msg - install_ui;; - 2) - clear - print_header - dwc2_install_routine - print_msg && clear_msg - install_ui;; - 3) - clear - print_header - mainsail_install_routine - print_msg && clear_msg - install_ui;; - 4) - clear - print_header - octoprint_install_routine - print_msg && clear_msg - install_ui;; - Q|q) - clear; main_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - install_ui;; - esac - done - install_menu -} - -update_menu(){ - print_header - #compare versions - ui_print_versions - print_msg && clear_msg - read_bb4u_stat - update_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 0) - clear - print_header - toggle_backups - print_msg && clear_msg - update_ui;; - 1) - clear - print_header - update_klipper && ui_print_versions - print_msg && clear_msg - update_ui;; - 2) - clear - print_header - update_dwc2fk && ui_print_versions - print_msg && clear_msg - update_ui;; - 3) - clear - print_header - update_dwc2 && ui_print_versions - print_msg && clear_msg - update_ui;; - 4) - clear - print_header - update_moonraker && ui_print_versions - print_msg && clear_msg - update_ui;; - 5) - clear - print_header - update_mainsail && ui_print_versions - print_msg && clear_msg - update_ui;; - Q|q) - clear; main_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - ui_print_versions - update_ui;; - esac - done - update_menu -} - -remove_menu(){ - print_header - remove_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 1) - clear - print_header - remove_klipper - print_msg && clear_msg - remove_ui;; - 2) - clear - print_header - remove_dwc2 - print_msg && clear_msg - remove_ui;; - 3) - clear - print_header - remove_mainsail - print_msg && clear_msg - remove_ui;; - 4) - clear - print_header - remove_octoprint - print_msg && clear_msg - remove_ui;; - 5) - clear - print_header - remove_nginx - print_msg && clear_msg - remove_ui;; - Q|q) - clear; main_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - remove_ui;; - esac - done - remove_menu -} - -advanced_menu(){ - print_header - print_msg && clear_msg - read_octoprint_service_status - advanced_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 0) - clear - print_header - toggle_octoprint_service - read_octoprint_service_status - print_msg && clear_msg - advanced_ui;; - 1) - clear - print_header - switch_menu - print_msg && clear_msg - advanced_ui;; - 2) - clear - print_header - load_klipper_state - print_msg && clear_msg - advanced_ui;; - 3) - clear - print_header - build_fw - print_msg && clear_msg - advanced_ui;; - 4) - clear - print_header - flash_routine - print_msg && clear_msg - advanced_ui;; - 5) - clear - print_header - get_printer_usb - print_msg && clear_msg - advanced_ui;; - 6) - clear - print_header - get_printer_usb && write_printer_usb - print_msg && clear_msg - advanced_ui;; - 7) - clear - print_header - create_dwc2fk_cfg - print_msg && clear_msg - advanced_ui;; - 8) - clear - print_header - create_custom_hostname - print_msg && clear_msg - advanced_ui;; - 9) - clear - print_header - remove_branding - print_msg && clear_msg - advanced_ui;; - Q|q) - clear; main_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - advanced_ui;; - esac - done - advanced_menu -} - -switch_menu(){ - if [ -d $KLIPPER_DIR ]; then - read_branch - print_msg && clear_msg - switch_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 1) - clear - print_header - switch_to_origin - read_branch - print_msg && clear_msg - switch_ui;; - 2) - clear - print_header - switch_to_scurve_shaping - read_branch - print_msg && clear_msg - switch_ui;; - 3) - clear - print_header - switch_to_scurve_smoothing - read_branch - print_msg && clear_msg - switch_ui;; - 4) - clear - print_header - switch_to_moonraker - read_branch - print_msg && clear_msg - switch_ui;; - Q|q) - clear; advanced_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - switch_ui;; - esac - done - else - ERROR_MSG="No Klipper directory found! Download Klipper first!" - fi -} - -#rollback_menu(){ -# load_klipper_state -# print_msg && clear_msg -# advanced_menu -#} - -backup_menu(){ - print_header - print_msg && clear_msg - backup_ui - while true; do - echo -e "${cyan}" - read -p "Perform action: " action; echo - echo -e "${default}" - case "$action" in - 1) - clear - print_header - backup_klipper - print_msg && clear_msg - backup_ui;; - 2) - clear - print_header - backup_dwc2 - print_msg && clear_msg - backup_ui;; - 3) - clear - print_header - backup_mainsail - print_msg && clear_msg - backup_ui;; - 4) - clear - print_header - backup_moonraker - print_msg && clear_msg - backup_ui;; - 5) - clear - print_header - backup_octoprint - print_msg && clear_msg - backup_ui;; - Q|q) - clear; main_menu; break;; - *) - clear - print_header - print_unkown_cmd - print_msg && clear_msg - backup_ui;; - esac - done - backup_menu + unset CONFIRM_MSG + unset ERROR_MSG } ### sourcing all additional scripts SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )" -for script in "${SRCDIR}/kiauh/scripts/"*; do . $script; done +for script in "${SRCDIR}/kiauh/scripts/"*.sh; do . $script; done +for script in "${SRCDIR}/kiauh/scripts/ui/"*.sh; do . $script; done check_euid kiauh_status diff --git a/resources/mainsail_macros.cfg b/resources/mainsail_macros.cfg new file mode 100644 index 0000000..214c6c3 --- /dev/null +++ b/resources/mainsail_macros.cfg @@ -0,0 +1,42 @@ +########################## +### CREATED WITH KIAUH ### +########################## +[gcode_macro CANCEL_PRINT] +rename_existing: BASE_CANCEL_PRINT +default_parameter_X: 230 +default_parameter_Y: 230 +default_parameter_Z: 10 +gcode: + M104 S0 + M140 S0 + M141 S0 + M106 S0 + CLEAR_PAUSE + SDCARD_RESET_FILE + BASE_CANCEL_PRINT + +[gcode_macro PAUSE] +rename_existing: BASE_PAUSE +default_parameter_X: 230 +default_parameter_Y: 230 +default_parameter_Z: 10 +gcode: + SAVE_GCODE_STATE NAME=PAUSE_state + BASE_PAUSE + G91 + G1 E-1.7 F2100 + G1 Z{Z} + G90 + G1 X{X} Y{Y} F6000 + G91 + +[gcode_macro RESUME] +rename_existing: BASE_RESUME +gcode: + G91 + G1 E1.7 F2100 + G91 + RESTORE_GCODE_STATE NAME=PAUSE_state MOVE=1 + BASE_RESUME +########################## +########################## \ No newline at end of file diff --git a/resources/moonraker.conf b/resources/moonraker.conf index 3fc16e1..715bd8e 100644 --- a/resources/moonraker.conf +++ b/resources/moonraker.conf @@ -5,5 +5,4 @@ config_path: ~/klipper_config [authorization] enabled: true trusted_clients: - 127.0.0.1 - 192.168.1.0/24 \ No newline at end of file + 127.0.0.1 \ No newline at end of file diff --git a/resources/shell_command.py b/resources/shell_command.py new file mode 100755 index 0000000..4138172 --- /dev/null +++ b/resources/shell_command.py @@ -0,0 +1,83 @@ +# Run a shell command via gcode +# +# Copyright (C) 2019 Eric Callahan +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import os +import shlex +import subprocess +import logging + +class ShellCommand: + def __init__(self, config): + self.name = config.get_name().split()[-1] + self.printer = config.get_printer() + self.gcode = self.printer.lookup_object('gcode') + cmd = config.get('command') + cmd = os.path.expanduser(cmd) + self.command = shlex.split(cmd) + self.timeout = config.getfloat('timeout', 2., above=0.) + self.verbose = config.getboolean('verbose', True) + self.proc_fd = None + self.partial_output = "" + self.gcode.register_mux_command( + "RUN_SHELL_COMMAND", "CMD", self.name, + self.cmd_RUN_SHELL_COMMAND, + desc=self.cmd_RUN_SHELL_COMMAND_help) + + def _process_output(self, eventime): + if self.proc_fd is None: + return + try: + data = os.read(self.proc_fd, 4096) + except Exception: + pass + data = self.partial_output + data + if '\n' not in data: + self.partial_output = data + return + elif data[-1] != '\n': + split = data.rfind('\n') + 1 + self.partial_output = data[split:] + data = data[:split] + self.gcode.respond_info(data) + + cmd_RUN_SHELL_COMMAND_help = "Run a linux shell command" + def cmd_RUN_SHELL_COMMAND(self, params): + reactor = self.printer.get_reactor() + try: + proc = subprocess.Popen( + self.command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + except Exception: + logging.exception( + "shell_command: Command {%s} failed" % (self.name)) + raise self.gcode.error("Error running command {%s}" % (self.name)) + if self.verbose: + self.proc_fd = proc.stdout.fileno() + self.gcode.respond_info("Running Command {%s}...:" % (self.name)) + hdl = reactor.register_fd(self.proc_fd, self._process_output) + eventtime = reactor.monotonic() + endtime = eventtime + self.timeout + complete = False + while eventtime < endtime: + eventtime = reactor.pause(eventtime + .05) + if proc.poll() is not None: + complete = True + break + if not complete: + proc.terminate() + if self.verbose: + if self.partial_output: + self.gcode.respond_info(self.partial_output) + self.partial_output = "" + if complete: + msg = "Command {%s} finished\n" % (self.name) + else: + msg = "Command {%s} timed out" % (self.name) + self.gcode.respond_info(msg) + reactor.unregister_fd(hdl) + self.proc_fd = None + + +def load_config_prefix(config): + return ShellCommand(config) diff --git a/scripts/backup.sh b/scripts/backup.sh index beced5d..2906e1c 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -1,6 +1,6 @@ check_for_backup_dir(){ if [ ! -d $BACKUP_DIR ]; then - status_msg "Create backup directory ..." + status_msg "Create KIAUH backup directory ..." mkdir -p $BACKUP_DIR && ok_msg "Directory created!" fi } diff --git a/scripts/functions.sh b/scripts/functions.sh index 7904a14..f21f922 100755 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -11,6 +11,20 @@ check_euid(){ fi } +locate_printer_cfg(){ + if [ -f $KLIPPER_SERVICE2 ]; then + #reads /etc/default/klipper and gets the default printer.cfg location + PRINTER_CFG_LOC=$(grep "KLIPPY_ARGS=" /etc/default/klipper | cut -d" " -f2) + if [ -e $PRINTER_CFG_LOC ]; then + PRINTER_CFG=$(readlink -e $PRINTER_CFG_LOC) + else + PRINTER_CFG="" + fi + else + PRINTER_CFG="" + fi +} + source_ini(){ source ${HOME}/kiauh/kiauh.ini } @@ -78,6 +92,50 @@ restart_octoprint(){ fi } +enable_octoprint_service(){ + if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then + status_msg "OctoPrint Service is disabled! Enabling now ..." + sudo systemctl enable octoprint -q && sudo systemctl start octoprint + fi +} + +disable_octoprint(){ + if [ "$DISABLE_OPRINT" = "true" ]; then + disable_octoprint_service + fi +} + +disable_octoprint_service(){ + if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then + status_msg "OctoPrint Service is enabled! Disabling now ..." + sudo systemctl stop octoprint && sudo systemctl disable octoprint -q + fi +} + +toggle_octoprint_service(){ + if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then + if systemctl is-enabled octoprint.service -q; then + disable_octoprint_service + sleep 2 + CONFIRM_MSG=" OctoPrint Service is now >>> DISABLED <<< !" + else + enable_octoprint_service + sleep 2 + CONFIRM_MSG=" OctoPrint Service is now >>> ENABLED <<< !" + fi + else + ERROR_MSG=" You cannot activate a service that does not exist!" + fi +} + +read_octoprint_service_status(){ + if ! systemctl is-enabled octoprint.service -q &>/dev/null; then + OPRINT_SERVICE_STATUS="${green}[Enable]${default} OctoPrint Service " + else + OPRINT_SERVICE_STATUS="${red}[Disable]${default} OctoPrint Service " + fi +} + restart_nginx(){ if [ -e /etc/init.d/nginx ]; then status_msg "Restarting Nginx Service ..." @@ -86,10 +144,11 @@ restart_nginx(){ } dependency_check(){ - status_msg "Checking for dependencies ..." + status_msg "Checking for the following dependencies:" #check if package is installed, if not write name into array for pkg in "${dep[@]}" do + echo -e "${cyan}● $pkg ${default}" if [[ ! $(dpkg-query -f'${Status}' --show $pkg 2>/dev/null) = *\ installed ]]; then inst+=($pkg) fi @@ -122,7 +181,7 @@ print_error(){ done sum=$(IFS=+; echo "$((${data_count[*]}))") if [ $sum -eq 0 ]; then - ERROR_MSG=" Looks like $1 was already removed!\n Skipping..." + ERROR_MSG="Looks like $1 was already removed!\n Skipping..." else ERROR_MSG="" fi @@ -239,138 +298,6 @@ flash_mcu(){ start_klipper } -enable_octoprint_service(){ - if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then - status_msg "OctoPrint Service is disabled! Enabling now ..." - sudo systemctl enable octoprint -q && sudo systemctl start octoprint - fi -} - -disable_octoprint_service(){ - if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then - status_msg "OctoPrint Service is enabled! Disabling now ..." - sudo systemctl stop octoprint && sudo systemctl disable octoprint -q - fi -} - -toggle_octoprint_service(){ - if [[ -f $OCTOPRINT_SERVICE1 && -f $OCTOPRINT_SERVICE2 ]]; then - if systemctl is-enabled octoprint.service -q; then - disable_octoprint_service - sleep 2 - CONFIRM_MSG=" OctoPrint Service is now >>> DISABLED <<< !" - else - enable_octoprint_service - sleep 2 - CONFIRM_MSG=" OctoPrint Service is now >>> ENABLED <<< !" - fi - else - ERROR_MSG=" You cannot activate a service that does not exist!" - fi -} - -read_octoprint_service_status(){ - if ! systemctl is-enabled octoprint.service -q &>/dev/null; then - OPRINT_SERVICE_STATUS="${green}[Enable]${default} OctoPrint Service " - else - OPRINT_SERVICE_STATUS="${red}[Disable]${default} OctoPrint Service " - fi -} - -create_reverse_proxy(){ - #check for dependencies - dep=(nginx) - dependency_check - #execute operations - status_msg "Creating Nginx configuration for $1 ..." - cat ${HOME}/kiauh/resources/$1_nginx.cfg > ${HOME}/kiauh/resources/$1 - sudo mv ${HOME}/kiauh/resources/$1 /etc/nginx/sites-available/$1 - #ONLY FOR MAINSAIL: replace username if not "pi" - if [ "$1" = "mainsail" ]; then - sudo sed -i "/root/s/pi/${USER}/" /etc/nginx/sites-available/mainsail - fi - ok_msg "Nginx configuration for $1 was set!" - #remove default config - if [ -e /etc/nginx/sites-enabled/default ]; then - sudo rm /etc/nginx/sites-enabled/default - fi - #create symlink for own configs - if [ ! -e /etc/nginx/sites-enabled/$1 ]; then - sudo ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/ - fi - restart_nginx -} - -create_custom_hostname(){ - echo - top_border - echo -e "| You can change the hostname of this machine to use |" - echo -e "| that name to open the Interface in your browser. |" - echo -e "| |" - echo -e "| Example: If you set the hostname to 'my-printer' |" - echo -e "| you can open Mainsail/Octoprint by |" - echo -e "| browsing to: http://my-printer.local |" - bottom_border - while true; do - echo -e "${cyan}" - read -p "###### Do you want to change the hostname? (Y/n): " yn - echo -e "${default}" - case "$yn" in - Y|y|Yes|yes|"") set_hostname; break;; - N|n|No|no) break;; - esac - done -} - -set_hostname(){ - #check for dependencies - dep=(avahi-daemon) - dependency_check - #execute operations - #get current hostname and write to variable - HOSTNAME=$(hostname) - #create host file if missing or create backup of existing one with current date&time - if [ -f /etc/hosts ]; then - status_msg "Creating backup of hosts file ..." - get_date - sudo cp /etc/hosts /etc/hosts."$current_date".bak - ok_msg "Backup done!" - ok_msg "File:'/etc/hosts."$current_date".bak'" - else - sudo touch /etc/hosts - fi - echo - top_border - echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${default} |" - echo -e "| ${red}No special characters allowed!${default} |" - echo -e "| ${red}No leading or trailing '-' allowed!${default} |" - bottom_border - while true; do - echo -e "${cyan}" - read -p "###### Please set the new hostname: " NEW_HOSTNAME - echo -e "${default}" - if [[ $NEW_HOSTNAME =~ ^[^\-]+([0-9a-z]\-{0,1})+[^\-]+$ ]]; then - ok_msg "'$NEW_HOSTNAME' is a valid hostname!" - #set hostname in /etc/hostname - status_msg "Setting hostname to '$NEW_HOSTNAME' ..." - status_msg "Please wait ..." - sudo hostnamectl set-hostname $NEW_HOSTNAME - #write new hostname to /etc/hosts - status_msg "Writing new hostname to /etc/hosts ..." - if cat /etc/hosts | grep "###set by kiauh" &>/dev/null; then - sudo sed -i "/###set by kiauh/s/\<$HOSTNAME\>/$NEW_HOSTNAME/" /etc/hosts - else - echo "127.0.0.1 $NEW_HOSTNAME ###set by kiauh" | sudo tee -a /etc/hosts &>/dev/null - fi - ok_msg "New hostname successfully configured!" - ok_msg "Remember to reboot your machine for the changes to take effect!" - break - else - warn_msg "'$NEW_HOSTNAME' is not a valid hostname!" - fi - done -} - remove_branding(){ echo top_border @@ -379,9 +306,7 @@ remove_branding(){ echo -e "| this action again, everytime you updated Mainsail. |" bottom_border while true; do - echo -e "${cyan}" - read -p "###### Do you want to continue? (Y/n): " yn - echo -e "${default}" + read -p "${cyan}###### Do you want to continue? (Y/n):${default} " yn case "$yn" in Y|y|Yes|yes|"") cd $MAINSAIL_DIR/css @@ -405,4 +330,77 @@ remove_branding(){ N|n|No|no) break;; esac done +} + +install_extension_shell_command(){ + echo + top_border + echo -e "| You are about to install the shell command extension. |" + echo -e "| Please make sure to read the instructions before you |" + echo -e "| continue and remember that there are potential risks! |" + bottom_border + while true; do + read -p "${cyan}###### Do you want to continue? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + if [ -d $KLIPPER_DIR/klippy/extras ] && [ ! -f $KLIPPER_DIR/klippy/extras/shell_command.py ] ; then + status_msg "Installing shell command extension ..." + stop_klipper + cp ${HOME}/kiauh/resources/shell_command.py $KLIPPER_DIR/klippy/extras + status_msg "Creating example macro ..." + create_shell_command_example + ok_msg "Example macro created!" + ok_msg "Shell command extension installed!" + restart_klipper + else + if [ ! -d $KLIPPER_DIR/klippy/extras ]; then + ERROR_MSG="Folder ~/klipper/klippy/extras not found!" + fi + if [ -f $KLIPPER_DIR/klippy/extras/shell_command.py ]; then + ERROR_MSG="Extension already installed!" + fi + fi + break;; + N|n|No|no) + break;; + esac + done +} + +create_shell_command_example(){ + unset SC_ENTRY + unset write_entries + #check for a SAVE_CONFIG entry + SC="#*# <---------------------- SAVE_CONFIG ---------------------->" + if [[ $(grep "$SC" ${HOME}/printer.cfg) ]]; then + SC_LINE=$(grep -n "$SC" $PRINTER_CFG | cut -d ":" -f1) + PRE_SC_LINE=$(expr $SC_LINE - 1) + SC_ENTRY="true" + else + SC_ENTRY="false" + fi + #example shell command + write_entries+=("[shell_command hello_world]\ncommand: echo hello world\ntimeout: 2.\nverbose: True") + #example macro + write_entries+=("[gcode_macro HELLO_WORLD]\ngcode:\n RUN_SHELL_COMMAND CMD=hello_world") + if [ "${#write_entries[@]}" != "0" ]; then + write_entries+=("\\\n############################\n##### CREATED BY KIAUH #####\n############################") + write_entries=("############################\n" "${write_entries[@]}") + fi + #execute writing + status_msg "Writing to printer.cfg ..." + if [ "$SC_ENTRY" = "true" ]; then + PRE_SC_LINE="$(expr $SC_LINE - 1)a" + for entry in "${write_entries[@]}" + do + sed -i "$PRE_SC_LINE $entry" $PRINTER_CFG + done + fi + if [ "$SC_ENTRY" = "false" ]; then + LINE_COUNT="$(wc -l < $PRINTER_CFG)a" + for entry in "${write_entries[@]}" + do + sed -i "$LINE_COUNT $entry" $PRINTER_CFG + done + fi } \ No newline at end of file diff --git a/scripts/install_dwc2.sh b/scripts/install_dwc2.sh index a1a3aad..dd1e07c 100755 --- a/scripts/install_dwc2.sh +++ b/scripts/install_dwc2.sh @@ -1,30 +1,134 @@ -#TODO: -# - ask for permission to disable octoprint service - -dwc2_install_routine(){ +install_dwc2(){ if [ -d $KLIPPER_DIR ]; then - # check for existing installation - if [ -d $DWC2FK_DIR ] && [ -d $DWC2_DIR ]; then - ERROR_MSG=" Looks like DWC2 is already installed!\n Skipping..." - return - fi - stop_klipper - #disable octoprint service if installed - if systemctl is-enabled octoprint.service -q 2>/dev/null; then - disable_octoprint_service - fi - install_tornado - install_dwc2fk && dwc2fk_cfg - install_dwc2 - dwc2_reverse_proxy_dialog - create_custom_hostname - start_klipper + system_check_dwc2 + #ask user for customization + get_user_selections_dwc2 + #dwc2 main installation + tornado_setup + dwc2_setup + #setup config + write_printer_cfg_dwc2 + write_custom_printer_cfg_dwc2 + #execute customizations + disable_octoprint + create_reverse_proxy "dwc2" + set_hostname + #after install actions + restart_klipper else ERROR_MSG=" Please install Klipper first!\n Skipping..." fi } -install_tornado(){ +system_check_dwc2(){ + status_msg "Initializing DWC2 installation ..." + stop_klipper + check_for_folder_dwc2 + #check for existing printer.cfg + locate_printer_cfg + if [ ! -z $PRINTER_CFG ]; then + PRINTER_CFG_FOUND="true" + else + PRINTER_CFG_FOUND="false" + fi + #check if octoprint is installed + if systemctl is-enabled octoprint.service -q 2>/dev/null; then + unset OCTOPRINT_ENABLED + OCTOPRINT_ENABLED="true" + fi +} + +get_user_selections_dwc2(){ + #user selection for printer.cfg + if [ "$PRINTER_CFG_FOUND" = "false" ]; then + unset SEL_DEF_CFG + unset SEL_CUS_CFG + while true; do + echo + top_border + echo -e "| ${red}WARNING! - No printer.cfg was found!${default} |" + hr + echo -e "| You can now either select to create a printer.cfg |" + echo -e "| with the default entries (1), customize the settings |" + echo -e "| before writing them (2) or you can skip the creation |" + echo -e "| of a printer.cfg at all. |" + echo -e "| |" + echo -e "| Please keep in mind that DWC2 will ONLY load if you |" + echo -e "| have a correctly defined printer.cfg. Any missing |" + echo -e "| option or error will prevent DWC2 from loading and |" + echo -e "| you need to check klippy.log to resolve the error. |" + echo -e "| |" + echo -e "| ${red}Neither option 1 or 2 of this script will create a |" + echo -e "| fully working printer.cfg for you!${default} |" + hr + echo -e "| 1) [Create default configuration] |" + echo -e "| 2) [Create custom configuration] |" + echo -e "| 3) ${red}[Skip]${default} |" + bottom_border + read -p "${cyan}###### Please select:${default} " choice + case "$choice" in + 1) + echo -e "###### > Create default configuration" + SEL_DEF_CFG="true" + SEL_CUS_CFG="false" + break;; + 2) + echo -e "###### > Create custom configuration" + SEL_DEF_CFG="false" + SEL_CUS_CFG="true" + break;; + 3) + echo -e "###### > Skip" + SEL_DEF_CFG="false" + SEL_CUS_CFG="false" + echo "${red}Skipping ...${default}"; break;; + esac + break + done + fi + # + setup_printer_config_dwc2 + #ask user to install reverse proxy + dwc2_reverse_proxy_dialog + #ask to change hostname + if [ "$SET_REVERSE_PROXY" = "true" ]; then + create_custom_hostname + fi + #ask user to disable octoprint when such installed service was found + if [ "$OCTOPRINT_ENABLED" = "true" ]; then + unset DISABLE_OPRINT + while true; do + echo + warn_msg "OctoPrint service found!" + echo -e "You might consider disabling the OctoPrint service," + echo -e "since an active OctoPrint service may lead to unexpected" + echo -e "behavior of the DWC2 Webinterface." + read -p "${cyan}###### Do you want to disable OctoPrint now? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + DISABLE_OPRINT="true";; + N|n|No|no) + echo -e "###### > No" + DISABLE_OPRINT="false";; + esac + break + done + fi + status_msg "Installation will start now! Please wait ..." +} + +############################################################# +############################################################# + +check_for_folder_dwc2(){ + #check for needed folder + if [ ! -d $DWC2_DIR/web ]; then + mkdir -p $DWC2_DIR/web + fi +} + +tornado_setup(){ if [ "$(cd $KLIPPY_ENV_DIR/bin/ && $_/pip list 2>/dev/null | grep "tornado" | cut -d" " -f9)" = "5.1.1" ]; then ok_msg "Tornado 5.1.1 is already installed! Continue..." else @@ -37,51 +141,136 @@ install_tornado(){ fi } -install_dwc2fk(){ +dwc2_setup(){ + #check dependencies + dep=(git wget gzip tar curl) + dependency_check + #get dwc2-for-klipper cd ${HOME} status_msg "Cloning DWC2-for-Klipper repository ..." - git clone $DWC2FK_REPO && ok_msg "DWC2-for-Klipper successfully cloned!" + git clone $DWC2FK_REPO + ok_msg "DWC2-for-Klipper successfully cloned!" #create a web_dwc2.py symlink if not already existing if [ -d $KLIPPER_DIR/klippy/extras ] && [ ! -e $KLIPPER_DIR/klippy/extras/web_dwc2.py ]; then status_msg "Creating web_dwc2.py Symlink ..." - ln -s $DWC2FK_DIR/web_dwc2.py $KLIPPER_DIR/klippy/extras/web_dwc2.py && ok_msg "Symlink created!" + ln -s $DWC2FK_DIR/web_dwc2.py $KLIPPER_DIR/klippy/extras/web_dwc2.py + ok_msg "Symlink created!" + fi + #get Duet Web Control + GET_DWC2_URL=`curl -s https://api.github.com/repositories/28820678/releases/latest | grep browser_download_url | cut -d'"' -f4` + cd $DWC2_DIR/web + status_msg "Downloading DWC2 Web UI ..." + wget -q $GET_DWC2_URL + ok_msg "Download complete!" + status_msg "Unzipping archive ..." + unzip -q -o *.zip + for f_ in $(find . | grep '.gz') + do + gunzip -f ${f_} + done + ok_msg "Done!" + status_msg "Writing DWC version to file ..." + echo $GET_DWC2_URL | cut -d/ -f8 > $DWC2_DIR/web/version + ok_msg "Done!" + status_msg "Do a little cleanup ..." + rm -rf DuetWebControl-SD.zip + ok_msg "Done!" + ok_msg "DWC2 Web UI installed!" +} + +############################################################# +############################################################# + +setup_printer_config_dwc2(){ + if [ "$PRINTER_CFG_FOUND" = "true" ]; then + backup_printer_cfg + #check printer.cfg for necessary dwc2 entries + read_printer_cfg_dwc2 + fi + if [ "$SEL_DEF_CFG" = "true" ]; then + create_default_dwc2_printer_cfg + locate_printer_cfg + fi + if [ "$SEL_CUS_CFG" = "true" ]; then + #get user input for custom config + create_custom_dwc2_printer_cfg + locate_printer_cfg fi } -dwc2fk_cfg(){ - while true; do - echo -e "${cyan}" - read -p "###### Do you want to create the config now? (Y/n): " yn - echo -e "${default}" - case "$yn" in - Y|y|Yes|yes|"") create_dwc2fk_cfg; break;; - N|n|No|no) break;; - esac - done +read_printer_cfg_dwc2(){ + unset SC_ENTRY + SC="#*# <---------------------- SAVE_CONFIG ---------------------->" + if [ ! $(grep '^\[virtual_sdcard\]$' $PRINTER_CFG) ]; then + VSD="false" + fi + if [ ! $(grep '^\[web_dwc2\]$' $PRINTER_CFG) ]; then + WEB_DWC2="false" + fi + #check for a SAVE_CONFIG entry + if [[ $(grep "$SC" $PRINTER_CFG) ]]; then + SC_LINE=$(grep -n "$SC" $PRINTER_CFG | cut -d ":" -f1) + PRE_SC_LINE=$(expr $SC_LINE - 1) + SC_ENTRY="true" + else + SC_ENTRY="false" + fi } -create_dwc2fk_cfg(){ - echo -e "/=================================================\ " - echo -e "| 1) [Default configuration] | " - echo -e "| 2) [Custom configuration] | " - echo -e "| 3) [Skip] | " - echo -e "\=================================================/ " - while true; do - read -p "Please select: " choice; echo - case "$choice" in - 1) dwc2fk_default_cfg && ok_msg "Config written ..."; break;; - 2) create_dwc2fk_custom_cfg && ok_msg "Config written ..."; break;; - 3) echo "Skipping ..."; break;; - esac - done +write_printer_cfg_dwc2(){ + unset write_entries + if [ "$WEB_DWC2" = "false" ]; then + write_entries+=("[web_dwc2]\nprinter_name: my_printer\nlisten_adress: 0.0.0.0\nlisten_port: 4750\nweb_path: dwc2/web") + fi + if [ "$VSD" = "false" ]; then + write_entries+=("[virtual_sdcard]\npath: ~/sdcard") + fi + if [ "${#write_entries[@]}" != "0" ]; then + write_entries+=("\\\n############################\n##### CREATED BY KIAUH #####\n############################") + write_entries=("############################\n" "${write_entries[@]}") + fi + #execute writing + status_msg "Writing to printer.cfg ..." + if [ "$SC_ENTRY" = "true" ]; then + PRE_SC_LINE="$(expr $SC_LINE - 1)a" + for entry in "${write_entries[@]}" + do + sed -i "$PRE_SC_LINE $entry" $PRINTER_CFG + done + fi + if [ "$SC_ENTRY" = "false" ]; then + LINE_COUNT="$(wc -l < $PRINTER_CFG)a" + for entry in "${write_entries[@]}" + do + sed -i "$LINE_COUNT $entry" $PRINTER_CFG + done + fi + ok_msg "Done!" } -dwc2fk_default_cfg(){ - cat <> $PRINTER_CFG +write_custom_printer_cfg_dwc2(){ + #create custom config + if [ "$PRINTER_CFG_FOUND" = "false" ] && [ "$CONFIRM_CUSTOM_CFG" = "true" ]; then + touch $PRINTER_CFG + echo -e "$DWC2_CFG" >> $PRINTER_CFG + fi +} +create_default_dwc2_printer_cfg(){ + #create default config + if [ "$PRINTER_CFG_FOUND" = "false" ] && [ "$SEL_CUS_CFG" = "true" ]; then + touch $PRINTER_CFG + cat <> $PRINTER_CFG ########################## ### CREATED WITH KIAUH ### ########################## +[printer] +kinematics: cartesian +max_velocity: 300 +max_accel: 3000 +max_z_velocity: 5 +max_z_accel: 100 + [virtual_sdcard] path: ~/sdcard @@ -91,11 +280,20 @@ listen_adress: 0.0.0.0 listen_port: 4750 web_path: dwc2/web ########################## -########################## -DWC2 +DEFAULT_DWC2_CFG + fi } -create_dwc2fk_custom_cfg(){ +############################################################# +############################################################# + +create_custom_dwc2_printer_cfg(){ + echo + top_border + echo -e "| Please fill in custom values for the following |" + echo -e "| configuration options. If you are unsure what to put |" + echo -e "| in, keep the pre-entered values. |" + bottom_border echo -e "${cyan}" read -e -p "Printer name: " -i "my_printer" PRINTER_NAME read -e -p "Listen adress: " -i "0.0.0.0" LISTEN_ADRESS @@ -103,7 +301,6 @@ create_dwc2fk_custom_cfg(){ read -e -p "Web path: " -i "dwc2/web" WEB_PATH echo -e "${default}" DWC2_CFG=$(cat <> $PRINTER_CFG; break;; - N|n|No|no) create_dwc2fk_custom_cfg;; + Y|y|Yes|yes|"") + CONFIRM_CUSTOM_CFG="true" + break;; + N|n|No|no) + CONFIRM_CUSTOM_CFG="false" + create_custom_dwc2_printer_cfg + break;; esac done } -install_dwc2(){ - #check dependencies - dep=(wget gzip tar curl) - dependency_check - #execute operation - GET_DWC2_URL=`curl -s https://api.github.com/repositories/28820678/releases/latest | grep browser_download_url | cut -d'"' -f4` - if [ ! -d $DWC2_DIR/web ]; then - mkdir -p $DWC2_DIR/web - fi - cd $DWC2_DIR/web - status_msg "Downloading DWC2 Web UI ..." - wget -q $GET_DWC2_URL && ok_msg "Download complete!" - status_msg "Unzipping archive ..." - unzip -q -o *.zip && for f_ in $(find . | grep '.gz');do gunzip -f ${f_};done && ok_msg "Done!" - status_msg "Writing version to file ..." - echo $GET_DWC2_URL | cut -d/ -f8 > $DWC2_DIR/web/version && ok_msg "Done!" - status_msg "Do a little cleanup ..." - rm -rf DuetWebControl-SD.zip && ok_msg "Done!" - ok_msg "DWC2 Web UI installed!" -} +############################################################# +############################################################# dwc2_reverse_proxy_dialog(){ + unset SET_REVERSE_PROXY + echo top_border echo -e "| If you want to have a nicer URL or simply need/want | " echo -e "| DWC2 to run on port 80 (http's default port) you | " echo -e "| can set up a reverse proxy to run DWC2 on port 80. | " bottom_border while true; do - echo -e "${cyan}" - read -p "###### Do you want to set up a reverse proxy now? (Y/n): " yn - echo -e "${default}" + read -p "${cyan}###### Do you want to set up a reverse proxy now? (y/N):${default} " yn case "$yn" in - Y|y|Yes|yes|"") create_reverse_proxy "dwc2"; break;; - N|n|No|no) break;; + Y|y|Yes|yes) + SET_REVERSE_PROXY="true" + break;; + N|n|No|no|"") + SET_REVERSE_PROXY="false" + break;; esac done } \ No newline at end of file diff --git a/scripts/install_mainsail.sh b/scripts/install_mainsail.sh index 2c2a9ea..4c9bf8e 100755 --- a/scripts/install_mainsail.sh +++ b/scripts/install_mainsail.sh @@ -1,179 +1,16 @@ -mainsail_install_routine(){ - if [ -d $KLIPPER_DIR ]; then - #disable octoprint service if installed - if systemctl is-enabled octoprint.service -q 2>/dev/null; then - disable_octoprint_service - fi +install_mainsail(){ + if [ "$INST_MAINSAIL" = "true" ]; then disable_haproxy_lighttpd - remove_haproxy_lighttpd - install_moonraker - check_printer_cfg - restart_moonraker - restart_klipper + unset SET_REVERSE_PROXY && SET_REVERSE_PROXY="true" #quick and dirty hack to make mainsail reverse proxy install, needs polish create_reverse_proxy "mainsail" - test_api + mainsail_setup test_nginx - install_mainsail - create_custom_hostname ok_msg "Mainsail installation complete!"; echo - else - ERROR_MSG=" Please install Klipper first!\n Skipping..." fi } -install_moonraker(){ - dep=(wget curl unzip) - dependency_check - status_msg "Downloading Moonraker ..." - cd ${HOME} && git clone $MOONRAKER_REPO - ok_msg "Download complete!" - backup_printer_cfg - status_msg "Installing Moonraker ..." - $MOONRAKER_DIR/scripts/install-moonraker.sh && ok_msg "Moonraker successfully installed!" - #copy basic moonraker.conf - if [ ! -e ${HOME}/moonraker.conf ]; then - status_msg "Creating moonraker.conf ..." - cp ${HOME}/kiauh/resources/moonraker.conf ${HOME} - ok_msg "moonraker.conf created!" - fi - #create sdcard folder - if [ ! -d ${HOME}/sdcard ]; then - status_msg "Creating sdcard directory ..." - mkdir ${HOME}/sdcard - ok_msg "sdcard directory created!" - fi - #create klipper_config folder - if [ ! -d ${HOME}/klipper_config ]; then - status_msg "Creating klipper_config directory ..." - mkdir ${HOME}/klipper_config - ok_msg "klipper_config directory created!" - fi - #move printer.cfg to new config location - if [ -e ${HOME}/printer.cfg ]; then - status_msg "Moving printer.cfg to its new location ..." - mv ${HOME}/printer.cfg ${HOME}/klipper_config - ok_msg "Done!" - status_msg "Create symlink in home directory ..." - ln -s ${HOME}/klipper_config/printer.cfg ${HOME} - ok_msg "Done!" - else - warn_msg "No printer.cfg was found!" - status_msg "Creating a default printer.cfg ..." - create_default_cfg - create_mainsail_macro_cfg - ln -s ${HOME}/klipper_config/printer.cfg ${HOME} - ok_msg "Default printer.cfg created!" - fi - #create a moonraker.log symlink in home-dir just for convenience - if [ ! -e ${HOME}/moonraker.log ]; then - status_msg "Creating moonraker.log symlink ..." - ln -s /tmp/moonraker.log ${HOME}/moonraker.log && ok_msg "Symlink created!" - fi -} - -check_printer_cfg(){ - if [ -e $PRINTER_CFG ]; then - check_vsdcard_section - fi -} - -check_vsdcard_section(){ - # check if virtual sdcard is present in printer.cfg - status_msg "Checking for virtual_sdcard configuration ..." - if [ $(grep '^\[virtual_sdcard\]$' $PRINTER_CFG) ]; then - ok_msg "Virtual sdcard already configured!" - else - status_msg "No virtual sdcard entry found." - ok_msg "Virtual sdcard entry added to printer.cfg!" -# append the following lines to printer.cfg -cat <> $PRINTER_CFG - -########################## -### CREATED WITH KIAUH ### -########################## -[virtual_sdcard] -path: ~/sdcard - -[pause_resume] -[display_status] -########################## -########################## -VSDCARD - fi -} - -create_default_cfg(){ -#create default config -touch ${HOME}/klipper_config/printer.cfg -cat <> ${HOME}/klipper_config/printer.cfg - -########################## -### CREATED WITH KIAUH ### -########################## -[virtual_sdcard] -path: ~/sdcard - -[pause_resume] -[display_status] -[include klipper_config/mainsail_macros.cfg] - -########################## -########################## -DEFAULT_CFG -} - -create_mainsail_macro_cfg(){ -#create extra mainsail macro config -touch ${HOME}/klipper_config/mainsail_macros.cfg -cat <> ${HOME}/klipper_config/mainsail_macros.cfg -########################## -### CREATED WITH KIAUH ### -########################## -[gcode_macro CANCEL] -default_parameter_X: 230 -default_parameter_Y: 230 -default_parameter_Z: 10 -gcode: - M104 S0 - M140 S0 - M141 S0 - M106 S0 - CLEAR_PAUSE - SDCARD_RESET_FILE - -[gcode_macro CANCEL_PRINT] -gcode: - CANCEL - -[gcode_macro PAUSE] -rename_existing: BASE_PAUSE -default_parameter_X: 230 -default_parameter_Y: 230 -default_parameter_Z: 10 -gcode: - SAVE_GCODE_STATE NAME=PAUSE_state - BASE_PAUSE - G91 - G1 E-1.7 F2100 - G1 Z{Z} - G90 - G1 X{X} Y{Y} F6000 - G91 - -[gcode_macro RESUME] -rename_existing: BASE_RESUME -gcode: - G91 - G1 E1.7 F2100 - G91 - RESTORE_GCODE_STATE NAME=PAUSE_state MOVE=1 - BASE_RESUME -########################## -########################## -MAINSAIL_MACRO_CFG -} - disable_haproxy_lighttpd(){ + disable_service=(haproxy lighttpd) if systemctl is-active haproxy -q; then status_msg "Stopping haproxy service ..." sudo /etc/init.d/haproxy stop && ok_msg "Service stopped!" @@ -182,40 +19,21 @@ disable_haproxy_lighttpd(){ status_msg "Stopping lighttpd service ..." sudo /etc/init.d/lighttpd stop && ok_msg "Service stopped!" fi -} - -remove_haproxy_lighttpd(){ - rem=(haproxy lighttpd) - for remove in "${rem[@]}" + for service in "${disable_service[@]}" do - if [[ $(dpkg-query -f'${Status}' --show $remove 2>/dev/null) = *\ installed ]]; then - delete+=($remove) + if [[ $(dpkg-query -f'${Status}' --show $service 2>/dev/null) = *\ installed ]]; then + status_msg "Disabling $service service ..." + sudo apt-get disable $service + ok_msg "$service service disabled!" fi done - if ! [ ${#delete[@]} -eq 0 ]; then - sudo apt-get remove ${delete[@]} -y - fi -} - -test_api(){ - status_msg "Testing API ..." - sleep 5 - status_msg "API response from http://localhost:7125/printer/info:" - API_RESPONSE="$(curl -sG4 http://localhost:7125/printer/info)" - echo -e "${cyan}$API_RESPONSE${default}" - if [ $(curl -sG4 "http://localhost:7125/printer/info" | grep '^{"result"' -c) -eq 1 ]; then - echo; ok_msg "Klipper API is working correctly!"; echo - else - echo; warn_msg "Klipper API not working correctly!"; echo - fi } test_nginx(){ - sudo /etc/init.d/nginx restart status_msg "Testing Nginx ..." sleep 5 - status_msg "API response from http://localhost/printer/info:" - API_RESPONSE="$(curl -sG4 http://localhost/printer/info)" + status_msg "API response from http://localhost/printer/info :" + API_RESPONSE="$(curl -sG4m5 http://localhost/printer/info)" echo -e "${cyan}$API_RESPONSE${default}" if [ $(curl -sG4 "http://localhost/printer/info" | grep '^{"result"' -c) -eq 1 ]; then echo; ok_msg "Nginx is working correctly!"; echo @@ -233,7 +51,7 @@ mainsail_dl_url(){ MAINSAIL_URL=https://github.com/meteyou/mainsail/releases/download/v"$MAINSAIL_VERSION"/mainsail-beta-"$MAINSAIL_VERSION".zip } -install_mainsail(){ +mainsail_setup(){ mainsail_dl_url if [ ! -d $MAINSAIL_DIR ]; then mkdir $MAINSAIL_DIR diff --git a/scripts/install_moonraker.sh b/scripts/install_moonraker.sh new file mode 100755 index 0000000..2e7743c --- /dev/null +++ b/scripts/install_moonraker.sh @@ -0,0 +1,490 @@ +install_moonraker(){ + system_check_moonraker + #ask user for customization + get_user_selections_moonraker + #moonraker main installation + moonraker_setup + check_for_folder_moonraker + #setup configs + setup_printer_config_mainsail + setup_moonraker_conf + #execute customizations + write_custom_trusted_clients + symlinks_moonraker + disable_octoprint + set_hostname + #after install actions + restart_moonraker + restart_klipper + test_api + install_mainsail +} + +system_check_moonraker(){ + status_msg "Initializing Moonraker installation ..." + #check for existing printer.cfg and for the location + locate_printer_cfg + if [ ! -z $PRINTER_CFG ]; then + PRINTER_CFG_FOUND="true" + PRINTER_CFG_LOC=$PRINTER_CFG + else + PRINTER_CFG_FOUND="false" + fi + #check for existing klippy.log symlink in /klipper_config + if [ ! -e ${HOME}/klipper_config/klippy.log ]; then + KLIPPY_SL_FOUND="false" + else + KLIPPY_SL_FOUND="true" + fi + #check for existing moonraker.log symlink in /klipper_config + if [ ! -e ${HOME}/klipper_config/moonraker.log ]; then + MOONRAKER_SL_FOUND="false" + else + MOONRAKER_SL_FOUND="true" + fi + #check for existing moonraker.conf + if [ ! -f ${HOME}/moonraker.conf ]; then + MOONRAKER_CONF_FOUND="false" + else + MOONRAKER_CONF_FOUND="true" + fi + #check if octoprint is installed + if systemctl is-enabled octoprint.service -q 2>/dev/null; then + unset OCTOPRINT_ENABLED + OCTOPRINT_ENABLED="true" + fi +} + +get_user_selections_moonraker(){ + #ask if moonraker only or moonraker + mainsail + while true; do + echo + top_border + echo -e "| Do you want to install Moonraker and Mainsail? |" + echo -e "| You can choose to install Moonraker only by answering |" + echo -e "| with 'No'. |" + hr + echo -e "| If you select 'Yes' please be aware that an existing |" + echo -e "| Mainsail installation will then be overwritten! |" + bottom_border + read -p "${cyan}###### Install Moonraker + Mainsail? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + INST_MAINSAIL="true";; + N|n|No|no) + echo -e "###### > No" + INST_MAINSAIL="false";; + esac + break + done + #ask to change hostname if mainsail should be installed as well + if [ "$INST_MAINSAIL" = "true" ]; then + create_custom_hostname + fi + #user selection for printer.cfg + if [ "$PRINTER_CFG_FOUND" = "false" ]; then + unset SEL_DEF_CFG + while true; do + echo + top_border + echo -e "| ${red}WARNING! - No printer.cfg was found!${default} |" + hr + echo -e "| KIAUH can create a minimal printer.cfg with only the |" + echo -e "| necessary Mainsail config entries if you wish. |" + echo -e "| |" + echo -e "| Please be aware, that this option will ${red}NOT${default} create a |" + echo -e "| fully working printer.cfg for you! |" + bottom_border + read -p "${cyan}###### Create a default printer.cfg? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + SEL_DEF_CFG="true";; + N|n|No|no) + echo -e "###### > No" + SEL_DEF_CFG="false";; + esac + break + done + fi + #user selection for moonraker.log symlink + if [ "$KLIPPY_SL_FOUND" = "false" ]; then + while true; do + echo + read -p "${cyan}###### Create klippy.log symlink? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + SEL_KLIPPYLOG_SL="true";; + N|n|No|no) + echo -e "###### > No" + SEL_KLIPPYLOG_SL="false";; + esac + break + done + fi + #user selection for moonraker.log symlink + if [ "$MOONRAKER_SL_FOUND" = "false" ]; then + while true; do + echo + read -p "${cyan}###### Create moonraker.log symlink? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + SEL_MRLOG_SL="true";; + N|n|No|no) + echo -e "###### > No" + SEL_MRLOG_SL="false";; + esac + break + done + fi + #ask user for more trusted clients + while true; do + echo + top_border + echo -e "| Apart from devices of your local network, you can add |" + echo -e "| additional trusted clients to the moonraker.conf file |" + bottom_border + read -p "${cyan}###### Add additional trusted clients? (y/N):${default} " yn + case "$yn" in + Y|y|Yes|yes) + echo -e "###### > Yes" + ADD_TRUSTED_CLIENT="true" + custom_trusted_clients + ;; + N|n|No|no|"") + echo -e "###### > No" + ADD_TRUSTED_CLIENT="false";; + esac + break + done + #ask user for mainsail default macros + while true; do + echo + read -p "${cyan}###### Add the recommended Mainsail macros? (Y/n):${default} " + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + ADD_MS_MCAROS="true";; + N|n|No|no) + echo -e "###### > No" + ADD_MS_MCAROS="false";; + esac + break + done + #ask user to disable octoprint when such installed service was found + if [ "$OCTOPRINT_ENABLED" = "true" ]; then + unset DISABLE_OPRINT + while true; do + echo + warn_msg "OctoPrint service found!" + echo -e "You might consider disabling the OctoPrint service," + echo -e "since an active OctoPrint service may lead to unexpected" + echo -e "behavior of the Mainsail Webinterface." + read -p "${cyan}###### Do you want to disable OctoPrint now? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + DISABLE_OPRINT="true";; + N|n|No|no) + echo -e "###### > No" + DISABLE_OPRINT="false";; + esac + break + done + fi + status_msg "Installation will start now! Please wait ..." +} + +############################################################# +############################################################# + +moonraker_setup(){ + dep=(wget curl unzip) + dependency_check + status_msg "Downloading Moonraker ..." + if [ -d $MOONRAKER_DIR ]; then + mv -f $MOONRAKER_DIR ${HOME}/moonraker_bak + fi + cd ${HOME} && git clone $MOONRAKER_REPO + ok_msg "Download complete!" + status_msg "Installing Moonraker ..." + $MOONRAKER_DIR/scripts/install-moonraker.sh + ok_msg "Moonraker successfully installed!" +} + +check_for_folder_moonraker(){ + #check for / create sdcard folder + if [ ! -d ${HOME}/sdcard ]; then + status_msg "Creating sdcard directory ..." + mkdir ${HOME}/sdcard + ok_msg "sdcard directory created!" + fi + ##check for / create klipper_config folder + if [ ! -d ${HOME}/klipper_config ]; then + status_msg "Creating klipper_config directory ..." + mkdir ${HOME}/klipper_config + ok_msg "klipper_config directory created!" + fi +} + +############################################################# +############################################################# + +setup_printer_config_mainsail(){ + if [ "$PRINTER_CFG_FOUND" = "true" ]; then + backup_printer_cfg + #create a printer.cfg symlink to make it accessible + #in the mainsail config editor if the printer.cfg is not + #located in klipper_config by default + if [ "$PRINTER_CFG" != "${HOME}/klipper_config/printer.cfg" ] && [ ! -f ${HOME}/klipper_config/printer.cfg ]; then + status_msg "Create printer.cfg symlink ..." + ln -s $PRINTER_CFG ${HOME}/klipper_config + ok_msg "Done!" + fi + #check printer.cfg for necessary mainsail entries + read_printer_cfg_mainsail + write_printer_cfg_mainsail + fi + if [ "$SEL_DEF_CFG" = "true" ]; then + create_default_mainsail_printer_cfg + status_msg "Create symlink in home directory ..." + ln -s ${HOME}/klipper_config/printer.cfg ${HOME} + ok_msg "Done!" + fi + #copy mainsail_macro.cfg + if [ "$ADD_MS_MCAROS" = "true" ]; then + status_msg "Create mainsail_macros.cfg ..." + if [ ! -f ${HOME}/klipper_config/mainsail_macros.cfg ]; then + cp ${HOME}/kiauh/resources/mainsail_macros.cfg ${HOME}/klipper_config + ok_msg "File created!" + else + warn_msg "File does already exist! Skipping ..." + fi + fi +} + +read_printer_cfg_mainsail(){ + SC="#*# <---------------------- SAVE_CONFIG ---------------------->" + if [ ! $(grep '^\[virtual_sdcard\]$' $PRINTER_CFG) ]; then + VSD="false" + fi + if [ ! $(grep '^\[pause_resume\]$' $PRINTER_CFG) ]; then + PAUSE_RESUME="false" + fi + if [ ! $(grep '^\[display_status\]$' $PRINTER_CFG) ]; then + DISPLAY_STATUS="false" + fi + if [ ! "$(grep '^\[include klipper_config\/mainsail_macros\.cfg\]$' $PRINTER_CFG)" ]; then + MS_MACRO="false" + fi + #check for a SAVE_CONFIG entry + if [[ $(grep "$SC" $PRINTER_CFG) ]]; then + SC_LINE=$(grep -n "$SC" $PRINTER_CFG | cut -d ":" -f1) + PRE_SC_LINE=$(expr $SC_LINE - 1) + SC_ENTRY="true" + else + SC_ENTRY="false" + fi +} + +write_printer_cfg_mainsail(){ + unset write_entries + if [ "$MS_MACRO" = "false" ] && [ "$ADD_MS_MCAROS" = "true" ]; then + write_entries+=("[include klipper_config/mainsail_macros.cfg]") + fi + if [ "$PAUSE_RESUME" = "false" ]; then + write_entries+=("[pause_resume]") + fi + if [ "$DISPLAY_STATUS" = "false" ]; then + write_entries+=("[display_status]") + fi + if [ "$VSD" = "false" ]; then + write_entries+=("[virtual_sdcard]\npath: ~/sdcard") + fi + if [ "${#write_entries[@]}" != "0" ]; then + write_entries+=("\\\n############################\n##### CREATED BY KIAUH #####\n############################") + write_entries=("############################\n" "${write_entries[@]}") + fi + #execute writing + status_msg "Writing to printer.cfg ..." + if [ "$SC_ENTRY" = "true" ]; then + PRE_SC_LINE="$(expr $SC_LINE - 1)a" + for entry in "${write_entries[@]}" + do + sed -i "$PRE_SC_LINE $entry" $PRINTER_CFG + done + fi + if [ "$SC_ENTRY" = "false" ]; then + LINE_COUNT="$(wc -l < $PRINTER_CFG)a" + for entry in "${write_entries[@]}" + do + sed -i "$LINE_COUNT $entry" $PRINTER_CFG + done + fi + ok_msg "Done!" +} + +setup_moonraker_conf(){ + if [ "$MOONRAKER_CONF_FOUND" = "false" ]; then + status_msg "Creating moonraker.conf ..." + cp ${HOME}/kiauh/resources/moonraker.conf ${HOME} + ok_msg "moonraker.conf created!" + status_msg "Writing trusted clients to config ..." + write_default_trusted_clients + ok_msg "Trusted clients written!" + fi + #check for at least one trusted client in an already existing moonraker.conf + #in no entry is found, write default trusted client + if [ "$MOONRAKER_CONF_FOUND" = "true" ]; then + if grep "trusted_clients:" ${HOME}/moonraker.conf -q; then + TC_LINE=$(grep -n "trusted_clients:" ${HOME}/moonraker.conf | cut -d ":" -f1) + FIRST_IP_LINE=$(expr $TC_LINE + 1) + FIRST_IP=$(sed -n 2p ${HOME}/moonraker.conf | cut -d" " -f5) + if [[ ! $FIRST_IP =~ ([0-9].[0-9].[0-9].[0-9]) ]]; then + status_msg "Writing trusted clients to config ..." + write_default_trusted_clients + ok_msg "Trusted clients written!" + fi + fi + fi +} + +############################################################# +############################################################# + +create_default_mainsail_printer_cfg(){ +#create default config +touch ${HOME}/klipper_config/printer.cfg +cat <> ${HOME}/klipper_config/printer.cfg + +########################## +### CREATED WITH KIAUH ### +########################## +[virtual_sdcard] +path: ~/sdcard + +[pause_resume] +[display_status] +[include klipper_config/mainsail_macros.cfg] + +########################## +########################## +DEFAULT_CFG +} + +write_default_trusted_clients(){ + DEFAULT_IP=$(hostname -I) + status_msg "Your devices current IP adress is:\n${cyan}● $DEFAULT_IP ${default}" + #make IP of the device KIAUH is exectuted on as + #default trusted client and expand the IP range from 0 - 255 + DEFAULT_IP_RANGE="$(echo "$DEFAULT_IP" | cut -d"." -f1-3).0/24" + status_msg "Writing the following IP range to moonraker.conf:\n${cyan}● $DEFAULT_IP_RANGE ${default}" + #write the ip range in the first line below "trusted clients" + #example: 192.168.1.0/24 + sed -i "/trusted_clients\:/a \ \ \ \ $DEFAULT_IP_RANGE" ${HOME}/moonraker.conf + ok_msg "IP range of ● $DEFAULT_IP_RANGE written to moonraker.conf!" +} + +############################################################# +############################################################# + +custom_trusted_clients(){ + if [ "$ADD_TRUSTED_CLIENT" = "true" ]; then + unset trusted_arr + echo + top_border + echo -e "| You can now add additional trusted clients to your |" + echo -e "| moonraker.conf file. Be warned, that there is no |" + echo -e "| spellcheck to check for valid input. |" + echo -e "| Make sure to type the IP correct! |" + echo -e "| |" + echo -e "| If you want to add IP ranges, you can type in e.g.: |" + echo -e "| 192.168.1.0/24 |" + echo -e "| This will add the IPs 192.168.1.1 to 192.168.1.254 |" + echo -e "|-------------------------------------------------------|" + echo -e "| You can add as many IPs / IP ranges as you want. |" + echo -e "| When you are done type '${cyan}done${default}' to exit this dialoge. |" + bottom_border + while true; do + read -p "${cyan}###### Enter IP and press ENTER:${default} " TRUSTED_IP + case "$TRUSTED_IP" in + done) + echo + echo -e "List of IPs to add:" + for ip in ${trusted_arr[@]} + do + echo -e "${cyan}● $ip ${default}" + done + while true; do + echo + echo -e "Select 'Yes' to confirm, 'No' to start again" + echo -e "or 'Q' to abort and skip." + read -p "${cyan}###### Confirm writing (Y/n/q):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + TUSTED_CLIENT_CONFIRM="true" + break;; + N|n|No|no) + echo -e "###### > No" + custom_trusted_clients + break;; + Q|q) + unset trusted_arr + echo -e "###### > Abort" + echo -e "${red}Aborting ...${default}" + break;; + esac + done + break;; + *) trusted_arr+=($TRUSTED_IP);; + esac + done + fi +} + +write_custom_trusted_clients(){ + if [ "$TUSTED_CLIENT_CONFIRM" = "true" ]; then + if [ "${#trusted_arr[@]}" != "0" ]; then + for ip in ${trusted_arr[@]} + do + sed -i "/trusted_clients\:/a \ \ \ \ $ip" ${HOME}/moonraker.conf + done + ok_msg "Custom IPs written to moonraker.conf!" + fi + fi +} + +symlinks_moonraker(){ + #create a klippy.log/moonraker.log symlink in + #klipper_config-dir just for convenience + if [ "$SEL_KLIPPYLOG_SL" = "true" ] && [ ! -e ${HOME}/klipper_config/klippy.log ]; then + status_msg "Creating klippy.log symlink ..." + ln -s /tmp/klippy.log ${HOME}/klipper_config + ok_msg "Symlink created!" + fi + if [ "$SEL_MRLOG_SL" = "true" ] && [ ! -e ${HOME}/klipper_config/moonraker.log ]; then + status_msg "Creating moonraker.log symlink ..." + ln -s /tmp/moonraker.log ${HOME}/klipper_config + ok_msg "Symlink created!" + fi +} + +############################################################# +############################################################# + +test_api(){ + status_msg "Testing API ..." + sleep 5 + status_msg "API response from http://localhost:7125/printer/info :" + API_RESPONSE=$(curl -sG4m5 http://localhost:7125/printer/info) + echo -e "${cyan}$API_RESPONSE${default}" + if [ $(curl -sG4 "http://localhost:7125/printer/info" | grep '^{"result"' -c) -eq 1 ]; then + echo; ok_msg "Klipper API is working correctly!"; echo + else + echo; warn_msg "Klipper API not working correctly!"; echo + fi +} diff --git a/scripts/install_octoprint.sh b/scripts/install_octoprint.sh index 2cc48fe..cc049ba 100755 --- a/scripts/install_octoprint.sh +++ b/scripts/install_octoprint.sh @@ -1,13 +1,29 @@ -octoprint_install_routine(){ +install_octoprint(){ + #ask user for customization + get_user_selections_octoprint + #octoprint main installation octoprint_dependencies - install_octoprint + octoprint_setup add_groups configure_autostart add_reboot_permission - octoprint_reverse_proxy_dialog - create_custom_hostname create_config_yaml - load_server + #execute customizations + create_reverse_proxy "octoprint" + set_hostname + #after install actions + load_octoprint_server +} + +get_user_selections_octoprint(){ + status_msg "Initializing OctoPrint installation ..." + #ask user to set a reverse proxy + octoprint_reverse_proxy_dialog + #ask to change hostname + if [ "$SET_REVERSE_PROXY" = "true" ]; then + create_custom_hostname + fi + status_msg "Installation will start now! Please wait ..." } octoprint_dependencies(){ @@ -24,7 +40,7 @@ octoprint_dependencies(){ dependency_check } -install_octoprint(){ +octoprint_setup(){ if [ ! -d $OCTOPRINT_DIR ];then status_msg "Create OctoPrint directory ..." mkdir -p $OCTOPRINT_DIR && ok_msg "Directory created!" @@ -97,6 +113,8 @@ add_reboot_permission(){ } octoprint_reverse_proxy_dialog(){ + unset SET_REVERSE_PROXY + echo top_border echo -e "| If you want to have nicer URLs or simply need | " echo -e "| OctoPrint to run on port 80 (http's default port) | " @@ -106,11 +124,15 @@ octoprint_reverse_proxy_dialog(){ bottom_border while true; do echo -e "${cyan}" - read -p "###### Do you want to set up a reverse proxy now? (Y/n): " yn + read -p "###### Do you want to set up a reverse proxy now? (y/N): " yn echo -e "${default}" case "$yn" in - Y|y|Yes|yes|"") create_reverse_proxy "octoprint"; break;; - N|n|No|no) break;; + Y|y|Yes|yes) + SET_REVERSE_PROXY="true" + break;; + N|n|No|no|"") + SET_REVERSE_PROXY="false" + break;; esac done } @@ -124,7 +146,7 @@ create_config_yaml(){ fi } -load_server(){ +load_octoprint_server(){ start_octoprint #create an octoprint.log symlink in home-dir just for convenience if [ ! -e ${HOME}/octoprint.log ]; then diff --git a/scripts/remove.sh b/scripts/remove.sh index bfa3c7a..6f9a9f4 100755 --- a/scripts/remove.sh +++ b/scripts/remove.sh @@ -58,21 +58,20 @@ remove_dwc2(){ fi } -remove_mainsail(){ +remove_moonraker(){ data_arr=( $MOONRAKER_SERVICE1 $MOONRAKER_SERVICE2 - $MAINSAIL_DIR $MOONRAKER_DIR $MOONRAKER_ENV_DIR ${HOME}/moonraker.conf ${HOME}/moonraker.log + ${HOME}/klipper_config/moonraker.log + ${HOME}/klipper_config/klippy.log ${HOME}/.klippy_api_key ${HOME}/.moonraker_api_key - /etc/nginx/sites-available/mainsail - /etc/nginx/sites-enabled/mainsail ) - print_error "Mainsail" && data_count=() + print_error "Moonraker" && data_count=() if [ "$ERROR_MSG" = "" ]; then stop_moonraker #remove moonraker services @@ -81,11 +80,6 @@ remove_mainsail(){ sudo update-rc.d -f moonraker remove sudo rm -rf /etc/init.d/moonraker /etc/default/moonraker && ok_msg "Moonraker Service removed!" fi - #remove mainsail dir - if [ -d $MAINSAIL_DIR ]; then - status_msg "Removing Mainsail directory ..." - rm -rf $MAINSAIL_DIR && ok_msg "Directory removed!" - fi #remove moonraker and moonraker-env dir if [[ -d $MOONRAKER_DIR || -d $MOONRAKER_ENV_DIR ]]; then status_msg "Removing Moonraker and moonraker-env directory ..." @@ -96,27 +90,11 @@ remove_mainsail(){ status_msg "Removing moonraker.conf ..." rm -rf ${HOME}/moonraker.conf && ok_msg "File removed!" fi - #remove printer.cfg symlink, copy printer.cfg back into home dir - if [ -L ${HOME}/printer.cfg ]; then - status_msg "Removing printer.cfg symlink ..." - rm -rf ${HOME}/printer.cfg && ok_msg "Symlink removed!" - status_msg "Copy printer.cfg back into '${HOME}' ..." - cp ${HOME}/klipper_config/printer.cfg ${HOME} && ok_msg "File copied!" - fi #remove moonraker.log and symlink - if [[ -L ${HOME}/moonraker.log || -e /tmp/moonraker.log ]]; then - status_msg "Removing moonraker.log and Symlink ..." - rm -rf ${HOME}/moonraker.log /tmp/moonraker.log && ok_msg "Files removed!" - fi - #remove mainsail cfg - if [ -e /etc/nginx/sites-available/mainsail ]; then - status_msg "Removing Mainsail configuration for Nginx ..." - sudo rm /etc/nginx/sites-available/mainsail && ok_msg "File removed!" - fi - #remove mainsail symlink - if [ -L /etc/nginx/sites-enabled/mainsail ]; then - status_msg "Removing Mainsail Symlink for Nginx ..." - sudo rm /etc/nginx/sites-enabled/mainsail && ok_msg "File removed!" + if [[ -L ${HOME}/moonraker.log || -L ${HOME}/klipper_config/moonraker.log || -L ${HOME}/klipper_config/klippy.log || -e /tmp/moonraker.log ]]; then + status_msg "Removing Logs and Symlinks ..." + rm -rf ${HOME}/moonraker.log ${HOME}/klipper_config/moonraker.log ${HOME}/klipper_config/klippy.log /tmp/moonraker.log + ok_msg "Files removed!" fi #remove legacy api key if [ -e ${HOME}/.klippy_api_key ]; then @@ -128,7 +106,34 @@ remove_mainsail(){ status_msg "Removing API Key ..." rm ${HOME}/.moonraker_api_key && ok_msg "Done!" fi - CONFIRM_MSG=" Mainsail successfully removed!" + CONFIRM_MSG="Moonraker successfully removed!" + fi +} + +remove_mainsail(){ + data_arr=( + $MAINSAIL_DIR + /etc/nginx/sites-available/mainsail + /etc/nginx/sites-enabled/mainsail + ) + print_error "Mainsail" && data_count=() + if [ "$ERROR_MSG" = "" ]; then + #remove mainsail dir + if [ -d $MAINSAIL_DIR ]; then + status_msg "Removing Mainsail directory ..." + rm -rf $MAINSAIL_DIR && ok_msg "Directory removed!" + fi + #remove mainsail config for nginx + if [ -e /etc/nginx/sites-available/mainsail ]; then + status_msg "Removing Mainsail configuration for Nginx ..." + sudo rm /etc/nginx/sites-available/mainsail && ok_msg "File removed!" + fi + #remove mainsail symlink for nginx + if [ -L /etc/nginx/sites-enabled/mainsail ]; then + status_msg "Removing Mainsail Symlink for Nginx ..." + sudo rm /etc/nginx/sites-enabled/mainsail && ok_msg "File removed!" + fi + CONFIRM_MSG="Mainsail successfully removed!" fi } diff --git a/scripts/rollback.sh b/scripts/rollback.sh index cd5f9ae..a61c049 100755 --- a/scripts/rollback.sh +++ b/scripts/rollback.sh @@ -1,24 +1,29 @@ save_klipper_state(){ source_ini #read current klipper state + cd $KLIPPER_DIR COMMIT_STATE=$(git rev-parse --short HEAD) - if [ $GET_BRANCH = origin/master ]; then + if [ "$GET_BRANCH" = "origin/master" ]; then ORI_OLD=$previous_origin_state ORI_NEW=$COMMIT_STATE sed -i "/previous_origin_state=/s/$ORI_OLD/$ORI_NEW/" $INI_FILE - elif [ $GET_BRANCH = dmbutyugin/scurve-shaping ]; then + elif [ "$GET_BRANCH" = "origin" ]; then + ORI_OLD=$previous_origin_state + ORI_NEW=$COMMIT_STATE + sed -i "/previous_origin_state=/s/$ORI_OLD/$ORI_NEW/" $INI_FILE + elif [ "$GET_BRANCH" = "dmbutyugin/scurve-shaping" ]; then SHA_OLD=$previous_shaping_state SHA_NEW=$COMMIT_STATE sed -i "/previous_shaping_state=/s/$SHA_OLD/$SHA_NEW/" $INI_FILE - elif [ $GET_BRANCH = dmbutyugin/scurve-smoothing ]; then + elif [ "$GET_BRANCH" = "dmbutyugin/scurve-smoothing" ]; then SMO_OLD=$previous_smoothing_state SMO_NEW=$COMMIT_STATE sed -i "/previous_smoothing_state=/s/$SMO_OLD/$SMO_NEW/" $INI_FILE - elif [ $GET_BRANCH = Arksine/work-web_server-20200131 ]; then + elif [ "$GET_BRANCH" = "Arksine/work-web_server-20200131" ]; then WWS_OLD=$previous_moonraker_state WWS_NEW=$COMMIT_STATE sed -i "/previous_moonraker_state=/s/$WWS_OLD/$WWS_NEW/" $INI_FILE - elif [ $GET_BRANCH = Arksine/dev-moonraker-testing ]; then + elif [ "$GET_BRANCH" = "Arksine/dev-moonraker-testing" ]; then DMT_OLD=$previous_dev_moonraker_state DMT_NEW=$COMMIT_STATE sed -i "/previous_dev_moonraker_state=/s/$DMT_OLD/$DMT_NEW/" $INI_FILE @@ -28,43 +33,51 @@ save_klipper_state(){ load_klipper_state(){ source_ini print_branch - CURR_COMM=$(git rev-parse --short=8 HEAD) - if [ "$GET_BRANCH" == "origin/master" ]; then - PREV_COMM=$previous_origin_state - elif [ "$GET_BRANCH" == "dmbutyugin/scurve-shaping" ]; then - PREV_COMM=$previous_shaping_state - elif [ "$GET_BRANCH" == "dmbutyugin/scurve-smoothing" ]; then - PREV_COMM=$previous_smoothing_state - elif [ "$GET_BRANCH" == "Arksine/work-web_server-20200131" ]; then - PREV_COMM=$previous_moonraker_state - elif [ "$GET_BRANCH" == "Arksine/dev-moonraker-testing" ]; then - PREV_COMM=$previous_dev_moonraker_state + cd $KLIPPER_DIR + CURRENT_COMMIT=$(git rev-parse --short=8 HEAD) + if [ "$GET_BRANCH" = "origin/master" ] || [ "$GET_BRANCH" = "origin" ]; then + PREVIOUS_COMMIT=$previous_origin_state + elif [ "$GET_BRANCH" = "dmbutyugin/scurve-shaping" ]; then + PREVIOUS_COMMIT=$previous_shaping_state + elif [ "$GET_BRANCH" = "dmbutyugin/scurve-smoothing" ]; then + PREVIOUS_COMMIT=$previous_smoothing_state + elif [ "$GET_BRANCH" = "Arksine/work-web_server-20200131" ]; then + PREVIOUS_COMMIT=$previous_moonraker_state + elif [ "$GET_BRANCH" = "Arksine/dev-moonraker-testing" ]; then + PREVIOUS_COMMIT=$previous_dev_moonraker_state fi - PREV_COMM_DATE=$(git show -s --format=%cd --date=short $PREV_COMM) - CURR_COMM_DATE=$(git show -s --format=%cd --date=short $CURR_COMM) - if [ $CURR_COMM = $PREV_COMM ]; then - CURR_UI=$(echo -e "${green}$CURR_COMM from $CURR_COMM_DATE${default}") - PREV_UI=$(echo -e "${green}$PREV_COMM from $PREV_COMM_DATE${default}") + CURRENT_COMMIT_DATE=$(git show -s --format=%cd --date=short $CURRENT_COMMIT) + if [ "$PREVIOUS_COMMIT" != "0" ]; then + PREVIOUS_COMMIT_DATE=$(git show -s --format=%cd --date=short $PREVIOUS_COMMIT) + fi + if [ "$PREVIOUS_COMMIT" = "0" ]; then + CURR_UI=$(echo -e "${green}$CURRENT_COMMIT from $CURRENT_COMMIT_DATE${default}") + PREV_UI=$(echo -e "${red}None${default} ") else - CURR_UI=$(echo -e "${yellow}$CURR_COMM from $CURR_COMM_DATE${default}") - PREV_UI=$(echo -e "${yellow}$PREV_COMM from $PREV_COMM_DATE${default}") + if [ "$CURRENT_COMMIT" = "$PREVIOUS_COMMIT" ]; then + CURR_UI=$(echo -e "${green}$CURRENT_COMMIT from $CURRENT_COMMIT_DATE${default}") + PREV_UI=$(echo -e "${green}$PREVIOUS_COMMIT from $PREVIOUS_COMMIT_DATE${default}") + else + CURR_UI=$(echo -e "${yellow}$CURRENT_COMMIT from $CURRENT_COMMIT_DATE${default}") + PREV_UI=$(echo -e "${yellow}$PREVIOUS_COMMIT from $PREVIOUS_COMMIT_DATE${default}") + fi fi rollback_ui rollback_klipper } rollback_klipper(){ - if [ "$CURR_COMM" != "$PREV_COMM" ]; then + if [ "$PREVIOUS_COMMIT" != "0" ] && [ "$CURRENT_COMMIT" != "$PREVIOUS_COMMIT" ]; then while true; do echo -e "${cyan}" - read -p "###### Do you want to rollback to $PREV_COMM? (Y/n): " yn + read -p "###### Do you want to rollback to $PREVIOUS_COMMIT? (Y/n): " yn echo -e "${default}" case "$yn" in Y|y|Yes|yes|"") clear print_header - status_msg "Rolling back to $PREV_COMM ..." - git reset --hard $PREV_COMM -q + status_msg "Rolling back to $PREVIOUS_COMMIT ..." + git reset --hard $PREVIOUS_COMMIT -q ok_msg "Rollback complete!"; echo load_klipper_state break;; diff --git a/scripts/set_reverse_proxy.sh b/scripts/set_reverse_proxy.sh new file mode 100755 index 0000000..b9164a1 --- /dev/null +++ b/scripts/set_reverse_proxy.sh @@ -0,0 +1,117 @@ +create_reverse_proxy(){ + if [ "$SET_REVERSE_PROXY" = "true" ]; then + #check for dependencies + dep=(nginx) + dependency_check + #execute operations + status_msg "Creating Nginx configuration for $1 ..." + cat ${HOME}/kiauh/resources/$1_nginx.cfg > ${HOME}/kiauh/resources/$1 + sudo mv ${HOME}/kiauh/resources/$1 /etc/nginx/sites-available/$1 + #ONLY FOR MAINSAIL: replace username if not "pi" + if [ "$1" = "mainsail" ]; then + sudo sed -i "/root/s/pi/${USER}/" /etc/nginx/sites-available/mainsail + fi + ok_msg "Nginx configuration for $1 was set!" + #remove default config + if [ -e /etc/nginx/sites-enabled/default ]; then + sudo rm /etc/nginx/sites-enabled/default + fi + #create symlink for own configs + if [ ! -e /etc/nginx/sites-enabled/$1 ]; then + sudo ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/ + fi + restart_nginx + fi +} + +create_custom_hostname(){ + echo + top_border + echo -e "| You can change the hostname of this machine to use |" + echo -e "| that name to open the Interface in your browser. |" + echo -e "| |" + echo -e "| Example: If you set the hostname to 'my-printer' |" + echo -e "| you can open DWC2/Mainsail/Octoprint by |" + echo -e "| browsing to: http://my-printer.local |" + bottom_border + while true; do + read -p "${cyan}###### Do you want to change the hostname? (y/N):${default} " yn + case "$yn" in + Y|y|Yes|yes) + user_input_hostname + break;; + N|n|No|no|"") break;; + esac + done +} + +user_input_hostname(){ + unset NEW_HOSTNAME + unset HOSTNAME_VALID + unset HOSTENAME_CONFIRM + echo + top_border + echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${default} |" + echo -e "| ${red}No special characters allowed!${default} |" + echo -e "| ${red}No leading or trailing '-' allowed!${default} |" + bottom_border + while true; do + read -p "${cyan}###### Please set the new hostname:${default} " NEW_HOSTNAME + if [[ $NEW_HOSTNAME =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then + ok_msg "'$NEW_HOSTNAME' is a valid hostname!" + HOSTNAME_VALID="true" + while true; do + echo + read -p "${cyan}###### Do you want '$NEW_HOSTNAME' to be the new hostname? (Y/n):${default} " yn + case "$yn" in + Y|y|Yes|yes|"") + echo -e "###### > Yes" + HOSTENAME_CONFIRM="true" + break;; + N|n|No|no) + echo -e "###### > No" + echo -e "${red}Skip hostname change ...${default}" + HOSTENAME_CONFIRM="false" + break;; + esac + done + break + else + warn_msg "'$NEW_HOSTNAME' is not a valid hostname!" + fi + done +} + +set_hostname(){ + if [ "$HOSTNAME_VALID" = "true" ] && [ "$HOSTENAME_CONFIRM" = "true" ]; then + #check for dependencies + dep=(avahi-daemon) + dependency_check + #execute operations + #get current hostname and write to variable + HOSTNAME=$(hostname) + #create host file if missing or create backup of existing one with current date&time + if [ -f /etc/hosts ]; then + status_msg "Creating backup of hosts file ..." + get_date + sudo cp /etc/hosts /etc/hosts."$current_date".bak + ok_msg "Backup done!" + ok_msg "File:'/etc/hosts."$current_date".bak'" + else + sudo touch /etc/hosts + fi + #set hostname in /etc/hostname + status_msg "Setting hostname to '$NEW_HOSTNAME' ..." + status_msg "Please wait ..." + sudo hostnamectl set-hostname "$NEW_HOSTNAME" + #write new hostname to /etc/hosts + status_msg "Writing new hostname to /etc/hosts ..." + if cat /etc/hosts | grep "###set by kiauh" &>/dev/null; then + sudo sed -i "/###set by kiauh/s/\<$HOSTNAME\>/$NEW_HOSTNAME/" /etc/hosts + else + echo "127.0.0.1 $NEW_HOSTNAME ###set by kiauh" | sudo tee -a /etc/hosts &>/dev/null + fi + ok_msg "New hostname successfully configured!" + ok_msg "Remember to reboot for the changes to take effect!" + fi +} diff --git a/scripts/status.sh b/scripts/status.sh index f454f89..a96c79c 100755 --- a/scripts/status.sh +++ b/scripts/status.sh @@ -1,21 +1,14 @@ kiauh_status(){ cd ${HOME}/kiauh git fetch --all -q - if [ $(git branch -a | grep "* master" | cut -d" " -f2) ]; then - CURR_KIAUH_BRANCH="master" - elif [ $(git branch -a | grep "* dev-2.0" | cut -d" " -f2) ]; then - CURR_KIAUH_BRANCH="dev-2.0" - fi - if [ "$CURR_KIAUH_BRANCH" = "master" ]; then - REMOTE_KIAUH_COMMIT=$(git rev-parse --short=8 origin/master) - LOCAL_KIAUH_COMMIT=$(git rev-parse --short=8 HEAD) - if [ "$REMOTE_KIAUH_COMMIT" != "$LOCAL_KIAUH_COMMIT" ]; then + if git branch -a | grep "* master" -q; then + CURR_KIAUH_BRANCH="master" #needed to display branch in UI + if [[ "$(git rev-parse --short=8 origin/master)" != "$(git rev-parse --short=8 HEAD)" ]]; then KIAUH_UPDATE_AVAIL="true" fi - elif [ "$CURR_KIAUH_BRANCH" = "dev-2.0" ]; then - REMOTE_KIAUH_COMMIT=$(git rev-parse --short=8 dev-2.0) - LOCAL_KIAUH_COMMIT=$(git rev-parse --short=8 HEAD) - if [ "$REMOTE_KIAUH_COMMIT" != "$LOCAL_KIAUH_COMMIT" ]; then + elif git branch -a | grep "* dev-2.0" -q; then + CURR_KIAUH_BRANCH="dev-2.0" #needed to display branch in UI + if [[ "$(git rev-parse --short=8 origin/dev-2.0)" != "$(git rev-parse --short=8 HEAD)" ]]; then KIAUH_UPDATE_AVAIL="true" fi fi @@ -118,21 +111,6 @@ octoprint_status(){ fi } -#read_branch(){ -# if [ -d $KLIPPER_DIR ] && [ -d $KLIPPER_DIR/.git ]; then -# cd $KLIPPER_DIR -# GET_BRANCH=$(git branch -a | head -1 | cut -d " " -f5 | cut -d ")" -f1) -# #if reading the branch gives an empty string -# #we are on non-detached HEAD state on origin/master -# #and need to set GET_BRANCH to make a non-empty string -# if [ -z "$GET_BRANCH" ]; then -# GET_BRANCH="origin/master" -# fi -# else -# GET_BRANCH="" -# fi -#} - #reading the log for the last branch that got checked out assuming that this is also the currently active branch. read_branch(){ if [ -d $KLIPPER_DIR/.git ]; then @@ -151,6 +129,8 @@ print_branch(){ read_branch if [ "$GET_BRANCH" == "origin/master" ]; then PRINT_BRANCH="$GET_BRANCH " + elif [ "$GET_BRANCH" == "origin" ]; then + PRINT_BRANCH="origin/master " elif [ "$GET_BRANCH" == "master" ]; then PRINT_BRANCH="origin/master " elif [ "$GET_BRANCH" == "dmbutyugin/scurve-shaping" ]; then diff --git a/scripts/ui.sh b/scripts/ui.sh deleted file mode 100755 index f331ce8..0000000 --- a/scripts/ui.sh +++ /dev/null @@ -1,191 +0,0 @@ -### set up some UI stuff - -#ui total width = 57 chars -top_border(){ - echo -e "/=======================================================\ " -} - -bottom_border(){ - echo -e "\=======================================================/" -} - -hr(){ - echo -e "|-------------------------------------------------------|" -} - -quit_footer(){ - hr - echo -e "| ${red}Q) Quit${default} | " - bottom_border - echo -e " KIAUH: $CURR_KIAUH_BRANCH" -} - -print_header(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~~~~~ [ KIAUH ] ~~~~~~~~~~~~~~~~~") |" - echo -e "| $(title_msg " Klipper Installation And Update Helper ") |" - echo -e "| $(title_msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") |" - bottom_border -} - -main_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~~~ [ Main Menu ] ~~~~~~~~~~~~~~~") |" - hr - echo -e "| 0) [System status] | |" - echo -e "| | Klipper: $KLIPPER_STATUS|" - echo -e "| 1) [Install] | Branch: ${cyan}$PRINT_BRANCH${default}|" - echo -e "| 2) [Update] | |" - echo -e "| 3) [Remove] | DWC2: $DWC2_STATUS|" - echo -e "| | Mainsail: $MAINSAIL_STATUS|" - echo -e "| 4) [Advanced] | Octoprint: $OCTOPRINT_STATUS|" - echo -e "| 5) [Backup] | |" - quit_footer -} - -install_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~") | " - hr - echo -e "| You need this menu usually only for installing | " - echo -e "| all necessary dependencies for the various | " - echo -e "| functions on a completely fresh system. | " - hr - echo -e "| Firmware: | | " - echo -e "| 1) [Klipper] | | " - echo -e "| | | " - echo -e "| Webinterface: | | " - echo -e "| 2) [DWC2] | | " - echo -e "| 3) [Mainsail] | | " - echo -e "| 4) [Octoprint] | | " - quit_footer -} - -update_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~") | " - hr - echo -e "| It is a good idea to check the following website | " - echo -e "| for important software changes to the config file | " - echo -e "| >> BEFORE << updating your klipper installation: | " - echo -e "| | " - echo -e "| ${yellow}https://www.klipper3d.org/Config_Changes.html${default} | " - bottom_border - top_border - echo -e "| 0) $BB4U_STATUS| " - hr - echo -e "| | Local Vers: | Remote Vers: | " - echo -e "| Firmware: | | | " - echo -e "| 1) [Klipper] | $(echo "$LOCAL_COMMIT") | $(echo "$REMOTE_COMMIT") | " - echo -e "| | | | " - echo -e "| Webinterface: |---------------|--------------| " - echo -e "| 2) [DWC2-for-Klipper] | $(echo "$LOCAL_DWC2FK_COMMIT") | $(echo "$REMOTE_DWC2FK_COMMIT") | " - echo -e "| 3) [DWC2 Web UI] | $(echo "$DWC2_LOCAL_VER") | $(echo "$DWC2_REMOTE_VER") | " - echo -e "| |---------------|--------------| " - echo -e "| 4) [Moonraker] | $(echo "$LOCAL_MOONRAKER_COMMIT") | $(echo "$REMOTE_MOONRAKER_COMMIT") | " - echo -e "| 5) [Mainsail] | $(echo "$MAINSAIL_LOCAL_VER") | $(echo "$MAINSAIL_REMOTE_VER") | " - quit_footer -} - -remove_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Remove Menu ] ~~~~~~~~~~~~~~") | " - hr - echo -e "| Files and directories which remain untouched: | " - echo -e "| --> ~/printer.cfg | " - echo -e "| --> ~/kiauh-backups | " - echo -e "| You need remove them manually if you wish so. | " - hr - echo -e "| Firmware: | | " - echo -e "| 1) [Klipper] | 5) [Nginx] | " - echo -e "| | | " - echo -e "| Webinterface: | | " - echo -e "| 2) [DWC2] | | " - echo -e "| 3) [Mainsail] | | " - echo -e "| 4) [Octoprint] | | " - quit_footer -} - -advanced_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~") | " - hr - echo -e "| 0) $OPRINT_SERVICE_STATUS| " - hr - echo -e "| | | " - echo -e "| Klipper: | System: | " - echo -e "| 1) [Switch Version] | 8) [Change hostname] | " - echo -e "| 2) [Rollback] | | " - echo -e "| | Mainsail: | " - echo -e "| Firmware: | 9) [Remove branding] | " - echo -e "| 3) [Build Firmware] | | " - echo -e "| 4) [Flash MCU] | | " - echo -e "| 5) [Get Printer-USB] | | " - echo -e "| 6) [Write Printer-USB] | | " - echo -e "| 7) [Write DWC2 config] | | " - echo -e "| | | " -quit_footer -} - -backup_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Backup Menu ] ~~~~~~~~~~~~~~") | " - hr - echo -e "| ${yellow}Backup location: ~/kiauh-backups${default} | " - hr - echo -e "| Firmware: | " - echo -e "| 1) [Klipper] | " - echo -e "| | " - echo -e "| Webinterface: | " - echo -e "| 2) [DWC2 Web UI] | " - echo -e "| | " - echo -e "| 3) [Mainsail] | " - echo -e "| 4) [Moonraker] | " - echo -e "| | " - echo -e "| 5) [OctoPrint] | " - echo -e "| | " - quit_footer -} - -switch_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~ [ Switch Klipper Branch ] ~~~~~~~~~") |" - bottom_border - echo - echo -e " $(title_msg "Active Branch: ")${green}$GET_BRANCH${default}" - echo - top_border - echo -e "| 1) [--> origin/master] | " - echo -e "| | " - echo -e "| 2) [--> scurve-shaping] | " - echo -e "| 3) [--> scurve-smoothing] | " - echo -e "| | " - echo -e "| 4) [--> moonraker] | " - quit_footer -} - -kiauh_update_msg(){ - top_border - echo -e "| ${yellow}There is a newer version of this script available!${default} | " - echo -e "| ${yellow}Type 'update' if you want to update KIAUH now.${default} | " - bottom_border -} - -rollback_ui(){ - top_border - echo -e "| $(title_msg "~~~~~~~~~~~~~ [ Rollback Menu ] ~~~~~~~~~~~~~") | " - hr - echo -e "| If serious errors occured after updating Klipper, | " - echo -e "| you can use this menu to return to the previously | " - echo -e "| used commit from which you have updated. | " - bottom_border - top_border - echo -e "| Active branch: ${green}$PRINT_BRANCH${default} | " - hr - echo -e "| Currently on commit: | " - echo -e "| $CURR_UI | " - hr - echo -e "| Commit last updated from: | " - echo -e "| $PREV_UI | " - quit_footer -} \ No newline at end of file diff --git a/scripts/ui/advanced_menu.sh b/scripts/ui/advanced_menu.sh new file mode 100755 index 0000000..9508328 --- /dev/null +++ b/scripts/ui/advanced_menu.sh @@ -0,0 +1,205 @@ +advanced_ui(){ + top_border + echo -e "| ${yellow}~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~${default} | " + hr + echo -e "| 0) $OPRINT_SERVICE_STATUS| " + hr + echo -e "| | | " + echo -e "| Klipper: | System: | " + echo -e "| 1) [Switch Version] | 8) [Change hostname] | " + echo -e "| 2) [Rollback] | | " + echo -e "| | Mainsail: | " + echo -e "| Firmware: | 9) [Remove branding] | " + echo -e "| 3) [Build Firmware] | | " + echo -e "| 4) [Flash MCU] | Extensions: | " + echo -e "| 5) [Get Printer-USB] | 10) [Shell Command] | " + echo -e "| 6) [Write Printer-USB] | | " + echo -e "| 7) [Write DWC2 config] | | " + echo -e "| | | " +quit_footer +} + +advanced_menu(){ + print_header + print_msg && clear_msg + read_octoprint_service_status + advanced_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 0) + clear + print_header + toggle_octoprint_service + read_octoprint_service_status + print_msg && clear_msg + advanced_ui;; + 1) + clear + print_header + switch_menu + print_msg && clear_msg + advanced_ui;; + 2) + clear + print_header + load_klipper_state + print_msg && clear_msg + advanced_ui;; + 3) + clear + print_header + build_fw + print_msg && clear_msg + advanced_ui;; + 4) + clear + print_header + flash_routine + print_msg && clear_msg + advanced_ui;; + 5) + clear + print_header + get_printer_usb + print_msg && clear_msg + advanced_ui;; + 6) + clear + print_header + get_printer_usb && write_printer_usb + print_msg && clear_msg + advanced_ui;; + 7) + clear + print_header + create_dwc2fk_cfg + print_msg && clear_msg + advanced_ui;; + 8) + clear + print_header + create_custom_hostname + print_msg && clear_msg + advanced_ui;; + 9) + clear + print_header + remove_branding + print_msg && clear_msg + advanced_ui;; + 10) + clear + print_header + install_extension_shell_command + print_msg && clear_msg + advanced_ui;; + Q|q) + clear; main_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + advanced_ui;; + esac + done + advanced_menu +} + +############################################################# +############################################################# + +switch_ui(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~ [ Switch Klipper Branch ] ~~~~~~~~~") |" + bottom_border + echo + echo -e " $(title_msg "Active Branch: ")${green}$GET_BRANCH${default}" + echo + top_border + echo -e "| 1) [--> origin/master] | " + echo -e "| | " + echo -e "| 2) [--> scurve-shaping] | " + echo -e "| 3) [--> scurve-smoothing] | " + echo -e "| | " + echo -e "| 4) [--> moonraker] | " + quit_footer +} + +switch_menu(){ + if [ -d $KLIPPER_DIR ]; then + read_branch + print_msg && clear_msg + switch_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 1) + clear + print_header + switch_to_origin + read_branch + print_msg && clear_msg + switch_ui;; + 2) + clear + print_header + switch_to_scurve_shaping + read_branch + print_msg && clear_msg + switch_ui;; + 3) + clear + print_header + switch_to_scurve_smoothing + read_branch + print_msg && clear_msg + switch_ui;; + 4) + clear + print_header + switch_to_moonraker + read_branch + print_msg && clear_msg + switch_ui;; + Q|q) + clear; advanced_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + switch_ui;; + esac + done + else + ERROR_MSG="No Klipper directory found! Download Klipper first!" + fi +} + +############################################################# +############################################################# + +rollback_ui(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~~~~~ [ Rollback Menu ] ~~~~~~~~~~~~~") | " + hr + echo -e "| If serious errors occured after updating Klipper, | " + echo -e "| you can use this menu to return to the previously | " + echo -e "| used commit from which you have updated. | " + bottom_border + top_border + echo -e "| Active branch: ${green}$PRINT_BRANCH${default} | " + hr + echo -e "| Currently on commit: | " + echo -e "| $CURR_UI | " + hr + echo -e "| Commit last updated from: | " + echo -e "| $PREV_UI | " + quit_footer +} diff --git a/scripts/ui/backup_menu.sh b/scripts/ui/backup_menu.sh new file mode 100755 index 0000000..5738821 --- /dev/null +++ b/scripts/ui/backup_menu.sh @@ -0,0 +1,71 @@ +backup_ui(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Backup Menu ] ~~~~~~~~~~~~~~") | " + hr + echo -e "| ${yellow}Backup location: ~/kiauh-backups${default} | " + hr + echo -e "| Firmware: | " + echo -e "| 1) [Klipper] | " + echo -e "| | " + echo -e "| Webinterface: | " + echo -e "| 2) [DWC2 Web UI] | " + echo -e "| | " + echo -e "| 3) [Mainsail] | " + echo -e "| 4) [Moonraker] | " + echo -e "| | " + echo -e "| 5) [OctoPrint] | " + echo -e "| | " + quit_footer +} + +backup_menu(){ + print_header + print_msg && clear_msg + backup_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 1) + clear + print_header + backup_klipper + print_msg && clear_msg + backup_ui;; + 2) + clear + print_header + backup_dwc2 + print_msg && clear_msg + backup_ui;; + 3) + clear + print_header + backup_mainsail + print_msg && clear_msg + backup_ui;; + 4) + clear + print_header + backup_moonraker + print_msg && clear_msg + backup_ui;; + 5) + clear + print_header + backup_octoprint + print_msg && clear_msg + backup_ui;; + Q|q) + clear; main_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + backup_ui;; + esac + done + backup_menu +} diff --git a/scripts/ui/general_ui.sh b/scripts/ui/general_ui.sh new file mode 100755 index 0000000..865af8d --- /dev/null +++ b/scripts/ui/general_ui.sh @@ -0,0 +1,34 @@ +#ui total width = 57 chars +top_border(){ + echo -e "/=======================================================\ " +} + +bottom_border(){ + echo -e "\=======================================================/" +} + +hr(){ + echo -e "|-------------------------------------------------------|" +} + +quit_footer(){ + hr + echo -e "| ${red}Q) Quit${default} | " + bottom_border + echo -e " KIAUH: $CURR_KIAUH_BRANCH" +} + +print_header(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~~~~~~~~~ [ KIAUH ] ~~~~~~~~~~~~~~~~~") |" + echo -e "| $(title_msg " Klipper Installation And Update Helper ") |" + echo -e "| $(title_msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") |" + bottom_border +} + +kiauh_update_msg(){ + top_border + echo -e "| ${yellow}There is a newer version of this script available!${default} | " + echo -e "| ${yellow}Type 'update' if you want to update KIAUH now.${default} | " + bottom_border +} diff --git a/scripts/ui/install_menu.sh b/scripts/ui/install_menu.sh new file mode 100755 index 0000000..8e0ec75 --- /dev/null +++ b/scripts/ui/install_menu.sh @@ -0,0 +1,66 @@ +install_ui(){ + top_border + echo -e "| ${green}~~~~~~~~~~~ [ Installation Menu ] ~~~~~~~~~~~${default} | " + hr + echo -e "| You need this menu usually only for installing | " + echo -e "| all necessary dependencies for the various | " + echo -e "| functions on a completely fresh system. | " + hr + echo -e "| Firmware: | Webinterface: | " + echo -e "| 1) [Klipper] | 3) [DWC2] | " + echo -e "| | 4) [Mainsail] | " + echo -e "| Klipper API: | 5) [Octoprint] | " + echo -e "| 2) [Moonraker] | | " + quit_footer +} + +install_menu(){ + print_header + install_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 1) + clear + print_header + install_klipper + print_msg && clear_msg + install_ui;; + 2) + clear + print_header + install_moonraker + print_msg && clear_msg + install_ui;; + 3) + clear + print_header + install_dwc2 + print_msg && clear_msg + install_ui;; + 4) + clear + print_header + INST_MAINSAIL="true" && install_mainsail + print_msg && clear_msg + install_ui;; + 5) + clear + print_header + install_octoprint + print_msg && clear_msg + install_ui;; + Q|q) + clear; main_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + install_ui;; + esac + done + install_menu +} diff --git a/scripts/ui/main_menu.sh b/scripts/ui/main_menu.sh new file mode 100755 index 0000000..07d86c4 --- /dev/null +++ b/scripts/ui/main_menu.sh @@ -0,0 +1,79 @@ +main_ui(){ + top_border + echo -e "| $(title_msg "~~~~~~~~~~~~~~~ [ Main Menu ] ~~~~~~~~~~~~~~~") |" + hr + echo -e "| | |" + echo -e "| 1) [Install] | Klipper: $KLIPPER_STATUS|" + echo -e "| 2) [Update] | Branch: ${cyan}$PRINT_BRANCH${default}|" + echo -e "| 3) [Remove] | |" + echo -e "| | DWC2: $DWC2_STATUS|" + echo -e "| 4) [Advanced] | Mainsail: $MAINSAIL_STATUS|" + echo -e "| 5) [Backup] | Octoprint: $OCTOPRINT_STATUS|" + echo -e "| | |" + quit_footer +} + +main_menu(){ + print_header + #print KIAUH update msg if update available + if [ "$KIAUH_UPDATE_AVAIL" = "true" ]; then + kiauh_update_msg + fi + #check install status + klipper_status + dwc2_status + mainsail_status + octoprint_status + print_branch + print_msg && clear_msg + main_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + update) + clear + print_header + update_kiauh + print_msg && clear_msg + main_ui;; + 0) + clear + print_header + ERROR_MSG="Sorry this function is not implemented yet!" + print_msg && clear_msg + 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;; + Q|q) + echo -e "${green}###### Happy printing! ######${default}"; echo + exit -1;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + main_ui;; + esac + done + clear; main_menu +} diff --git a/scripts/ui/remove_menu.sh b/scripts/ui/remove_menu.sh new file mode 100755 index 0000000..f0692b5 --- /dev/null +++ b/scripts/ui/remove_menu.sh @@ -0,0 +1,76 @@ +remove_ui(){ + top_border + echo -e "| ${red}~~~~~~~~~~~~~~ [ Remove Menu ] ~~~~~~~~~~~~~~${default} | " + hr + echo -e "| Files and directories which remain untouched: | " + echo -e "| --> ~/printer.cfg | " + echo -e "| --> ~/klipper_config | " + echo -e "| --> ~/kiauh-backups | " + echo -e "| You need remove them manually if you wish so. | " + hr + echo -e "| Firmware: | Webinterface: | " + echo -e "| 1) [Klipper] | 3) [DWC2] | " + echo -e "| | 4) [Mainsail] | " + echo -e "| Klipper API: | 5) [Octoprint] | " + echo -e "| 2) [Moonraker] | | " + echo -e "| | Webserver: | " + echo -e "| | 6) [Nginx] | " + quit_footer +} + +remove_menu(){ + print_header + remove_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 1) + clear + print_header + remove_klipper + print_msg && clear_msg + remove_ui;; + 2) + clear + print_header + remove_moonraker + print_msg && clear_msg + remove_ui;; + 3) + clear + print_header + remove_dwc2 + print_msg && clear_msg + remove_ui;; + 4) + clear + print_header + remove_mainsail + print_msg && clear_msg + remove_ui;; + 5) + clear + print_header + remove_octoprint + print_msg && clear_msg + remove_ui;; + 6) + clear + print_header + remove_nginx + print_msg && clear_msg + remove_ui;; + Q|q) + clear; main_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + remove_ui;; + esac + done + remove_menu +} diff --git a/scripts/ui/update_menu.sh b/scripts/ui/update_menu.sh new file mode 100755 index 0000000..928311e --- /dev/null +++ b/scripts/ui/update_menu.sh @@ -0,0 +1,87 @@ +update_ui(){ + top_border + echo -e "| ${green}~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~${default} | " + hr + echo -e "| It is a good idea to check the following website | " + echo -e "| for important software changes to the config file | " + echo -e "| >> BEFORE << updating your klipper installation: | " + echo -e "| | " + echo -e "| ${yellow}https://www.klipper3d.org/Config_Changes.html${default} | " + bottom_border + top_border + echo -e "| 0) $BB4U_STATUS| " + hr + echo -e "| | Local Vers: | Remote Vers: | " + echo -e "| Firmware: | | | " + echo -e "| 1) [Klipper] | $(echo "$LOCAL_COMMIT") | $(echo "$REMOTE_COMMIT") | " + echo -e "| | | | " + echo -e "| Webinterface: |---------------|--------------| " + echo -e "| 2) [DWC2-for-Klipper] | $(echo "$LOCAL_DWC2FK_COMMIT") | $(echo "$REMOTE_DWC2FK_COMMIT") | " + echo -e "| 3) [DWC2 Web UI] | $(echo "$DWC2_LOCAL_VER") | $(echo "$DWC2_REMOTE_VER") | " + echo -e "| |---------------|--------------| " + echo -e "| 4) [Moonraker] | $(echo "$LOCAL_MOONRAKER_COMMIT") | $(echo "$REMOTE_MOONRAKER_COMMIT") | " + echo -e "| 5) [Mainsail] | $(echo "$MAINSAIL_LOCAL_VER") | $(echo "$MAINSAIL_REMOTE_VER") | " + quit_footer +} + +update_menu(){ + print_header + #compare versions + ui_print_versions + print_msg && clear_msg + read_bb4u_stat + update_ui + while true; do + echo -e "${cyan}" + read -p "Perform action: " action; echo + echo -e "${default}" + case "$action" in + 0) + clear + print_header + toggle_backups + print_msg && clear_msg + update_ui;; + 1) + clear + print_header + update_klipper && ui_print_versions + print_msg && clear_msg + update_ui;; + 2) + clear + print_header + update_dwc2fk && ui_print_versions + print_msg && clear_msg + update_ui;; + 3) + clear + print_header + update_dwc2 && ui_print_versions + print_msg && clear_msg + update_ui;; + 4) + clear + print_header + update_moonraker && ui_print_versions + print_msg && clear_msg + update_ui;; + 5) + clear + print_header + update_mainsail && ui_print_versions + print_msg && clear_msg + update_ui;; + Q|q) + clear; main_menu; break;; + *) + clear + print_header + print_unkown_cmd + print_msg && clear_msg + ui_print_versions + update_ui;; + esac + done + update_menu +} diff --git a/scripts/update.sh b/scripts/update.sh index 65491ca..27cf319 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -18,15 +18,16 @@ update_klipper(){ status_msg "Updating $GET_BRANCH" #fetching origin/master -> error #rewriting origin/master to origin - if [ "$GET_BRANCH" == "origin/master" ]; then + if [ "$GET_BRANCH" = "origin/master" ]; then FETCH_BRANCH="origin" else FETCH_BRANCH=$(echo "$GET_BRANCH" | cut -d "/" -f1) fi status_msg "Fetching from $FETCH_BRANCH" + cd $KLIPPER_DIR git fetch $FETCH_BRANCH -q && ok_msg "Fetch successfull!" - status_msg "Checking out $GET_BRANCH" - git checkout $GET_BRANCH -q && ok_msg "Checkout successfull!" && echo; ok_msg "Update complete!" + status_msg "Checking out $FETCH_BRANCH" + git checkout $FETCH_BRANCH -q && ok_msg "Checkout successfull!" && echo; ok_msg "Update complete!" fi start_klipper; echo }