Files
linux-server-setup/scripts/setup.sh
Francesco Picone 87d6ffe8a0 Primo caricamento
2025-12-29 16:00:23 +01:00

141 lines
4.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# Questo script prepara un Ubuntu Server con utilità di base,
# installa Docker, chiede un utente da creare e lo aggiunge al gruppo docker,
# esegue i post-install tramite Make, e deploya Portainer dal repository indicato.
TIMEZONE_DEFAULT="Europe/Rome"
require_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "[ERRORE] Esegui questo script come utente root (sudo)." >&2
exit 1
fi
}
prompt_user() {
read -rp "Inserisci il nome utente da creare: " NEW_USER
if [ -z "${NEW_USER}" ]; then
echo "[ERRORE] Il nome utente non può essere vuoto." >&2
exit 1
fi
read -rp "Inserisci il timezone (es. Europe/Rome) [${TIMEZONE_DEFAULT}]: " TIMEZONE
TIMEZONE=${TIMEZONE:-$TIMEZONE_DEFAULT}
}
create_user_if_needed() {
if id -u "${NEW_USER}" >/dev/null 2>&1; then
echo "[INFO] L'utente '${NEW_USER}' esiste già, procedo."
else
echo "[INFO] Creo l'utente '${NEW_USER}'."
useradd -m -s /bin/bash "${NEW_USER}"
echo "[INFO] Imposto la password per '${NEW_USER}'."
read -srp "Inserisci la password per ${NEW_USER}: " USER_PASS
echo
echo "${NEW_USER}:${USER_PASS}" | chpasswd
fi
}
configure_timezone() {
echo "[INFO] Configuro timezone su '${TIMEZONE}'..."
if command -v timedatectl >/dev/null 2>&1; then
timedatectl set-timezone "${TIMEZONE}" || echo "[ATTENZIONE] Impossibile impostare il timezone."
else
echo "[ATTENZIONE] 'timedatectl' non disponibile, salta configurazione timezone."
fi
}
install_base_packages() {
echo "[INFO] Aggiorno il sistema e installo pacchetti di base..."
export DEBIAN_FRONTEND=noninteractive
apt update && apt -y full-upgrade
apt install -y git curl htop zip unzip make
apt autoremove -y --purge
}
install_docker() {
echo "[INFO] Preparo repository Docker e installo pacchetti Docker..."
apt update
apt install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
tee /etc/apt/sources.list.d/docker.sources <<'EOF'
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
}
add_user_to_docker_group() {
echo "[INFO] Aggiungo l'utente '${NEW_USER}' al gruppo docker..."
groupadd -f docker
usermod -aG docker "${NEW_USER}"
echo "[INFO] Ricorda: per applicare il gruppo docker, '${NEW_USER}' deve fare logout/login."
}
run_make_postinstall() {
echo "[INFO] Eseguo Make per installare le utilità post-installazione..."
# Esegue il Makefile nella root del progetto (una cartella sopra 'scripts')
local ROOT_DIR
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
make -C "${ROOT_DIR}" postinstall
}
clone_and_deploy_portainer() {
echo "[INFO] Clono il repository Portainer e avvio il deploy..."
local PORTAINER_DIR
PORTAINER_DIR="/opt/portainer"
if [ ! -d "${PORTAINER_DIR}" ]; then
git clone https://git.pyconetwork.it/francesco/portainer.git "${PORTAINER_DIR}"
else
(
cd "${PORTAINER_DIR}" && git pull --rebase
)
fi
(
cd "${PORTAINER_DIR}"
if [ -f docker-compose.yml ] || [ -f compose.yml ]; then
docker compose up -d
else
if [ -f Makefile ]; then
make deploy || true
else
echo "[ATTENZIONE] Nessun docker-compose.yml/compose.yml trovato. Verifica il repository per le istruzioni di deploy."
fi
fi
)
chown -R "${NEW_USER}":"${NEW_USER}" "${PORTAINER_DIR}"
}
main() {
require_root
prompt_user
create_user_if_needed
configure_timezone
install_base_packages
install_docker
add_user_to_docker_group
run_make_postinstall
clone_and_deploy_portainer
echo "[FATTO] Setup completato. Docker è installato e Portainer è stato deployato (se possibile)."
echo "- Utente: ${NEW_USER} (aggiunto al gruppo docker)"
echo "- Timezone: ${TIMEZONE}"
echo "- Ricorda di eseguire logout/login per applicare i gruppi."
}
main "$@"