# abshkbh/arrakis architecture

> Architecture diagram of the GitHub repository abshkbh/arrakis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abshkbh/arrakis
- Repository: https://github.com/abshkbh/arrakis

## Overview

##### Project Type & Purpose
- This is a full-stack system focused on virtualization security and sandbox management.
- The primary functionalities include spawning/stopping VMs, managing network and port forwarding, and snapshot/restore capabilities.
- External interfaces include a REST API (exposed by the “arrakis-restserver”), a CLI client (under cmd/client and related subcommands), and a Python SDK (py-arrakis) for programmatic access.
- Additional support is extended for MCP-based clients, which use the MCP server integration.

##### File Structure
- The top-level directories like “cmd”, “pkg”, “api”, “initramfs”, “resources”, and “setup” indicate a split between command-line tools, backend logic, microVM orchestration and VM initialization scripts.
- The “cmd” directory houses multiple executables (restserver, client, guestinit, vsockclient/server, etc.), evidencing separate entry points that manage various aspects of the VM lifecycle.
- The “pkg” directory includes modular internal components such as “server” functionality with submodules for CID/IP/port allocation, which help build the overall orchestration engine.
- The “api” folder holds API definitions (YAML files) that document the REST interface, reinforcing the centrality of the REST API to the design.
- The “initramfs” and “resources” folders detail scripts and Dockerfiles for customizing the guest VM’s root filesystem and boot process, thus emphasizing customization and security via containerization.

##### Review of README Insights
- The README elaborates on core features including running each sandbox in a MicroVM powered by cloud-hypervisor, container-based rootfs customization, and secure port forwarding for VNC/GUI access.

a. Main Components:
- External Clients: CLI client, Python SDK (py-arrakis), MCP clients.
- REST API Server: The “arrakis-restserver” that exposes endpoints for starting/stopping, listing, snapshotting, and restoring VMs.
- VM Manager Core: Internal services (grouped under pkg/server and pkg/cmdserver) responsible for:
– VM lifecycle management (creation, destruction, snapshot/restore)
– Resource allocation (IP, port, and CID allocation modules)
- Virtualization Layer: Cloud-hypervisor integration handling the actual MicroVM instantiation.
- Guest VMs/Sandboxes: Each VM runs its own Ubuntu OS with pre-installed code execution services, VNC server, and snapshot/restore capability.
- Networking and Port Forwarding: Components managing Linux tap devices, bridges, and port forwarding to expose guest services (e.g., VNC ports).
- Customization/Initialization: Scripts and configuration (Dockerfile, initramfs scripts) for building and initializing the guest VM environment.

b. Relationships & Interactions:
- External clients interact with the REST API server (using HTTP calls) to send commands such as “start sandbox”, “list VMs”, “snapshot”, “restore”, etc.
- The REST API server translates these calls into actions processed by the internal VM Manager Core, which in turn interacts with lower-level modules (IP/port allocators, command servers).
- The VM Manager makes calls to the virtualization layer (cloud-hypervisor) to actually spin up a MicroVM and configure its resources.
- Networking modules handle tasks like configuring tap devices and managing port forwarding (for example, forwarding a host port to a guest VNC server).
- The CLI client and Python SDK serve as thin layers on top of the REST API, providing user-friendly credentials.

c. Architectural Patterns:
- Layered (or tiered) design: Separation of external interfaces (API/CLI/SDK) from internal management logic (VM Manager, resource allocators) and the low-level virtualization layer.
- Modular design: Each subcomponent (like IP allocator, port allocator, snapshot/restore service) is decoupled for ease of customization and potential reuse.
- Security by isolation: Every sandbox is run in its own secure MicroVM ensuring that untrusted code execution is safely contained.

