Skip to content

Instantly share code, notes, and snippets.

@leedohyung-dba
Created May 24, 2018 05:10
Show Gist options
  • Select an option

  • Save leedohyung-dba/87e18e90e86d559b30ebf81c8263c425 to your computer and use it in GitHub Desktop.

Select an option

Save leedohyung-dba/87e18e90e86d559b30ebf81c8263c425 to your computer and use it in GitHub Desktop.

Revisions

  1. leedohyung-dba created this gist May 24, 2018.
    1 change: 1 addition & 0 deletions file0.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    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"
    1 change: 1 addition & 0 deletions file2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    0 4 1 1,3,5,7,9,11 * root /bin/sh /run/ssl_certificate_renew.sh
    77 changes: 77 additions & 0 deletions ssl_certificate_renew.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/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