fix prepare

This commit is contained in:
paukstelis
2022-01-22 11:05:59 -05:00
parent 3c3f541614
commit 3c0f99352e
2 changed files with 396 additions and 400 deletions

View File

@@ -9,14 +9,14 @@ fi
# from stackoverflow.com/questions/3231804 # from stackoverflow.com/questions/3231804
prompt_confirm() { prompt_confirm() {
while true; do while true; do
read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
case $REPLY in case $REPLY in
[yY]) echo ; return 0 ;; [yY]) echo ; return 0 ;;
[nN]) echo ; return 1 ;; [nN]) echo ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input" *) printf " \033[31m %s \n\033[0m" "invalid input"
esac esac
done done
} }
# from unix.stackexchange.com/questions/391293 # from unix.stackexchange.com/questions/391293
log () { log () {
@@ -29,408 +29,403 @@ log () {
new_instance () { new_instance () {
echo "$(date) starting instance installation" >> $logfile echo "$(date) starting instance installation" | log
if [ $SUDO_USER ]; then user=$SUDO_USER; fi if [ $SUDO_USER ]; then user=$SUDO_USER; fi
SCRIPTDIR=$(dirname $(readlink -f $0)) SCRIPTDIR=$(dirname $(readlink -f $0))
PIDEFAULT="/home/$user/oprint/bin/octoprint" PIDEFAULT="/home/$user/oprint/bin/octoprint"
BUDEFAULT="/home/$user/OctoPrint/bin/octoprint" BUDEFAULT="/home/$user/OctoPrint/bin/octoprint"
OTHERDEFAULT="" OTHERDEFAULT=""
PS3='Installation type: ' PS3='Installation type: '
options=("OctoPi" "OctoBuntu" "Other" "Quit") options=("OctoPi" "OctoBuntu" "Other" "Quit")
select opt in "${options[@]}" select opt in "${options[@]}"
do do
case $opt in case $opt in
"OctoPi") "OctoPi")
DAEMONPATH=$PIDEFAULT DAEMONPATH=$PIDEFAULT
INSTALL=1 INSTALL=1
break break
;; ;;
"OctoBuntu") "OctoBuntu")
DAEMONPATH=$BUDEFAULT DAEMONPATH=$BUDEFAULT
INSTALL=2 INSTALL=2
break break
;; ;;
"Other") "Other")
DAEMONPATH=$OTHERDEFAULT DAEMONPATH=$OTHERDEFAULT
break break
;; ;;
"Quit") "Quit")
exit 1 exit 1
;; ;;
*) echo "invalid option $REPLY";; *) echo "invalid option $REPLY";;
esac esac
done done
echo "UNPLUG PRINTER YOU ARE INSTALLING NOW (other printers can remain)" echo "UNPLUG PRINTER YOU ARE INSTALLING NOW (other printers can remain)"
echo "Enter the name for new printer/instance (no spaces):" echo "Enter the name for new printer/instance (no spaces):"
read INSTANCE read INSTANCE
if [ -z "$INSTANCE" ]; then if [ -z "$INSTANCE" ]; then
echo "No instance given. Exiting" | log echo "No instance given. Exiting" | log
exit 1 exit 1
fi
if test -f "/etc/systemd/system/$INSTANCE.service"; then
echo "Already have an entry for $INSTANCE. Exiting." | log
exit 1
fi
echo "Port on which this instance will run (ENTER will increment from last value in /etc/octoprint_instances):"
read PORT
if [ -z "$PORT" ]; then
PORT=$(tail -1 /etc/octoprint_instances | sed -n -e 's/^.*\(port:\)\(.*\)/\2/p')
if [ -z "$PORT" ]; then
PORT=5000
fi fi
PORT=$((PORT+1)) if test -f "/etc/systemd/system/$INSTANCE.service"; then
echo Selected port is: $PORT | log echo "Already have an entry for $INSTANCE. Exiting." | log
exit 1
fi
fi echo "Port on which this instance will run (ENTER will increment from last value in /etc/octoprint_instances):"
read PORT
if [ -z "$PORT" ]; then
PORT=$(tail -1 /etc/octoprint_instances | sed -n -e 's/^.*\(port:\)\(.*\)/\2/p')
if [ -f /etc/octoprint_instances ]; then if [ -z "$PORT" ]; then
if grep -q $PORT /etc/octoprint_instances; then PORT=5000
echo "Port may be in use! Check /etc/octoprint_instances and select a different port. Exiting." | log fi
exit 1
fi
fi
#collect user, basedir path, daemon path PORT=$((PORT+1))
echo "Octoprint Daemon User [$user]:" echo Selected port is: $PORT | log
read OCTOUSER
if [ -z "$OCTOUSER" ]; then
OCTOUSER=$user
fi
echo "Octoprint Executable Daemon Path [$DAEMONPATH]:" fi
read OCTOPATH
if [ -z "$OCTOPATH" ]; then
OCTOPATH=$DAEMONPATH
fi
if [ -f "$OCTOPATH" ]; then if [ -f /etc/octoprint_instances ]; then
echo "Executable path is valid" | log if grep -q $PORT /etc/octoprint_instances; then
else echo "Port may be in use! Check /etc/octoprint_instances and select a different port. Exiting." | log
echo "Exectuable path is not valid! Aborting" | log exit 1
exit 1 fi
fi fi
echo "Octoprint Config Path (where the hidden instance directory will be) [/home/$user/]:" #collect user, basedir path, daemon path
read OCTOCONFIG echo "Octoprint Daemon User [$user]:"
if [ -z "$OCTOCONFIG" ]; then read OCTOUSER
OCTOCONFIG="/home/$user/" if [ -z "$OCTOUSER" ]; then
fi OCTOUSER=$user
fi
#octoprint_base is the generic .octoprint folder that contains all configuration, upload, etc. echo "Octoprint Executable Daemon Path [$DAEMONPATH]:"
echo "Octoprint instance template path [/home/$user/.octoprint]:" read OCTOPATH
read BFOLD if [ -z "$OCTOPATH" ]; then
if [ -z "$BFOLD" ]; then OCTOPATH=$DAEMONPATH
BFOLD="/home/$user/.octoprint" fi
fi
if [ -d "$BFOLD" ]; then if [ -f "$OCTOPATH" ]; then
echo "Template path is valid" | log echo "Executable path is valid" | log
else else
echo "Template path is not valid! Aborting" | log echo "Exectuable path is not valid! Aborting" | log
exit 1 exit 1
fi fi
#check to make sure first run is complete echo "Octoprint Config Path (where the hidden instance directory will be) [/home/$user/]:"
if grep -q 'firstRun: true' $BFOLD/config.yaml; then read OCTOCONFIG
echo "WARNING!! You must setup the base profile and admin user before continuing" | log if [ -z "$OCTOCONFIG" ]; then
exit 1 OCTOCONFIG="/home/$user/"
fi fi
if prompt_confirm "Begin auto-detect printer serial number for udev entry?" #octoprint_base is the generic .octoprint folder that contains all configuration, upload, etc.
then echo "Octoprint instance template path [/home/$user/.octoprint]:"
echo read BFOLD
#clear out journalctl - probably a better way to do this if [ -z "$BFOLD" ]; then
journalctl --rotate > /dev/null 2>&1 BFOLD="/home/$user/.octoprint"
journalctl --vacuum-time=1seconds > /dev/null 2>&1 fi
echo "Plug your printer in via USB now (detection time-out in 1 min)"
counter=0 if [ -d "$BFOLD" ]; then
while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do echo "Template path is valid" | log
UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p') else
TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p') echo "Template path is not valid! Aborting" | log
counter=$(( $counter + 1 )) exit 1
done fi
else
echo "OK. Restart when you are ready" | log; exit 0 #check to make sure first run is complete
fi if grep -q 'firstRun: true' $BFOLD/config.yaml; then
echo "WARNING!! You must setup the base profile and admin user before continuing" | log
exit 1
fi
if prompt_confirm "Begin auto-detect printer serial number for udev entry?"
then
echo
#clear out journalctl - probably a better way to do this
journalctl --rotate > /dev/null 2>&1
journalctl --vacuum-time=1seconds > /dev/null 2>&1
echo "Plug your printer in via USB now (detection time-out in 1 min)"
counter=0
while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do
UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p')
TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p')
counter=$(( $counter + 1 ))
done
else
echo "OK. Restart when you are ready" | log; exit 0
fi
if [ -z "$UDEV" ]; then if [ -z "$UDEV" ]; then
echo "Printer Serial Number not detected" echo "Printer Serial Number not detected"
prompt_confirm "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now" || exit 0 prompt_confirm "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now" || exit 0
#if [[ $REPLY =~ ^[Yy]$ ]]; then echo
echo USB=$TEMPUSB
USB=$TEMPUSB echo "Your printer will be setup at the following usb address: $USB" | log
echo "Your printer will be setup at the following usb address:" echo
echo $USB | log else
echo echo "Serial number detected as: $UDEV" | log
else fi
echo "Serial number detected as: $UDEV" | log
fi
echo echo
#USB cameras #USB cameras
if [[ -n $INSTALL ]]; then if [[ -n $INSTALL ]]; then
if prompt_confirm "Would you like to auto detect an associated USB camera (experimental)?" if prompt_confirm "Would you like to auto detect an associated USB camera (experimental)?"
then then
add_camera add_camera
fi fi
fi fi
echo echo
if prompt_confirm "Ready to write all changes. Do you want to proceed?" if prompt_confirm "Ready to write all changes. Do you want to proceed?"
then then
cat $SCRIPTDIR/octoprint_generic.service | \ cat $SCRIPTDIR/octoprint_generic.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \ sed -e "s/OCTOUSER/$OCTOUSER/" \
-e "s#OCTOPATH#$OCTOPATH#" \ -e "s#OCTOPATH#$OCTOPATH#" \
-e "s#OCTOCONFIG#$OCTOCONFIG#" \ -e "s#OCTOCONFIG#$OCTOCONFIG#" \
-e "s/NEWINSTANCE/$INSTANCE/" \ -e "s/NEWINSTANCE/$INSTANCE/" \
-e "s/NEWPORT/$PORT/" > /etc/systemd/system/$INSTANCE.service -e "s/NEWPORT/$PORT/" > /etc/systemd/system/$INSTANCE.service
#If a default octoprint service exists, stop and disable it #If a default octoprint service exists, stop and disable it
if [ -f "/etc/systemd/system/octoprint_default.service" ]; then if [ -f "/etc/systemd/system/octoprint_default.service" ]; then
systemctl stop octoprint_default.service systemctl stop octoprint_default.service
systemctl disable octoprint_default.service systemctl disable octoprint_default.service
fi fi
#stop and disable default octoprint service (octopi) #stop and disable default octoprint service (octopi)
if [ -f "/etc/systemd/system/octoprint.service" ]; then if [ -f "/etc/systemd/system/octoprint.service" ]; then
systemctl stop octoprint.service systemctl stop octoprint.service
systemctl disable octoprint.service systemctl disable octoprint.service
fi fi
#Printer udev identifier technique - either Serial number or USB port #Printer udev identifier technique - either Serial number or USB port
#Serial Number #Serial Number
if [ -n "$UDEV" ]; then if [ -n "$UDEV" ]; then
echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi fi
#USB port #USB port
if [ -n "$USB" ]; then if [ -n "$USB" ]; then
echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi fi
#just to be on the safe side, add user to dialout and video #just to be on the safe side, add user to dialout and video
usermod -a -G dialout,video $OCTOUSER usermod -a -G dialout,video $OCTOUSER
#Append instance name to list for removal tool #Append instance name to list for removal tool
echo instance:$INSTANCE port:$PORT >> /etc/octoprint_instances echo instance:$INSTANCE port:$PORT >> /etc/octoprint_instances
#copy all files to our new directory #copy all files to our new directory
cp -rp $BFOLD $OCTOCONFIG/.$INSTANCE cp -rp $BFOLD $OCTOCONFIG/.$INSTANCE
#Do config.yaml modifications here if needed.. #Do config.yaml modifications here if needed..
cat $BFOLD/config.yaml | sed -e "s/INSTANCE/$INSTANCE/" > $OCTOCONFIG/.$INSTANCE/config.yaml cat $BFOLD/config.yaml | sed -e "s/INSTANCE/$INSTANCE/" > $OCTOCONFIG/.$INSTANCE/config.yaml
#MAJOR WORKAROUND - for some reason this will not cat and sed directly into systemd/system. no idea why. create and mv for now #MAJOR WORKAROUND - for some reason this will not cat and sed directly into systemd/system. no idea why. create and mv for now
if [[ -n $CAM || -n $USBCAM ]]; then if [[ -n $CAM || -n $USBCAM ]]; then
cat $SCRIPTDIR/octocam_generic.service | \ cat $SCRIPTDIR/octocam_generic.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \ sed -e "s/OCTOUSER/$OCTOUSER/" \
-e "s/OCTOCAM/cam_$INSTANCE/" \ -e "s/OCTOCAM/cam_$INSTANCE/" \
-e "s/RESOLUTION/$RESOLUTION/" \ -e "s/RESOLUTION/$RESOLUTION/" \
-e "s/FRAMERATE/$FRAMERATE/" \ -e "s/FRAMERATE/$FRAMERATE/" \
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service -e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service
mv $SCRIPTDIR/cam_$INSTANCE.service /etc/systemd/system/ mv $SCRIPTDIR/cam_$INSTANCE.service /etc/systemd/system/
echo $CAMPORT >> /etc/camera_ports echo $CAMPORT >> /etc/camera_ports
#config.yaml modifications #config.yaml modifications
echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $OCTOCONFIG/.$INSTANCE/config.yaml echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo echo
fi fi
#Octobuntu Cameras udev identifier - either Serial number or USB port #Octobuntu Cameras udev identifier - either Serial number or USB port
#Serial Number #Serial Number
if [ -n "$CAM" ]; then if [ -n "$CAM" ]; then
echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi fi
#USB port camera #USB port camera
if [ -n "$USBCAM" ]; then if [ -n "$USBCAM" ]; then
#echo KERNELS==\"$USBCAM\",SUBSYSTEMS==\"video4linux\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules #echo KERNELS==\"$USBCAM\",SUBSYSTEMS==\"video4linux\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\",SUBSYSTEMS==\"usb\",DRIVERS==\"uvcvideo\",SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\",SUBSYSTEMS==\"usb\",DRIVERS==\"uvcvideo\",SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi fi
#Reset udev #Reset udev
udevadm control --reload-rules udevadm control --reload-rules
udevadm trigger udevadm trigger
systemctl daemon-reload systemctl daemon-reload
sleep 1 sleep 1
#Start and enable system processes #Start and enable system processes
systemctl start $INSTANCE systemctl start $INSTANCE
systemctl enable $INSTANCE systemctl enable $INSTANCE
if [[ -n $CAM || -n $USBCAM ]]; then if [[ -n $CAM || -n $USBCAM ]]; then
systemctl start cam_$INSTANCE.service systemctl start cam_$INSTANCE.service
systemctl enable cam_$INSTANCE.service systemctl enable cam_$INSTANCE.service
fi fi
#if we are on octopi, add in haproxy entry #if we are on octopi, add in haproxy entry
if [ $INSTALL = 1 ]; then if [ $INSTALL = 1 ]; then
#find frontend line, do insert #find frontend line, do insert
sed -i "/option forwardfor except 127.0.0.1/a\ use_backend $INSTANCE if { path_beg /$INSTANCE/ }" /etc/haproxy/haprox.conf sed -i "/option forwardfor except 127.0.0.1/a\ use_backend $INSTANCE if { path_beg /$INSTANCE/ }" /etc/haproxy/haprox.conf
#add backend info, bracket with comments so we can remove later if needed #add backend info, bracket with comments so we can remove later if needed
echo '#octoprint_deploy for port $PORT' >> /etc/haproxy/haprox.conf echo '#octoprint_deploy for port $PORT' >> /etc/haproxy/haprox.conf
echo 'backend $INSTANCE' >> /etc/haproxy/haprox.conf echo 'backend $INSTANCE' >> /etc/haproxy/haprox.conf
echo echo
#restart haproxy #restart haproxy
sudo systemctl restart haproxy.service sudo systemctl restart haproxy.service
fi fi
fi fi
} }
add_camera() { add_camera() {
#INSTANCE must be set for this to work #INSTANCE must be set for this to work
echo 'Adding camera' | log echo 'Adding camera' | log
if [ -z "$INSTANCE" ]; then if [ -z "$INSTANCE" ]; then
PS3='Select instance to add camera to: ' PS3='Select instance to add camera to: '
readarray -t options < <(cat /etc/octoprint_instances | sed -n -e 's/^instance:\([[:alnum:]]*\) .*/\1/p') readarray -t options < <(cat /etc/octoprint_instances | sed -n -e 's/^instance:\([[:alnum:]]*\) .*/\1/p')
#Not yet check to see if instance already has a camera
select opt in "${options[@]}" select opt in "${options[@]}"
do do
echo "Selected instance for camera: $opt" | log echo "Selected instance for camera: $opt" | log
INSTANCE = $opt INSTANCE = $opt
break break
done done
fi fi
#clear out journalctl - probably a better way to do this #clear out journalctl - probably a better way to do this
journalctl --rotate > /dev/null 2>&1 journalctl --rotate > /dev/null 2>&1
journalctl --vacuum-time=1seconds > /dev/null 2>&1 journalctl --vacuum-time=1seconds > /dev/null 2>&1
echo "Plug your CAMERA in via USB now (detection time-out in 1 min)" echo "Plug your CAMERA in via USB now (detection time-out in 1 min)"
counter=0 counter=0
while [[ -z "$CAM" ]] && [[ $counter -lt 30 ]]; do while [[ -z "$CAM" ]] && [[ $counter -lt 30 ]]; do
CAM=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p') CAM=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p')
TEMPUSBCAM=$(timeout 1s journalctl -kf | sed -n -e 's|^.*input:.*/\(.*\)/input/input.*|\1|p') TEMPUSBCAM=$(timeout 1s journalctl -kf | sed -n -e 's|^.*input:.*/\(.*\)/input/input.*|\1|p')
counter=$(( $counter + 1 )) counter=$(( $counter + 1 ))
done done
if [ -z "$CAM" ]; then if [ -z "$CAM" ]; then
echo "Camera Serial Number not detected" | log echo "Camera Serial Number not detected" | log
echo "Camera will be setup with physical USB address of $TEMPUSBCAM." | log echo "Camera will be setup with physical USB address of $TEMPUSBCAM." | log
echo "The camera will have to stay plugged into this location." | log echo "The camera will have to stay plugged into this location." | log
USBCAM=$TEMPUSBCAM USBCAM=$TEMPUSBCAM
else else
echo "Camera detected with serial number: $CAM" | log echo "Camera detected with serial number: $CAM" | log
fi fi
echo "Camera Port (ENTER will increment last value in /etc/camera_ports):" echo "Camera Port (ENTER will increment last value in /etc/camera_ports):"
read CAMPORT read CAMPORT
if [ -z "$CAMPORT" ]; then if [ -z "$CAMPORT" ]; then
CAMPORT=$(tail -1 /etc/camera_ports) CAMPORT=$(tail -1 /etc/camera_ports)
if [ -z "$CAMPORT" ]; then if [ -z "$CAMPORT" ]; then
CAMPORT=8000 CAMPORT=8000
fi fi
CAMPORT=$((CAMPORT+1)) CAMPORT=$((CAMPORT+1))
echo Selected port is: $CAMPORT | log echo Selected port is: $CAMPORT | log
fi fi
echo "Settings can be modified after initial setup in /etc/systemd/system/octocam_$INSTANCE" echo "Settings can be modified after initial setup in /etc/systemd/system/octocam_$INSTANCE"
echo echo
echo "Camera Resolution (no sanity check, so get it right) [640x480]:" echo "Camera Resolution (no sanity check, so get it right) [640x480]:"
read RESOLUTION read RESOLUTION
if [ -z "$RESOLUTION" ]; then if [ -z "$RESOLUTION" ]; then
RESOLUTION="640x480" RESOLUTION="640x480"
fi fi
echo "Selected camera resolution: $RESOLUTION" | log echo "Selected camera resolution: $RESOLUTION" | log
#TODO check formating #TODO check formating
echo "Camera Framerate (no sanity check, so get it right) [5]:" echo "Camera Framerate (no sanity check, so get it right) [5]:"
read FRAMERATE read FRAMERATE
if [ -z "$FRAMERATE" ]; then if [ -z "$FRAMERATE" ]; then
FRAMERATE=5 FRAMERATE=5
fi fi
echo "Selected camera framerate: $FRAMERATE" | log echo "Selected camera framerate: $FRAMERATE" | log
#Need to check if this is a one-off install #Need to check if this is a one-off install
} }
remove_instance() { remove_instance() {
if [ $SUDO_USER ]; then user=$SUDO_USER; fi if [ $SUDO_USER ]; then user=$SUDO_USER; fi
echo 'Do not remove the generic instance!' | log #Check to see that octoprint_instances exists before continuing
PS3='Select instance to remove: ' echo 'Do not remove the generic instance!' | log
readarray -t options < <(cat /etc/octoprint_instances | sed -n -e 's/^instance:\([[:alnum:]]*\) .*/\1/p') PS3='Select instance to remove: '
select opt in "${options[@]}" readarray -t options < <(cat /etc/octoprint_instances | sed -n -e 's/^instance:\([[:alnum:]]*\) .*/\1/p')
do select opt in "${options[@]}"
echo "Selected instance to remove: $opt" | log do
break echo "Selected instance to remove: $opt" | log
done break
done
read -p "Do you want to remove everything associated with this instance?" -n 1 -r if prompt_confirm "Do you want to remove everything associated with this instance?"
echo #new line then
if [[ $REPLY =~ ^[Yy]$ ]]; then #disable and remove service file
#disable and remove service file if [ -f /etc/systemd/system/$opt.service ]; then
if [ -f /etc/systemd/system/$opt.service ]; then systemctl stop $opt.service
systemctl stop $opt.service systemctl disable $opt.service
systemctl disable $opt.service rm /etc/systemd/system/$opt.service
rm /etc/systemd/system/$opt.service fi
fi
if [ -f /etc/systemd/system/cam_$opt.service ]; then if [ -f /etc/systemd/system/cam_$opt.service ]; then
systemctl stop cam_$opt.service systemctl stop cam_$opt.service
systemctl disable cam_$opt.service systemctl disable cam_$opt.service
rm /etc/systemd/system/cam_$opt.service rm /etc/systemd/system/cam_$opt.service
sed -i "/cam_$opt/d" /etc/udev/rules.d/99-octoprint.rules sed -i "/cam_$opt/d" /etc/udev/rules.d/99-octoprint.rules
fi fi
#remove udev entry #remove udev entry
sed -i "/$opt/d" /etc/udev/rules.d/99-octoprint.rules sed -i "/$opt/d" /etc/udev/rules.d/99-octoprint.rules
#remove files #remove files
rm -rf /home/$user/.$opt rm -rf /home/$user/.$opt
#remove from octoprint_instances #remove from octoprint_instances
sed -i "/$opt/d" /etc/octoprint_instances sed -i "/$opt/d" /etc/octoprint_instances
fi fi
} }
usb_testing() { usb_testing() {
echo 'USB testing' | log echo 'USB testing' | log
journalctl --rotate > /dev/null 2>&1 journalctl --rotate > /dev/null 2>&1
journalctl --vacuum-time=1seconds > /dev/null 2>&1 journalctl --vacuum-time=1seconds > /dev/null 2>&1
echo "Plug your printer in via USB now (detection time-out in 1 min)" echo "Plug your printer in via USB now (detection time-out in 1 min)"
counter=0 counter=0
while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do
UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p') UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p')
TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p') TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p')
counter=$(( $counter + 1 )) counter=$(( $counter + 1 ))
if [ -n "$TEMPUSB" ]; then if [ -n "$TEMPUSB" ]; then
echo 'Detected devince at $TEMPUSB' | log echo 'Detected devince at $TEMPUSB' | log
fi fi
if [ -n "$UDEV" ]; then if [ -n "$UDEV" ]; then
echo "Serial Number detected: $UDEV" | log echo "Serial Number detected: $UDEV" | log
fi fi
done done
} }
prepare () { prepare () {
echo 'Beginning system preparation' | log echo 'Beginning system preparation' | log
echo 'This only needs to be run once to prepare your system to use octoprint_deploy and it is only relevant for OctoPi images.' echo 'This only needs to be run once to prepare your system to use octoprint_deploy and it is only relevant for OctoPi images.'
echo 'Run this setup and then connect to octopi.local through your browser to setup your admin user.' echo 'Run this setup and then connect to octopi.local through your browser to setup your admin user.'
if prompt_confirm "Ready to begin?" if prompt_confirm "Ready to begin?"
then then
echo #new line echo 'Adding instance records'
if [[ $REPLY =~ ^[Yy]$ ]]; then if [ -f "/etc/octoprint_instances" ]; then
echo 'Adding instance records' echo "octoprint_instances already exists. Trying to run prepare a second time? Exiting" | log
if [ -f "/etc/octoprint_instances" ]; then exit 1
echo "octoprint_instances already exists. Trying to run prepare a second time? Exiting" | log else
exit 1 echo 'instance:generic port:5000' > /etc/octoprint_instances
else fi
echo 'instance:generic port:5000' > /etc/octoprint_instances echo 'Disabling unneeded services....'
fi systemctl disable octoprint.service
echo 'Disabling unneeded services....' systemctl disable webcamd.service
systemctl disable octoprint.service systemctl disable streamer_select.service
systemctl disable webcamd.service echo 'Adding camera port records'
systemctl disable streamer_select.service touch /etc/camera_ports
echo 'Adding camera port records' echo 'Modifying config.yaml'
touch /etc/camera_ports cp -p $SCRIPTDIR/config.basic /home/pi/.octoprint/config.yaml
echo 'Modifying config.yaml'
cp -p $SCRIPTDIR/config.basic /home/pi/.octoprint/config.yaml
fi fi
fi
} }
# initiate logging # initiate logging
@@ -442,26 +437,27 @@ select opt in "${options[@]}"
do do
case $opt in case $opt in
"Prepare system (OctoPi)") "Prepare system (OctoPi)")
prepare
break break
;; ;;
"New instance") "New instance")
new_instance new_instance
break ;; break ;;
"Delete instance") "Delete instance")
remove_instance remove_instance
break break
;; ;;
"Add camera") "Add camera")
add_camera add_camera
break break
;; ;;
"USB port testing") "USB port testing")
usb_testing usb_testing
break break
;; ;;
"Quit") "Quit")
exit 1 exit 1
;; ;;
*) echo "invalid option $REPLY";; *) echo "invalid option $REPLY";;
esac esac
done done

View File

@@ -6,7 +6,7 @@ Wants=network.online.target
[Service] [Service]
User=OCTOUSER User=OCTOUSER
Environment="LD_LIBRARY_PATH=/home/OCTOUSER/mjpg-streamer" Environment="LD_LIBRARY_PATH=/home/OCTOUSER/mjpg-streamer"
ExecStart=/home/OCTOUSER/mjpg-streamer/mjpg_streamer -i "input_uvc.so -d /dev/OCTOCAM -f 5" -o "output_http.so -p CAMPORT" ExecStart=/home/OCTOUSER/mjpg-streamer/mjpg_streamer -i "input_uvc.so -d /dev/OCTOCAM -r RESOLUTION -f FRAMERATE" -o "output_http.so -p CAMPORT"
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target