diff --git a/kiauh.sh b/kiauh.sh index 826b88d..9db6058 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -46,6 +46,11 @@ TORNADO_DIR2=${HOME}/klippy-env/lib/python2.7/site-packages/tornado-5.1.1.dist-i MAINSAIL_DIR=${HOME}/mainsail 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 BACKUP_DIR=${HOME}/kiauh-backups PRINTER_CFG=${HOME}/printer.cfg @@ -78,6 +83,7 @@ main_menu(){ klipper_status dwc2_status mainsail_status + octoprint_status print_branch main_ui while true; do @@ -148,6 +154,12 @@ install_menu(){ mainsail_install_routine print_error_msg && ERROR_MSG="" install_ui;; + 4) + clear + print_header + octoprint_install_routine + print_error_msg && ERROR_MSG="" + install_ui;; Q|q) clear; main_menu; break;; *) @@ -238,6 +250,12 @@ remove_menu(){ remove_mainsail print_error_msg && ERROR_MSG="" remove_ui;; + 4) + clear + print_header + remove_octoprint + print_error_msg && ERROR_MSG="" + remove_ui;; Q|q) clear; main_menu; break;; *) diff --git a/scripts/functions.sh b/scripts/functions.sh index d5aef86..71de49f 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -57,6 +57,27 @@ restart_moonraker(){ fi } +start_octoprint(){ + if [ -e /etc/init.d/octoprint ]; then + status_msg "Starting octoprint service ..." + sudo /etc/init.d/octoprint start && sleep 2 && ok_msg "Octoprint service started!" + fi +} + +stop_octoprint(){ + if [ -e /etc/init.d/octoprint ]; then + status_msg "Stopping octoprint service ..." + sudo /etc/init.d/octoprint stop && sleep 2 && ok_msg "Octoprint service stopped!" + fi +} + +restart_octoprint(){ + if [ -e /etc/init.d/octoprint ]; then + status_msg "Restarting octoprint service ..." + sudo /etc/init.d/octoprint restart && sleep 2 && ok_msg "Octoprint service restarted!" + fi +} + dep_check(){ for package in "${dep[@]}" do diff --git a/scripts/install_octoprint.sh b/scripts/install_octoprint.sh new file mode 100755 index 0000000..1a2b29e --- /dev/null +++ b/scripts/install_octoprint.sh @@ -0,0 +1,121 @@ +octoprint_install_routine(){ + #experimental new dependency check + octoprint_dependencies + #execute operations + install_octoprint + add_groups + configure_autostart + add_reboot_permission + load_server +} + +octoprint_dependencies(){ + octo_dep=( + python-pip + python-dev + python-setuptools + python-virtualenv + git + libyaml-dev + build-essential + wget + ) + status_msg "Checking for dependencies ..." + for octo_dep_pgk in "${octo_dep[@]}" + do + if [[ $(dpkg-query -f'${Status}' --show $octo_dep_pgk 2>/dev/null) = *\ installed ]]; then + install+=($octo_dep_pgk) + fi + done + if ! [ ${#install[@]} -eq 0 ]; then + status_msg "Installing dependencies ..." + sudo apt-get install ${install[@]} -y && ok_msg "Dependencies installed!" + else + ok_msg "All dependencies already met!" + fi +} + +install_octoprint(){ + if [ ! -d $OCTOPRINT_DIR ];then + status_msg "Create Octoprint directory ..." + mkdir -p $OCTOPRINT_DIR && ok_msg "Directory created!" + fi + cd $OCTOPRINT_DIR + #create the virtualenv + status_msg "Set up virtualenv ..." + virtualenv venv + source venv/bin/activate + #install octoprint with pip + status_msg "Download and install octoprint ..." + pip install pip --upgrade + pip install --no-cache-dir octoprint + ok_msg "Download complete!" + #leave virtualenv + deactivate +} + +add_groups(){ + USER=$(whoami) + if [[ ! $(groups | grep tty) ]]; then + status_msg "Adding the current user to group 'tty' ..." + sudo usermod -a -G tty $USER && ok_msg "Done!" + fi + if [[ ! $(groups | grep tty) ]]; then + status_msg "Adding the current user to group 'dialout' ..." + sudo usermod -a -G dialout $USER && ok_msg "Done!" + fi +} + +configure_autostart(){ + USER=$(whoami) + cd $OCTOPRINT_DIR + status_msg "Downloading files ..." + wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.init + wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.default + ok_msg "Files downloaded successfully!" + #make necessary changes in default file + status_msg "Configure octoprint service ..." + DEFAULT_FILE=$OCTOPRINT_DIR/octoprint.default + sed -i "s/pi/$USER/g" $DEFAULT_FILE + sed -i "s/#BASEDIR=/BASEDIR=/" $DEFAULT_FILE + sed -i "s/#CONFIGFILE=/CONFIGFILE=/" $DEFAULT_FILE + sed -i "s/#DAEMON=/DAEMON=/" $DEFAULT_FILE + #move files to correct location + sudo mv octoprint.init $OCTOPRINT_SERVICE1 + sudo mv octoprint.default $OCTOPRINT_SERVICE2 + #make file in init.d executable + sudo chmod +x $OCTOPRINT_SERVICE1 + status_msg "Reload systemd configuration files" + sudo update-rc.d octoprint defaults + sudo systemctl daemon-reload + ok_msg "Configuration complete!" + ok_msg "Octoprint installed!" +} + +add_reboot_permission(){ + USER=$(whoami) + #create a backup when file already exists + if [ -f /etc/sudoers.d/octoprint-shutdown ]; then + sudo mv /etc/sudoers.d/octoprint-shutdown /etc/sudoers.d/octoprint-shutdown.old + fi + #create new permission file + status_msg "Add reboot permission to user '$USER' ..." + cd $OCTOPRINT_DIR + echo "$USER ALL=NOPASSWD: /sbin/shutdown" > octoprint-shutdown + sudo chown 0 octoprint-shutdown + sudo mv octoprint-shutdown /etc/sudoers.d/octoprint-shutdown + ok_msg "Permission set!" + sleep 2 +} + +load_server(){ + start_octoprint + #create an octoprint.log symlink in home-dir just for convenience + if [ ! -e ${HOME}/octoprint.log ]; then + status_msg "Creating octoprint.log symlink ..." + ln -s ${HOME}/.octoprint/logs/octoprint.log ${HOME}/octoprint.log && ok_msg "Symlink created!" + fi + ok_msg "Octoprint is now running on:" + ok_msg "$(hostname -I | cut -d " " -f1):5000 or" + ok_msg "http://localhost:5000"; echo +} \ No newline at end of file diff --git a/scripts/remove.sh b/scripts/remove.sh index ab4d92c..445d5e8 100644 --- a/scripts/remove.sh +++ b/scripts/remove.sh @@ -15,7 +15,7 @@ remove_klipper(){ sudo rm -rf /etc/init.d/klipper /etc/default/klipper && ok_msg "Klipper service removed!" fi if [[ -d $KLIPPER_DIR || -d $KLIPPY_ENV_DIR ]]; then - status_msg "Removing klipper and klippy-env diretory ..." + status_msg "Removing klipper and klippy-env directory ..." rm -rf $KLIPPER_DIR $KLIPPY_ENV_DIR && ok_msg "Directories removed!" fi if [[ -L ${HOME}/klippy.log || -e /tmp/klippy.log ]]; then @@ -142,4 +142,36 @@ remove_nginx(){ esac done fi +} + +remove_octoprint(){ + data_arr=( + $OCTOPRINT_SERVICE1 + $OCTOPRINT_SERVICE2 + $OCTOPRINT_DIR + $OCTOPRINT_CFG_DIR + ${HOME}/octoprint.log + /etc/sudoers.d/octoprint-shutdown + ) + print_error "Octoprint" && data_count=() + if [ "$ERROR_MSG" = "" ]; then + stop_octoprint + if [[ -e $OCTOPRINT_SERVICE1 || -e $OCTOPRINT_SERVICE2 ]]; then + status_msg "Removing octoprint service ..." + sudo update-rc.d -f octoprint remove + sudo rm -rf $OCTOPRINT_SERVICE1 $OCTOPRINT_SERVICE2 && ok_msg "Octoprint service removed!" + fi + if [[ -d $OCTOPRINT_DIR || -d $OCTOPRINT_CFG_DIR ]]; then + status_msg "Removing Octoprint and .octoprint directory ..." + rm -rf $OCTOPRINT_DIR $OCTOPRINT_CFG_DIR && ok_msg "Directories removed!" + fi + if [ -f /etc/sudoers.d/octoprint-shutdown ]; then + sudo rm -rf /etc/sudoers.d/octoprint-shutdown + fi + if [ -L ${HOME}/octoprint.log ]; then + status_msg "Removing octoprint.log symlink ..." + rm -rf ${HOME}/octoprint.log && ok_msg "Symlink removed!" + fi + ok_msg "Octoprint successfully removed!"; echo + fi } \ No newline at end of file diff --git a/scripts/status.sh b/scripts/status.sh index be8aae4..04654fa 100644 --- a/scripts/status.sh +++ b/scripts/status.sh @@ -77,6 +77,30 @@ mainsail_status(){ fi } +octoprint_status(){ + ocount=0 + octoprint_data=( + $OCTOPRINT_DIR + $OCTOPRINT_CFG_DIR + $OCTOPRINT_SERVICE1 + $OCTOPRINT_SERVICE2 + ) + #count+1 for each found data-item from array + for op in "${octoprint_data[@]}" + do + if [ -e $op ]; then + ocount=$(expr $ocount + 1) + fi + done + if [ "$ocount" == "${#octoprint_data[*]}" ]; then + OCTOPRINT_STATUS="${green}Installed!${default} " + elif [ "$ocount" == 0 ]; then + OCTOPRINT_STATUS="${red}Not installed!${default} " + else + OCTOPRINT_STATUS="${yellow}Incomplete!${default} " + fi +} + read_branch(){ if [ -d $KLIPPER_DIR ] && [ -d $KLIPPER_DIR/.git ]; then cd $KLIPPER_DIR diff --git a/scripts/ui.sh b/scripts/ui.sh index 19bb106..e724eb9 100644 --- a/scripts/ui.sh +++ b/scripts/ui.sh @@ -37,7 +37,7 @@ main_ui(){ echo -e "| 2) [Update] | |" echo -e "| 3) [Remove] | DWC2: $DWC2_STATUS|" echo -e "| | Mainsail: $MAINSAIL_STATUS|" - echo -e "| 4) [Advanced] | Octoprint: #### WIP #### |" + echo -e "| 4) [Advanced] | Octoprint: $OCTOPRINT_STATUS|" echo -e "| 5) [Backup] | |" quit_footer } @@ -56,7 +56,7 @@ install_ui(){ echo -e "| Webinterface: | | " echo -e "| 2) [DWC2] | | " echo -e "| 3) [Mainsail] | | " - echo -e "| | | " + echo -e "| 4) [Octoprint] | | " quit_footer } @@ -97,7 +97,7 @@ remove_ui(){ echo -e "| 1) [Klipper] | | " echo -e "| 2) [DWC2-for-Klipper] | | " echo -e "| 3) [Mainsail] | | " - echo -e "| | | " + echo -e "| 4) [Octoprint] | | " quit_footer }