mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
feat(sandbox): add KVM-backed agent sandboxing via microsandbox
This commit is contained in:
1 parent
1d477bdee4
commit
aa3fb48a62
80 files changed
+17304
-204
No files matched your search
@@ -7,9 +7,45 @@ export PATH="$BUN_INSTALL/bin:$HOME/.opencode/bin:/usr/local/bin:$PATH"
|
||||
|
||||
source /usr/local/lib/ocm/container-user.sh
|
||||
|
||||
grant_kvm_access() {
|
||||
local dev="${1:-/dev/kvm}"
|
||||
[ -e "$dev" ] || return 0
|
||||
|
||||
local dev_gid group_name holder
|
||||
dev_gid="$(stat -c '%g' "$dev" 2>/dev/null)" || return 0
|
||||
case "$dev_gid" in
|
||||
''|*[!0-9]*) return 0 ;;
|
||||
esac
|
||||
|
||||
holder="$(getent group "$dev_gid" 2>/dev/null | cut -d: -f1 || true)"
|
||||
if [ -n "$holder" ]; then
|
||||
group_name="$holder"
|
||||
else
|
||||
group_name="kvm"
|
||||
if ! groupadd -g "$dev_gid" "$group_name"; then
|
||||
echo "ERROR: could not create group '$group_name' (gid $dev_gid) required for $dev access" >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! usermod -aG "$group_name" node; then
|
||||
echo "ERROR: could not add node to group '$group_name' (gid $dev_gid) required for $dev access" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if ! runuser -u node -- test -r "$dev" || ! runuser -u node -- test -w "$dev"; then
|
||||
echo "ERROR: node cannot access $dev (group '$group_name', gid $dev_gid)" >&2
|
||||
echo "ERROR: grant the container group access to $dev or run the sandbox overlay (docker-compose.sandbox.yml)" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "Granted node access to $dev (group '$group_name', gid $dev_gid)"
|
||||
}
|
||||
|
||||
install_opencode() {
|
||||
echo "Installing OpenCode latest..."
|
||||
curl -fsSL "https://github.com/anomalyco/opencode/releases/latest/download/opencode-linux-$(uname -m | sed 's/x86_64/x64/; s/aarch64/arm64/').tar.gz" \
|
||||
local opencode_version="${OPENCODE_BUNDLED_VERSION:-1.18.16}"
|
||||
echo "Installing OpenCode ${opencode_version}..."
|
||||
curl -fsSL "https://github.com/anomalyco/opencode/releases/download/v${opencode_version}/opencode-linux-$(uname -m | sed 's/x86_64/x64/; s/aarch64/arm64/').tar.gz" \
|
||||
-o /tmp/opencode.tar.gz
|
||||
tar -xzf /tmp/opencode.tar.gz -C /tmp
|
||||
mkdir -p "$HOME/.opencode/bin"
|
||||
@@ -62,11 +98,11 @@ if [ "$OPENCODE_VERSION" != "unknown" ]; then
|
||||
echo "OpenCode version meets minimum requirement (>=$MIN_OPENCODE_VERSION)"
|
||||
else
|
||||
echo "OpenCode version $OPENCODE_VERSION is below minimum required version $MIN_OPENCODE_VERSION"
|
||||
echo "Upgrading OpenCode..."
|
||||
opencode upgrade || install_opencode
|
||||
echo "Reinstalling bundled OpenCode version ${OPENCODE_BUNDLED_VERSION:-1.18.16}..."
|
||||
install_opencode
|
||||
|
||||
OPENCODE_VERSION=$(opencode --version 2>&1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "unknown")
|
||||
echo "OpenCode upgraded to version: $OPENCODE_VERSION"
|
||||
echo "OpenCode reinstalled as version: $OPENCODE_VERSION"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -92,9 +128,13 @@ if ! align_container_user node; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grant_kvm_access; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
warn_if_workspace_owner_differs /workspace "$OCM_TARGET_UID" "$OCM_TARGET_GID"
|
||||
|
||||
mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode
|
||||
mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode /home/node/.microsandbox
|
||||
chown -R node:node /app/data /workspace /home/node
|
||||
|
||||
exec runuser -u node -- "$@"
|
||||
Reference in new issue
Block a user