nm-certs.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. CONFIG_FILE=netmaker.env
  3. SCRIPT_DIR=$(dirname "$(realpath "$0")")
  4. # get and check the config
  5. if [ ! -f "$SCRIPT_DIR/$CONFIG_FILE" ]; then
  6. echo "Config file missing"
  7. exit 1
  8. fi
  9. source "$SCRIPT_DIR/$CONFIG_FILE"
  10. if [ -z "$NM_DOMAIN" ] || [ -z "$NM_EMAIL" ]; then
  11. echo "Config not valid"
  12. exit 1
  13. fi
  14. # TODO make sure this doesnt break, parse `certbot certificates` if yes
  15. CERT_DIR="$SCRIPT_DIR/letsencrypt/live/stun.$NM_DOMAIN"
  16. echo "Setting up SSL certificates..."
  17. # preserve the env state
  18. RESTART_CADDY=false
  19. if [ -n "$(docker ps | grep caddy)" ]; then
  20. echo "Caddy is running, stopping for now..."
  21. RESTART_CADDY=true
  22. docker-compose -f /root/docker-compose.yml stop caddy
  23. fi
  24. CERTBOT_PARAMS=$(cat <<EOF
  25. certonly --standalone \
  26. --non-interactive --agree-tos \
  27. -m "$NM_EMAIL" \
  28. -d "stun.$NM_DOMAIN" \
  29. -d "api.$NM_DOMAIN" \
  30. -d "broker.$NM_DOMAIN" \
  31. -d "dashboard.$NM_DOMAIN" \
  32. -d "turn.$NM_DOMAIN" \
  33. -d "turnapi.$NM_DOMAIN" \
  34. -d "netmaker-exporter.$NM_DOMAIN" \
  35. -d "grafana.$NM_DOMAIN" \
  36. -d "prometheus.$NM_DOMAIN"
  37. EOF
  38. )
  39. # generate an entrypoint for zerossl-certbot
  40. cat <<EOF >"$SCRIPT_DIR/certbot-entry.sh"
  41. #!/bin/sh
  42. # deps
  43. apk add bash curl
  44. # zerossl
  45. wget -qO zerossl-bot.sh "https://github.com/zerossl/zerossl-bot/raw/master/zerossl-bot.sh"
  46. chmod +x zerossl-bot.sh
  47. # request the certs
  48. ./zerossl-bot.sh "$CERTBOT_PARAMS"
  49. EOF
  50. chmod +x certbot-entry.sh
  51. # request certs
  52. sudo docker run -it --rm --name certbot \
  53. -p 80:80 -p 443:443 \
  54. -v "$SCRIPT_DIR/certbot-entry.sh:/opt/certbot/certbot-entry.sh" \
  55. -v "$SCRIPT_DIR/letsencrypt:/etc/letsencrypt" \
  56. --entrypoint "/opt/certbot/certbot-entry.sh" \
  57. certbot/certbot
  58. # clean up
  59. rm "$SCRIPT_DIR/certbot-entry.sh"
  60. # check if successful
  61. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  62. # fallback to letsencrypt-certbot
  63. sudo docker run -it --rm --name certbot \
  64. -p 80:80 -p 443:443 \
  65. -v "$SCRIPT_DIR/letsencrypt:/etc/letsencrypt" \
  66. --entrypoint "/opt/certbot/certbot-entry.sh" \
  67. certbot/certbot "$CERTBOT_PARAMS"
  68. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  69. echo "Missing file: $CERT_DIR/fullchain.pem"
  70. echo "SSL certificates failed"
  71. exit 1
  72. fi
  73. fi
  74. # copy for mounting
  75. mkdir -p certs
  76. cp -L "$CERT_DIR/fullchain.pem" "$SCRIPT_DIR/certs/fullchain.pem"
  77. cp -L "$CERT_DIR/privkey.pem" "$SCRIPT_DIR/certs/privkey.pem"
  78. echo "SSL certificates ready"
  79. # preserve the env state
  80. if [ "$RESTART_CADDY" = true ]; then
  81. echo "Starting Caddy..."
  82. docker-compose -f /root/docker-compose.yml start caddy
  83. fi
  84. # install crontab
  85. ln -sfn "$SCRIPT_DIR"/nm-certs.sh /etc/cron.monthly/nm-certs.sh