2025-07-22 13:16:53 +02:00
#!/bin/sh
oci = $( which podman || which docker)
2025-07-30 15:09:03 +02:00
test -n " $APPNAME " || APPNAME = xrforge
2025-08-06 14:21:12 +02:00
test -n " $UPLOAD_PATH " || UPLOAD_PATH = /mnt/experiences
2025-08-11 16:19:41 +02:00
test -n " $THEME " || THEME = vapor # xrforge (public/assets/themes/xrforge.css gives err)
test -n " $HOMEPAGE " || HOMEPAGE = /models
2025-07-28 17:52:11 +02:00
db = /config/manyfold.sqlite3
# utility funcs
error( ) { echocolor "[error]" " $* " ; exit 1; }
echocolor( ) { printf "\033[96m%s\033[0m \033[95m%s\033[0m %s\n" " $1 " " $2 " " $3 " ; }
debug( ) { set -x; " $@ " ; set +x; }
2025-07-22 13:16:53 +02:00
create_config( ) {
test -d ./config || mkdir config
test -d ./experiences || mkdir experiences
}
run( ) {
test -x ${ oci } || { echo "warning: not running manyfold OCI container [install podman/docker/etc]" ; exit; }
2025-07-28 17:52:11 +02:00
echocolor " [ $APPNAME ] " " $( basename ${ oci } ) detected..starting OCI container "
${ oci } rm -f xrforge
2025-08-01 11:41:59 +02:00
#-e NO_OVERLAYFS=true \
#-e NO_DEFAULTDB=true \
#-e PUBLIC_HOSTNAME=localhost \
#-e PUBLIC_PORT=80 \
2025-08-05 18:17:22 +02:00
debug ${ oci } run " $@ " -p 8790:3214 -p 8791:3215 --name xrforge \
2025-07-22 13:16:53 +02:00
-e SECRET_KEY_BASE = lkjwljlkwejrlkjek34k234l \
-e DATABASE_ADAPTER = sqlite3 \
-e SUDO_RUN_UNSAFELY = enabled \
-e MULTIUSER = enabled \
-e FEDERATION = enabled \
2025-08-11 16:19:41 +02:00
-e THEME = $THEME \
-e HOMEPAGE = $HOMEPAGE \
2025-08-05 18:59:03 +02:00
-e FEDERATE_DRIVE_CACHE = 5s \
2025-07-28 17:52:11 +02:00
--cap-add SYS_ADMIN --security-opt apparmor:unconfined --device /dev/fuse \
2025-08-01 11:41:59 +02:00
xrforge
#ghcr.io/manyfold3d/manyfold-solo:latest
2025-07-28 17:52:11 +02:00
}
overlayfs( ) {
test -d /manyfold || return 0; # nothing to override
echocolor " [ $APPNAME ] " "applying filesystem overlay"
cd /manyfold
2025-08-01 16:20:48 +02:00
rsync -rvzi * /.
}
# cron-like function using sleep (./manifold.sh infinite 3600 zip -r /backup.zip /)
infinite( ) {
trap 'echocolor "/bin/infinite $*: process ended..infinite does not care.."; sleep 2s' INT
interval = $1
shift
loop( ) {
echocolor " [/bin/infinite $* ] " " $( date) started "
while sleep ${ interval } ; do
echocolor " [/bin/infinite $* ] " " $( date) executing "
" $@ "
done ;
}
loop " $@ "
2025-07-28 17:52:11 +02:00
}
2025-08-04 18:06:41 +02:00
# flexible unixy hook-mechanism which executes all files in ~/hook.d/foo/* when
# calling 'hook foo bar'
hook( ) {
test -z " $1 " && { echo "usage: hook <cmd_or_jsfunction> [args]" ; return 0; }
logger " $ hook $* "
cmd = $1
shift
test -d ~/hook.d/$cmd && {
find -L ~/hook.d/$cmd / -type f -executable -maxdepth 1 | while read hook; do
logger " |+ hook $hook $* "
{ $hook " $@ " || true; } 2>& 1 | awk '{ gsub(/\/root\/\//,"",$1); $1 = sprintf("%-40s", $1)} 1' | logger
done
}
}
start_hook_daemon( ) {
# every day call scripts in ~/hook.d/cleanup_daily/*
# 86400 secs = 1 day 3600 = 1 hour
$0 infinite 86400 hook daily &
$0 infinite 3600 hook hourly &
2025-08-06 14:21:12 +02:00
# trigger hooks when files change in /mnt/experiences
inotifywait -r -m $UPLOAD_PATH | awk '$2 ~ /(CREATE|MODIFY|MOVED_TO|DELETE)/ { system("' $0 ' hook inotify_"$2" "$1""$3) }' &
2025-08-04 18:06:41 +02:00
}
2025-07-28 17:52:11 +02:00
db( ) {
default( ) {
test -f /manyfold/manyfold.sql && ! test -f /config/manyfold.sql && {
echocolor " [ $APPNAME ] " "copying default database"
test -d /config || mkdir /config
cat /manyfold/manyfold.sql | sqlite3 $db
}
}
dump( ) {
sqlite3 source.db ".dump" > /manyfold/manyfold.sql
}
import( ) {
sqlite3 $db < " $1 " #/manyfold/manyfold.sql
}
" $@ "
}
2025-07-30 15:09:03 +02:00
add_lib_to_db( ) {
name = $( basename $1 )
2025-08-12 15:41:45 +02:00
sqlite3 $db " INSERT INTO libraries SELECT NULL, ' $1 ', DATE('NOW'), DATE('NOW'), '', '', ' $name ', NULL, '', 'filesystem', '', '', '', '', '', ' $name ', 1 WHERE NOT EXISTS (SELECT 1 FROM libraries WHERE path = ' $1 '); "
2025-07-30 15:09:03 +02:00
}
set_upload_path( ) {
echocolor " [ $APPNAME ] " "configuring upload library"
test -d $UPLOAD_PATH || mkdir $UPLOAD_PATH
add_lib_to_db $UPLOAD_PATH
id = $( sqlite3 $db " select id from libraries where path = ' $UPLOAD_PATH '; " )
2025-08-12 15:41:45 +02:00
sqlite3 $db " UPDATE settings set value = $id where var = 'default_library'; "
2025-07-30 15:09:03 +02:00
}
2025-08-12 15:41:45 +02:00
mount_dir( ) {
find /mnt -type d -mindepth 1 -maxdepth 1 | while read dir; do
echocolor " [ $APPNAME ] " " mounting $dir as library "
add_lib_to_db " $dir "
done
}
mount_rclone( ) {
2025-07-28 17:52:11 +02:00
libraries( ) {
rclone listremotes | while read remote; do
dir = " ${ remote / : / } "
test -d /mnt/$dir || mkdir /mnt/$dir
echocolor " [ $APPNAME ] " " rclone: mounting $remote to /mnt/ $dir "
debug rclone mount --daemon $remote /mnt/$dir -vv
2025-07-30 15:09:03 +02:00
add_lib_to_db /mnt/$dir
2025-07-28 17:52:11 +02:00
done
}
library( ) {
2025-07-30 15:09:03 +02:00
echocolor " [ $APPNAME ] " " rclone: mounting $RCLONE_REMOTE to /mnt/ $RCLONE_REMOTE "
test -d /mnt/$RCLONE_REMOTE || mkdir /mnt/$RCLONE_REMOTE
debug rclone mount --daemon ${ RCLONE_REMOTE } : /mnt/$RCLONE_REMOTE -vv
add_lib_to_db /mnt/$RCLONE_REMOTE
2025-07-28 17:52:11 +02:00
}
test -n " $RCLONE_REMOTE " && library
test -n " $RCLONE_REMOTE " || libraries
2025-07-22 13:16:53 +02:00
}
2025-07-28 17:52:11 +02:00
set_theme( ) {
test -z " $THEME " && return 0 # nothing to do
echocolor " [ $APPNAME ] " " setting theme to ' $THEME ' "
debug sqlite3 /config/manyfold.sqlite3 "UPDATE settings SET value = '" $THEME "' WHERE var == 'theme';"
2025-07-22 13:16:53 +02:00
}
2025-07-28 18:02:11 +02:00
set_modelpath( ) {
echocolor " [ $APPNAME ] " "enforcing modelpath"
2025-08-04 20:27:55 +02:00
debug sqlite3 /config/manyfold.sqlite3 "UPDATE settings SET value = replace('--- \"{creator}/{modelId}\"\n','\n',char(10)) WHERE var == 'model_path_template';"
2025-07-28 18:02:11 +02:00
}
2025-08-11 16:19:41 +02:00
set_homepage( ) {
test " $HOMEPAGE " = "/" && return 0 # nothing to do
echocolor " [ $APPNAME ] " "enforcing homepage path"
debug sed -i 's|root to:.*|root to: redirect("' $HOMEPAGE '")|g' /usr/src/app/config/routes.rb
}
2025-07-28 17:52:11 +02:00
rename_app( ) {
echocolor " [ $APPNAME ] " " renaming manyfold to $APPNAME "
sed -i 's/title: Manyfold/title: ' $APPNAME '/g' /usr/src/app/config/locales/*.yml
2025-08-05 13:57:59 +02:00
sed -i 's|powered_by_html:.*|powered_by_html: Radically opensource-powered by <a href="https://forgejo.isvery.ninja/coderofsalvation/xrforge">XR Forge</a>, <a href="https://manifold.app" target="_blank">Manyfold</a>, <a href="https://xrfragment.org">XR Fragments</a> and <a href="https://nixos.org" target="_blank">NIX</a>|g' /usr/src/app/config/locales/*.yml
2025-08-06 17:22:29 +02:00
sed -i 's|Models|Experiences|g' /usr/src/app/config/locales/*.yml /usr/src/app/config/locales/*/*.yml
sed -i 's|Model|Experience|g' /usr/src/app/config/locales/*.yml /usr/src/app/config/locales/*.yml
2025-07-28 17:52:11 +02:00
}
2025-08-04 18:06:41 +02:00
start_syslog( ) {
2025-08-06 09:45:55 +02:00
touch /var/log/messages
2025-08-04 18:06:41 +02:00
syslogd -n & # start syslogd
echocolor started syslog | logger
tail -f /var/log/messages &
2025-08-01 16:20:48 +02:00
}
2025-08-12 15:41:45 +02:00
scan_libraries( ) {
2025-08-13 10:52:28 +02:00
sleep 10 # wait for manyfold/redis to boot first
2025-08-12 15:41:45 +02:00
cd /usr/src/app
2025-08-12 17:03:26 +02:00
echocolor "scanning libraries"
2025-08-12 15:41:45 +02:00
bin/manyfold libraries scan
}
2025-08-13 10:52:28 +02:00
rails_query( ) {
cd /usr/src/app
echo " $* " | bin/rails console
}
force_public( ) {
test -n " $NO_PUBLIC_ONLY " && return 0 # nothing to do
echocolor "forcing public-only models"
infinite 60 rails_query 'Model.find_each { |it| it.grant_permission_to("view", nil) }' &
}
get_xrfragment_assets( ) {
test -n " $NO_ASSETS " && return 0 # nothing to do here
test -d /mnt/assets || {
echocolor "fetching XR Fragments assets & templates"
2025-08-14 15:01:59 +02:00
mkdir -p /mnt/assets/xrfragment /mnt/templates/xrfragment
2025-08-13 10:52:28 +02:00
cd /tmp
timeout 50 wget "https://codeberg.org/coderofsalvation/xrfragment/archive/main.zip"
unzip main.zip
2025-08-14 15:01:59 +02:00
cp -r xrfragment/assets/library /mnt/assets/xrfragment/\# 1
cp -r xrfragment/assets/template /mnt/templates/xrfragment/\# 2
2025-08-13 10:52:28 +02:00
rm -rf main.zip xrfragment
}
add_lib_to_db /mnt/assets
2025-08-14 15:01:59 +02:00
add_lib_to_db /mnt/templates
2025-08-13 10:52:28 +02:00
}
2025-07-28 17:52:11 +02:00
# The new entrypoint of the docker
boot( ) {
echocolor " [ $APPNAME ] " "booting..."
test -z " $NO_OVERLAYFS " && overlayfs
test -z " $NO_DEFAULTDB " && db default
2025-08-04 18:06:41 +02:00
start_syslog
2025-07-28 17:52:11 +02:00
rename_app
2025-08-11 16:19:41 +02:00
set_homepage
2025-07-28 17:52:11 +02:00
set_theme
2025-07-28 18:02:11 +02:00
set_modelpath
2025-08-12 15:41:45 +02:00
mount_dir
mount_rclone
2025-07-30 15:09:03 +02:00
set_upload_path
2025-08-04 18:06:41 +02:00
start_hook_daemon
2025-08-13 10:52:28 +02:00
force_public
get_xrfragment_assets
2025-08-12 15:41:45 +02:00
scan_libraries &
2025-08-05 18:59:03 +02:00
hook boot # emit unixy hook-event (/root/hook.d/boot/* scripts)
2025-08-01 16:20:48 +02:00
2025-07-28 17:52:11 +02:00
exec " $@ " # exec prevents error 's6-overlay-suexec: fatal: can only run as pid 1'
2025-07-22 13:16:53 +02:00
}
is_inside_container( ) {
test -d /package/admin && return 0 || return 1
}
usage( ) {
2025-07-28 17:52:11 +02:00
echocolor "Usage:" manyfold.sh "<cmd>"
echocolor "Cmds:"
echocolor " " "run [-d] " "# runs a OCI container (needs podman/docker)"
2025-07-22 13:16:53 +02:00
exit 0
}
2025-07-28 17:52:11 +02:00
test -n " $1 " && " $@ "
test -n " $1 " || usage