install-certbot.sh 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. set -ex
  3. [[ $(uname) = Linux ]] || exit 1
  4. [[ $USER = root ]] || exit 1
  5. apt install -y certbot
  6. cat > /certbot-restore.sh <<'EOF'
  7. #!/usr/bin/env bash
  8. set -e
  9. domain=$1
  10. email=$2
  11. if [[ $USER != root ]]; then
  12. echo "Run as root"
  13. exit 1
  14. fi
  15. if [[ -f /.cert.pem ]]; then
  16. echo "Certificate already initialized"
  17. exit 0
  18. fi
  19. set -x
  20. if [[ -r letsencrypt.tgz ]]; then
  21. tar -C / -zxf letsencrypt.tgz
  22. fi
  23. cd /
  24. rm -f letsencrypt.tgz
  25. maybeDryRun=
  26. # Uncomment during testing to avoid hitting LetsEncrypt API limits while iterating
  27. #maybeDryRun="--dry-run"
  28. certbot certonly --standalone -d "$domain" --email "$email" --agree-tos -n $maybeDryRun
  29. tar zcf letsencrypt.tgz /etc/letsencrypt
  30. ls -l letsencrypt.tgz
  31. # Copy certificates to / for easy access without knowing the value of "$domain"
  32. rm -f /.key.pem /.cert.pem
  33. cp /etc/letsencrypt/live/$domain/privkey.pem /.key.pem
  34. cp /etc/letsencrypt/live/$domain/cert.pem /.cert.pem
  35. EOF
  36. chmod +x /certbot-restore.sh