diff --git a/stage1/01-sys-tweaks/00-run.sh b/stage1/01-sys-tweaks/00-run.sh index feca584..51331c7 100755 --- a/stage1/01-sys-tweaks/00-run.sh +++ b/stage1/01-sys-tweaks/00-run.sh @@ -6,6 +6,7 @@ install -v -m 644 files/fstab "${ROOTFS_DIR}/etc/fstab" install -m 755 files/.xprofile "${ROOTFS_DIR}/etc/skel/.xprofile" install -m 755 files/.browser.sh "${ROOTFS_DIR}/etc/skel/.browser.sh" install -m 644 files/.dialogrc "${ROOTFS_DIR}/etc/skel/.dialogrc" +install -m 644 files/.dialogrc "${ROOTFS_DIR}/root/.dialogrc" mkdir -p "${ROOTFS_DIR}/etc/skel/.config/openbox" install -m 644 files/autostart "${ROOTFS_DIR}/etc/skel/.config/openbox/autostart" diff --git a/stage1/01-sys-tweaks/files/.browser.sh b/stage1/01-sys-tweaks/files/.browser.sh index 1f866fc..9630935 100644 --- a/stage1/01-sys-tweaks/files/.browser.sh +++ b/stage1/01-sys-tweaks/files/.browser.sh @@ -1,11 +1,12 @@ #!/bin/bash -source /srv/octoprint/host +LISTEN=$(grep -i listen /etc/nginx/port.conf | awk '{gsub(";",""); print $2}') + if [[ -f ~/.overrideurl.sh ]]; then source ./.overrideurl.sh; fi # Wait until OctoPrint comes up -while ! curl "$HOST:$PORT" 2>&1 >/dev/null; do +while ! curl "$LISTEN" 2>&1 >/dev/null; do sleep 1 done -surf -d -F -g -K -n -p "$HOST:$PORT" +surf -d -F -g -K -n -p "$LISTEN" diff --git a/stage2/04-octoprint/00-packages b/stage2/04-octoprint/00-packages index a6d0575..3b88333 100644 --- a/stage2/04-octoprint/00-packages +++ b/stage2/04-octoprint/00-packages @@ -3,4 +3,5 @@ python-dev python-setuptools python-virtualenv git -libyaml-dev +libyaml-dev +nginx diff --git a/stage2/04-octoprint/01-run.sh b/stage2/04-octoprint/01-run.sh index 0442859..cde9096 100755 --- a/stage2/04-octoprint/01-run.sh +++ b/stage2/04-octoprint/01-run.sh @@ -2,7 +2,11 @@ install -m 644 files/octoprint.service ${ROOTFS_DIR}/etc/systemd/system/octoprint.service +install -m 644 files/nginx.conf ${ROOTFS_DIR}/etc/nginx/nginx.conf +echo -e "listen 80;" > ${ROOTFS_DIR}/etc/nginx/listen.conf + on_chroot << EOF +systemctl enable nginx if ! pip list | grep -F octoprint; then if [[ ! -d /srv/octoprint ]]; then mkdir -p /srv/octoprint; fi cd /srv/octoprint || exit 1 diff --git a/stage2/04-octoprint/files/nginx.conf b/stage2/04-octoprint/files/nginx.conf new file mode 100644 index 0000000..e02d32d --- /dev/null +++ b/stage2/04-octoprint/files/nginx.conf @@ -0,0 +1,53 @@ + worker_processes auto; + + events { + worker_connections 1024; + } + + http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + + map $http_upgrade $connection_upgrade { + default upgrade; + '' close; + } + + upstream "octoprint" { + server 127.0.0.1:5000; + } + + upstream "mjpg-streamer" { + server 127.0.0.1:8080; + } + + server { + include /etc/nginx/listen.conf; + server_name localhost; + + location / { + proxy_pass http://octoprint/; + proxy_set_header Host $http_host; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Scheme $scheme; + proxy_http_version 1.1; + + client_max_body_size 0; + } + + location /webcam/ { + proxy_pass http://mjpg-streamer/; + } + + # redirect server error pages to the static page /50x.html + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } + } + } diff --git a/stage2/04-octoprint/files/octoprint.service b/stage2/04-octoprint/files/octoprint.service index f4bfced..2d1ecba 100644 --- a/stage2/04-octoprint/files/octoprint.service +++ b/stage2/04-octoprint/files/octoprint.service @@ -5,7 +5,7 @@ Description=OctoPrint Daemon Type=simple User=octoprint Group=octoprint -ExecStart=/usr/local/bin/start-octoprint +ExecStart=/srv/octoprint/venv/bin/octoprint --host localhost --port 5000 [Install] WantedBy=multi-user.target diff --git a/stage2/05-mjpgstreamer/00-packages b/stage2/05-mjpgstreamer/00-packages new file mode 100644 index 0000000..f62499f --- /dev/null +++ b/stage2/05-mjpgstreamer/00-packages @@ -0,0 +1,6 @@ +cmake +libjpeg8-dev +v4l-utils +libv4l-dev +gcc +g++ diff --git a/stage2/05-mjpgstreamer/01-run.sh b/stage2/05-mjpgstreamer/01-run.sh new file mode 100755 index 0000000..4a86b21 --- /dev/null +++ b/stage2/05-mjpgstreamer/01-run.sh @@ -0,0 +1,18 @@ +#!/bin/bash -e + +install -m 644 files/mjpg-streamer.service ${ROOTFS_DIR}/etc/systemd/system/mjpg-streamer.service +install -m 755 files/start-mjpg ${ROOTFS_DIR}/usr/local/bin/start-mjpg + +on_chroot << EOF +if ! which mjpg_streamer; then + cd /tmp + rm -rf /tmp/mjpg-streamer + git clone https://github.com/jacksonliam/mjpg-streamer/ + cd mjpg-streamer/mjpg-streamer-experimental/ + make + make install + cd / + rm -rf /tmp/mjpg-streamer +fi +EOF + diff --git a/stage2/05-mjpgstreamer/files/mjpg-streamer.service b/stage2/05-mjpgstreamer/files/mjpg-streamer.service new file mode 100644 index 0000000..17ad723 --- /dev/null +++ b/stage2/05-mjpgstreamer/files/mjpg-streamer.service @@ -0,0 +1,12 @@ +[Unit] +Description=MJPG-Streamer Daemon + +[Service] +Type=simple +User=octoprint +Group=octoprint +ExecStart=/usr/local/bin/start-mjpg + +[Install] +WantedBy=multi-user.target + diff --git a/stage2/05-mjpgstreamer/files/start-mjpg b/stage2/05-mjpgstreamer/files/start-mjpg new file mode 100644 index 0000000..24b2340 --- /dev/null +++ b/stage2/05-mjpgstreamer/files/start-mjpg @@ -0,0 +1,2 @@ +#!/bin/bash +/usr/local/bin/mjpg_streamer -i input_uvc.so -o "output_http.so --port 8080" diff --git a/stage2/05-utils/files/start-octoprint b/stage2/05-utils/files/start-octoprint deleted file mode 100644 index d0d3b43..0000000 --- a/stage2/05-utils/files/start-octoprint +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source /srv/octoprint/venv/bin/activate - -source /srv/octoprint/host - -[[ "$HOST" == "" ]] && HOST="0.0.0.0" -[[ "$PORT" == "" ]] && PORT="5000" - -/srv/octoprint/venv/bin/octoprint serve --host $HOST --port $PORT diff --git a/stage2/05-utils/00-run.sh b/stage2/06-utils/00-run.sh similarity index 87% rename from stage2/05-utils/00-run.sh rename to stage2/06-utils/00-run.sh index 3781863..f85dc02 100755 --- a/stage2/05-utils/00-run.sh +++ b/stage2/06-utils/00-run.sh @@ -4,7 +4,6 @@ mkdir -p "${ROOTFS_DIR}/usr/local/bin/" install -m 755 files/octo-config "${ROOTFS_DIR}/usr/local/bin/octo-config" install -m 755 files/first-time.sh "${ROOTFS_DIR}/etc/profile.d/first-time.sh" install -m 755 files/octo-lib.sh "${ROOTFS_DIR}/usr/local/lib/octo-lib.sh" -install -m 755 files/start-octoprint "${ROOTFS_DIR}/usr/local/bin/start-octoprint" mkdir -p ${ROOTFS_DIR}/etc/systemd/system/getty@tty1.service.d/ cat > ${ROOTFS_DIR}/etc/systemd/system/getty@tty1.service.d/override.conf << 'EOF' diff --git a/stage2/05-utils/files/first-time.sh b/stage2/06-utils/files/first-time.sh similarity index 90% rename from stage2/05-utils/files/first-time.sh rename to stage2/06-utils/files/first-time.sh index 9f6501c..65377f2 100755 --- a/stage2/05-utils/files/first-time.sh +++ b/stage2/06-utils/files/first-time.sh @@ -73,11 +73,15 @@ service_select || return 1 screen_timeout || return 1 -if dialog --title "OctoPrint Config" --yesno "Do you wish to change the default OctoPrint listening address and/or port?" 10 60; then - octo_config +if [[ -f /etc/systemd/system/multi-user.target.wants/octoprint.service ]] && dialog --title "OctoPrint Config" --yesno "Do you wish to change the default OctoPrint listening address and/or port?" 10 60; then + nginx_config fi -if dialog --title "Plugin Manager" --yesno "Do you wish to preinstall some suggested plugins?" 10 60; then +if [[ -f /etc/systemd/system/multi-user.target.wants/mjpg-streamer.service ]]; then + video_config +fi + +if [[ -f /etc/systemd/system/multi-user.target.wants/octoprint.service ]] && dialog --title "Plugin Manager" --yesno "Do you wish to preinstall some suggested plugins?" 10 60; then recommended_menu || return 1 suggested_menu || return 1 chown -R octoprint:octoprint /srv/octoprint diff --git a/stage2/05-utils/files/octo-config b/stage2/06-utils/files/octo-config similarity index 75% rename from stage2/05-utils/files/octo-config rename to stage2/06-utils/files/octo-config index 05592fe..797aa67 100755 --- a/stage2/05-utils/files/octo-config +++ b/stage2/06-utils/files/octo-config @@ -9,20 +9,22 @@ source /usr/local/lib/octo-lib.sh main_menu () { local MAINMENU=$(dialog --nocancel --title "Pi Setup" --menu "" 10 50 0 \ - "1" "Configure Networking" \ + "1" "Configure networking" \ "2" "Change password for pi" \ "3" "Configure services" \ "4" "Configure screen timeout" \ - "5" "Configure OctoPrint" \ - "6" "Exit" 3>&1 1>&2 2>&3) + "5" "Configure Nginx" \ + "6" "Configure MJPG" \ + "7" "Exit" 3>&1 1>&2 2>&3) case $MAINMENU in "1") nmtui; main_menu; return 0;; "2") change_password; main_menu; return 0;; "3") service_select; main_menu; return 0;; "4") screen_timeout; main_menu; return 0;; - "5") octo_config; main_menu; return 0;; - "6") return 0;; + "5") nginx_config; main_menu; return 0;; + "6") video_config; main_menu; return 0;; + "7") return 0;; esac } diff --git a/stage2/05-utils/files/octo-lib.sh b/stage2/06-utils/files/octo-lib.sh similarity index 62% rename from stage2/05-utils/files/octo-lib.sh rename to stage2/06-utils/files/octo-lib.sh index f00ceb8..88ba7a2 100644 --- a/stage2/05-utils/files/octo-lib.sh +++ b/stage2/06-utils/files/octo-lib.sh @@ -20,20 +20,23 @@ change_password () { service_select () { local SERVICE_MENU=$(dialog --separate-output --nocancel --title "Select services" --checklist "Enable/disable services" 0 0 0 \ "1" "OctoPrint" $(if [[ -f /etc/systemd/system/multi-user.target.wants/octoprint.service ]]; then echo "ON"; else echo "OFF"; fi) \ - "2" "GUI" $(if [[ $(systemctl get-default) == "graphical.target" ]]; then echo "ON"; else echo "OFF"; fi) \ - "3" "SSH" $(if [[ -f /etc/systemd/system/multi-user.target.wants/ssh.service ]]; then echo "ON"; else echo "OFF"; fi) 3>&1 1>&2 2>&3) + "2" "MJPG-Streamer" $(if [[ -f /etc/systemd/system/multi-user.target.wants/mjpg-streamer.service ]]; then echo "ON"; else echo "OFF"; fi) \ + "3" "GUI" $(if [[ $(systemctl get-default) == "graphical.target" ]]; then echo "ON"; else echo "OFF"; fi) \ + "4" "SSH" $(if [[ -f /etc/systemd/system/multi-user.target.wants/ssh.service ]]; then echo "ON"; else echo "OFF"; fi) 3>&1 1>&2 2>&3) SERVICE_MENU=($SERVICE_MENU) local ENABLE_OCTO=false + local ENABLE_MJPG=false local ENABLE_GUI=false local ENABLE_SSH=false for i in "${SERVICE_MENU[@]}"; do case $i in "1") ENABLE_OCTO=true ;; - "2") ENABLE_GUI=true ;; - "3") ENABLE_SSH=true ;; + "2") ENABLE_MJPG=true ;; + "3") ENABLE_GUI=true ;; + "4") ENABLE_SSH=true ;; esac done @@ -43,6 +46,12 @@ service_select () { systemctl disable octoprint fi + if [[ $ENABLE_MJPG == true ]]; then + systemctl enable mjpg-streamer + else + systemctl disable mjpg-streamer + fi + if [[ $ENABLE_GUI == true ]]; then systemctl set-default graphical.target else @@ -78,30 +87,39 @@ EOF } screen_timeout() { - local TIMEOUT=$(dialog --nocancel --title "Screen Timeout" --inputbox "Input your desired screen timeout in seconds.\nEnter \"off\" to disable the screen timeout.\n\nAdding a screen timeout can reduce screen burn in." 11 60 "0" 3>&1 1>&2 2>&3) + local TIMEOUT=$(dialog --nocancel --title "Screen Timeout" --inputbox "Input your desired screen timeout in seconds.\nEnter \"off\" to disable the screen timeout.\n\nAdding a screen timeout can reduce screen burn in.\n\nDefault: off" 12 60 "off" 3>&1 1>&2 2>&3) cat > /home/pi/.xtimeout << EOF xset s ${TIMEOUT} xset -dpms EOF } -octo_config () { - if [[ -f /srv/octoprint/host ]]; then - source /srv/octoprint/host +nginx_config () { + local LISTEN="" + + if [[ -f /etc/nginx/listen.conf ]]; then + LISTEN=$(grep -i listen /etc/nginx/listen.conf | awk '{gsub(";",""); print $2}') else - HOST="0.0.0.0" - PORT="5000" + LISTEN="80" fi + + LISTEN=$(dialog --title "Nginx Config" --nocancel --inputbox "Configure what port and IP Nginx should listen on.\nTo listen on all IPs, just enter the port.\nDefault: 80" 11 50 "$LISTEN" 3>&1 1>&2 2>&3) - local OCTOFORM=$(dialog --title "OctoPrint Config" --nocancel --form "Configure what port and IP OctoPrint listen on." 10 50 0 \ - "Listening IP:" 1 1 "$HOST" 1 17 20 0 \ - "Listening Port:" 2 1 "$PORT" 2 17 6 0 3>&1 1>&2 2>&3) - OCTOFORM=($OCTOFORM) - - HOST=${OCTOFORM[0]} - PORT=${OCTOFORM[1]} - - echo -e "HOST=$HOST\nPORT=$PORT" > /srv/octoprint/host + echo "listen $LISTEN;" > /etc/nginx/listen.conf } +video_config () { + local DEVICES=($(ls /dev/video*)) + + local DEVICELIST="" + for ((i = 0; i < ${#DEVICES[@]}; i++)); do + DEVICELIST+="${DEVICES[$i]} $i OFF " + done + + local DEVICE_MENU=$(dialog --title "Video Config" --nocancel --radiolist "Choose which video device you wish to use for MJPG-Streamer" 10 50 0 $DEVICELIST 3>&1 1>&2 2>&3) + + [[ "$DEVICE_MENU" == "" ]] && return 0 + + echo -e '#!/bin/bash'"\n/usr/local/bin/mjpg_streamer -i \"input_uvc.so -d $DEVICE_MENU\" -o \"output_http.so --port 8080\"" > /usr/local/bin/start-mjpg +}