feat: 完善安装部署与数字溯源码
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
# /// script
|
||||
# requires-python = ">=3.9"
|
||||
# dependencies = ["pyyaml"]
|
||||
# ///
|
||||
"""Build a portable Artveris release archive.
|
||||
|
||||
The archive contains the compiled API, admin/client/install static assets,
|
||||
runtime templates, and PM2 startup config. It does not bake database, Redis,
|
||||
domain, OSS, CDN, or blockchain secrets into the build.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
DIST = ROOT / ".release"
|
||||
|
||||
|
||||
def run(cmd: str) -> None:
|
||||
print(f"[artveris-release] $ {cmd}")
|
||||
proc = subprocess.run(cmd, cwd=str(ROOT), shell=True)
|
||||
if proc.returncode != 0:
|
||||
sys.exit(proc.returncode)
|
||||
|
||||
|
||||
def ensure_pnpm() -> None:
|
||||
if shutil.which("pnpm"):
|
||||
return
|
||||
if not shutil.which("corepack"):
|
||||
raise SystemExit("pnpm/corepack not found. Please install Node.js 20+.")
|
||||
run("corepack enable")
|
||||
run("corepack prepare pnpm@10.28.0 --activate")
|
||||
|
||||
|
||||
def copytree(src: Path, dst: Path) -> None:
|
||||
if dst.exists():
|
||||
shutil.rmtree(dst)
|
||||
shutil.copytree(src, dst)
|
||||
|
||||
|
||||
def write_json(path: Path, data: dict) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
def write_text(path: Path, text: str) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8", newline="\n")
|
||||
|
||||
|
||||
def build() -> Path:
|
||||
stamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
package_dir = DIST / f"artveris-{stamp}"
|
||||
if DIST.exists():
|
||||
shutil.rmtree(DIST)
|
||||
package_dir.mkdir(parents=True)
|
||||
|
||||
ensure_pnpm()
|
||||
run("pnpm install --frozen-lockfile")
|
||||
run("pnpm build:shared")
|
||||
run("pnpm --filter @artveris/api exec prisma generate")
|
||||
run("pnpm --filter @artveris/api build")
|
||||
run("pnpm --filter @artveris/admin build")
|
||||
run("pnpm --filter @artveris/client build")
|
||||
run("pnpm --filter @artveris/install build")
|
||||
|
||||
copytree(ROOT / "apps/api/dist", package_dir / "apps/api/dist")
|
||||
copytree(ROOT / "apps/api/prisma", package_dir / "apps/api/prisma")
|
||||
shutil.copy2(ROOT / "apps/api/package.json", package_dir / "apps/api/package.json")
|
||||
shared_dir = package_dir / "packages/shared"
|
||||
shared_dir.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(ROOT / "packages/shared/package.json", shared_dir / "package.json")
|
||||
copytree(ROOT / "packages/shared/dist", shared_dir / "dist")
|
||||
for app in ("admin", "client", "install"):
|
||||
app_dir = package_dir / "apps" / app
|
||||
app_dir.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(ROOT / "apps" / app / "package.json", app_dir / "package.json")
|
||||
copytree(ROOT / "apps/admin/dist", package_dir / "apps/api/public/admin")
|
||||
copytree(ROOT / "apps/client/dist", package_dir / "apps/api/public/trace")
|
||||
copytree(ROOT / "apps/install/dist", package_dir / "apps/api/public/install")
|
||||
shutil.copy2(ROOT / "package.json", package_dir / "package.json")
|
||||
shutil.copy2(ROOT / "pnpm-lock.yaml", package_dir / "pnpm-lock.yaml")
|
||||
shutil.copy2(ROOT / "pnpm-workspace.yaml", package_dir / "pnpm-workspace.yaml")
|
||||
shutil.copy2(ROOT / ".env.example", package_dir / ".env.example")
|
||||
|
||||
write_json(
|
||||
package_dir / "ecosystem.config.cjs",
|
||||
{
|
||||
"apps": [
|
||||
{
|
||||
"name": "artveris",
|
||||
"cwd": "./apps/api",
|
||||
"script": "dist/main.js",
|
||||
"instances": 1,
|
||||
"exec_mode": "fork",
|
||||
"env": {
|
||||
"NODE_ENV": "production",
|
||||
"ARTVERIS_RUNTIME_DIR": "../../runtime",
|
||||
"ARTVERIS_PUBLIC_DIR": "./public",
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
write_text(
|
||||
package_dir / "install-deps.sh",
|
||||
"""#!/usr/bin/env sh
|
||||
set -eu
|
||||
pnpm install --prod --frozen-lockfile
|
||||
pnpm --dir apps/api exec prisma generate --schema ./prisma/schema.prisma
|
||||
""",
|
||||
)
|
||||
write_text(
|
||||
package_dir / "install-deps.ps1",
|
||||
"""$ErrorActionPreference = 'Stop'
|
||||
pnpm install --prod --frozen-lockfile
|
||||
pnpm --dir apps/api exec prisma generate --schema ./prisma/schema.prisma
|
||||
""",
|
||||
)
|
||||
write_text(
|
||||
package_dir / "README-deploy.md",
|
||||
"""# Artveris Release
|
||||
|
||||
1. Install Node.js 20+ and pnpm.
|
||||
2. Run `sh install-deps.sh` in this directory.
|
||||
3. Start with `pm2 start ecosystem.config.cjs`.
|
||||
4. Open `http://server:3000/install` for first-time initialization.
|
||||
|
||||
After installation the process exits once. PM2, systemd, 1Panel, BaoTa, or Docker
|
||||
should restart it automatically, then `/admin`, `/trace`, `/api`, `/t/:code`,
|
||||
and `/uploads` are served by the same port.
|
||||
""",
|
||||
)
|
||||
|
||||
zip_path = DIST / f"{package_dir.name}.zip"
|
||||
tar_path = DIST / f"{package_dir.name}.tar.gz"
|
||||
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for file in package_dir.rglob("*"):
|
||||
zf.write(file, file.relative_to(package_dir.parent))
|
||||
with tarfile.open(tar_path, "w:gz") as tf:
|
||||
tf.add(package_dir, arcname=package_dir.name)
|
||||
return package_dir
|
||||
|
||||
|
||||
def main() -> None:
|
||||
package_dir = build()
|
||||
print("[artveris] Release package created:")
|
||||
print(f" {package_dir}")
|
||||
print(f" {package_dir.with_suffix('.zip')}")
|
||||
print(f" {package_dir.with_suffix('.tar.gz')}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user