#!/usr/bin/env bash set -Eeuo pipefail usage() { echo '用法:generate-env.sh --public-host admin.example.com --files-host files.example.com [--output /etc/lehuo/lehuo.env]' } public_host='' files_host='' output='/etc/lehuo/lehuo.env' while (($#)); do case "$1" in --public-host) public_host="${2:-}"; shift 2 ;; --files-host) files_host="${2:-}"; shift 2 ;; --output) output="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) usage >&2; exit 1 ;; esac done [[ "$public_host" =~ ^[A-Za-z0-9.-]+$ ]] || { echo 'PUBLIC_HOST 格式无效。' >&2; exit 1; } [[ "$files_host" =~ ^[A-Za-z0-9.-]+$ ]] || { echo 'FILES_HOST 格式无效。' >&2; exit 1; } [[ "$public_host" != "$files_host" ]] || { echo '管理后台/API 域名与文件域名必须不同。' >&2; exit 1; } command -v openssl >/dev/null 2>&1 || { echo '缺少 openssl。' >&2; exit 1; } command -v git >/dev/null 2>&1 || { echo '缺少 git。' >&2; exit 1; } if [[ -e "$output" ]]; then printf '拒绝覆盖已有环境文件:%s\n' "$output" >&2 exit 1 fi install -d -m 0700 "$(dirname -- "$output")" umask 077 mysql_password="$(openssl rand -hex 24)" mysql_root_password="$(openssl rand -hex 32)" redis_password="$(openssl rand -hex 32)" minio_password="$(openssl rand -hex 32)" session_secret="$(openssl rand -hex 32)" session_pepper="$(openssl rand -hex 32)" audit_key="$(openssl rand -hex 32)" field_key="$(openssl rand -base64 32 | tr -d '\n')" image_tag="$(git -C "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../../.." && pwd)" rev-parse --short=12 HEAD)" cat > "$output" <