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

@@ -8,433 +8,428 @@ 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 () {
if [ -z "$1" ]; then if [ -z "$1" ]; then
cat cat
else else
printf '%s\n' "$@" printf '%s\n' "$@"
fi | tee -a "$logfile" fi | tee -a "$logfile"
} }
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
if [ -f /etc/octoprint_instances ]; then echo "Port on which this instance will run (ENTER will increment from last value in /etc/octoprint_instances):"
if grep -q $PORT /etc/octoprint_instances; then read PORT
echo "Port may be in use! Check /etc/octoprint_instances and select a different port. Exiting." | log if [ -z "$PORT" ]; then
exit 1 PORT=$(tail -1 /etc/octoprint_instances | sed -n -e 's/^.*\(port:\)\(.*\)/\2/p')
fi
fi if [ -z "$PORT" ]; then
PORT=5000
#collect user, basedir path, daemon path fi
echo "Octoprint Daemon User [$user]:"
read OCTOUSER PORT=$((PORT+1))
if [ -z "$OCTOUSER" ]; then echo Selected port is: $PORT | log
OCTOUSER=$user
fi fi
echo "Octoprint Executable Daemon Path [$DAEMONPATH]:" if [ -f /etc/octoprint_instances ]; then
read OCTOPATH if grep -q $PORT /etc/octoprint_instances; then
if [ -z "$OCTOPATH" ]; then echo "Port may be in use! Check /etc/octoprint_instances and select a different port. Exiting." | log
OCTOPATH=$DAEMONPATH exit 1
fi fi
fi
if [ -f "$OCTOPATH" ]; then
echo "Executable path is valid" | log #collect user, basedir path, daemon path
else echo "Octoprint Daemon User [$user]:"
echo "Exectuable path is not valid! Aborting" | log read OCTOUSER
exit 1 if [ -z "$OCTOUSER" ]; then
fi OCTOUSER=$user
fi
echo "Octoprint Config Path (where the hidden instance directory will be) [/home/$user/]:"
read OCTOCONFIG echo "Octoprint Executable Daemon Path [$DAEMONPATH]:"
if [ -z "$OCTOCONFIG" ]; then read OCTOPATH
OCTOCONFIG="/home/$user/" if [ -z "$OCTOPATH" ]; then
fi OCTOPATH=$DAEMONPATH
fi
#octoprint_base is the generic .octoprint folder that contains all configuration, upload, etc.
echo "Octoprint instance template path [/home/$user/.octoprint]:" if [ -f "$OCTOPATH" ]; then
read BFOLD echo "Executable path is valid" | log
if [ -z "$BFOLD" ]; then else
BFOLD="/home/$user/.octoprint" echo "Exectuable path is not valid! Aborting" | log
fi exit 1
fi
if [ -d "$BFOLD" ]; then
echo "Template path is valid" | log echo "Octoprint Config Path (where the hidden instance directory will be) [/home/$user/]:"
else read OCTOCONFIG
echo "Template path is not valid! Aborting" | log if [ -z "$OCTOCONFIG" ]; then
exit 1 OCTOCONFIG="/home/$user/"
fi fi
#check to make sure first run is complete #octoprint_base is the generic .octoprint folder that contains all configuration, upload, etc.
if grep -q 'firstRun: true' $BFOLD/config.yaml; then echo "Octoprint instance template path [/home/$user/.octoprint]:"
echo "WARNING!! You must setup the base profile and admin user before continuing" | log read BFOLD
exit 1 if [ -z "$BFOLD" ]; then
fi BFOLD="/home/$user/.octoprint"
fi
if prompt_confirm "Begin auto-detect printer serial number for udev entry?"
then if [ -d "$BFOLD" ]; then
echo echo "Template path is valid" | log
#clear out journalctl - probably a better way to do this else
journalctl --rotate > /dev/null 2>&1 echo "Template path is not valid! Aborting" | log
journalctl --vacuum-time=1seconds > /dev/null 2>&1 exit 1
echo "Plug your printer in via USB now (detection time-out in 1 min)" fi
counter=0
while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do #check to make sure first run is complete
UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p') if grep -q 'firstRun: true' $BFOLD/config.yaml; then
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 "WARNING!! You must setup the base profile and admin user before continuing" | log
counter=$(( $counter + 1 )) exit 1
done fi
else
echo "OK. Restart when you are ready" | log; exit 0 if prompt_confirm "Begin auto-detect printer serial number for udev entry?"
fi then
echo
#clear out journalctl - probably a better way to do this
if [ -z "$UDEV" ]; then journalctl --rotate > /dev/null 2>&1
echo "Printer Serial Number not detected" journalctl --vacuum-time=1seconds > /dev/null 2>&1
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 echo "Plug your printer in via USB now (detection time-out in 1 min)"
#if [[ $REPLY =~ ^[Yy]$ ]]; then counter=0
echo while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do
USB=$TEMPUSB UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p')
echo "Your printer will be setup at the following usb address:" 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 $USB | log counter=$(( $counter + 1 ))
echo done
else else
echo "Serial number detected as: $UDEV" | log echo "OK. Restart when you are ready" | log; exit 0
fi fi
echo
if [ -z "$UDEV" ]; then
#USB cameras echo "Printer Serial Number not detected"
if [[ -n $INSTALL ]]; then 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 prompt_confirm "Would you like to auto detect an associated USB camera (experimental)?" echo
then USB=$TEMPUSB
add_camera echo "Your printer will be setup at the following usb address: $USB" | log
fi echo
fi else
echo echo "Serial number detected as: $UDEV" | log
fi
if prompt_confirm "Ready to write all changes. Do you want to proceed?"
then echo
cat $SCRIPTDIR/octoprint_generic.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \ #USB cameras
-e "s#OCTOPATH#$OCTOPATH#" \ if [[ -n $INSTALL ]]; then
-e "s#OCTOCONFIG#$OCTOCONFIG#" \ if prompt_confirm "Would you like to auto detect an associated USB camera (experimental)?"
-e "s/NEWINSTANCE/$INSTANCE/" \ then
-e "s/NEWPORT/$PORT/" > /etc/systemd/system/$INSTANCE.service add_camera
fi
#If a default octoprint service exists, stop and disable it fi
if [ -f "/etc/systemd/system/octoprint_default.service" ]; then echo
systemctl stop octoprint_default.service
systemctl disable octoprint_default.service if prompt_confirm "Ready to write all changes. Do you want to proceed?"
fi then
cat $SCRIPTDIR/octoprint_generic.service | \
#stop and disable default octoprint service (octopi) sed -e "s/OCTOUSER/$OCTOUSER/" \
if [ -f "/etc/systemd/system/octoprint.service" ]; then -e "s#OCTOPATH#$OCTOPATH#" \
systemctl stop octoprint.service -e "s#OCTOCONFIG#$OCTOCONFIG#" \
systemctl disable octoprint.service -e "s/NEWINSTANCE/$INSTANCE/" \
fi -e "s/NEWPORT/$PORT/" > /etc/systemd/system/$INSTANCE.service
#Printer udev identifier technique - either Serial number or USB port #If a default octoprint service exists, stop and disable it
#Serial Number if [ -f "/etc/systemd/system/octoprint_default.service" ]; then
if [ -n "$UDEV" ]; then systemctl stop octoprint_default.service
echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules systemctl disable octoprint_default.service
fi fi
#USB port #stop and disable default octoprint service (octopi)
if [ -n "$USB" ]; then if [ -f "/etc/systemd/system/octoprint.service" ]; then
echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules systemctl stop octoprint.service
fi systemctl disable octoprint.service
fi
#just to be on the safe side, add user to dialout and video
usermod -a -G dialout,video $OCTOUSER #Printer udev identifier technique - either Serial number or USB port
#Serial Number
#Append instance name to list for removal tool if [ -n "$UDEV" ]; then
echo instance:$INSTANCE port:$PORT >> /etc/octoprint_instances echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
#copy all files to our new directory
cp -rp $BFOLD $OCTOCONFIG/.$INSTANCE #USB port
if [ -n "$USB" ]; then
#Do config.yaml modifications here if needed.. echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
cat $BFOLD/config.yaml | sed -e "s/INSTANCE/$INSTANCE/" > $OCTOCONFIG/.$INSTANCE/config.yaml fi
#MAJOR WORKAROUND - for some reason this will not cat and sed directly into systemd/system. no idea why. create and mv for now #just to be on the safe side, add user to dialout and video
if [[ -n $CAM || -n $USBCAM ]]; then usermod -a -G dialout,video $OCTOUSER
cat $SCRIPTDIR/octocam_generic.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \ #Append instance name to list for removal tool
-e "s/OCTOCAM/cam_$INSTANCE/" \ echo instance:$INSTANCE port:$PORT >> /etc/octoprint_instances
-e "s/RESOLUTION/$RESOLUTION/" \
-e "s/FRAMERATE/$FRAMERATE/" \ #copy all files to our new directory
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service cp -rp $BFOLD $OCTOCONFIG/.$INSTANCE
mv $SCRIPTDIR/cam_$INSTANCE.service /etc/systemd/system/
echo $CAMPORT >> /etc/camera_ports #Do config.yaml modifications here if needed..
#config.yaml modifications cat $BFOLD/config.yaml | sed -e "s/INSTANCE/$INSTANCE/" > $OCTOCONFIG/.$INSTANCE/config.yaml
echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $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
echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml if [[ -n $CAM || -n $USBCAM ]]; then
echo cat $SCRIPTDIR/octocam_generic.service | \
fi sed -e "s/OCTOUSER/$OCTOUSER/" \
#Octobuntu Cameras udev identifier - either Serial number or USB port -e "s/OCTOCAM/cam_$INSTANCE/" \
#Serial Number -e "s/RESOLUTION/$RESOLUTION/" \
if [ -n "$CAM" ]; then -e "s/FRAMERATE/$FRAMERATE/" \
echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules -e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service
fi mv $SCRIPTDIR/cam_$INSTANCE.service /etc/systemd/system/
echo $CAMPORT >> /etc/camera_ports
#USB port camera #config.yaml modifications
if [ -n "$USBCAM" ]; then echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml
#echo KERNELS==\"$USBCAM\",SUBSYSTEMS==\"video4linux\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\",SUBSYSTEMS==\"usb\",DRIVERS==\"uvcvideo\",SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml
fi echo
fi
#Reset udev #Octobuntu Cameras udev identifier - either Serial number or USB port
udevadm control --reload-rules #Serial Number
udevadm trigger if [ -n "$CAM" ]; then
systemctl daemon-reload echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
sleep 1 fi
#Start and enable system processes #USB port camera
systemctl start $INSTANCE if [ -n "$USBCAM" ]; then
systemctl enable $INSTANCE #echo KERNELS==\"$USBCAM\",SUBSYSTEMS==\"video4linux\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
if [[ -n $CAM || -n $USBCAM ]]; then echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\",SUBSYSTEMS==\"usb\",DRIVERS==\"uvcvideo\",SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
systemctl start cam_$INSTANCE.service fi
systemctl enable cam_$INSTANCE.service
fi #Reset udev
udevadm control --reload-rules
#if we are on octopi, add in haproxy entry udevadm trigger
if [ $INSTALL = 1 ]; then systemctl daemon-reload
#find frontend line, do insert sleep 1
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 #Start and enable system processes
echo '#octoprint_deploy for port $PORT' >> /etc/haproxy/haprox.conf systemctl start $INSTANCE
echo 'backend $INSTANCE' >> /etc/haproxy/haprox.conf systemctl enable $INSTANCE
if [[ -n $CAM || -n $USBCAM ]]; then
echo systemctl start cam_$INSTANCE.service
#restart haproxy systemctl enable cam_$INSTANCE.service
sudo systemctl restart haproxy.service fi
fi
fi #if we are on octopi, add in haproxy entry
if [ $INSTALL = 1 ]; then
#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
#add backend info, bracket with comments so we can remove later if needed
echo '#octoprint_deploy for port $PORT' >> /etc/haproxy/haprox.conf
echo 'backend $INSTANCE' >> /etc/haproxy/haprox.conf
echo
#restart haproxy
sudo systemctl restart haproxy.service
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
CAMPORT=8000
fi
CAMPORT=$((CAMPORT+1))
echo Selected port is: $CAMPORT | log
fi
echo "Settings can be modified after initial setup in /etc/systemd/system/octocam_$INSTANCE"
echo
echo "Camera Resolution (no sanity check, so get it right) [640x480]:"
read RESOLUTION
if [ -z "$RESOLUTION" ]; then
RESOLUTION="640x480"
fi
echo "Selected camera resolution: $RESOLUTION" | log
#TODO check formating
echo "Camera Framerate (no sanity check, so get it right) [5]:"
read FRAMERATE
if [ -z "$FRAMERATE" ]; then
FRAMERATE=5
fi
echo "Selected camera framerate: $FRAMERATE" | log
#Need to check if this is a one-off install
if [ -z "$CAMPORT" ]; then
CAMPORT=8000
fi
CAMPORT=$((CAMPORT+1))
echo Selected port is: $CAMPORT | log
fi
echo "Settings can be modified after initial setup in /etc/systemd/system/octocam_$INSTANCE"
echo
echo "Camera Resolution (no sanity check, so get it right) [640x480]:"
read RESOLUTION
if [ -z "$RESOLUTION" ]; then
RESOLUTION="640x480"
fi
echo "Selected camera resolution: $RESOLUTION" | log
#TODO check formating
echo "Camera Framerate (no sanity check, so get it right) [5]:"
read FRAMERATE
if [ -z "$FRAMERATE" ]; then
FRAMERATE=5
fi
echo "Selected camera framerate: $FRAMERATE" | log
#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
echo #new line if prompt_confirm "Do you want to remove everything associated with this instance?"
if [[ $REPLY =~ ^[Yy]$ ]]; then 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
logfile='octoprint_deploy.log' logfile='octoprint_deploy.log'
SCRIPTDIR=$(dirname $(readlink -f $0)) SCRIPTDIR=$(dirname $(readlink -f $0))
PS3='Select operation: ' PS3='Select operation: '
options=("Prepare system (OctoPi)" "New instance" "Delete instance" "Add Camera" "USB port testing" "Quit") options=("Prepare system (OctoPi)" "New instance" "Delete instance" "Add Camera" "USB port testing" "Quit")
@@ -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