diff --git a/config.basic b/config.basic index deab693..097e5dd 100644 --- a/config.basic +++ b/config.basic @@ -7,7 +7,7 @@ serial: - /dev/octo_INSTANCE server: commands: - serverRestartCommand: sudo systemctl restart INSTANCE - systemRestartCommand: sudo reboot - systemShutdownCommand: sudo shutdown -n now + serverRestartCommand: s6-svc -r /var/run/s6/services/octoprint +# systemRestartCommand: ssh root@172.18.0.1 reboot +# systemShutdownCommand: ssh root@172.18.0.1 poweroff firstRun: true diff --git a/instance.sh b/instance.sh index be174c6..b4aa6b7 100755 --- a/instance.sh +++ b/instance.sh @@ -38,7 +38,7 @@ new_instance() { done if [ "$firstrun" != "true" ]; then - if test -f "/etc/systemd/system/$INSTANCE.service"; then + if [ -d "/opt/octoprint/$INSTANCE" ]; then echo "Already have an entry for $INSTANCE. Exiting." main_menu fi @@ -143,7 +143,8 @@ new_instance() { cat $SCRIPTDIR/docker-compose.yml | \ sed -e "s/NEWINSTANCE/$INSTANCE/" \ - -e "s/NEWPORT/$PORT/" > /opt/octoprint/docker-compose.yml + -e "s/CAMPORT/$CAMPORT/" > \ + -e "s/NEWPORT/$PORT/" > $OCTOCONFIG/$INSTANCE/docker-compose.yml #write phase if [ -n "$UDEV" ] || [ -n "$USB" ]; then @@ -173,6 +174,9 @@ new_instance() { $BFOLD/* $OCTOCONFIG/$INSTANCE/octoprint/ fi fi + + #build docker + docker compose -f $OCTOCONFIG/$INSTANCE/docker-compose.yml up -d #uniquify instances echo 'Uniquifying instance...' @@ -236,8 +240,8 @@ new_instance() { systemctl daemon-reload sleep 1 - #Start and enable system processes - systemctl start $INSTANCE.service + #restart docker + docker restart $INSTANCE systemctl enable $INSTANCE.service if [[ -n $CAM || -n $USBCAM || -n $BYIDCAM ]]; then systemctl start cam_$INSTANCE.service diff --git a/menu.sh b/menu.sh index c8ec345..095794c 100755 --- a/menu.sh +++ b/menu.sh @@ -9,7 +9,7 @@ yellow=$(echo -en "\e[93m") main_menu() { - VERSION=0.1 + VERSION=0.2 #reset UDEV='' TEMPUSB='' @@ -35,7 +35,7 @@ main_menu() { do case $opt in "Prepare system") - detect_installs + prepare break ;; "Add instance") diff --git a/prepare.sh b/prepare.sh old mode 100644 new mode 100755 index 8e86929..68cb0ef --- a/prepare.sh +++ b/prepare.sh @@ -1,204 +1,5 @@ #!/bin/bash -detect_installs() { - #OctoPi will be the most common so do a search for that: - if [ -f "/etc/octopi_version" ]; then - echo "OctoPi installation detected." - echo "Adding standard OctoPi instance to instance list." - echo "instance:octoprint port:5000 udev:false" >> /etc/octoprint_instances - echo "octoexec: /opt/octoprint/oprint/bin/octoprint" >> /etc/octodocker_deploy - echo "octopip: /opt/octoprint/oprint/bin/pip" >> /etc/octodocker_deploy - echo "haproxy: true" >> /etc/octodocker_deploy - echo "octopi: true" >> /etc/octodocker_deploy - echo "Adding systemctl and reboot to sudo" - echo "$user ALL=NOPASSWD: /usr/bin/systemctl" > /etc/sudoers.d/octoprint_systemctl - echo "$user ALL=NOPASSWD: /usr/sbin/reboot" > /etc/sudoers.d/octoprint_reboot - INSTANCE=octoprint - apt-get update - deb_packages - #rename - - #detect - echo "If you plan to have multiple printers on your Pi it is helpful to assign printer udev rules." - echo "This will make sure the correct printer is associated with each OctoPrint instance." - echo "This can also be done in the Utility menu at a later time." - get_settings - if prompt_confirm "${green}Would you like to generate a udev rule now?${white}"; then - echo "Unplug your printer from the USB connection now." - if prompt_confirm "${green}Ready to begin printer auto-detection?${white}"; then - detect_printer - printer_udev false - printer_udev true - udevadm control --reload-rules - udevadm trigger - docker exec -it $INSTANCE $OCTOEXEC config set serial.port /dev/octo_$INSTANCE - docker exec -it $INSTANCE $OCTOEXEC config append_value serial.additionalPorts "/dev/octo_$INSTANCE" - #convert UDEV to true - sed -i "s/udev:false/udev:true/" /etc/octoprint_instances - systemctl restart $INSTANCE - fi - - fi - #streamer_install - echo "streamer: camera-streamer" >> /etc/octodocker_deploy - main_menu - fi - - echo "Searching home directory for existing OctoPrint venv/binary....." - octopresent=$(find /opt/octoprint/ -type f -executable -print | grep "bin/octoprint") - if [ -n "$octopresent" ]; then - echo "OctoPrint binary found at $octopresent" - echo "Did you try and setup OctoPrint in another way?" - echo "${red}This may cause issues!${white}" - : ' - PS3="${green}Select option number: ${white}" - options=("Use existing binary" "Install most recent OctoPrint" "More information") - select opt in "${options[@]}" - do - case $opt in - "Use existing binary") - OCTOEXEC=$octopresent - break - ;; - "Install most recent OctoPrint") - OCTOEXEC=thing - break - ;; - "More information") - exit 1 - ;; - *) echo "invalid option $REPLY";; - esac - done - ' - else - echo "No OctoPrint binary found in the current user's home directory. Doing complete install." - FULLINSTALL=1 - fi - FULLINSTALL=1 - echo "Looking for existing OctoPrint systemd files....." - #get any service files that have bin/octoprint - readarray -t syspresent < <(fgrep -l bin/octoprint /etc/systemd/system/*.service) - prepare -} - -#https://askubuntu.com/questions/39497 -deb_packages() { - #All extra packages needed can be added here for deb based systems. Only available will be selected. - apt-cache --generate pkgnames \ - | grep --line-regexp --fixed-strings \ - -e make \ - -e v4l-utils \ - -e python-is-python3 \ - -e python3-venv \ - -e python3.9-venv \ - -e python3.10-venv \ - -e python3.11-venv \ - -e python3.11-dev \ - -e virtualenv \ - -e python3-dev \ - -e build-essential \ - -e python3-setuptools \ - -e libyaml-dev \ - -e python3-pip \ - -e cmake \ - -e libjpeg8-dev \ - -e libjpeg62-turbo-dev \ - -e gcc \ - -e g++ \ - -e libevent-dev \ - -e libjpeg-dev \ - -e libbsd-dev \ - -e ffmpeg \ - -e uuid-runtime\ - -e ssh\ - -e libffi-dev\ - -e haproxy\ - -e libavformat-dev\ - -e libavutil-dev\ - -e libavcodec-dev\ - -e libcamera-dev\ - -e libcamera-tools\ - -e libcamera-v4l2\ - -e liblivemedia-dev\ - -e v4l-utils\ - -e pkg-config\ - -e xxd\ - -e build-essential\ - -e libssl-dev\ - -e rsync\ - | xargs apt-get install -y - - #pacakges to REMOVE go here - apt-cache --generate pkgnames \ - | grep --line-regexp --fixed-strings \ - -e brltty \ - | xargs apt-get remove -y - -} - -dnf_packages() { - #untested - dnf install -y \ - gcc \ - python3-devel \ - cmake \ - libjpeg-turbo-devel \ - libbsd-devel \ - libevent-devel \ - haproxy \ - openssh \ - openssh-server \ - libffi-devel \ - libcamera-devel \ - v4l-utils \ - xxd \ - openssl-devel \ - rsync - -} - -pacman_packages() { - pacman -S --noconfirm --needed \ - make \ - cmake \ - python \ - python-virtualenv \ - libyaml \ - python-pip \ - libjpeg-turbo \ - python-yaml \ - python-setuptools \ - libffi \ - ffmpeg \ - gcc \ - libevent \ - libbsd \ - openssh \ - haproxy \ - v4l-utils \ - rsync -} - -zypper_packages() { - zypper in -y \ - gcc \ - python3-devel \ - cmake \ - libjpeg-devel \ - libbsd-devel \ - libevent-devel \ - haproxy \ - openssh \ - openssh-server \ - libffi-devel \ - v4l-utils \ - xxd \ - libopenssl-devel \ - rsync - -} - user_groups() { echo 'Adding current user to dialout and video groups.' @@ -210,6 +11,14 @@ user_groups() { } prepare () { + echo "Checking if Docker is installed....." + if command_exists docker; then + echo "Docker found at `which docker`" + else + echo "Docker not found, will install." + DOCKERINSTALL=1 + fi + echo echo @@ -242,102 +51,42 @@ prepare () { done if prompt_confirm "Ready to begin?"; then - - #CHOICE HERE between new_install or directly to haproxy/streamer based on if existing binary is used - if [ -n "$FULLINSTALL" ]; then - new_install - else - old_install - fi - + install fi main_menu } -old_install() { - echo "octoexec:$octopresent" >> /etc/octodocker_deploy - user_groups - haproxy_install - streamer_install - #add existing instance(s) to /etc/octoprint_instances -} - -new_install() { +install() { user_groups OCTOEXEC=/usr/local/bin/octoprint echo "Adding systemctl and reboot to sudo" echo "$user ALL=NOPASSWD: /usr/bin/systemctl" > /etc/sudoers.d/octoprint_systemctl echo "$user ALL=NOPASSWD: /usr/sbin/reboot" > /etc/sudoers.d/octoprint_reboot echo "This will install necessary packages, install OctoPrint and setup an instance." - #install packages - #All DEB based - PYVERSION="python3" - if [ $INSTALL -eq 2 ]; then - apt-get update > /dev/null - PYV=$(python3 -c"import sys; print(sys.version_info.minor)") - #if [ $PYV -gt 11 ]; then - # PYVERSION='python3.11' - #fi - deb_packages - fi - #Fedora35/CentOS - if [ $INSTALL -eq 3 ]; then - echo "Fedora and variants have SELinux enabled by default." + #SELinux + if command_exists getenforce && [ "$(getenforce)" == "Enforcing" ]; then + echo "SELinux is enabled" echo "This causes a fair bit of trouble for running OctoPrint." echo "You have the option of disabling this now." if prompt_confirm "${green}Disable SELinux?${white}"; then sed -i s/^SELINUX=.*$/SELINUX=disabled/ /etc/selinux/config echo "${magenta}You will need to reboot after system preparation.${white}" fi - systemctl enable sshd.service - PYV=$(python3 -c"import sys; print(sys.version_info.minor)") - if [ $PYV -gt 11 ]; then - dnf -y install python3.11-devel - PYVERSION='python3.11' - fi - dnf_packages - fi - - #ArchLinux - if [ $INSTALL -eq 4 ]; then - pacman_packages - fi - - if [ $INSTALL -eq 5 ]; then - zypper_packages - systemctl enable sshd.service fi + if [ -n "$DOCKERINSTALL" ]; then + curl -sL https://get.docker.com | sh + fi echo "Enabling ssh server..." systemctl enable ssh.service - echo "Installing OctoPrint virtual environment in /opt/octoprint/OctoPrint" - #make venv - $PYVERSION -m venv /opt/octoprint/OctoPrint - #update pip - /opt/octoprint/OctoPrint/bin/pip install --upgrade pip - #pre-install wheel - /opt/octoprint/OctoPrint/bin/pip install wheel - #install oprint - /opt/octoprint/OctoPrint/bin/pip install OctoPrint - - #Check to verify that OctoPrint binary is installed - if [ -f "/opt/octoprint/OctoPrint/bin/octoprint" ]; then - echo "${cyan}OctoPrint apppears to have been installed successfully${white}" - else - echo "${red}WARNING! WARNING! WARNING!${white}" - echo "OctoPrint has not been installed correctly." - echo "Please answer Y to remove everything and try running prepare system again." - remove_everything - exit - fi haproxy_install streamer_install #These will retreived as settings - echo "octoexec: /opt/octoprint/OctoPrint/bin/octoprint" >> /etc/octodocker_deploy - echo "octopip: /opt/octoprint/OctoPrint/bin/pip" >> /etc/octodocker_deploy + echo "octoexec: /usr/local/bin/octoprint" >> /etc/octodocker_deploy + echo "octopip: /usr/local/bin/pip" >> /etc/octodocker_deploy #Create first instance echo @@ -421,12 +170,12 @@ streamer_install() { rm -rf /opt/octoprint/mjpg_streamer 2>/dev/null #install mjpg-streamer, not doing any error checking or anything echo 'Installing mjpeg-streamer' - git -C /opt/octoprint/ clone https://github.com/jacksonliam/mjpg-streamer.git mjpeg + git -C /opt/octoprint/ clone https://github.com/jacksonliam/mjpg-streamer.git mjpeg #apt -y install - make -C /opt/octoprint/mjpeg/mjpg-streamer-experimental > /dev/null + make -C /opt/octoprint/mjpeg/mjpg-streamer-experimental > /dev/null - mv /opt/octoprint/mjpeg/mjpg-streamer-experimental /opt/octoprint/mjpg-streamer - rm -rf /opt/octoprint/mjpeg + mv /opt/octoprint/mjpeg/mjpg-streamer-experimental /opt/octoprint/mjpg-streamer + rm -rf /opt/octoprint/mjpeg if [ -f "/opt/octoprint/mjpg-streamer/mjpg_streamer" ]; then echo "Streamer installed successfully" else @@ -442,8 +191,8 @@ streamer_install() { if [ $VID -eq 2 ]; then rm -rf /opt/octoprint/ustreamer 2>/dev/null #install ustreamer - git -C /opt/octoprint clone --depth=1 https://github.com/pikvm/ustreamer - make -C /opt/octoprint/ustreamer > /dev/null + git -C /opt/octoprint clone --depth=1 https://github.com/pikvm/ustreamer + make -C /opt/octoprint/ustreamer > /dev/null if [ -f "/opt/octoprint/ustreamer/ustreamer" ] || [ -f "/opt/octoprint/ustreamer/ustreamer.bin" ]; then echo "Streamer installed successfully" else @@ -462,8 +211,8 @@ streamer_install() { if [ $VID -eq 4 ]; then rm -rf /opt/octoprint/camera-streamer 2>/dev/null #install camera-streamer - git -C /opt/octoprint clone https://github.com/ayufan-research/camera-streamer.git --recursive - make -C /opt/octoprint/camera-streamer > /dev/null + git -C /opt/octoprint clone https://github.com/ayufan-research/camera-streamer.git --recursive + make -C /opt/octoprint/camera-streamer > /dev/null if [ -f "/opt/octoprint/camera-streamer/camera-streamer" ]; then echo "Streamer installed successfully" else diff --git a/util.sh b/util.sh index 357fc1d..84e0207 100755 --- a/util.sh +++ b/util.sh @@ -403,4 +403,7 @@ diagnostics() { systemctl status haproxy -l --no-pager | log logfile='octodocker_deploy.log' main_menu -} \ No newline at end of file +} + +command_exists() { command -v "$@" > /dev/null 2>&1 } +join_by() { local IFS="$1"; shift; echo "$*"; }