feat(dobotai): containerize test runtime and admin bootstrap
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# DobotAI 容器化运行(测试环境)
|
||||
|
||||
容器和Compose服务固定名为 `dobotai-dingtalk`。容器使用宿主网络,以兼容测试环境已有的 `127.0.0.1` PostgreSQL、WeKnora和健康检查地址;它不暴露新端口,不更改数据库容器或WeKnora容器。
|
||||
|
||||
## 日常操作
|
||||
|
||||
完成一次安装后,日常仅需要两条命令:
|
||||
|
||||
```bash
|
||||
sudo dobotai-dingtalk-configure
|
||||
sudo dobotai-dingtalk-bind-admin
|
||||
```
|
||||
|
||||
第一条命令以隐藏输入方式更新 `/etc/dobotai-test/dobotai.env` 中的必填配置;不会将秘密传入命令行、终端输出或Git。环境文件写入成功后,它会受控停止原生 `dobotai-test.service`、启动 `dobotai-dingtalk`,并以 `/healthz` 验证。容器健康检查失败时自动停止容器并恢复原生服务。
|
||||
|
||||
第二条命令在已运行的容器内执行 `dobotai bootstrap-admin`。终端显示六码后,候选管理员私聊钉钉机器人:
|
||||
|
||||
```text
|
||||
/setting <六码>
|
||||
```
|
||||
|
||||
绑定成功后终端自动退出。该命令不会启动第二个钉钉 Stream 消费者。
|
||||
|
||||
## 安装前置条件
|
||||
|
||||
安装脚本只接受已构建、已审查的本地镜像,并在切换前备份原生二进制与Systemd Unit。它不读取或打印环境文件中的值,也不停止旧服务。生产环境必须独立审查、备份和验证,不复用测试镜像以外的未验证构建。
|
||||
@@ -0,0 +1,22 @@
|
||||
services:
|
||||
dobotai-dingtalk:
|
||||
image: ${DOBOTAI_IMAGE:?set DOBOTAI_IMAGE in runtime.env}
|
||||
container_name: dobotai-dingtalk
|
||||
network_mode: host
|
||||
user: "${DOBOTAI_RUNTIME_UID:?set DOBOTAI_RUNTIME_UID}:${DOBOTAI_RUNTIME_GID:?set DOBOTAI_RUNTIME_GID}"
|
||||
env_file:
|
||||
- ${DOBOTAI_ENV_FILE:?set DOBOTAI_ENV_FILE in runtime.env}
|
||||
environment:
|
||||
DOBOTAI_SECRET_DIR: /var/lib/dobotai/secrets
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ${DOBOTAI_SECRET_DIR_HOST:?set DOBOTAI_SECRET_DIR_HOST in runtime.env}
|
||||
target: /var/lib/dobotai/secrets
|
||||
read_only: false
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
read_only: true
|
||||
tmpfs:
|
||||
- /tmp:rw,noexec,nosuid,size=32m
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import getpass
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
FIELDS = (
|
||||
("DATABASE_URL", "DobotAI PostgreSQL 连接串", True),
|
||||
("WEKNORA_BASE_URL", "WeKnora 地址", False),
|
||||
("WEKNORA_API_KEY", "WeKnora API Key", True),
|
||||
("DOBOTAI_INGRESS_TOKEN", "DobotAI Ingress Token", True),
|
||||
("DOBOTAI_TENANT_KEY", "DobotAI 租户键", False),
|
||||
("DOBOTAI_AGENT_BINDINGS", "DobotAI Agent Bindings JSON", False),
|
||||
("DINGTALK_CLIENT_ID", "钉钉 Client ID", False),
|
||||
("DINGTALK_CLIENT_SECRET", "钉钉 Client Secret", True),
|
||||
)
|
||||
|
||||
|
||||
def parse_env(path: Path):
|
||||
lines = path.read_text(encoding="utf-8").splitlines()
|
||||
values = {}
|
||||
for line in lines:
|
||||
if line and not line.lstrip().startswith("#") and "=" in line:
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value
|
||||
return lines, values
|
||||
|
||||
|
||||
def prompt(label: str, secret: bool, configured: bool) -> str:
|
||||
suffix = "(已配置,直接回车保留)" if configured else "(必填)"
|
||||
if secret:
|
||||
return getpass.getpass(f"{label}{suffix}: ").strip()
|
||||
return input(f"{label}{suffix}: ").strip()
|
||||
|
||||
|
||||
def write_env(path: Path, lines, values):
|
||||
rendered = []
|
||||
written = set()
|
||||
for line in lines:
|
||||
if line and not line.lstrip().startswith("#") and "=" in line:
|
||||
key = line.split("=", 1)[0].strip()
|
||||
if key in values:
|
||||
rendered.append(f"{key}={values[key]}")
|
||||
written.add(key)
|
||||
continue
|
||||
rendered.append(line)
|
||||
for key, value in values.items():
|
||||
if key not in written:
|
||||
rendered.append(f"{key}={value}")
|
||||
temporary = path.with_name(path.name + ".new")
|
||||
temporary.write_text("\n".join(rendered) + "\n", encoding="utf-8")
|
||||
os.chmod(temporary,0o600)
|
||||
os.replace(temporary, path)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="配置 DobotAI 容器运行环境")
|
||||
parser.add_argument("--env-file", required=True)
|
||||
parser.add_argument("--start-script", required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
env_file = Path(args.env_file)
|
||||
if not env_file.is_file():
|
||||
raise SystemExit("DobotAI 环境文件不存在,请先完成部署。")
|
||||
lines, values = parse_env(env_file)
|
||||
for key, label, secret in FIELDS:
|
||||
entered = prompt(label, secret, bool(values.get(key, "").strip()))
|
||||
if entered:
|
||||
values[key] = entered
|
||||
missing = [label for key, label, _ in FIELDS if not values.get(key, "").strip()]
|
||||
if missing:
|
||||
raise SystemExit("以下必填项未配置:" + "、".join(missing))
|
||||
write_env(env_file, lines, values)
|
||||
os.execv(args.start_script, [args.start_script])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(130)
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
container="dobotai-dingtalk"
|
||||
if [[ "$(docker inspect --format '{{.State.Running}}' "$container" 2>/dev/null || true)" != "true" ]]; then
|
||||
echo "DobotAI 容器未运行。请先执行:sudo dobotai-dingtalk-configure" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exec docker exec -it dobotai-dingtalk /usr/local/bin/dobotai bootstrap-admin
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
exec python3 /opt/dobotai-test/container/configure_env.py \
|
||||
--env-file /etc/dobotai-test/dobotai.env \
|
||||
--start-script /usr/local/sbin/dobotai-dingtalk-start
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
container_dir="/opt/dobotai-test/container"
|
||||
compose_file="$container_dir/compose.yaml"
|
||||
runtime_env="$container_dir/runtime.env"
|
||||
legacy_service="dobotai-test.service"
|
||||
health_url="http://127.0.0.1:8090/healthz"
|
||||
|
||||
if [[ ! -r "$compose_file" || ! -r "$runtime_env" ]]; then
|
||||
echo "容器运行文件未安装。请先部署 dobotai-dingtalk。" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
legacy_was_active=false
|
||||
if systemctl is-active --quiet "$legacy_service"; then
|
||||
legacy_was_active=true
|
||||
systemctl stop "$legacy_service"
|
||||
fi
|
||||
|
||||
rollback() {
|
||||
docker compose --env-file "$runtime_env" -f "$compose_file" down --remove-orphans >/dev/null 2>&1 || true
|
||||
if [[ "$legacy_was_active" == "true" ]]; then
|
||||
systemctl start "$legacy_service" || true
|
||||
fi
|
||||
}
|
||||
trap rollback ERR
|
||||
|
||||
docker compose --env-file "$runtime_env" -f "$compose_file" up -d --no-build --force-recreate
|
||||
for _ in $(seq 1 30); do
|
||||
if curl --noproxy '*' --fail --silent --max-time 2 "$health_url" >/dev/null; then
|
||||
systemctl disable "$legacy_service" >/dev/null
|
||||
trap - ERR
|
||||
echo "DobotAI 容器已启动:dobotai-dingtalk"
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "DobotAI 容器健康检查失败,已回滚到原生服务。" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
container_dir="${DOBOTAI_CONTAINER_DIR:-/opt/dobotai-test/container}"
|
||||
service="${DOBOTAI_LEGACY_SERVICE:-dobotai-test.service}"
|
||||
runtime_user="${DOBOTAI_RUNTIME_USER:-dobotai-test}"
|
||||
env_file="${DOBOTAI_ENV_FILE:-/etc/dobotai-test/dobotai.env}"
|
||||
secret_dir="${DOBOTAI_SECRET_DIR_HOST:-/var/lib/dobotai-test/secrets}"
|
||||
image="${DOBOTAI_IMAGE:?set DOBOTAI_IMAGE to the reviewed image tag before installation}"
|
||||
backup_root="${DOBOTAI_BACKUP_ROOT:-/opt/dobotai-test/backups}"
|
||||
|
||||
if [[ ${EUID} -ne 0 ]]; then
|
||||
echo "请使用 sudo 执行安装脚本。" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! docker image inspect "$image" >/dev/null 2>&1; then
|
||||
echo "候选镜像不存在:$image" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! -r "$env_file" || ! -d "$secret_dir" ]]; then
|
||||
echo "现有 DobotAI 环境文件或密钥目录不存在,拒绝安装。" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uid="$(id -u "$runtime_user")"
|
||||
gid="$(id -g "$runtime_user")"
|
||||
stamp="$(date -u '+%Y%m%dT%H%M%SZ')"
|
||||
backup_dir="$backup_root/pre-container-$stamp"
|
||||
install -d -m 0700 "$backup_dir" "$container_dir"
|
||||
cp -p /opt/dobotai-test/bin/dobotai "$backup_dir/dobotai.native"
|
||||
cp -p /etc/systemd/system/dobotai-test.service "$backup_dir/dobotai-test.service"
|
||||
sha256sum "$backup_dir/dobotai.native" > "$backup_dir/SHA256SUMS"
|
||||
|
||||
install -m 0644 "$script_dir/compose.yaml" "$container_dir/compose.yaml"
|
||||
install -m 0750 "$script_dir/dobotai-dingtalk-start" /usr/local/sbin/dobotai-dingtalk-start
|
||||
install -m 0750 "$script_dir/dobotai-dingtalk-configure" /usr/local/sbin/dobotai-dingtalk-configure
|
||||
install -m 0750 "$script_dir/dobotai-dingtalk-bind-admin" /usr/local/sbin/dobotai-dingtalk-bind-admin
|
||||
install -m 0700 "$script_dir/configure_env.py" "$container_dir/configure_env.py"
|
||||
|
||||
runtime_tmp="$container_dir/runtime.env.new"
|
||||
umask 077
|
||||
cat > "$runtime_tmp" <<EOF
|
||||
DOBOTAI_IMAGE=$image
|
||||
DOBOTAI_RUNTIME_UID=$uid
|
||||
DOBOTAI_RUNTIME_GID=$gid
|
||||
DOBOTAI_ENV_FILE=$env_file
|
||||
DOBOTAI_SECRET_DIR_HOST=$secret_dir
|
||||
EOF
|
||||
chmod 0600 "$runtime_tmp"
|
||||
mv -f "$runtime_tmp" "$container_dir/runtime.env"
|
||||
|
||||
echo "容器资产已安装,旧服务仍在运行。"
|
||||
echo "下一步执行:sudo dobotai-dingtalk-configure"
|
||||
@@ -0,0 +1,6 @@
|
||||
# 非敏感容器运行参数;install-test.sh 会根据测试服务器实际用户和目录生成此文件。
|
||||
DOBOTAI_IMAGE=dobotai-dingtalk:test
|
||||
DOBOTAI_RUNTIME_UID=996
|
||||
DOBOTAI_RUNTIME_GID=988
|
||||
DOBOTAI_ENV_FILE=/etc/dobotai-test/dobotai.env
|
||||
DOBOTAI_SECRET_DIR_HOST=/var/lib/dobotai-test/secrets
|
||||
Reference in New Issue
Block a user