d. Relevant Technologies:
- Golang for the REST API server and CLI commands.
- cloud-hypervisor as the backend Virtual Machine Manager.
- Docker (via provided Dockerfile in resources/scripts/rootfs) and initramfs scripts for constructing the guest environment.
- Linux networking (tap devices, bridges) and overlayfs for filesystem isolation.
- Python (through the py-arrakis SDK) as an important client-side technology.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Clients Layer
    subgraph "External Clients"
        CLI["External CLI Client"]:::client
        SDK["Python SDK (py-arrakis)"]:::client
        MCP["MCP Clients"]:::client
    end

    %% API/Ops Layer
    subgraph "API/Ops Layer"
        REST["REST API Server (arrakis-restserver)"]:::api
    end

    %% VM Manager Core Layer
    subgraph "VM Manager Core"
        CMD_SRV["Command Server"]:::core
        IP_ALLOC["IP Allocator"]:::core
        PORT_ALLOC["Port Allocator"]:::core
        CID_ALLOC["CID Allocator"]:::core
        CORE_VM["Core VM Management Logic"]:::core
    end

    %% Virtualization & Infrastructure Layer
    subgraph "Virtualization & Infrastructure"
        HYP["Cloud-Hypervisor (MicroVM)"]:::virt
        NET["Networking & Port Forwarding"]:::virt
        GUEST["Guest VM / Sandbox (Ubuntu, VNC)"]:::virt
        GUEST_INIT["Guest Initialization & Customization"]:::virt
        SYS_SERV["System Services"]:::virt
    end

    %% Connections between layers
    CLI -->|"HTTP"| REST
    SDK -->|"HTTP"| REST
    MCP -->|"HTTP"| REST

    REST -->|"Invoke"| CMD_SRV

    CMD_SRV -->|"Manage"| IP_ALLOC
    CMD_SRV -->|"Manage"| PORT_ALLOC
    CMD_SRV -->|"Manage"| CID_ALLOC
    CMD_SRV -->|"Manage"| CORE_VM

    CORE_VM -->|"Spawns"| HYP
    HYP -->|"Instantiates"| GUEST
    HYP -->|"Configures"| NET

    CMD_SRV -->|"Triggers_Init"| GUEST_INIT
    GUEST_INIT -->|"Configures"| GUEST

    CMD_SRV -->|"Triggers_Svc"| SYS_SERV
    SYS_SERV -->|"Monitors"| GUEST

    %% Click Events for component mapping
    click CLI "https://github.com/abshkbh/arrakis/blob/main/cmd/client/main.go"
    click REST "https://github.com/abshkbh/arrakis/blob/main/cmd/restserver/main.go"
    click CMD_SRV "https://github.com/abshkbh/arrakis/blob/main/pkg/cmdserver/cmdserver.go"
    click CMD_SRV "https://github.com/abshkbh/arrakis/blob/main/cmd/cmdserver/main.go"
    click IP_ALLOC "https://github.com/abshkbh/arrakis/blob/main/pkg/server/ipallocator/ipallocator.go"
    click PORT_ALLOC "https://github.com/abshkbh/arrakis/blob/main/pkg/server/portallocator/portallocator.go"
    click CID_ALLOC "https://github.com/abshkbh/arrakis/blob/main/pkg/server/cidallocator/cidallocator.go"
    click CORE_VM "https://github.com/abshkbh/arrakis/blob/main/pkg/server/server.go"
    click GUEST_INIT "https://github.com/abshkbh/arrakis/tree/main/initramfs/"
    click GUEST_INIT "https://github.com/abshkbh/arrakis/tree/main/resources/scripts/rootfs/Dockerfile"
    click SYS_SERV "https://github.com/abshkbh/arrakis/blob/main/resources/arrakis-cmdserver.service"
    click SYS_SERV "https://github.com/abshkbh/arrakis/blob/main/resources/arrakis-guestinit.service"
    click SYS_SERV "https://github.com/abshkbh/arrakis/blob/main/resources/arrakis-vncserver.service"
    click SYS_SERV "https://github.com/abshkbh/arrakis/blob/main/resources/arrakis-vsockserver.service"

    %% Class styles
    classDef client fill:#DFF0D8,stroke:#3C763D,stroke-width:2px;
    classDef api fill:#D9EDF7,stroke:#31708F,stroke-width:2px;
    classDef core fill:#FCF8E3,stroke:#8A6D3B,stroke-width:2px;
    classDef virt fill:#F2DEDE,stroke:#A94442,stroke-width:2px;
```
