You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

101 lines
3.1 KiB

#!/usr/bin/env bash
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/hw}"
CONFIG_DIR="${CONFIG_DIR:-/opt/hw.conf.d}"
DEFAULT_HOST_USER="${SUDO_USER:-$(id -un)}"
APP_USER="${APP_USER:-$DEFAULT_HOST_USER}"
APP_GROUP="${APP_GROUP:-$(id -gn "$APP_USER" 2>/dev/null || id -gn)}"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ENV_TEMPLATE_SOURCE="${ENV_TEMPLATE_SOURCE:-$PROJECT_ROOT/deploy/env/hw.env.example}"
ENV_FILE_NAME="${ENV_FILE_NAME:-hw.env}"
DOCKER_INSTALL_SCRIPT_URL="https://get.docker.com"
ALLOW_NON_ROOT="${ALLOW_NON_ROOT:-0}"
CONTAINER_UID="${CONTAINER_UID:-10001}"
CONTAINER_GID="${CONTAINER_GID:-10001}"
require_root() {
if [[ "$ALLOW_NON_ROOT" == "1" ]]; then
return
fi
if [[ "${EUID}" -ne 0 ]]; then
printf 'Please run this script as root.\n' >&2
exit 1
fi
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
require_command() {
if ! command_exists "$1"; then
printf 'Required command not found: %s\n' "$1" >&2
exit 1
fi
}
install_docker_if_missing() {
if command_exists docker; then
return
fi
if command_exists apt-get; then
printf 'Docker not found. Installing Docker via %s ...\n' "$DOCKER_INSTALL_SCRIPT_URL"
curl -fsSL "$DOCKER_INSTALL_SCRIPT_URL" | sh
return
fi
printf 'Docker is not installed and automatic installation is only supported on apt-based hosts.\n' >&2
printf 'Please install Docker manually, then re-run this script.\n' >&2
exit 1
}
main() {
require_root
require_command curl
install_docker_if_missing
install -d -m 755 -o "$APP_USER" -g "$APP_GROUP" "$APP_DIR"
install -d -m 755 -o "$APP_USER" -g "$APP_GROUP" "$APP_DIR/instance"
install -d -m 755 -o "$APP_USER" -g "$APP_GROUP" "$APP_DIR/instance/csv_previews"
if [[ "$ALLOW_NON_ROOT" == "1" ]]; then
install -d -m 750 "$CONFIG_DIR"
else
install -d -m 750 -o root -g "$APP_GROUP" "$CONFIG_DIR"
fi
if [[ "$ALLOW_NON_ROOT" == "1" ]]; then
chown -R "$CONTAINER_UID:$CONTAINER_GID" "$APP_DIR/instance" >/dev/null 2>&1 || true
else
chown -R "$CONTAINER_UID:$CONTAINER_GID" "$APP_DIR/instance"
fi
if [[ ! -f "$CONFIG_DIR/$ENV_FILE_NAME" ]]; then
if [[ "$ALLOW_NON_ROOT" == "1" ]]; then
install -m 640 "$ENV_TEMPLATE_SOURCE" "$CONFIG_DIR/$ENV_FILE_NAME"
else
install -m 640 -o root -g "$APP_GROUP" "$ENV_TEMPLATE_SOURCE" "$CONFIG_DIR/$ENV_FILE_NAME"
fi
printf 'Created %s from template. Please edit it before deployment.\n' "$CONFIG_DIR/$ENV_FILE_NAME"
else
printf 'Keeping existing %s\n' "$CONFIG_DIR/$ENV_FILE_NAME"
fi
if [[ "$ALLOW_NON_ROOT" != "1" ]] && id -u "$APP_USER" >/dev/null 2>&1; then
usermod -aG docker "$APP_USER" >/dev/null 2>&1 || true
fi
printf '\nContainer deployment prerequisites are ready.\n'
printf 'Next steps:\n'
printf '1. Sync project files into %s\n' "$APP_DIR"
printf '2. Edit %s/%s with real production values\n' "$CONFIG_DIR" "$ENV_FILE_NAME"
if [[ "$ALLOW_NON_ROOT" == "1" ]]; then
printf '3. Run ALLOW_NON_ROOT=1 bash scripts/deploy-container.sh\n'
else
printf '3. Run sudo bash scripts/deploy-container.sh\n'
fi
}
main "$@"