93 lines
2.3 KiB
Bash
Executable File
93 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
target_dir="${1:-$(pwd)}"
|
|
deploy_env="${2:-production}"
|
|
|
|
case "${deploy_env}" in
|
|
test)
|
|
mysql_env="${target_dir}/.env.test.mysql"
|
|
app_env="${target_dir}/.env.test"
|
|
node_env="test"
|
|
app_port="${ACCESS_MANAGE_TEST_PORT:-3501}"
|
|
mysql_port="${ACCESS_MANAGE_TEST_DB_PORT:-3308}"
|
|
mysql_database="${ACCESS_MANAGE_TEST_DB_NAME:-access_manage_test}"
|
|
;;
|
|
production)
|
|
mysql_env="${target_dir}/.env.production.mysql"
|
|
app_env="${target_dir}/.env.production"
|
|
node_env="production"
|
|
app_port="${ACCESS_MANAGE_PRODUCTION_PORT:-3500}"
|
|
mysql_port="${ACCESS_MANAGE_PRODUCTION_DB_PORT:-3307}"
|
|
mysql_database="${ACCESS_MANAGE_PRODUCTION_DB_NAME:-access_manage}"
|
|
;;
|
|
*)
|
|
echo "Usage: $0 [target_dir] [test|production]" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
if [[ -e "${app_env}" ]]; then
|
|
echo "Refuse to overwrite existing $(basename "${app_env}") in ${target_dir}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v openssl >/dev/null 2>&1; then
|
|
echo "openssl is required to generate production secrets" >&2
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p "${target_dir}"
|
|
umask 077
|
|
|
|
if [[ -e "${mysql_env}" ]]; then
|
|
# shellcheck disable=SC1090
|
|
source "${mysql_env}"
|
|
|
|
mysql_database="${MYSQL_DATABASE:-${mysql_database}}"
|
|
mysql_user="${MYSQL_USER:-access_user}"
|
|
mysql_password="${MYSQL_PASSWORD:-}"
|
|
|
|
if [[ -z "${mysql_password}" ]]; then
|
|
echo "MYSQL_PASSWORD is missing in ${mysql_env}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "Found existing ${mysql_env}; only creating ${app_env}"
|
|
else
|
|
mysql_root_password="root_$(openssl rand -hex 24)"
|
|
mysql_user="access_user"
|
|
mysql_password="app_$(openssl rand -hex 24)"
|
|
|
|
cat > "${mysql_env}" <<EOF
|
|
MYSQL_ROOT_PASSWORD=${mysql_root_password}
|
|
MYSQL_DATABASE=${mysql_database}
|
|
MYSQL_USER=${mysql_user}
|
|
MYSQL_PASSWORD=${mysql_password}
|
|
MYSQL_HOST_PORT=${mysql_port}
|
|
EOF
|
|
|
|
echo "Created ${mysql_env}"
|
|
fi
|
|
|
|
jwt_secret="$(openssl rand -hex 48)"
|
|
|
|
cat > "${app_env}" <<EOF
|
|
NODE_ENV=${node_env}
|
|
APP_ENV=${node_env}
|
|
APP_ENV_LABEL=$([[ "${deploy_env}" == "test" ]] && echo "测试环境" || echo "生产环境")
|
|
PORT=${app_port}
|
|
|
|
DB_HOST=127.0.0.1
|
|
DB_PORT=${mysql_port}
|
|
DB_USER=${mysql_user}
|
|
DB_PASSWORD=${mysql_password}
|
|
DB_NAME=${mysql_database}
|
|
DB_CONNECTION_LIMIT=10
|
|
|
|
JWT_SECRET=${jwt_secret}
|
|
JWT_EXPIRES_IN=2h
|
|
EOF
|
|
|
|
echo "Created ${app_env}"
|