78 lines
2.8 KiB
Bash
Executable File
78 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# crm-new 一键启动:生成 .env、渲染 LibreDesk 配置、启动全栈、等待健康检查。
|
||
set -euo pipefail
|
||
|
||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
cd "$ROOT"
|
||
|
||
if [ ! -f .env ]; then
|
||
echo "==> 生成 .env(随机密钥)"
|
||
cp .env.example .env
|
||
node - "$ROOT" <<'NODE'
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const crypto = require('crypto');
|
||
const p = path.join(process.argv[2], '.env');
|
||
let s = fs.readFileSync(p, 'utf8');
|
||
const set = (k, v) => {
|
||
const re = new RegExp('^' + k + '=.*$', 'm');
|
||
s = re.test(s) ? s.replace(re, `${k}=${v}`) : `${s}\n${k}=${v}`;
|
||
};
|
||
const strongPassword = () => `Yehwang${crypto.randomInt(1000, 9999)}!${crypto.randomBytes(4).toString('hex')}`;
|
||
|
||
set('POSTGRES_PASSWORD', crypto.randomBytes(12).toString('hex'));
|
||
set('LIBREDESK_ENC_KEY', crypto.randomBytes(16).toString('hex'));
|
||
set('LIBREDESK_SYSTEM_USER_PASSWORD', strongPassword());
|
||
set('TOOLS_SHARED_KEY', crypto.randomBytes(16).toString('hex'));
|
||
set('TWENTY_APP_SECRET', crypto.randomBytes(32).toString('hex'));
|
||
set('TWENTY_ENCRYPTION_KEY', crypto.randomBytes(32).toString('base64'));
|
||
set('TWENTY_FALLBACK_ENCRYPTION_KEY', crypto.randomBytes(32).toString('base64'));
|
||
fs.writeFileSync(p, s, { mode: 0o600 });
|
||
NODE
|
||
echo " .env 已生成(含密钥,勿提交)"
|
||
fi
|
||
|
||
echo "==> 渲染 LibreDesk 配置"
|
||
node scripts/render-config.js
|
||
|
||
mkdir -p libredesk/uploads
|
||
touch libredesk/uploads/.gitkeep
|
||
|
||
echo "==> 构建并启动全栈(首次 Twenty 迁移约 1-3 分钟)"
|
||
docker compose up -d --build
|
||
|
||
wait_http() {
|
||
local name="$1" url="$2" tries="${3:-150}"
|
||
for i in $(seq 1 "$tries"); do
|
||
if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
|
||
echo " ✓ ${name} (${i} 次探测)"
|
||
return 0
|
||
fi
|
||
sleep 2
|
||
done
|
||
echo "!! ${name} 等待超时:$url" >&2
|
||
return 1
|
||
}
|
||
|
||
echo "==> 等待服务就绪"
|
||
wait_http "LibreDesk" "http://localhost:9000/health" 150
|
||
wait_http "Twenty" "http://localhost:3100/healthz" 180
|
||
wait_http "bridge" "http://localhost:4100/healthz" 60
|
||
|
||
# shellcheck disable=SC1091
|
||
set -a; . ./.env; set +a
|
||
echo
|
||
echo "==================== crm-new 已启动 ===================="
|
||
echo " LibreDesk : http://localhost:9000 (System / ${LIBREDESK_SYSTEM_USER_PASSWORD})"
|
||
echo " Twenty : http://localhost:3100 (首次访问需注册 workspace)"
|
||
echo " bridge : http://localhost:4100 (/healthz, /tools/manifest)"
|
||
echo " Postgres : localhost:5440 (libredesk / twenty / bridge)"
|
||
echo " Redis : localhost:6390"
|
||
echo
|
||
echo " 下一步:"
|
||
echo " 1. 打开 LibreDesk 配 AI Provider(DashScope)"
|
||
echo " 2. 打开 Twenty 注册首个账号,创建 API key 填进 .env"
|
||
echo " 3. 生成 LibreDesk System API key 填进 .env,然后 node scripts/register-tools.js"
|
||
echo " 4. 建 Live chat / Email / WhatsApp inbox 与 AI Assistant"
|
||
echo "======================================================="
|