# 桌面环境
<subtitle>带有 Ubuntu 桌面和 VNC 访问的沙箱。</subtitle>

此模板创建一个带有完整 Ubuntu 22.04 桌面环境的沙箱，包括 XFCE 桌面、常用应用程序以及用于远程访问的 VNC 流。它非常适合构建需要与图形用户界面交互的 AI Agent。

该模板包括：
- **Ubuntu 22.04** 与 XFCE 桌面环境
- **VNC 流**：通过 [noVNC](https://novnc.com/) 实现基于浏览器的访问
- **预装应用程序**：LibreOffice、文本编辑器、文件管理器和常用工具
- **自动化工具**：[xdotool](https://github.com/jordansissel/xdotool) 和 [scrot](https://github.com/resurrecting-open-source-projects/scrot)，用于程序化控制桌面

## 模板定义

该模板安装桌面环境，通过 [x11vnc](https://github.com/LibVNC/x11vnc) 和 noVNC 设置 VNC 流，并配置启动脚本。

```python
# template.py
from ucloud_sandbox import Template, wait_for_port

template = (
    Template()
    .from_image("ubuntu:22.04")
    # 初始系统设置和软件包
    # 这里不使用 .apt_install()，因为部分软件包有交互式提示（键盘布局设置等）
    .run_cmd(
        [
            "yes | unminimize",
            "apt-get update",
            "apt-get install -y \
                xserver-xorg \
                xorg \
                x11-xserver-utils \
                xvfb \
                x11-utils \
                xauth \
                xfce4 \
                xfce4-goodies \
                util-linux \
                sudo \
                curl \
                git \
                wget \
                python3-pip \
                xdotool \
                scrot \
                ffmpeg \
                x11vnc \
                net-tools \
                netcat \
                x11-apps \
                libreoffice \
                xpdf \
                gedit \
                xpaint \
                tint2 \
                galculator \
                pcmanfm",
            "apt-get clean",
            "rm -rf /var/lib/apt/lists/*",
        ]
    )
    # 设置 NoVNC 和 websockify
    .run_cmd(
        [
            "git clone --branch e2b-desktop https://github.com/e2b-dev/noVNC.git /opt/noVNC",
            "ln -s /opt/noVNC/vnc.html /opt/noVNC/index.html",
            "git clone --branch v0.12.0 https://github.com/novnc/websockify /opt/noVNC/utils/websockify",
        ]
    )
    # 设置默认终端
    .run_cmd(
        "ln -sf /usr/bin/xfce4-terminal.wrapper /etc/alternatives/x-terminal-emulator"
    )
    # 复制启动命令
    .copy("start_command.sh", "/start_command.sh")
    .run_cmd("chmod +x /start_command.sh")
    # 设置启动命令以启动桌面环境
    .set_start_cmd("/start_command.sh", wait_for_port(6080))
)
```

## 启动脚本

启动脚本使用 [Xvfb](https://www.x.org/releases/X11R7.6/doc/man/man1/Xvfb.1.xhtml)（X 虚拟帧缓冲）初始化虚拟显示，启动 XFCE 桌面会话，启动 VNC 服务器，并通过 noVNC 在 6080 端口上暴露桌面。此脚本在沙箱启动时自动运行。

```bash
#!/bin/bash

# 设置显示
export DISPLAY=${DISPLAY:-:0}

# 启动 Xvfb
Xvfb $DISPLAY -ac -screen 0 1024x768x24 -nolisten tcp &
sleep 2

# 启动 XFCE 会话
startxfce4 &
sleep 5

# 启动 VNC 服务器
x11vnc -bg -display $DISPLAY -forever -wait 50 -shared -rfbport 5900 -nopw \
    -noxdamage -noxfixes -nowf -noscr -ping 1 -repeat -speeds lan &
sleep 2

# 启动 noVNC 服务器
cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:5900 --listen 6080 --web /opt/noVNC --heartbeat 30 &
sleep 2
```

## 构建模板

使用更高的 CPU 和内存分配来构建模板，以处理桌面环境的安装。由于要安装的软件包体积较大，构建过程可能需要几分钟。

```python
# build.py
from ucloud_sandbox import Template, default_build_logger
from .template import template as desktopTemplate

Template.build(desktopTemplate, 'desktop',
    cpu_count=8,
    memory_mb=8192,
    on_build_logs=default_build_logger(),
)
```
