renewal.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. 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_config() {
  25. cd /${git_dir}
  26. git config --global user.email "${JENKINS_MAIL_USER}"
  27. git config --global user.name "Jenkins"
  28. }
  29. clear_log() {
  30. echo -n > ${LOG_FILE}
  31. }
  32. clear_log
  33. echo "Delete /${git_dir}"
  34. [[ -d /${git_dir} ]] && rm -rf /${git_dir}
  35. echo "Git clone ${GIT_URL}"
  36. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} && cd /${git_dir} && git checkout ${branch}
  37. #certbot renew --dry-run --config-dir /${git_dir}/letsencrypt
  38. #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
  39. #clear_log
  40. echo "Renewal certs and keys"
  41. certbot renew --config-dir /${git_dir}/${CERT_SUBDIR}/letsencrypt >> ${LOG_FILE} 2>&1
  42. echo "Git push ${GIT_URL}"
  43. echo "Renew keys and certs" > /tmp/commit.txt
  44. git_config && git add -A && git commit -F /tmp/commit.txt
  45. git push https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} ${branch}
  46. echo "Send ${LOG_FILE} to ${RECIPIENT_MAIL_BOX}"
  47. mail_send "LETSENCRYPT!!! Renew Certs and Keys are success!!!"