nm-certs.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. CONFIG_FILE=netmaker.env
  3. # TODO make sure this doesnt break, parse `certbot certificates` if yes
  4. CERT_DIR=/etc/letsencrypt/live/stun.$NM_DOMAIN
  5. SCRIPT_DIR=$(dirname "$(realpath "$0")")
  6. # get and check the config
  7. if [ ! -f "$SCRIPT_DIR/$CONFIG_FILE" ]; then
  8. echo "Config file missing"
  9. exit 1
  10. fi
  11. source "$SCRIPT_DIR/$CONFIG_FILE"
  12. if [ -z "$NM_DOMAIN" ] || [ -z "$NM_EMAIL" ]; then
  13. echo "Config not valid"
  14. exit 1
  15. fi
  16. echo "Setting up SSL certificates..."
  17. # get the zerossl wrapper for certbot
  18. wget -qO /root/zerossl-bot.sh "https://github.com/zerossl/zerossl-bot/raw/master/zerossl-bot.sh"
  19. chmod +x /root/zerossl-bot.sh
  20. # preserve the env state
  21. RESTART_CADDY=false
  22. if [ -n "$(docker ps | grep caddy)" ]; then
  23. echo "Caddy is running, stopping for now..."
  24. RESTART_CADDY=true
  25. docker-compose -f /root/docker-compose.yml stop caddy
  26. fi
  27. # request certs
  28. ./zerossl-bot.sh certonly --standalone \
  29. -m "$NM_EMAIL" \
  30. -d "stun.$NM_DOMAIN" \
  31. -d "broker.$NM_DOMAIN" \
  32. -d "dashboard.$NM_DOMAIN" \
  33. -d "turnapi.$NM_DOMAIN" \
  34. -d "netmaker-exporter.$NM_DOMAIN" \
  35. -d "grafana.$NM_DOMAIN" \
  36. -d "prometheus.$NM_DOMAIN"
  37. # TODO fallback to letsencrypt
  38. # check if successful
  39. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  40. echo "SSL certificates failed"
  41. exit 1
  42. fi
  43. # copy for mounting
  44. cp "$CERT_DIR"/fullchain.pem /root
  45. cp "$CERT_DIR"/privkey.pem /root
  46. echo "SSL certificates ready"
  47. # preserve the env state
  48. if [ "$RESTART_CADDY" = true ]; then
  49. echo "Starting Caddy..."
  50. docker-compose -f /root/docker-compose.yml start caddy
  51. fi
  52. # install crontab
  53. ln -sfn "$SCRIPT_DIR"/nm-certs.sh /etc/cron.monthly/nm-certs.sh