generate.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/bash
  2. git_dir="pki"
  3. branch="100883"
  4. git_url="https://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. if [ -z "$*" ]; then message="No domain specified!!!"; mail_send; exit 1;fi
  18. if [ -d /${git_dir} ]
  19. then
  20. cd /${git_dir} && git pull https://${GIT_USER}:$(cat /run/provision-pass)@${git_url} && git checkout ${branch}
  21. if [ $? -ne 0 ];then message="Can't pull ${git_url}"; mail_send; exit 1;fi
  22. else
  23. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${git_url} && cd /${git_dir} && git checkout ${branch}
  24. if [ $? -ne 0 ];then message="Can't clone ${git_url}"; mail_send; exit 1;fi
  25. fi
  26. domain=$(echo "$*" | sed 's/ / -d /g')
  27. certbot certonly --dry-run --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  28. if [ $? -ne 0 ];then message="Can't execute "dry-run" for $(echo $*). Generate cert and key will be skipped!!!"; mail_send; exit 1;fi
  29. certbot certonly --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  30. if [ $? -ne 0 ];then message="Can't generate cert and key for $(echo $*). See log !!!"; mail_send; exit 1;fi
  31. echo "Generate new key and cert for $(echo $*)" > /tmp/commit.txt
  32. cd /${git_dir} && git add -A && git config --global user.email "${JENKINS_MAIL}" && git config --global user.name "Jenkins" && git commit -F /tmp/commit.txt
  33. git push https://${GIT_USER}:$(cat /run/secrets/provision-pass)@${git_url} ${branch}
  34. if [ $? -ne 0 ];then message="Can't push diff to ${git_url} !!!"; mail_send; exit 1;fi
  35. message="Certs for domain $(echo $*) is generated!!!"
  36. mail_send