wem_lib.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # shellcheck shell=bash disable=SC2154
  2. # wem_lib.sh - shared setup of the we_manager checks (sourced by video_optimize_check.sh, desktop_icons_auto_check.sh,
  3. # wem_check.sh and desktop_icons_wem_check.sh, which define HERE, vm, app, sshv, pass and fail).
  4. # wem_install puts the app into the guest (a .deb is installed, an unpacked dir is copied unless the guest already has this very
  5. # build) and sets $wem_start_line the way the other we_manager scripts build it, with WEM_LAUNCH and WEM_FLAGS. wem_run starts
  6. # the app (after the previous instance is gone) and waits for its DevTools port. cdp <js> evaluates JavaScript in the page (CDP_TIMEOUT seconds, default 90).
  7. cdp() { sshv ". ~/sessenv; cd ~/wem; timeout ${CDP_TIMEOUT:-90} python3 cdp.py \"$1\" 2>&1 | cut -c1-600"; }
  8. wem_copy_app() {
  9. local name stamp
  10. name=$(basename "$app")
  11. stamp=$(find "$app" -type f -printf '%P %s %T@\n' | sort | sha256sum | cut -c1-16)
  12. if [ "$(sshv "cat ~/wem/.$name.stamp 2>/dev/null; test -x ~/wem/$name/we-manager || echo gone")" = "$stamp" ]; then return 0; fi
  13. tar -C "$(dirname "$app")" -cf - "$name" | ssh -i "$HOME/.local/share/linuxvm/id_ed25519" \
  14. -p "$(cat "$HOME/.local/share/linuxvm/vms/$vm/port")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
  15. -o LogLevel=ERROR lab@127.0.0.1 "mkdir -p ~/wem && rm -f ~/wem/.$name.stamp && rm -rf ~/wem/$name && tar -C ~/wem -xf - && echo $stamp > ~/wem/.$name.stamp"
  16. local rc=("${PIPESTATUS[@]}")
  17. [ "${rc[0]}" = 0 ] && [ "${rc[1]}" = 0 ]
  18. }
  19. wem_install() {
  20. sshv 'mkdir -p ~/wem'
  21. "$HERE/linuxvm.sh" scp "$vm" "$HERE/cdp.py" wem/cdp.py
  22. if [[ $app == *.deb ]]; then
  23. "$HERE/linuxvm.sh" scp "$vm" "$app" wem/app.deb
  24. if ! sshv 'sudo DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=300 install -y --allow-downgrades ./wem/app.deb >/tmp/wem-deb.log 2>&1'; then
  25. fail "the .deb did not install: $(sshv 'tail -3 /tmp/wem-deb.log')"
  26. return 1
  27. fi
  28. pass "the .deb installs"
  29. wem_start_line="${WEM_LAUNCH:-} we-manager ${WEM_FLAGS:-}"
  30. else
  31. if ! wem_copy_app; then fail "could not copy the app into the guest"; return 1; fi
  32. wem_start_line="${WEM_LAUNCH:-} ~/wem/$(basename "$app")/we-manager --no-sandbox ${WEM_FLAGS:-}"
  33. fi
  34. }
  35. # wem_steam_lib [workshop id]... - a Steam library like a real user has: the Wallpaper Engine assets and the workshop items
  36. # (copies, the library scan skips symlinked folders)
  37. wem_steam_lib() {
  38. local id
  39. sshv 'mkdir -p ~/.steam/steam/steamapps/common/wallpaper_engine ~/.steam/steam/steamapps/workshop/content/431960
  40. ln -sfn ~/lwe/assets ~/.steam/steam/steamapps/common/wallpaper_engine/assets'
  41. for id in "$@"; do
  42. sshv "d=~/.steam/steam/steamapps/workshop/content/431960/$id; [ -L \$d ] && rm \$d; [ -d \$d ] || cp -al ~/lwe/wp-$id \$d"
  43. done
  44. }
  45. wem_engine_fallback() {
  46. sshv 'mkdir -p ~/.local/share; [ -x ~/.local/share/linux-wallpaperengine-kde/linux-wallpaperengine ] || cp -a ~/lwe/lwe-hints ~/.local/share/linux-wallpaperengine-kde'
  47. }
  48. wem_wait_gone() {
  49. sshv 'for _ in $(seq 20); do pgrep -f "[w]e-manager" >/dev/null || break; sleep 1; done; true'
  50. }
  51. wem_ffmpeg_cache=${XDG_CACHE_HOME:-$HOME/.cache}/lwe-release-prep/we-manager-ffmpeg
  52. wem_ffmpeg_seed() {
  53. [ -x "$wem_ffmpeg_cache/ffmpeg" ] && [ -x "$wem_ffmpeg_cache/ffprobe" ] || return 0
  54. sshv '. ~/sessenv; mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/we-manager"'
  55. local dir
  56. dir=$(sshv '. ~/sessenv; echo "${XDG_DATA_HOME:-$HOME/.local/share}/we-manager"')
  57. # scp -r into an existing directory nests the copy
  58. sshv "test -x $dir/ffmpeg/ffmpeg" && return 0
  59. "$HERE/linuxvm.sh" scp "$vm" "$wem_ffmpeg_cache" "$dir/ffmpeg" >/dev/null 2>&1
  60. }
  61. wem_ffmpeg_keep() {
  62. local dir
  63. dir=$(sshv '. ~/sessenv; echo "${XDG_DATA_HOME:-$HOME/.local/share}/we-manager/ffmpeg"')
  64. [ -x "$wem_ffmpeg_cache/ffmpeg" ] && return 0
  65. sshv "test -x $dir/ffmpeg && test -x $dir/ffprobe" || return 0
  66. mkdir -p "$wem_ffmpeg_cache"
  67. sshv "tar -C $dir -cf - ffmpeg ffprobe" | tar -C "$wem_ffmpeg_cache" -xf -
  68. }
  69. # wem_ffmpeg_fake_start <work dir> - serves the cached ffmpeg and ffprobe as the latest BtbN/FFmpeg-Builds release from fake_github.py
  70. # (like update_check.sh does for the app's own releases), so the app's download in a guest without an encoder comes from the host and
  71. # a 504 from GitHub no longer fails the check. Returns 1 without a cache (the real download is used then); sets $fake, $fgh_port.
  72. # The app has to be started after this with NODE_EXTRA_CA_CERTS=$HOME/wem/fake-ca.pem. wem_ffmpeg_fake_stop undoes the guest changes.
  73. wem_ffmpeg_fake_start() {
  74. local work=$1 name=ffmpeg-n7.1-latest-linux64-gpl-7.1 pack
  75. [ -x "$wem_ffmpeg_cache/ffmpeg" ] && [ -x "$wem_ffmpeg_cache/ffprobe" ] || return 1
  76. pack=$(dirname "$wem_ffmpeg_cache")/ffmpeg-fake/$name.tar.xz
  77. if [ ! -s "$pack" ]; then
  78. mkdir -p "$(dirname "$pack")"
  79. tar -C "$wem_ffmpeg_cache" --transform "s,^,$name/bin/," -cf - ffmpeg ffprobe | xz -0 -T4 > "$pack.part" && mv "$pack.part" "$pack" || return 1
  80. fi
  81. mkdir -p "$work/certs"
  82. . "$HERE/fake_github_lib.sh"
  83. fgh_start "$vm" "$work" --release "BtbN/FFmpeg-Builds=latest:$pack" || return 1
  84. kill -0 "$fake" 2>/dev/null || return 1
  85. "$HERE/linuxvm.sh" scp "$vm" "$work/certs/ca.pem" wem/fake-ca.pem
  86. sshv 'grep -q "api.github.com" /etc/hosts || echo "10.0.2.2 api.github.com" | sudo tee -a /etc/hosts >/dev/null'
  87. }
  88. wem_ffmpeg_fake_stop() {
  89. kill "$fake" 2>/dev/null
  90. fgh_unredirect
  91. sshv "sudo sed -i '/api.github.com/d' /etc/hosts; true"
  92. }
  93. wem_layer_shell_install() {
  94. sshv 'if command -v dnf >/dev/null; then sudo dnf install -y gtk4-layer-shell python3-gobject python3-cairo
  95. elif command -v pacman >/dev/null; then sudo pacman -Sy --noconfirm --needed gtk4-layer-shell python-gobject python-cairo
  96. elif command -v apt-get >/dev/null; then sudo apt-get install -y gir1.2-gtk4layershell-1.0 libgtk4-layer-shell0 python3-gi python3-gi-cairo gir1.2-gtk-4.0
  97. elif command -v zypper >/dev/null; then sudo zypper -n update libglib-2_0-0 libgio-2_0-0 libgobject-2_0-0 libgmodule-2_0-0 libgthread-2_0-0 glib2-tools; sudo zypper -n install libgtk4-layer-shell0 typelib-1_0-Gtk4LayerShell-1_0 python3-gobject python3-cairo typelib-1_0-Gtk-4_0
  98. fi'
  99. }
  100. wem_run() {
  101. wem_wait_gone
  102. sshv ". ~/sessenv; (setsid nohup $wem_start_line --remote-debugging-port=9222 > ~/wem/app.log 2>&1 </dev/null &); for _ in \$(seq 40); do grep -q 'DevTools listening' ~/wem/app.log 2>/dev/null && break; sleep 1; done; sleep 8"
  103. if [ "$(sshv 'pgrep -fc "[w]e-manager"')" -gt 0 ] && sshv "grep -q 'DevTools listening' ~/wem/app.log"; then return 0; fi
  104. fail "we_manager did not start: $(sshv 'tail -3 ~/wem/app.log')"
  105. return 1
  106. }