Skip to content

Commit

Permalink
fix: octoprint install path
Browse files Browse the repository at this point in the history
feelfreelinux committed May 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 4104382 commit 212fe24
Showing 6 changed files with 155 additions and 17 deletions.
2 changes: 1 addition & 1 deletion scripts/alpine-make-rootfs.sh
Original file line number Diff line number Diff line change
@@ -457,7 +457,7 @@ if [ "$SCRIPT" ]; then
bind_dir="$(pwd)"
script_file2="/$script_name"
else
bind_dir="$(dirname "$script_file")"
bind_dir="../$(dirname "$script_file")"
script_file2="./$script_name"
fi
echo "Mounting $bind_dir to /mnt inside chroot" >&2
2 changes: 1 addition & 1 deletion scripts/build-octo4a-bootstrap.sh
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
set -eu

# alpine packages required to install octoprint
readonly O4A_PKGS='alpine-keys apk-tools gcc python3 py3-pip libffi-dev python3-dev musl-dev curl git linux-headers openssh-server p7zip bash unzip ffmpeg'
readonly O4A_PKGS='alpine-keys apk-tools gcc python3 py3-pip libffi-dev python3-dev musl-dev curl git linux-headers openssh-server p7zip bash unzip ffmpeg ttyd'
readonly ALPINE_BRANCH='3.19'

sudo sh scripts/alpine-make-rootfs.sh \
16 changes: 10 additions & 6 deletions scripts/create-bootstrap.sh
Original file line number Diff line number Diff line change
@@ -27,6 +27,10 @@ mkdir build/
echo "Downloading OctoPrint release $OCTOPRINT_VERSION"
curl -o build/octoprint.tar.gz -L https://github.com/OctoPrint/OctoPrint/releases/download/$OCTOPRINT_VERSION/OctoPrint-$OCTOPRINT_VERSION.tar.gz

# Unpack octoprint, move to a dir easily accessible from the chroot
tar -xzf build/octoprint.tar.gz -C build/
mv build/OctoPrint-$OCTOPRINT_VERSION build/octoprint

# Build talloc, proot
. ./scripts/build-talloc.sh
. ./scripts/build-proot.sh
@@ -36,12 +40,15 @@ cd external/minitar
sh build.sh
cd ../../

# Build alpine bootstrap
. ./scripts/build-octo4a-bootstrap.sh

echo "Building ioctl hook"
. ./scripts/build-ioctl-hook.sh

# Copy into non-arch specfic file, for access from chroot bind
cp build/ioctlHook-$ARCH.so build/ioctl-hook.so

# Build alpine bootstrap
. ./scripts/build-octo4a-bootstrap.sh

echo "Preparing full bootstrap archive"
mkdir build/bootstrap-dir

@@ -54,9 +61,6 @@ mv build/rootfs.tar.xz build/bootstrap-dir/
# include minitar
cp external/minitar/build/libs/$ARCH_NDK/minitar build/bootstrap-dir/bin

# include ioctl hook
cp build/ioctlHook-$ARCH.so build/bootstrap-dir/ioctl-hook.so

# include entrypoint script
cp scripts/run-bootstrap-android.sh build/bootstrap-dir/entrypoint.sh

23 changes: 18 additions & 5 deletions scripts/run-bootstrap-android.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/system/bin/sh
# Minimal proot run script, used as entrypoint in bootstrap

if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: <user> <command>"
exit 1
fi

if [ ! -d "bootstrap" ]; then
echo "No bootstrap detected, extracting"
@@ -16,10 +22,17 @@ if [ ! -d "bootstrap" ]; then
cd ..
fi

PATH='/sbin:/usr/sbin:/bin:/usr/bin'
USER='root'
HOME='/root'
OP="-0"
if [ "$1" = "root" ]; then
PATH='/sbin:/usr/sbin:/bin:/usr/bin'
USER='root'
HOME='/root'
OP="-0"
else
OP=""
USER="$1"
PATH='/sbin:/usr/sbin:/bin:/usr/bin'
HOME="/home/$USER"
fi

BASE_DIR="$PWD"

@@ -42,4 +55,4 @@ export HOME
# proot refers to ../libexec, hence PWD is necessary
cd bin/

