86 lines
2.1 KiB
Bash
86 lines
2.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
|
|
APP_PORT="${FURNITURE_PROD_APP_PORT:-3001}"
|
|
SERVICE_NAME="${EMPLOYEE_DIRECTORY_SYNC_SERVICE_NAME:-furniture-employee-directory-sync}"
|
|
APP_ROOT="$BASE/current/furniture-3d-system"
|
|
SHARED="$BASE/shared"
|
|
APP_ENV="$SHARED/.env"
|
|
SYNC_ENV="$SHARED/employee-directory-sync.env"
|
|
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
|
|
TIMER_FILE="/etc/systemd/system/$SERVICE_NAME.timer"
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "This installer must run as root." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "$APP_ROOT" ]; then
|
|
echo "Missing app root: $APP_ROOT" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$APP_ENV" ]; then
|
|
echo "Missing app env: $APP_ENV" >&2
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p "$SHARED"
|
|
if [ ! -f "$SYNC_ENV" ]; then
|
|
umask 077
|
|
cat >"$SYNC_ENV" <<EOF
|
|
# Furniture employee directory sync source.
|
|
# Fill these values on the production server. Do not commit secrets to Git.
|
|
HR_SQLSERVER_HOST=
|
|
HR_SQLSERVER_PORT=1433
|
|
HR_SQLSERVER_DATABASE=
|
|
HR_SQLSERVER_USERNAME=
|
|
HR_SQLSERVER_PASSWORD=
|
|
HR_SQLSERVER_ENCRYPT=false
|
|
HR_SQLSERVER_TRUST_SERVER_CERTIFICATE=true
|
|
HR_SQLSERVER_CONNECTION_TIMEOUT_MS=15000
|
|
HR_SQLSERVER_REQUEST_TIMEOUT_MS=60000
|
|
HR_SYNC_ENDPOINT=http://127.0.0.1:${APP_PORT}/api/internal/hr-sync
|
|
EOF
|
|
echo "Created env template: $SYNC_ENV"
|
|
fi
|
|
|
|
chmod 600 "$SYNC_ENV"
|
|
|
|
cat >"$SERVICE_FILE" <<EOF
|
|
[Unit]
|
|
Description=Furniture employee directory HR sync
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
WorkingDirectory=$APP_ROOT
|
|
Environment=NODE_ENV=production
|
|
EnvironmentFile=$APP_ENV
|
|
EnvironmentFile=$SYNC_ENV
|
|
ExecStart=/bin/bash -lc 'cd "$APP_ROOT" && ./node_modules/.bin/tsx scripts/sync-employee-directory.ts --apply'
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
EOF
|
|
|
|
cat >"$TIMER_FILE" <<EOF
|
|
[Unit]
|
|
Description=Run furniture employee directory HR sync twice daily
|
|
|
|
[Timer]
|
|
OnCalendar=*-*-* 03:00:00
|
|
OnCalendar=*-*-* 15:00:00
|
|
Persistent=true
|
|
RandomizedDelaySec=120
|
|
Unit=$SERVICE_NAME.service
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now "$SERVICE_NAME.timer"
|
|
systemctl list-timers --all "$SERVICE_NAME.timer" --no-pager
|