generate.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. git_dir="pki"
  3. branch="100883"
  4. #git_url="git.sdsys.ru/sdsys/pki.git"
  5. function mail_send {
  6. echo "${message}"|mail -s "Attention! Certificate status!" \
  7. -S smtp=${SMTP_SERVER} \
  8. -S smtp-use-starttls \
  9. -S smtp-auth=login \
  10. -S ssl-verify=ignore \
  11. -S smtp-auth-user=${JENKINS_MAIL_USER} \
  12. -S smtp-auth-password=$(cat /run/secrets/jenkins-mail-pass) \
  13. -S nss-config-dir=/etc/pki/nssdb \
  14. -S from=${JENKINS_MAIL_USER} \
  15. ${RECIPIENT_MAIL_BOX}
  16. }
  17. function git_config {
  18. cd /${git_dir}
  19. git config --global user.email "${JENKINS_MAIL_USER}"
  20. git config --global user.name "Jenkins"
  21. }
  22. if [ -z "$*" ]; then message="letsencrypt. No domain specified!!!"; mail_send; exit 1;fi
  23. if [ -d /${git_dir} ]
  24. then
  25. git_config && git checkout ${branch} && git pull https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL}
  26. if [ $? -ne 0 ];then message="letsencrypt. Can't pull https://${GIT_URL}"; mail_send; exit 1;fi
  27. else
  28. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} && cd /${git_dir} && git checkout ${branch}
  29. if [ $? -ne 0 ];then message="letsencrypt. Can't clone https://${GIT_URL}"; mail_send; exit 1;fi
  30. fi
  31. domain=$(echo "$*" | sed 's/ / -d /g')
  32. certbot certonly --dry-run --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  33. if [ $? -ne 0 ];then message="letsencrypt. Can't execute "dry-run" for $(echo $*). Generate cert and key will be skipped!!!"; mail_send; exit 1;fi
  34. #certbot certonly --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  35. #if [ $? -ne 0 ];then message="letsencrypt. Can't generate cert and key for $(echo $*). See log !!!"; mail_send; exit 1;fi
  36. echo "Generate new key and cert for $(echo $*)" > /tmp/commit.txt
  37. git_config && git add -A && git commit -F /tmp/commit.txt
  38. git push https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${GIT_URL} ${branch}
  39. if [ $? -ne 0 ];then message="letsencrypt. Can't push diff to https://${GIT_URL} !!!"; mail_send; exit 1;fi
  40. message="letsencrypt. Certs for domain $(echo $*) is generated!!!"
  41. mail_send