./proot -r ../bootstrap -0 --kill-on-exit -b /dev -b /proc -b /system:/system -b /vendor:/vendor -b /apex:/apex --link2symlink -w $HOME
./proot -r ../bootstrap $OP -b /dev -b /proc -b /storage -b /system -b /vendor -b /apex -b ${PWD}/fake_proc_stat:/proc/stat $EXTRA_BIND --link2symlink -w $HOME "$@"
14 changes: 10 additions & 4 deletions scripts/setup-alpine-rootfs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
# Ran as part of alpine-make-rootfs, chrooted as the bootstrap thats being built
set -eu

export PATH='/sbin:/usr/sbin:/bin:/usr/bin'
@@ -36,8 +37,13 @@ adduser -D -g "octoprint" octoprint

export HOME='/home/octoprint'

# chown -R octoprint /mnt/octoprint-release
mkdir -p /home/octoprint/.octoprint/plugins
cp /mnt/src/comm-fix.py /home/octoprint/.octoprint/plugins
cp /mnt/build/ioctl-hook.so /home/octoprint/ioctl-hook.so

# # switch to octoprint user
# su -s /bin/bash -c "python3 -m venv ~/octoprint-venv" octoprint
# su -s /bin/bash -c ". ~/octoprint-venv/bin/activate && cd /mnt/octoprint-release && ls -l && pip3 install ." octoprint
chown -R octoprint /mnt/build/octoprint
chown -R octoprint /home/octoprint/.octoprint

# switch to octoprint user
su -s /bin/bash -c "python3 -m venv ~/octoprint-venv" octoprint
su -s /bin/bash -c ". ~/octoprint-venv/bin/activate && cd /mnt/build/octoprint && ls -l && pip3 install ." octoprint
115 changes: 115 additions & 0 deletions src/comm-fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import octoprint.plugin

from octoprint.util.pip import create_pip_caller
from octoprint.util.version import ( get_comparable_version, is_octoprint_compatible )

# Imports from comm.py
import contextlib
import copy
import fnmatch
import glob
import logging
import os
import queue
import re
import threading
import time
from collections import deque

import serial
import wrapt
import octoprint.util.version
import octoprint.plugin
from octoprint.events import Events, eventManager
from octoprint.filemanager import valid_file_type
from octoprint.filemanager.destinations import FileDestinations
from octoprint.settings import settings
from octoprint.systemcommands import system_command_manager
from octoprint.util import (
CountedEvent,
PrependableQueue,
RepeatedTimer,
ResettableTimer,
TypeAlreadyInQueue,
TypedQueue,
chunks,
filter_non_ascii,
filter_non_utf8,
get_bom,
get_exception_string,
sanitize_ascii,
to_unicode,
comm
)

from octoprint.util.platform import get_os, set_close_exec

_logger = logging.getLogger(__name__)

class Octo4a18Fix(octoprint.plugin.StartupPlugin):
def on_startup(self, host, port):
self._logger.info("Monkey patching comm.py")
def patchedSerialList():
if os.name == "nt":
candidates = []
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM"
)
i = 0
while True:
candidates += [winreg.EnumValue(key, i)[1]]
i += 1
except Exception:
pass

else:
candidates = []
try:
with os.scandir("/dev") as it:
for entry in it:
if regex_serial_devices.match(entry.name):
candidates.append(entry.path)
except Exception:
pass

# additional ports
additionalPorts = settings().get(["serial", "additionalPorts"])
if additionalPorts:
for additional in additionalPorts:
candidates += glob.glob(additional)

hooks = octoprint.plugin.plugin_manager().get_hooks(
"octoprint.comm.transport.serial.additional_port_names"
)
for name, hook in hooks.items():
try:
candidates += hook(candidates)
except Exception:
logging.getLogger(__name__).exception(
"Error while retrieving additional "
"serial port names from hook {}".format(name)
)

# blacklisted ports
blacklistedPorts = settings().get(["serial", "blacklistedPorts"])
if blacklistedPorts:
for pattern in settings().get(["serial", "blacklistedPorts"]):
candidates = list(
filter(lambda x: not fnmatch.fnmatch(x, pattern), candidates)
)

# last used port = first to try, move to start
prev = settings().get(["serial", "port"])
if prev in candidates:
candidates.remove(prev)
candidates.insert(0, prev)

return candidates

comm.serialList = patchedSerialList

__plugin_pythoncompat__ = ">=3.0,<4"
__plugin_implementation__ = Octo4a18Fix()
def __plugin_check__():
return is_octoprint_compatible(">=1.8.0")

0 comments on commit 212fe24

Please sign in to comment.