generate.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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=$(cat /run/secrets/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=$(cat /run/secrets/jenkins-mail-user) \
  15. ${RECIPIENT-MAIL-BOX}
  16. }
  17. if [ -d /${git_dir} ]
  18. then
  19. cd /${git_dir} && git pull https://${GIT_USER}:$(cat /run/secrets/jenkins-mail-pass)@${git_url} && git checkout ${branch}
  20. if [ $? -ne 0 ];then message="Can't pull ${git_url}"; mail_send; exit 1;fi
  21. else
  22. cd / && git clone https://${GIT_USER}:$(cat /run/secrets/jenkins-mail-pass)@${git_url} && cd /${git_dir} && git checkout ${branch}
  23. if [ $? -ne 0 ];then message="Can't clone ${git_url}"; mail_send; exit 1;fi
  24. fi
  25. if [ ! -z "$*" ]
  26. then
  27. domain=$(echo "$*" | sed 's/ / -d /g')
  28. certbot certonly --dry-run --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  29. if [ $? -ne 0 ];then message="Can't execute "dry-run" for $(echo $*). Generate cert and key will be skipped!!!"; mail_send; exit 1;fi
  30. certbot certonly --webroot -w /var/www/html --config-dir /${git_dir}/letsencrypt -m admin@sdsys.ru -d ${domain}
  31. if [ $? -ne 0 ];then message="Can't generate cert and key for $(echo $*). See log !!!"; mail_send; exit 1;fi
  32. echo "Generate new key and cert for $(echo $*)" > /tmp/commit.txt
  33. 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
  34. git push https://${GIT_USER}:$(cat /run/secrets/jenkins-mail-pass)@${git_url} ${branch}
  35. if [ $? -ne 0 ];then message="Can't push diff to ${git_url} !!!"; mail_send; exit 1;fi
  36. message="Certs for domain $(echo $*) is generated!!!"
  37. mail_send
  38. else
  39. message="No domain specified!!!"
  40. mail_send
  41. fi