renewal.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. . /tmp/err_trap
  8. #git_url="git.sdsys.ru/sdsys/pki.git"
  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. set +e
  13. echo "$1"|mail -s "Attention! Certificate renewal status!" \
  14. -S smtp=${SMTP_SERVER} \
  15. -S smtp-use-starttls \
  16. -S smtp-auth=login \
  17. -S ssl-verify=ignore \
  18. -S smtp-auth-user=${JENKINS_MAIL_USER} \
  19. -S smtp-auth-password=$(cat /run/secrets/jenkins-mail-pass) \
  20. -S nss-config-dir=/etc/pki/nssdb \
  21. -S from=${JENKINS_MAIL_USER} \
  22. -a ${LOG_FILE} \
  23. ${RECIPIENT_MAIL_BOX}
  24. }
  25. git_config() {
  26. cd /${git_dir}
  27. git config --global user.email "${JENKINS_MAIL_USER}"
  28. git config --global user.name "Jenkins"
  29. }
  30. clear_log() {
  31. echo -n > ${LOG_FILE}
  32. }
  33. clear_log
  34. echo "Delete /${git_dir}"
  35. [[ -d /${git_dir} ]] && rm -rf /${git_dir}
  36. echo "Git clone ${GIT_URL}"
  37. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} && cd /${git_dir} && git checkout ${branch}
  38. #certbot renew --dry-run --config-dir /${git_dir}/letsencrypt
  39. #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
  40. #clear_log
  41. echo "Renewal certs and keys"
  42. certbot renew --config-dir /${git_dir}/${CERT_SUBDIR}/letsencrypt >> ${LOG_FILE} 2>&1
  43. echo "Git push ${GIT_URL}"
  44. echo "Renew keys and certs" > /tmp/commit.txt
  45. git_config && git add -A && git commit -F /tmp/commit.txt
  46. git push https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} ${branch}
  47. echo "Send ${LOG_FILE} to ${RECIPIENT_MAIL_BOX}"
  48. mail_send "LETSENCRYPT!!! Renew Certs and Keys are success!!!"