nm-certs.sh 2.4 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/api.$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 api.$NM_DOMAIN \
  29. -d broker.$NM_DOMAIN \
  30. -d dashboard.$NM_DOMAIN \
  31. -d turn.$NM_DOMAIN \
  32. -d turnapi.$NM_DOMAIN \
  33. -d netmaker-exporter.$NM_DOMAIN \
  34. -d grafana.$NM_DOMAIN \
  35. -d prometheus.$NM_DOMAIN
  36. EOF
  37. )
  38. # generate an entrypoint for zerossl-certbot
  39. cat <<EOF >"$SCRIPT_DIR/certbot-entry.sh"
  40. #!/bin/sh
  41. # deps
  42. apk update
  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 "$SCRIPT_DIR/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. certbot/certbot $CERTBOT_PARAMS
  67. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  68. echo "Missing file: $CERT_DIR/fullchain.pem"
  69. echo "SSL certificates failed"
  70. exit 1
  71. fi
  72. fi
  73. # copy for mounting
  74. mkdir -p certs
  75. cp -L "$CERT_DIR/fullchain.pem" "$SCRIPT_DIR/certs/fullchain.pem"
  76. cp -L "$CERT_DIR/privkey.pem" "$SCRIPT_DIR/certs/privkey.pem"
  77. echo "SSL certificates ready"
  78. # preserve the env state
  79. if [ "$RESTART_CADDY" = true ]; then
  80. echo "Starting Caddy..."
  81. docker-compose -f /root/docker-compose.yml start caddy
  82. fi
  83. # install crontab
  84. ln -sfn "$SCRIPT_DIR"/nm-certs.sh /etc/cron.monthly/nm-certs.sh