You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl.sh 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. set -e
  3. . ../conf/ssl.conf
  4. user="$(stat -c %U "$0")"
  5. asuser() {
  6. echo "Running as $user: " "$@"
  7. sudo -u "$user" "$@"
  8. }
  9. printcol() {
  10. str="$1"
  11. shift
  12. for x in "$@"; do
  13. tput $x
  14. done
  15. printf "%s%s\n" "$str" $(tput sgr0)
  16. }
  17. printed=0
  18. printstatus() {
  19. if [ "$printed" = 1 ]; then
  20. echo
  21. fi
  22. printed=1
  23. printcol "$1" bold
  24. }
  25. certbot="https://github.com/certbot/certbot.git"
  26. validate() {
  27. if [ -z "$email" ]; then
  28. echo "Missing config 'email'."
  29. elif [ -z "$domains" ]; then
  30. echo "Missing config 'domains'."
  31. elif [ -z "$testcert" ]; then
  32. echo "Missing config 'testcert'."
  33. elif [ -z "$dryrun" ]; then
  34. echo "Missing config 'dryrun'."
  35. elif [ -z "$precmd" ]; then
  36. echo "Missing config 'precmd'."
  37. elif [ -z "$postcmd" ]; then
  38. echo "Missing config 'postcmd'."
  39. else
  40. return 0
  41. fi
  42. exit 1
  43. }
  44. validate
  45. if ! [ -d certbot ]; then
  46. asuser git clone "$certbot" certbot
  47. fi
  48. if [ "$dryrun" = 1 ]; then
  49. printstatus "Running a dry run."
  50. fi
  51. cd certbot
  52. printstatus "Updating certbot..."
  53. # necessary because certbot sometime schanges itself
  54. asuser git reset --hard
  55. asuser git pull
  56. printstatus "Running pre command..."
  57. echo "$precmd"
  58. sh -c "$precmd"
  59. printstatus "Obtaining certificates..."
  60. domains="$(echo "$domains" \
  61. | tr '\n' ' ' \
  62. | sed 's/\s*//g; s/\;/\n/g')"
  63. while read line; do
  64. if [ -z "$line" ]; then
  65. continue
  66. fi
  67. cname=$(echo "$line" | sed 's/:.*//')
  68. dom=$(echo "$line" | sed 's/.*://')
  69. printstatus "Certificate $cname"
  70. if ./certbot-auto certonly \
  71. -n --standalone --agree-tos \
  72. -m "$email" \
  73. --cert-name "$cname" \
  74. $([ "$testcert" = 1 ] && echo --test-cert) \
  75. $([ "$dryrun" = 1 ] && echo "--dry-run") \
  76. -d "$dom"
  77. then
  78. echo
  79. printcol "Certbot succeeded." "setaf 2" "bold"
  80. else
  81. fail=1
  82. echo
  83. printcol "Certbot failed." "setaf 1" "bold"
  84. fi
  85. done <<< "$domains"
  86. printstatus "Running post command..."
  87. echo "$postcmd"
  88. sh -c "$postcmd"
  89. if [ "$fail" = 1 ]; then
  90. exit 1
  91. else
  92. exit 0
  93. fi