renewal.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. set -e
  3. ERR_SUBJ='Error during renewal certs and keys!!!'
  4. LOG_FILE=/var/log/letsencrypt/renewal-$(date +%Y-%m-%d).log
  5. branch=master
  6. git_dir=pki
  7. #git_url="git.sdsys.ru/sdsys/pki.git"
  8. . /tmp/err_trap
  9. if [[ -z ${CERT_SUBDIR} ]];then echo "variable CERT_SUBDIR doesn't set"; exit 1;fi
  10. [[ ${CERT_SUBDIR} == "dev_iru" ]] && exit 0
  11. mail_send() {
  12. echo "$1"|mail -s "Attention! Certificate renewal status!" \
  13. -S smtp=${SMTP_SERVER} \
  14. -S smtp-use-starttls \
  15. -S smtp-auth=login \
  16. -S ssl-verify=ignore \
  17. -S smtp-auth-user=${JENKINS_MAIL_USER} \
  18. -S smtp-auth-password=$(cat /run/secrets/jenkins-mail-pass) \
  19. -S nss-config-dir=/etc/pki/nssdb \
  20. -S from=${JENKINS_MAIL_USER} \
  21. -a ${LOG_FILE} \
  22. ${RECIPIENT_MAIL_BOX}
  23. }
  24. git_push() {
  25. cd /${git_dir}
  26. echo "Renew keys and certs" > /tmp/commit.txt
  27. git config --global user.email "${JENKINS_MAIL_USER}"
  28. git config --global user.name "Jenkins"
  29. git add -A
  30. if [[ ! -z $(git status -s) ]];then
  31. git commit -F /tmp/commit.txt
  32. git push https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} ${branch}
  33. fi
  34. }
  35. clear_log() {
  36. echo -n > ${LOG_FILE}
  37. }
  38. clear_log
  39. echo "Delete /${git_dir}"
  40. [[ -d /${git_dir} ]] && rm -rf /${git_dir}
  41. echo "Git clone ${GIT_URL}"
  42. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} && cd /${git_dir} && git checkout ${branch}
  43. certbot renew --dry-run --config-dir /${git_dir}/${CERT_SUBDIR}/letsencrypt
  44. if [ $? -ne 0 ];then message="letsencrypt. Can't execute "dry-run" renew procces. Renew certs and keys will be skipped!!!"; mail_send; exit 1;fi
  45. clear_log
  46. echo "Renewal certs and keys"
  47. certbot renew --config-dir /${git_dir}/${CERT_SUBDIR}/letsencrypt >> ${LOG_FILE} 2>&1
  48. echo "Git push ${GIT_URL}"
  49. git_push
  50. echo "Send ${LOG_FILE} to ${RECIPIENT_MAIL_BOX}"
  51. mail_send "LETSENCRYPT!!! Renew Certs and Keys are success!!!"