168 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
REPO_ROOT="$(cd "${INSTALL_DIR}/.." && pwd)"
# shellcheck source=install/lib/common.sh
source "${INSTALL_DIR}/lib/common.sh"
# shellcheck source=install/lib/logging.sh
source "${INSTALL_DIR}/lib/logging.sh"
# shellcheck source=install/lib/packages.sh
source "${INSTALL_DIR}/lib/packages.sh"
atlas_set_script_name "40-nginx.sh"
atlas_install_error_trap
ATLAS_SITE_ROOT="/var/www/atlas"
ATLAS_SITE_CONF_SOURCE="${REPO_ROOT}/config/nginx/atlas-default.conf"
ATLAS_SITE_CONF_DEST="/etc/nginx/sites-available/atlas-default.conf"
ATLAS_SITE_LINK="/etc/nginx/sites-enabled/atlas-default.conf"
NGINX_DEFAULT_LINK="/etc/nginx/sites-enabled/default"
NGINX_BACKUP_DIR="/etc/nginx/atlas-backups"
usage() {
cat <<'USAGE'
Usage: install/phases/40-nginx.sh [options]
Install nginx from Ubuntu repositories and deploy the temporary Atlas default
HTTP status site. This does not configure HTTPS or PlotDirector proxying.
Options:
--help Show this help text.
--version Show script/framework version.
--dry-run Show intended actions without making changes.
--verbose Print additional diagnostic output.
USAGE
}
while [[ "$#" -gt 0 ]]; do
case "$1" in
--help)
usage
exit "${ATLAS_EXIT_OK}"
;;
--version)
atlas_print_version
exit "${ATLAS_EXIT_OK}"
;;
--dry-run)
atlas_enable_dry_run
;;
--verbose)
atlas_enable_verbose
;;
*)
atlas_usage_error "Unknown option: $1"
exit "${ATLAS_EXIT_USAGE}"
;;
esac
shift
done
install_nginx() {
if atlas_package_installed nginx; then
atlas_log_ok "nginx is already installed"
else
atlas_install_packages nginx
fi
}
write_site_content() {
local tmp_file
tmp_file="$(mktemp)"
cat >"$tmp_file" <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Atlas</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; background: #f6f7f9; color: #1f2933; }
main { max-width: 42rem; margin: 12vh auto; padding: 2rem; }
h1 { font-size: 2rem; margin: 0 0 1rem; }
p { line-height: 1.5; }
</style>
</head>
<body>
<main>
<h1>Atlas is operational</h1>
<p>This is the temporary Atlas status site. Application routing has not been configured yet.</p>
</main>
</body>
</html>
HTML
if [[ "${ATLAS_DRY_RUN}" == "1" ]]; then
atlas_log_info "Dry-run mode: would deploy static site content to ${ATLAS_SITE_ROOT}"
rm -f "$tmp_file"
return 0
fi
sudo -n install -d -m 0755 -o www-data -g www-data "$ATLAS_SITE_ROOT"
sudo -n install -m 0644 -o www-data -g www-data "$tmp_file" "${ATLAS_SITE_ROOT}/index.html"
rm -f "$tmp_file"
}
deploy_nginx_config() {
if [[ "${ATLAS_DRY_RUN}" == "1" ]]; then
atlas_log_info "Dry-run mode: would deploy ${ATLAS_SITE_CONF_DEST}"
atlas_log_info "Dry-run mode: would enable Atlas nginx site and disable Ubuntu default site"
return 0
fi
sudo -n install -d -m 0755 /etc/nginx/sites-available /etc/nginx/sites-enabled
sudo -n install -d -m 0750 "$NGINX_BACKUP_DIR"
sudo -n nginx -t
if [[ -e "$ATLAS_SITE_CONF_DEST" ]] && ! cmp -s "$ATLAS_SITE_CONF_SOURCE" "$ATLAS_SITE_CONF_DEST"; then
sudo -n cp -a "$ATLAS_SITE_CONF_DEST" "${NGINX_BACKUP_DIR}/atlas-default.conf.$(date -u '+%Y%m%dT%H%M%SZ').bak"
fi
sudo -n install -m 0644 "$ATLAS_SITE_CONF_SOURCE" "$ATLAS_SITE_CONF_DEST"
if [[ -e "$NGINX_DEFAULT_LINK" ]]; then
sudo -n cp -a "$NGINX_DEFAULT_LINK" "${NGINX_BACKUP_DIR}/default-link.$(date -u '+%Y%m%dT%H%M%SZ').bak"
sudo -n rm -f "$NGINX_DEFAULT_LINK"
fi
sudo -n ln -sfn "$ATLAS_SITE_CONF_DEST" "$ATLAS_SITE_LINK"
sudo -n nginx -t
}
enable_nginx() {
atlas_sudo_run systemctl enable nginx
atlas_sudo_run systemctl restart nginx
}
verify_nginx() {
if [[ "${ATLAS_DRY_RUN}" == "1" ]]; then
atlas_log_info "Dry-run mode: would verify nginx service and health endpoint"
return 0
fi
sudo -n nginx -t
systemctl is-enabled nginx >/dev/null
systemctl is-active nginx >/dev/null
curl -fsS http://127.0.0.1/health >/dev/null
curl -fsS http://192.168.10.10/health >/dev/null
atlas_log_ok "nginx verification passed"
}
main() {
atlas_require_sudo_noninteractive || {
atlas_log_error "Passwordless non-interactive sudo is required"
exit "${ATLAS_EXIT_PREREQ}"
}
install_nginx
write_site_content
deploy_nginx_config
enable_nginx
verify_nginx
}
main