agenthub/deploy/nanobot-gateway/bootstrap.sh

108 lines
4.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 一键把「两个内嵌接入的 nanobot」接进 AgentHub:
# 1) 给 alpha / beta 各签一份 Agent 凭证(ag_…),只写进 .env
# 2) 生成 .env(模型密钥只进 .env,不进镜像)
# 3) build nanobot 镜像并 docker compose up -d
#
# 前置:hub 已在跑(默认 http://127.0.0.1:8100),本机 docker 可用。
# 这条路里没有桥接进程、也没有入站端口:nanobot 自己装着 agenthub 渠道,自己往外拨。
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(cd "${here}/../.." && pwd)"
env_file="${here}/.env"
hub_url="${AGENTHUB_URL:-http://127.0.0.1:8100}"
api_key="${AGENTHUB_API_KEY:-}"
if [ -z "${api_key}" ] && [ -f "${root}/data/hub-credentials.json" ]; then
api_key="$(python3 -c "import json;print(json.load(open('${root}/data/hub-credentials.json'))['api_key'])")"
fi
if [ -z "${api_key}" ]; then
echo "拿不到 hub API Key:设 AGENTHUB_API_KEY,或先跑一次 hub 生成 data/hub-credentials.json" >&2
exit 1
fi
# 复用已有 .env 里的密钥,重复执行不会每次都重新读一遍
old_key="$(sed -n 's/^DASHSCOPE_API_KEY=//p' "${env_file}" 2>/dev/null || true)"
key="${DASHSCOPE_API_KEY:-${old_key}}"
nanobot_cfg="${NANOBOT_CONFIG_SOURCE:-$HOME/.nanobot/config.json}"
if [ -z "${key}" ]; then
key="$(python3 - "${nanobot_cfg}" <<'PY'
import json, sys
try:
print(json.load(open(sys.argv[1]))["providers"]["dashscope"].get("apiKey") or "")
except Exception:
print("")
PY
)"
fi
# 有些部署走私有网关,端点跟密钥一样得带上
old_base="$(sed -n 's/^NANOBOT_PROVIDER_BASE=//p' "${env_file}" 2>/dev/null || true)"
provider_base="${NANOBOT_PROVIDER_BASE-${old_base}}"
if [ -z "${provider_base}" ]; then
provider_base="$(python3 - "${nanobot_cfg}" <<'PY'
import json, sys
try:
print(json.load(open(sys.argv[1]))["providers"]["dashscope"].get("apiBase") or "")
except Exception:
print("")
PY
)"
fi
if [ -z "${key}" ]; then
echo "缺 dashscope 的 API Key:设 DASHSCOPE_API_KEY 后再跑" >&2
exit 1
fi
# 一个 Agent 一份凭证。已存在就轮换(重复跑 bootstrap 不该因为重名而失败)。
issue_token() {
local name="$1" token
token="$(curl -s --noproxy '*' -X POST "${hub_url}/api/v1/agents" \
-H "X-API-Key: ${api_key}" -H 'content-type: application/json' \
-d "{\"name\":\"${name}\",\"driver\":\"nanobot\",\"connection_mode\":\"embedded\",\"deployment_kind\":\"docker\",\"issue_credential\":true}" \
| python3 -c "import json,sys;print(json.load(sys.stdin).get('agent_token',''))" 2>/dev/null || true)"
[ -n "${token}" ] && { echo "${token}"; return; }
curl -s --noproxy '*' -X POST "${hub_url}/api/v1/agents/${name}/credentials" \
-H "X-API-Key: ${api_key}" -H 'content-type: application/json' -d '{"grace_seconds":0}' \
| python3 -c "import json,sys;print(json.load(sys.stdin).get('agent_token',''))" 2>/dev/null || true
}
token_alpha="$(issue_token alpha)"
token_beta="$(issue_token beta)"
if [ -z "${token_alpha}" ] || [ -z "${token_beta}" ]; then
echo "签凭证失败,检查 hub 是否在 ${hub_url}" >&2
exit 1
fi
umask 077
cat > "${env_file}" <<ENV
# bootstrap.sh 生成,勿提交
HUB_URL=http://host.docker.internal:8100
DASHSCOPE_API_KEY=${key}
NANOBOT_PROVIDER=dashscope
NANOBOT_MODEL=${NANOBOT_MODEL:-qwen3.6-plus}
NANOBOT_PROVIDER_BASE=${provider_base}
AGENT_TOKEN_ALPHA=${token_alpha}
AGENT_TOKEN_BETA=${token_beta}
ENV
echo "写好了 ${env_file}"
# 宿主机 ~/.docker/buildx/activity 里若有 root 拥有的文件(历史上 sudo docker build 留下的),
# buildx 会 permission denied。给自己换一个 buildx 配置目录绕开,不需要 sudo。
unwritable=""
for f in "${HOME}"/.docker/buildx/activity/*; do
[ -f "${f}" ] && [ ! -w "${f}" ] && unwritable="${f}"
done
if [ -n "${unwritable}" ]; then
export BUILDX_CONFIG="${here}/.buildx"
mkdir -p "${BUILDX_CONFIG}"
echo "提示:${unwritable} 不可写,改用 BUILDX_CONFIG=${BUILDX_CONFIG}"
fi
echo "build nanobot:agenthub(首次比较久)..."
docker build -q -f "${root}/deploy/Dockerfile.nanobot" -t nanobot:agenthub "${root}/soucecode/nanobot" >/dev/null && echo " ok"
cd "${here}"
docker compose up -d
echo
docker compose ps