diff --git a/Linux/install-aws-cli.sh b/Linux/install-aws-cli.sh new file mode 100644 index 0000000..b05eaa0 --- /dev/null +++ b/Linux/install-aws-cli.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# +# AWS CLI Installation for Debian/Ubuntu +# Installs the AWS CLI v2 via the official installer bundle when it is not +# already available on the system. Required by install-caddy.sh for +# downloading the Caddy binary from S3-compatible storage. +# + +set -euo pipefail + +# Ensure script is executed with root privileges +if [ "$(id -u)" -ne 0 ]; then + echo "Error: This script must be run as root." >&2 + exit 1 +fi + +if command -v aws >/dev/null 2>&1; then + echo "==> aws-cli already installed at '$(command -v aws)'. Nothing to do." + exit 0 +fi + +echo "==> aws-cli not found, installing via official bundle..." +TMP_DIR=$(mktemp -d) +trap 'rm -rf "${TMP_DIR}"' EXIT + +curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "${TMP_DIR}/awscliv2.zip" +unzip -q "${TMP_DIR}/awscliv2.zip" -d "${TMP_DIR}" +"${TMP_DIR}/aws/install" + +echo "=========================================================" +echo " aws-cli installed successfully: $(command -v aws)" +echo "=========================================================" diff --git a/Linux/install-base-deps.sh b/Linux/install-base-deps.sh new file mode 100644 index 0000000..673601a --- /dev/null +++ b/Linux/install-base-deps.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# +# Base Dependencies Installation for Debian/Ubuntu +# Installs the packages required by the other server initialisation scripts. +# + +set -euo pipefail + +# Ensure script is executed with root privileges +if [ "$(id -u)" -ne 0 ]; then + echo "Error: This script must be run as root." >&2 + exit 1 +fi + +echo "==> Updating package index..." +apt-get update -qq + +echo "==> Installing base dependencies (acl, libcap2-bin, rsync, curl, unzip)..." +apt-get install -y acl libcap2-bin rsync curl unzip + +echo "=========================================================" +echo " Base dependencies installed successfully!" +echo "=========================================================" diff --git a/Linux/init-caddy.sh b/Linux/install-caddy.sh similarity index 51% rename from Linux/init-caddy.sh rename to Linux/install-caddy.sh index 55504d4..f87d7d9 100644 --- a/Linux/init-caddy.sh +++ b/Linux/install-caddy.sh @@ -1,12 +1,16 @@ #!/usr/bin/env bash # -# Caddy & Bot Deployment Environment Initialisation Script for Debian -# Supports automatic binary download from S3-compatible storage. +# Caddy Installation for Debian/Ubuntu +# Downloads the Caddy binary (from a local file or S3-compatible storage), +# creates the caddy:caddy system user and group, installs the binary with +# cap_net_bind_service, prepares the config directory and registers the +# systemd unit. The service itself is started by prepare-caddy-cicd.sh once +# the site Caddyfile and web root exist. # set -euo pipefail -# S3 and Binary Environment Variables (with fallback defaults) +# S3 and binary environment variables (with fallback defaults) S3_ENDPOINT="${S3_ENDPOINT:-}" S3_BUCKET="${S3_BUCKET:-}" S3_REGION="${S3_REGION:-us-east-1}" @@ -16,7 +20,6 @@ BIN_URI="${BIN_URI:-caddy-custom-linux-amd64}" BIN_SOURCE="./${BIN_URI}" BIN_DEST="/usr/local/bin/caddy" -WWW_ROOT="/var/www" CADDY_CONF_DIR="/etc/caddy" CADDY_DATA_DIR="/var/lib/caddy" @@ -26,21 +29,7 @@ if [ "$(id -u)" -ne 0 ]; then exit 1 fi -echo "==> [1/8] Installing base dependencies..." -apt-get update -qq -apt-get install -y acl libcap2-bin rsync curl unzip - -echo "==> [2/8] Ensuring aws-cli is installed..." -if ! command -v aws >/dev/null 2>&1; then - echo "aws-cli not found, installing via official bundle..." - TMP_DIR=$(mktemp -d) - curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "${TMP_DIR}/awscliv2.zip" - unzip -q "${TMP_DIR}/awscliv2.zip" -d "${TMP_DIR}" - "${TMP_DIR}/aws/install" - rm -rf "${TMP_DIR}" -fi - -echo "==> [3/8] Resolving Caddy binary..." +echo "==> [1/5] Resolving Caddy binary..." if [ ! -f "${BIN_SOURCE}" ]; then if [ -n "${S3_BUCKET}" ]; then echo "Local binary '${BIN_SOURCE}' not found. Downloading from S3 bucket '${S3_BUCKET}'..." @@ -65,7 +54,7 @@ else echo "Found local binary at '${BIN_SOURCE}', skipping S3 download." fi -echo "==> [4/8] Creating system users and groups..." +echo "==> [2/5] Creating caddy system user and group..." # Create caddy system user and group (no login shell, dedicated home) if ! getent group caddy >/dev/null 2>&1; then groupadd --system caddy @@ -81,72 +70,20 @@ if ! id -u caddy >/dev/null 2>&1; then caddy fi -# Create deployment user 'bot' -if ! id -u bot >/dev/null 2>&1; then - useradd -m -s /bin/bash bot -fi - -# Configure SSH directory for bot -install -d -m 700 -o bot -g bot /home/bot/.ssh -touch /home/bot/.ssh/authorized_keys -chown bot:bot /home/bot/.ssh/authorized_keys -chmod 600 /home/bot/.ssh/authorized_keys - -echo "==> [5/8] Installing Caddy binary and granting capabilities..." +echo "==> [3/5] Installing Caddy binary and granting capabilities..." install -m 750 -o root -g caddy "${BIN_SOURCE}" "${BIN_DEST}" # Grant capability to bind to privileged ports (<1024) without running as root setcap 'cap_net_bind_service=+ep' "${BIN_DEST}" -echo "==> [6/8] Setting up web root directory permissions (${WWW_ROOT})..." -mkdir -p "${WWW_ROOT}" - -# Set base ownership: bot (deployment) and caddy (web server group) -chown -R bot:caddy "${WWW_ROOT}" - -# Directory base permissions (SGID ensures newly created items inherit group 'caddy') -find "${WWW_ROOT}" -type d -exec chmod 2755 {} + -find "${WWW_ROOT}" -type f -exec chmod 644 {} + - -# Apply POSIX ACLs: bot has rwx, caddy has read/execute, with default inheritance -setfacl -R -m u:bot:rwx,u:caddy:rX "${WWW_ROOT}" -setfacl -R -d -m u:bot:rwx,u:caddy:rX "${WWW_ROOT}" - -echo "==> [7/8] Creating Caddy configuration and sample site..." +echo "==> [4/5] Preparing config directory (${CADDY_CONF_DIR})..." mkdir -p "${CADDY_CONF_DIR}" chown root:caddy "${CADDY_CONF_DIR}" chmod 755 "${CADDY_CONF_DIR}" -if [ ! -f "${CADDY_CONF_DIR}/Caddyfile" ]; then - cat <<'EOF' > "${CADDY_CONF_DIR}/Caddyfile" -:80 { - root * /var/www - file_server -} -EOF - chown root:caddy "${CADDY_CONF_DIR}/Caddyfile" - chmod 644 "${CADDY_CONF_DIR}/Caddyfile" -fi - -if [ ! -f "${WWW_ROOT}/index.html" ]; then - cat <<'EOF' > "${WWW_ROOT}/index.html" - - -
- -Static files root: /var/www
Static files root: ${WEB_ROOT}