pve-trunks-plugin/install.sh

94 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# PVE Trunks Plugin - Installer
# Adds a "Trunks" field to the VM Network Device editor in the Proxmox VE UI.
#
# Usage: bash install.sh
#
# Both the plugin JS and the index template belong to the `pve-manager`
# package, so an `apt upgrade` wipes them. This installer therefore also sets
# up a reapply hook so the plugin survives Proxmox updates.
set -euo pipefail
PLUGIN_JS="pve-trunks-plugin.js"
PVE_JS_DIR="/usr/share/pve-manager/js"
INDEX_TPL="/usr/share/pve-manager/index.html.tpl"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Persistent copy + reapply machinery (so upgrades can restore the plugin).
STORE_DIR="/usr/local/share/pve-trunks-plugin"
REAPPLY="/usr/local/sbin/pve-trunks-reapply.sh"
APT_HOOK="/etc/apt/apt.conf.d/99-pve-trunks-plugin"
SCRIPT_TAG='<script type="text/javascript" src="/pve2/js/pve-trunks-plugin.js?ver=2"></script>'
if [ "$(id -u)" -ne 0 ]; then
echo "Error: run as root" >&2
exit 1
fi
if [ ! -d "$PVE_JS_DIR" ] || [ ! -f "$INDEX_TPL" ]; then
echo "Error: Proxmox VE not found ($PVE_JS_DIR / $INDEX_TPL missing)" >&2
exit 1
fi
if [ ! -f "$SCRIPT_DIR/$PLUGIN_JS" ]; then
echo "Error: $PLUGIN_JS not found next to installer" >&2
exit 1
fi
# 1) Persist the plugin source for the reapply hook.
install -d "$STORE_DIR"
install -m 0644 "$SCRIPT_DIR/$PLUGIN_JS" "$STORE_DIR/$PLUGIN_JS"
# 2) Install the reapply script (idempotent; restarts pveproxy only on change).
cat > "$REAPPLY" <<EOF
#!/bin/bash
# Reapply PVE Trunks Plugin (after pve-manager upgrade or manual run). Idempotent.
set -u
PVE_JS_DIR="$PVE_JS_DIR"
INDEX_TPL="$INDEX_TPL"
STORE_DIR="$STORE_DIR"
PLUGIN_JS="$PLUGIN_JS"
SCRIPT_TAG='$SCRIPT_TAG'
[ -d "\$PVE_JS_DIR" ] && [ -f "\$INDEX_TPL" ] || exit 0
[ -f "\$STORE_DIR/\$PLUGIN_JS" ] || exit 0
changed=0
# Restore the JS file if missing or outdated.
if ! cmp -s "\$STORE_DIR/\$PLUGIN_JS" "\$PVE_JS_DIR/\$PLUGIN_JS" 2>/dev/null; then
install -m 0644 "\$STORE_DIR/\$PLUGIN_JS" "\$PVE_JS_DIR/\$PLUGIN_JS"
changed=1
fi
# Re-add the <script> tag if missing (must sit before </body>).
if ! grep -q "pve-trunks-plugin" "\$INDEX_TPL"; then
if grep -q "</body>" "\$INDEX_TPL"; then
cp -a "\$INDEX_TPL" "\$INDEX_TPL.pve-trunks-bak"
awk -v tag="\$SCRIPT_TAG" '/<\/body>/ && !done { print tag; done=1 } { print }' \
"\$INDEX_TPL" > "\$INDEX_TPL.tmp" && mv "\$INDEX_TPL.tmp" "\$INDEX_TPL"
changed=1
else
logger -t pve-trunks-plugin "no </body> in \$INDEX_TPL; cannot patch"
fi
fi
if [ "\$changed" -eq 1 ]; then
systemctl try-restart pveproxy
logger -t pve-trunks-plugin "plugin reapplied"
fi
EOF
chmod 0755 "$REAPPLY"
# 3) apt hook: reapply at the end of any apt/dpkg transaction (e.g. pve-manager upgrade).
cat > "$APT_HOOK" <<EOF
// pve-trunks-plugin: restore UI plugin after pve-manager upgrades
DPkg::Post-Invoke { "test -x $REAPPLY && $REAPPLY >/dev/null 2>&1 || true"; };
EOF
# 4) Apply now.
"$REAPPLY"
echo "Installed. Reapply hook: $APT_HOOK -> $REAPPLY"
echo "Done. Reload the Proxmox web UI (Ctrl+Shift+R)."