diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..52d1194 --- /dev/null +++ b/Makefile @@ -0,0 +1,107 @@ +.DEFAULT_GOAL := help +ENV_FILE := .env +PLACEHOLDER := crea_una_chiave_esadecimale_di_64_caratteri_per_la_cifratura + +# --------------------------------------------------------------------------- +# Rileva OS per generazione chiave +# --------------------------------------------------------------------------- +ifeq ($(OS),Windows_NT) + KEYGEN = powershell -NoProfile -Command \ + "$$b = New-Object byte[] 32; \ + [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($$b); \ + ($$b | ForEach-Object { $$_.ToString('x2') }) -join ''" +else + KEYGEN = openssl rand -hex 32 +endif + +# --------------------------------------------------------------------------- +# Legge ENCRYPTION_KEY dal .env per validazione +# --------------------------------------------------------------------------- +-include $(ENV_FILE) +export + +# --------------------------------------------------------------------------- +# Targets +# --------------------------------------------------------------------------- + +.PHONY: help +help: ## Mostra questo messaggio di aiuto + @echo "" + @echo " Nexterm - guida al deploy" + @echo "" + @echo " Flusso consigliato:" + @echo " 1. make genkey — genera e scrive ENCRYPTION_KEY nel .env" + @echo " 2. make check — verifica prerequisiti e configurazione" + @echo " 3. make up — avvia i container" + @echo "" + @echo " Altri comandi:" + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ + | awk 'BEGIN {FS = ":.*?## "}; {printf " %-14s %s\n", $$1, $$2}' + @echo "" + +.PHONY: genkey +genkey: ## Genera ENCRYPTION_KEY casuale e la scrive nel .env + $(eval NEW_KEY := $(shell $(KEYGEN))) + @if [ -z "$(NEW_KEY)" ]; then \ + echo "ERRORE: generazione chiave fallita. Assicurati che openssl sia installato."; \ + exit 1; \ + fi +ifeq ($(OS),Windows_NT) + @powershell -NoProfile -Command \ + "(Get-Content $(ENV_FILE)) -replace '$(PLACEHOLDER)|^ENCRYPTION_KEY=.*', \ + { if ($$_ -match '^ENCRYPTION_KEY=') { 'ENCRYPTION_KEY=$(NEW_KEY)' } else { $$_ } } \ + | Set-Content $(ENV_FILE)" +else + @sed -i "s|^ENCRYPTION_KEY=.*|ENCRYPTION_KEY=$(NEW_KEY)|" $(ENV_FILE) +endif + @echo "ENCRYPTION_KEY aggiornata nel file $(ENV_FILE)." + +.PHONY: check +check: ## Verifica prerequisiti e configurazione prima del deploy + @echo "==> Controllo Docker..." + @docker info > /dev/null 2>&1 || (echo "ERRORE: Docker non raggiungibile." && exit 1) + @echo " OK" + @echo "==> Controllo docker compose..." + @docker compose version > /dev/null 2>&1 || (echo "ERRORE: docker compose v2 non trovato." && exit 1) + @echo " OK" + @echo "==> Controllo file $(ENV_FILE)..." + @test -f $(ENV_FILE) || (echo "ERRORE: file $(ENV_FILE) non trovato." && exit 1) + @echo " OK" + @echo "==> Controllo ENCRYPTION_KEY..." + @if echo "$(ENCRYPTION_KEY)" | grep -qE '^[0-9a-fA-F]{64}$$'; then \ + echo " OK"; \ + else \ + echo "ERRORE: ENCRYPTION_KEY non valida (deve essere hex di 64 caratteri)."; \ + echo " Esegui: make genkey"; \ + exit 1; \ + fi + @echo "" + @echo "Tutto OK. Puoi eseguire: make up" + +.PHONY: up +up: ## Avvia i container in background + docker compose --env-file $(ENV_FILE) up -d + +.PHONY: down +down: ## Ferma e rimuove i container + docker compose --env-file $(ENV_FILE) down + +.PHONY: restart +restart: ## Riavvia i container + docker compose --env-file $(ENV_FILE) restart + +.PHONY: logs +logs: ## Segue i log di nexterm in tempo reale + docker compose --env-file $(ENV_FILE) logs -f nexterm + +.PHONY: ps +ps: ## Mostra lo stato dei container + docker compose --env-file $(ENV_FILE) ps + +.PHONY: pull +pull: ## Scarica le immagini aggiornate senza riavviare + docker compose --env-file $(ENV_FILE) pull + +.PHONY: update +update: pull ## Aggiorna le immagini e ricrea i container + docker compose --env-file $(ENV_FILE) up -d --remove-orphans diff --git a/README.md b/README.md index 04e8599..1029a2c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,25 @@ Esempio rapido (Linux/macOS): openssl rand -hex 32 ``` -## Avvio +## Avvio rapido con Make + +```bash +make genkey # genera e scrive ENCRYPTION_KEY nel .env +make check # verifica prerequisiti e configurazione +make up # avvia i container +``` + +Altri comandi disponibili: + +```bash +make logs # segue i log di nexterm +make ps # stato dei container +make update # aggiorna le immagini e ricrea i container +make down # ferma e rimuove i container +make help # lista completa dei comandi +``` + +## Avvio manuale ```bash docker compose up -d @@ -65,7 +83,7 @@ docker compose logs -f nexterm Apri poi il browser su: -- `http://localhost:${NEXTERM_PORT_HOST}` +- `http://localhost:${PORT}` ## Stop e rimozione