feat(deploy): add clone-and-run docker installer
This commit is contained in:
@@ -81,7 +81,7 @@ DobotAI 是 WeKnora 的企业外部渠道集成层,负责钉钉协议、身份
|
|||||||
| 方式 | 适用场景 | 入口 | 数据库与迁移 | 推荐程度 |
|
| 方式 | 适用场景 | 入口 | 数据库与迁移 | 推荐程度 |
|
||||||
| --- | --- | --- | --- | --- |
|
| --- | --- | --- | --- | --- |
|
||||||
| 源码/原生二进制 | 本地开发、快速调试、临时问题定位 | `go run ./cmd/dobotai` 或受控构建的二进制 | 操作人员准备独立PostgreSQL并显式执行迁移 | 仅开发 |
|
| 源码/原生二进制 | 本地开发、快速调试、临时问题定位 | `go run ./cmd/dobotai` 或受控构建的二进制 | 操作人员准备独立PostgreSQL并显式执行迁移 | 仅开发 |
|
||||||
| Docker Compose | 测试、预发布、生产候选 | `deploy/dobotai-dingtalk/install-docker.sh` | 脚本创建`dobotai-postgres`和`dobotai-postgres-data`,并按序执行迁移 | 推荐 |
|
| Docker Compose | 测试、预发布、生产候选 | 仓库根目录`install-docker.sh` | 脚本构建镜像、创建`dobotai-postgres`和`dobotai-postgres-data`,并按序执行迁移 | 推荐 |
|
||||||
|
|
||||||
### 源码/原生部署
|
### 源码/原生部署
|
||||||
|
|
||||||
@@ -96,9 +96,9 @@ curl http://127.0.0.1:8090/healthz
|
|||||||
### Docker Compose 部署
|
### Docker Compose 部署
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 安装:自动准备目录、独立PostgreSQL、命名卷、env模板和迁移
|
# 克隆后进入仓库根目录执行:自动安装Docker/Compose(如缺失)、构建镜像、
|
||||||
sudo DOBOTAI_IMAGE=dobotai-dingtalk:test-<commit-short> \
|
# 准备目录、独立PostgreSQL、命名卷、env模板和迁移。
|
||||||
bash deploy/dobotai-dingtalk/install-docker.sh
|
sudo bash ./install-docker.sh
|
||||||
|
|
||||||
# 配置WeKnora和钉钉后自动启动应用
|
# 配置WeKnora和钉钉后自动启动应用
|
||||||
sudo dobotai-dingtalk-configure
|
sudo dobotai-dingtalk-configure
|
||||||
|
|||||||
@@ -109,46 +109,27 @@ services:
|
|||||||
## 4. 前置条件
|
## 4. 前置条件
|
||||||
|
|
||||||
```text
|
```text
|
||||||
Docker Engine 和 Docker Compose 已安装
|
|
||||||
候选 DobotAI 镜像已 build 或 docker load 导入
|
|
||||||
部署用户具有 sudo 和 Docker daemon 访问权限
|
部署用户具有 sudo 和 Docker daemon 访问权限
|
||||||
宿主机 WeKnora 已健康,容器可经 host.docker.internal:8080 访问
|
宿主机 WeKnora 已健康,容器可经 host.docker.internal:8080 访问
|
||||||
```
|
```
|
||||||
|
|
||||||
## 5. 构建或导入镜像
|
Docker与Docker Compose缺失时由根安装脚本自动安装(当前支持APT系统)。不必手工构建、导入或命名镜像。
|
||||||
|
|
||||||
在干净、已审查的源码目录构建:
|
## 5. 全新安装
|
||||||
|
|
||||||
|
从源码仓库克隆后,进入仓库根目录执行唯一安装入口:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker build \
|
sudo bash ./install-docker.sh
|
||||||
--build-arg VERSION=test-<commit-short> \
|
|
||||||
--build-arg COMMIT_SHA=<完整提交SHA> \
|
|
||||||
--build-arg BUILD_TIME=<UTC时间> \
|
|
||||||
-t dobotai-dingtalk:test-<commit-short> .
|
|
||||||
```
|
```
|
||||||
|
|
||||||
跨主机传输时:
|
脚本内部以当前Git提交构建固定内部镜像名:
|
||||||
|
|
||||||
```bash
|
```text
|
||||||
docker save -o dobotai-dingtalk.tar dobotai-dingtalk:test-<commit-short>
|
dobotai-dingtalk:local-<当前Git短SHA>
|
||||||
sha256sum dobotai-dingtalk.tar
|
|
||||||
|
|
||||||
# 目标服务器先核验SHA,再导入
|
|
||||||
docker load -i dobotai-dingtalk.tar
|
|
||||||
```
|
```
|
||||||
|
|
||||||
生产必须使用已验证的同一归档或不可变摘要,禁止目标机重新构建。
|
镜像名、PostgreSQL镜像和构建元数据只由根目录`install-docker.sh`维护,用户不传`DOBOTAI_IMAGE`。脚本完成后数据库和迁移已就绪,应用尚未启动。然后执行:
|
||||||
|
|
||||||
## 6. 全新安装
|
|
||||||
|
|
||||||
在包含本目录部署资产的发布目录执行:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
sudo DOBOTAI_IMAGE=dobotai-dingtalk:test-<commit-short> \
|
|
||||||
bash ./install-docker.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
脚本完成后数据库和迁移已就绪,应用尚未启动。然后执行:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo dobotai-dingtalk-configure
|
sudo dobotai-dingtalk-configure
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
source_root="${DOBOTAI_SOURCE_ROOT:-$(cd "$script_dir/../.." && pwd)}"
|
||||||
home="${DOBOTAI_HOME:-/opt/dobotai}"
|
home="${DOBOTAI_HOME:-/opt/dobotai}"
|
||||||
container_dir="$home/container"
|
container_dir="$home/container"
|
||||||
config_dir="$home/config"
|
config_dir="$home/config"
|
||||||
@@ -31,8 +32,8 @@ install -m 0750 "$script_dir/dobotai-dingtalk-start" "$container_dir/dobotai-din
|
|||||||
install -m 0750 "$script_dir/dobotai-dingtalk-bind-admin" "$container_dir/dobotai-dingtalk-bind-admin"
|
install -m 0750 "$script_dir/dobotai-dingtalk-bind-admin" "$container_dir/dobotai-dingtalk-bind-admin"
|
||||||
install -m 0750 "$script_dir/apply-migrations.sh" "$container_dir/apply-migrations.sh"
|
install -m 0750 "$script_dir/apply-migrations.sh" "$container_dir/apply-migrations.sh"
|
||||||
install -m 0750 "$script_dir/migrate-existing-database.sh" "$container_dir/migrate-existing-database.sh"
|
install -m 0750 "$script_dir/migrate-existing-database.sh" "$container_dir/migrate-existing-database.sh"
|
||||||
install -m 0644 "$script_dir"/../../db/migrations/*.up.sql "$container_dir/migrations/"
|
install -m 0644 "$source_root"/db/migrations/*.up.sql "$container_dir/migrations/"
|
||||||
install -m 0644 "$script_dir"/../../db/migrations/*.down.sql "$container_dir/migrations/"
|
install -m 0644 "$source_root"/db/migrations/*.down.sql "$container_dir/migrations/"
|
||||||
|
|
||||||
if [[ ! -f "$postgres_env" || ! -f "$app_env" ]]; then
|
if [[ ! -f "$postgres_env" || ! -f "$app_env" ]]; then
|
||||||
umask 077
|
umask 077
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# 唯一安装入口:镜像仓库名与标签策略只在本脚本维护。
|
||||||
|
postgres_image="postgres:16-alpine"
|
||||||
|
|
||||||
|
if [[ ${EUID} -ne 0 ]]; then
|
||||||
|
echo "请在克隆仓库根目录执行:sudo bash ./install-docker.sh" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
cd "$repo_root"
|
||||||
|
[[ -f Dockerfile && -f deploy/dobotai-dingtalk/install-docker.sh ]] || {
|
||||||
|
echo "安装脚本必须从完整DobotAI源码仓库根目录运行。" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
command -v git >/dev/null || { echo "未找到 git,无法确定当前源码版本。" >&2; exit 1; }
|
||||||
|
git -C "$repo_root" rev-parse --is-inside-work-tree >/dev/null || {
|
||||||
|
echo "请从 git clone 得到的仓库中执行本脚本。" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
install_docker_if_missing() {
|
||||||
|
if command -v docker >/dev/null && docker compose version >/dev/null 2>&1; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
command -v apt-get >/dev/null || {
|
||||||
|
echo "本脚本仅自动安装基于 apt 的 Docker;请先安装 Docker Engine 和 Docker Compose v2。" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
|
apt-get update
|
||||||
|
if apt-cache show docker-compose-v2 >/dev/null 2>&1; then
|
||||||
|
apt-get install -y docker.io docker-compose-v2
|
||||||
|
elif apt-cache show docker-compose-plugin >/dev/null 2>&1; then
|
||||||
|
apt-get install -y docker.io docker-compose-plugin
|
||||||
|
else
|
||||||
|
echo "当前APT源没有 Docker Compose v2 插件,无法安全继续。" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
systemctl enable --now docker
|
||||||
|
docker compose version >/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
install_docker_if_missing
|
||||||
|
|
||||||
|
commit_sha="$(git -C "$repo_root" rev-parse HEAD)"
|
||||||
|
commit_short="$(git -C "$repo_root" rev-parse --short=12 HEAD)"
|
||||||
|
build_time="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||||
|
image="dobotai-dingtalk:local-${commit_short}"
|
||||||
|
|
||||||
|
echo "[1/3] 构建 DobotAI 镜像:$image"
|
||||||
|
docker build \
|
||||||
|
--build-arg "VERSION=local-${commit_short}" \
|
||||||
|
--build-arg "COMMIT_SHA=${commit_sha}" \
|
||||||
|
--build-arg "BUILD_TIME=${build_time}" \
|
||||||
|
--label "org.opencontainers.image.revision=${commit_sha}" \
|
||||||
|
--tag "$image" \
|
||||||
|
"$repo_root"
|
||||||
|
|
||||||
|
echo "[2/3] 准备独立PostgreSQL、目录、配置文件和迁移"
|
||||||
|
export DOBOTAI_IMAGE="$image"
|
||||||
|
export DOBOTAI_POSTGRES_IMAGE="$postgres_image"
|
||||||
|
export DOBOTAI_SOURCE_ROOT="$repo_root"
|
||||||
|
exec bash "$repo_root/deploy/dobotai-dingtalk/install-docker.sh"
|
||||||
@@ -34,10 +34,7 @@ func TestDobotAIDingTalkComposeOwnsApplicationAndPostgres(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, required := range []string{
|
for _, required := range []string{"configure_env.py", "dobotai-dingtalk-start"} {
|
||||||
"configure_env.py",
|
|
||||||
"dobotai-dingtalk-start",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(string(configure), required) {
|
if !strings.Contains(string(configure), required) {
|
||||||
t.Errorf("configure command missing %q", required)
|
t.Errorf("configure command missing %q", required)
|
||||||
}
|
}
|
||||||
@@ -47,10 +44,7 @@ func TestDobotAIDingTalkComposeOwnsApplicationAndPostgres(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, required := range []string{
|
for _, required := range []string{"docker exec -it dobotai-dingtalk", "/usr/local/bin/dobotai bootstrap-admin"} {
|
||||||
"docker exec -it dobotai-dingtalk",
|
|
||||||
"/usr/local/bin/dobotai bootstrap-admin",
|
|
||||||
} {
|
|
||||||
if !strings.Contains(string(bind), required) {
|
if !strings.Contains(string(bind), required) {
|
||||||
t.Errorf("bind command missing %q", required)
|
t.Errorf("bind command missing %q", required)
|
||||||
}
|
}
|
||||||
@@ -133,15 +127,8 @@ func TestDobotAIDingTalkDockerInstallerPreparesDirectoriesDatabaseAndMigrations(
|
|||||||
}
|
}
|
||||||
source := string(data)
|
source := string(data)
|
||||||
for _, required := range []string{
|
for _, required := range []string{
|
||||||
"container",
|
"container", "secrets", "backups", "migrations", "postgres.env",
|
||||||
"secrets",
|
"dobotai-postgres", "apply-migrations.sh", "docker compose", "umask 077",
|
||||||
"backups",
|
|
||||||
"migrations",
|
|
||||||
"postgres.env",
|
|
||||||
"dobotai-postgres",
|
|
||||||
"apply-migrations.sh",
|
|
||||||
"docker compose",
|
|
||||||
"umask 077",
|
|
||||||
} {
|
} {
|
||||||
if !strings.Contains(source, required) {
|
if !strings.Contains(source, required) {
|
||||||
t.Errorf("docker installer missing %q", required)
|
t.Errorf("docker installer missing %q", required)
|
||||||
@@ -159,15 +146,34 @@ func TestDobotAIDingTalkExistingDatabaseMigrationHasBackupAndRollbackBoundary(t
|
|||||||
}
|
}
|
||||||
source := string(data)
|
source := string(data)
|
||||||
for _, required := range []string{
|
for _, required := range []string{
|
||||||
"pg_dump -Fc",
|
"pg_dump -Fc", "pg_restore --list", "dobotai-postgres-data",
|
||||||
"pg_restore --list",
|
"dobotai-dingtalk", "rollback", "apply-migrations.sh",
|
||||||
"dobotai-postgres-data",
|
|
||||||
"dobotai-dingtalk",
|
|
||||||
"rollback",
|
|
||||||
"apply-migrations.sh",
|
|
||||||
} {
|
} {
|
||||||
if !strings.Contains(source, required) {
|
if !strings.Contains(source, required) {
|
||||||
t.Errorf("database adoption script missing %q", required)
|
t.Errorf("database adoption script missing %q", required)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRootDockerInstallerIsCloneAndRunEntryPoint(t *testing.T) {
|
||||||
|
data, err := os.ReadFile("../install-docker.sh")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
source := string(data)
|
||||||
|
for _, required := range []string{
|
||||||
|
"docker build",
|
||||||
|
"dobotai-dingtalk:local-",
|
||||||
|
"install_docker_if_missing",
|
||||||
|
"docker compose version",
|
||||||
|
"DOBOTAI_SOURCE_ROOT",
|
||||||
|
"deploy/dobotai-dingtalk/install-docker.sh",
|
||||||
|
} {
|
||||||
|
if !strings.Contains(source, required) {
|
||||||
|
t.Errorf("root installer missing %q", required)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.Contains(source, "${DOBOTAI_IMAGE") || strings.Contains(source, "DOBOTAI_IMAGE=?") {
|
||||||
|
t.Fatal("root installer must not require an operator supplied DOBOTAI_IMAGE")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user