chore: initialize local deployment baseline
This commit is contained in:
commit
9e2196d69f
30 files changed
+1436
No files matched your search
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
if [[ "${EUID}" -eq 0 ]]; then
|
||||
sudo_command=()
|
||||
else
|
||||
sudo_command=(sudo)
|
||||
fi
|
||||
|
||||
if [[ ! -r /etc/os-release ]]; then
|
||||
printf '/etc/os-release is unavailable.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source /etc/os-release
|
||||
if [[ "${ID:-}" != "ubuntu" || "${VERSION_ID:-}" != "22.04" ]]; then
|
||||
printf 'This script requires Ubuntu 22.04; found %s %s.\n' "${ID:-unknown}" "${VERSION_ID:-unknown}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
timezone_name="${TIMEZONE_NAME:-Asia/Shanghai}"
|
||||
deploy_user="${DEPLOY_USER:-${SUDO_USER:-}}"
|
||||
|
||||
"${sudo_command[@]}" timedatectl set-timezone "$timezone_name"
|
||||
"${sudo_command[@]}" apt-get update
|
||||
"${sudo_command[@]}" apt-get install -y ca-certificates curl gnupg git jq rsync tmux htop sysstat iotop nvme-cli smartmontools unzip
|
||||
|
||||
if [[ "${RUN_UPGRADE:-0}" == "1" ]]; then
|
||||
"${sudo_command[@]}" apt-get upgrade -y
|
||||
fi
|
||||
|
||||
"${sudo_command[@]}" install -m 0755 -d /etc/apt/keyrings
|
||||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
|
||||
| "${sudo_command[@]}" gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
|
||||
"${sudo_command[@]}" chmod a+r /etc/apt/keyrings/docker.gpg
|
||||
|
||||
architecture="$(dpkg --print-architecture)"
|
||||
codename="${VERSION_CODENAME}"
|
||||
printf 'deb [arch=%s signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu %s stable\n' \
|
||||
"$architecture" "$codename" \
|
||||
| "${sudo_command[@]}" tee /etc/apt/sources.list.d/docker.list >/dev/null
|
||||
|
||||
"${sudo_command[@]}" apt-get update
|
||||
"${sudo_command[@]}" apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
||||
"${sudo_command[@]}" systemctl enable --now docker
|
||||
|
||||
if [[ -n "$deploy_user" && "$deploy_user" != "root" ]]; then
|
||||
"${sudo_command[@]}" usermod -aG docker "$deploy_user"
|
||||
printf 'Added %s to the docker group; start a new SSH session before using docker without sudo.\n' "$deploy_user"
|
||||
fi
|
||||
|
||||
"${sudo_command[@]}" docker version
|
||||
"${sudo_command[@]}" docker compose version
|
||||
"${sudo_command[@]}" systemctl is-active docker
|
||||
|
||||
if [[ -e /var/run/reboot-required ]]; then
|
||||
printf 'A reboot is required. Keep the current SSH session until key login and the reserved address are rechecked.\n'
|
||||
fi
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
section() {
|
||||
printf '\n## %s\n' "$1"
|
||||
}
|
||||
|
||||
section "Snapshot"
|
||||
printf 'captured_at=%s\n' "$(date --iso-8601=seconds)"
|
||||
uptime
|
||||
|
||||
section "Memory"
|
||||
free -h
|
||||
if command -v vmstat >/dev/null 2>&1; then
|
||||
vmstat 1 5
|
||||
fi
|
||||
|
||||
section "Storage"
|
||||
df --human-readable --print-type
|
||||
if command -v iostat >/dev/null 2>&1; then
|
||||
iostat -xz 1 3
|
||||
fi
|
||||
|
||||
section "Network summary"
|
||||
if command -v ss >/dev/null 2>&1; then
|
||||
ss --summary
|
||||
fi
|
||||
|
||||
section "Containers"
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
|
||||
docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}'
|
||||
else
|
||||
printf '%s\n' 'docker is not installed'
|
||||
fi
|
||||
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
section() {
|
||||
printf '\n## %s\n' "$1"
|
||||
}
|
||||
|
||||
run_optional() {
|
||||
local command_name="$1"
|
||||
shift
|
||||
|
||||
if command -v "$command_name" >/dev/null 2>&1; then
|
||||
"$command_name" "$@" 2>&1 || true
|
||||
else
|
||||
printf '%s is not installed\n' "$command_name"
|
||||
fi
|
||||
}
|
||||
|
||||
section "Collection"
|
||||
printf 'collected_at=%s\n' "$(date --iso-8601=seconds)"
|
||||
printf 'collector_version=1\n'
|
||||
|
||||
section "Operating system"
|
||||
if [[ -r /etc/os-release ]]; then
|
||||
sed -n -E '/^(NAME|VERSION|VERSION_ID|VERSION_CODENAME|PRETTY_NAME)=/p' /etc/os-release
|
||||
fi
|
||||
run_optional uname -a
|
||||
run_optional timedatectl status
|
||||
|
||||
section "CPU"
|
||||
run_optional lscpu
|
||||
|
||||
section "Memory"
|
||||
run_optional free -h
|
||||
run_optional swapon --show --bytes
|
||||
if command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null && command -v dmidecode >/dev/null 2>&1; then
|
||||
sudo -n dmidecode --type memory 2>/dev/null \
|
||||
| sed -n -E '/^[[:space:]]*(Error Correction Type|Size|Form Factor|Locator|Bank Locator|Type|Speed|Configured Memory Speed):/p'
|
||||
else
|
||||
printf 'dmidecode skipped: passwordless sudo or dmidecode is unavailable\n'
|
||||
fi
|
||||
|
||||
section "Block devices"
|
||||
run_optional lsblk --output NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,MODEL
|
||||
run_optional df --human-readable --print-type
|
||||
|
||||
section "Network link state"
|
||||
for interface_path in /sys/class/net/*; do
|
||||
[[ -e "$interface_path" ]] || continue
|
||||
interface_name="${interface_path##*/}"
|
||||
interface_state="$(cat "$interface_path/operstate" 2>/dev/null || printf 'unknown')"
|
||||
printf '%s state=%s\n' "$interface_name" "$interface_state"
|
||||
if command -v ethtool >/dev/null 2>&1 && [[ "$interface_name" != "lo" ]]; then
|
||||
ethtool "$interface_name" 2>/dev/null \
|
||||
| sed -n -E '/^[[:space:]]*(Speed|Duplex|Auto-negotiation|Link detected):/p' || true
|
||||
fi
|
||||
done
|
||||
|
||||
section "Container runtime"
|
||||
run_optional docker version
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
docker compose version 2>&1 || true
|
||||
fi
|
||||
|
||||
section "Security notice"
|
||||
printf '%s\n' 'This collector intentionally omits IP addresses, MAC addresses, serial numbers and machine UUIDs.'
|
||||
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
target_path="${1:-compose/.env}"
|
||||
force_write="${FORCE_WRITE:-0}"
|
||||
|
||||
if [[ -e "$target_path" && "$force_write" != "1" ]]; then
|
||||
printf 'Refusing to overwrite %s; set FORCE_WRITE=1 to replace it.\n' "$target_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v openssl >/dev/null 2>&1; then
|
||||
printf 'openssl is required to generate local secrets.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
target_directory="$(dirname -- "$target_path")"
|
||||
mkdir -p -- "$target_directory"
|
||||
umask 077
|
||||
|
||||
random_secret() {
|
||||
openssl rand -hex 32
|
||||
}
|
||||
|
||||
cat > "$target_path" <<EOF
|
||||
COMPOSE_PROJECT_NAME=ai-soulmate-infra
|
||||
DOCKER_NETWORK_NAME=ai-soulmate-internal
|
||||
TZ=Asia/Shanghai
|
||||
|
||||
POSTGRES_IMAGE=postgres:16-alpine
|
||||
POSTGRES_DB=ai_soulmate
|
||||
POSTGRES_USER=ai_soulmate
|
||||
POSTGRES_PASSWORD=$(random_secret)
|
||||
POSTGRES_HOST_PORT=5432
|
||||
POSTGRES_VOLUME_NAME=ai-soulmate-postgres-data
|
||||
|
||||
REDIS_IMAGE=redis:7-alpine
|
||||
REDIS_PASSWORD=$(random_secret)
|
||||
REDIS_HOST_PORT=6379
|
||||
REDIS_VOLUME_NAME=ai-soulmate-redis-data
|
||||
|
||||
MINIO_IMAGE=minio/minio:latest
|
||||
MINIO_MC_IMAGE=minio/mc:latest
|
||||
MINIO_ROOT_USER=ai_soulmate_admin
|
||||
MINIO_ROOT_PASSWORD=$(random_secret)
|
||||
MINIO_BUCKET=ai-soulmate
|
||||
MINIO_API_HOST_PORT=9000
|
||||
MINIO_CONSOLE_HOST_PORT=9001
|
||||
MINIO_VOLUME_NAME=ai-soulmate-minio-data
|
||||
EOF
|
||||
|
||||
chmod 600 "$target_path"
|
||||
printf 'Created %s with generated secrets.\n' "$target_path"
|
||||
printf 'Replace mutable image tags with verified tags or digests before a release.\n'
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
source_root="${SOURCE_ROOT:-../sources}"
|
||||
captured_at="$(date --iso-8601=seconds)"
|
||||
|
||||
printf 'captured_at: "%s"\n' "$captured_at"
|
||||
printf 'source_root: "%s"\n' "$source_root"
|
||||
printf 'sources:\n'
|
||||
|
||||
repo_names=(ai-soulmate-server ai-soulmate-client mlkj-admin nginx-server)
|
||||
for repo_name in "${repo_names[@]}"; do
|
||||
repo_path="$source_root/$repo_name"
|
||||
printf ' - name: "%s"\n' "$repo_name"
|
||||
printf ' path: "%s"\n' "$repo_path"
|
||||
if [[ ! -d "$repo_path/.git" ]]; then
|
||||
printf ' available: false\n'
|
||||
printf ' reason: "git repository not found"\n'
|
||||
continue
|
||||
fi
|
||||
|
||||
commit_id="$(git -C "$repo_path" rev-parse HEAD)"
|
||||
branch_name="$(git -C "$repo_path" branch --show-current)"
|
||||
remote_url="$(git -C "$repo_path" remote get-url origin 2>/dev/null || true)"
|
||||
sanitized_remote="$(printf '%s' "$remote_url" | sed -E 's#(https?://)[^/@]+@#\1#')"
|
||||
if [[ -n "$(git -C "$repo_path" status --porcelain)" ]]; then
|
||||
local_status=dirty
|
||||
else
|
||||
local_status=clean
|
||||
fi
|
||||
tracked_env_files="$(git -C "$repo_path" ls-files | grep -E '(^|/)\.env($|\.)' | grep -vE '\.example$' || true)"
|
||||
if [[ -n "$tracked_env_files" ]]; then
|
||||
tracked_env=true
|
||||
else
|
||||
tracked_env=false
|
||||
fi
|
||||
|
||||
printf ' available: true\n'
|
||||
printf ' branch: "%s"\n' "$branch_name"
|
||||
printf ' commit: "%s"\n' "$commit_id"
|
||||
printf ' local_status: "%s"\n' "$local_status"
|
||||
printf ' origin: "%s"\n' "$sanitized_remote"
|
||||
printf ' tracked_env_files: %s\n' "$tracked_env"
|
||||
done
|
||||
|
||||
printf 'notes:\n'
|
||||
printf ' - "This report records the checked-out state only; it does not validate deployment compatibility."\n'
|
||||
printf ' - "Review tracked environment files and rotate any credentials before deployment."\n'
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}"
|
||||
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
compose_command=(docker compose --env-file "$env_file" -f "$compose_file")
|
||||
|
||||
"${compose_command[@]}" config --quiet
|
||||
"${compose_command[@]}" exec -T postgres sh -ec 'pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"'
|
||||
"${compose_command[@]}" exec -T redis sh -ec 'redis-cli -a "$REDIS_PASSWORD" ping 2>/dev/null | grep -q PONG'
|
||||
"${compose_command[@]}" exec -T minio sh -ec 'curl -fsS http://127.0.0.1:9000/minio/health/live >/dev/null'
|
||||
|
||||
minio_init_status="$("${compose_command[@]}" ps --all --format json minio-init)"
|
||||
if ! printf '%s' "$minio_init_status" \
|
||||
| jq -e 'if type == "array" then length == 1 and .[0].ExitCode == 0 else .ExitCode == 0 end' >/dev/null; then
|
||||
printf 'MinIO bucket initialization did not complete successfully.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"${compose_command[@]}" ps
|
||||
printf 'Infrastructure checks passed.\n'
|
||||
Reference in new issue
Block a user