2
0

nm-certs.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if [ "$INSTALL_TYPE" = "ce" ]; then
  25. CERTBOT_PARAMS=$(cat <<EOF
  26. certonly --standalone \
  27. --non-interactive --agree-tos \
  28. -m $NM_EMAIL \
  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. EOF
  35. )
  36. elif [ "$INSTALL_TYPE" = "pro" ]; then
  37. CERTBOT_PARAMS=$(cat <<EOF
  38. certonly --standalone \
  39. --non-interactive --expand --agree-tos \
  40. -m $NM_EMAIL \
  41. -d api.$NM_DOMAIN \
  42. -d broker.$NM_DOMAIN \
  43. -d dashboard.$NM_DOMAIN \
  44. -d turn.$NM_DOMAIN \
  45. -d turnapi.$NM_DOMAIN \
  46. -d netmaker-exporter.$NM_DOMAIN \
  47. -d grafana.$NM_DOMAIN \
  48. -d prometheus.$NM_DOMAIN
  49. EOF
  50. )
  51. fi
  52. # generate an entrypoint for zerossl-certbot
  53. cat <<EOF >"$SCRIPT_DIR/certbot-entry.sh"
  54. #!/bin/sh
  55. # deps
  56. apk update
  57. apk add bash curl
  58. # zerossl
  59. wget -qO zerossl-bot.sh "https://github.com/zerossl/zerossl-bot/raw/master/zerossl-bot.sh"
  60. chmod +x zerossl-bot.sh
  61. # request the certs
  62. ./zerossl-bot.sh "$CERTBOT_PARAMS"
  63. EOF
  64. chmod +x "$SCRIPT_DIR/certbot-entry.sh"
  65. # request certs
  66. sudo docker run -it --rm --name certbot \
  67. -p 80:80 -p 443:443 \
  68. -v "$SCRIPT_DIR/certbot-entry.sh:/opt/certbot/certbot-entry.sh" \
  69. -v "$SCRIPT_DIR/letsencrypt:/etc/letsencrypt" \
  70. --entrypoint "/opt/certbot/certbot-entry.sh" \
  71. certbot/certbot
  72. # clean up
  73. rm "$SCRIPT_DIR/certbot-entry.sh"
  74. # check if successful
  75. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  76. # fallback to letsencrypt-certbot
  77. sudo docker run -it --rm --name certbot \
  78. -p 80:80 -p 443:443 \
  79. -v "$SCRIPT_DIR/letsencrypt:/etc/letsencrypt" \
  80. certbot/certbot $CERTBOT_PARAMS
  81. if [ ! -f "$CERT_DIR"/fullchain.pem ]; then
  82. echo "Missing file: $CERT_DIR/fullchain.pem"
  83. echo "SSL certificates failed"
  84. exit 1
  85. fi
  86. fi
  87. # copy for mounting
  88. mkdir -p certs
  89. cp -L "$CERT_DIR/fullchain.pem" "$SCRIPT_DIR/certs/fullchain.pem"
  90. cp -L "$CERT_DIR/privkey.pem" "$SCRIPT_DIR/certs/privkey.pem"
  91. echo "SSL certificates ready"
  92. # preserve the env state
  93. if [ "$RESTART_CADDY" = true ]; then
  94. echo "Starting Caddy..."
  95. docker-compose -f /root/docker-compose.yml start caddy
  96. fi
  97. # install crontab
  98. ln -sfn "$SCRIPT_DIR"/nm-certs.sh /etc/cron.monthly/nm-certs.sh