diff --git a/cicd/nginx/README.md b/cicd/nginx/README.md index 2dd519c..ce9320c 100644 --- a/cicd/nginx/README.md +++ b/cicd/nginx/README.md @@ -19,6 +19,7 @@ Il primo dominio e' il primario e da' il nome alla cartella ## Primo setup Prerequisiti: +- Docker con il plugin Compose v2 (comando `docker compose`, non `docker-compose`). - La rete docker esterna `nginx-network` deve esistere (`docker network create nginx-network`). - Le porte 80 e 443 dell'host devono essere pubblicamente raggiungibili per tutti i domini (la validazione HTTP-01 passa dalla porta 80). diff --git a/cicd/nginx/init-letsencrypt.sh b/cicd/nginx/init-letsencrypt.sh index 9d4d03f..9a31c82 100755 --- a/cicd/nginx/init-letsencrypt.sh +++ b/cicd/nginx/init-letsencrypt.sh @@ -8,11 +8,16 @@ # cartella /etc/letsencrypt/live// referenziata in nginx.conf. # # Lanciarlo una volta sola, al primo setup. Per i rinnovi usare renew.sh. +# +# NB: usa il plugin "docker compose" (v2). Con questo plugin --entrypoint accetta +# solo un eseguibile: i comandi con piu' token o con && vanno wrappati in +# `--entrypoint sh -c "..."`, altrimenti gli argomenti oltre il primo +# vengono scartati silenziosamente. set -e -if ! [ -x "$(command -v docker-compose)" ]; then - echo 'Error: docker-compose is not installed.' >&2 +if ! docker compose version >/dev/null 2>&1; then + echo 'Error: "docker compose" (plugin v2) is not available.' >&2 exit 1 fi @@ -35,9 +40,9 @@ email="riccardo.didato@gmail.com" staging=0 # Costruisce la sequenza di argomenti -d per certbot -domain_args="" +domain_args=() for domain in "${domains[@]}"; do - domain_args="$domain_args -d $domain" + domain_args+=(-d "$domain") done # --------------------------------------------------------------------------- @@ -62,45 +67,48 @@ else echo "### Creating dummy certificate for $primary_domain ..." path="/etc/letsencrypt/live/$primary_domain" mkdir -p "$data_path/conf/live/$primary_domain" - docker-compose run --rm --entrypoint "\ + docker compose run --rm --entrypoint sh certbot -c "\ openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1 \ -keyout '$path/privkey.pem' \ -out '$path/fullchain.pem' \ - -subj '/CN=localhost'" certbot + -subj '/CN=localhost'" echo fi echo "### Starting nginx ..." -docker-compose up --force-recreate -d nginx +docker compose up --force-recreate -d nginx echo # --------------------------------------------------------------------------- # FASE 2: richiesta del certificato REALE # --------------------------------------------------------------------------- echo "### Deleting dummy certificate ..." -docker-compose run --rm --entrypoint "\ +docker compose run --rm --entrypoint sh certbot -c "\ rm -Rf /etc/letsencrypt/live/$primary_domain && \ rm -Rf /etc/letsencrypt/archive/$primary_domain && \ - rm -Rf /etc/letsencrypt/renewal/$primary_domain.conf" certbot + rm -Rf /etc/letsencrypt/renewal/$primary_domain.conf" echo +email_arg=() case "$email" in - "") email_arg="--register-unsafely-without-email" ;; - *) email_arg="--email $email" ;; + "") email_arg=(--register-unsafely-without-email) ;; + *) email_arg=(--email "$email") ;; esac -if [ "$staging" != "0" ]; then staging_arg="--staging"; fi +staging_arg=() +if [ "$staging" != "0" ]; then staging_arg=(--staging); fi echo "### Requesting Let's Encrypt certificate for: ${domains[*]} ..." -docker-compose run --rm --entrypoint "\ - certbot certonly --webroot -w /var/www/certbot \ - $staging_arg \ - $email_arg \ - $domain_args \ - --rsa-key-size $rsa_key_size \ - --agree-tos \ - --force-renewal" certbot +# L'immagine certbot/certbot ha gia' "certbot" come entrypoint: passiamo solo +# gli argomenti come comando (niente --entrypoint da sovrascrivere). +docker compose run --rm certbot certonly --webroot -w /var/www/certbot \ + "${staging_arg[@]}" \ + "${email_arg[@]}" \ + "${domain_args[@]}" \ + --rsa-key-size "$rsa_key_size" \ + --agree-tos \ + --force-renewal echo echo "### Reloading nginx ..." -docker-compose exec nginx nginx -s reload +docker compose exec nginx nginx -s reload diff --git a/cicd/nginx/renew.sh b/cicd/nginx/renew.sh index a273ddb..4bd8a3e 100755 --- a/cicd/nginx/renew.sh +++ b/cicd/nginx/renew.sh @@ -2,6 +2,9 @@ # Rinnova il certificato SSL e ricarica nginx. # Schedulato via crontab sull'host (vedi README). certbot renew rinnova solo se # il certificato scade entro 30 giorni, quindi e' sicuro lanciarlo ogni giorno. +# +# NB: usa il plugin "docker compose" (v2). L'immagine certbot/certbot ha gia' +# "certbot" come entrypoint, quindi passiamo "renew" come comando. -/usr/local/bin/docker-compose run --rm --entrypoint "certbot renew" certbot -/usr/local/bin/docker-compose exec -T nginx nginx -s reload +docker compose run --rm certbot renew +docker compose exec -T nginx nginx -s reload