Skip to content

Instantly share code, notes, and snippets.

@leedohyung-dba
Created May 24, 2018 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leedohyung-dba/87e18e90e86d559b30ebf81c8263c425 to your computer and use it in GitHub Desktop.
Save leedohyung-dba/87e18e90e86d559b30ebf81c8263c425 to your computer and use it in GitHub Desktop.
定期的にLet's Encrypt証明書を更新してSlackでアナウンス ref: https://qiita.com/leedohyung-dba/items/947185ab37818b66bacd
0 4 1 1,3,5,7,9,11 * root /usr/bin/systemctl stop httpd.service && /usr/bin/certbot renew --force-renew --quiet --post-hook "/usr/bin/systemctl start httpd.service"
0 4 1 1,3,5,7,9,11 * root /bin/sh /run/ssl_certificate_renew.sh
#!/bin/bash
# WebHookのURL
WEBHOOK_URL='https://hooks.slack.com/services/***************************'
# メッセージを一時保存する場所
MESSAGEFILE=$(mktemp -t ssl-certificate-notice-XXXX)
# 送信先のチャンネル
CHANNEL=${CHANNEL:-'#lee_test'}
# 終了時に削除
trap "rm ${MESSAGEFILE}" 0
send_notice_to_slack_renew_start () {
# 見出し
hd=${HEAD:-"start to ssl certificate renew.\n"}
# json形式に整形
payload="payload={
\"channel\": \"${CHANNEL}\",
\"text\": \"${hd}\"
}"
curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}
send_notice_to_slack_renew_success () {
# 見出し
hd=${HEAD:-"ssl certificate renew success.\n"}
# 絵文字
emoji=${EMOJI:-':carlton:'}
# json形式に整形
payload="payload={
\"channel\": \"${CHANNEL}\",
\"icon_emoji\": \"${emoji}\",
\"text\": \"${hd}\"
}"
curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}
send_notice_to_slack_renew_fail () {
# 改行処理
cat ${MESSAGEFILE} | tr '\n' '\\' | sed 's/\\/\\n/g' > ${MESSAGEFILE}
# 絵文字
emoji=${EMOJI:-':aaw_yeah:'}
# 見出し
hd=${HEAD:-"<!here>ssl certificate renew fail.\n"}
# メッセージをシンタックスハイライト付きで取得
msg='```'`cat ${MESSAGEFILE}`'```'
# json形式に整形
payload="payload={
\"channel\": \"${CHANNEL}\",
\"icon_emoji\": \"${emoji}\",
\"text\": \"${hd}${msg}\"
}"
curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}
send_notice_to_slack_renew_start
/usr/bin/systemctl stop httpd.service
/usr/bin/certbot renew --force-renew --quiet 2> ${MESSAGEFILE}
RENEW_RESULT=$?
/usr/bin/systemctl start httpd.service
if [ ${RENEW_RESULT} -eq 0 ]; then
send_notice_to_slack_renew_success
else
send_notice_to_slack_renew_fail
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment