Skip to content

Instantly share code, notes, and snippets.

@rluisr
Created December 26, 2019 07:12
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 rluisr/7ffa209049b6c2ece17de5fed953b4d7 to your computer and use it in GitHub Desktop.
Save rluisr/7ffa209049b6c2ece17de5fed953b4d7 to your computer and use it in GitHub Desktop.
wp2static + S3 + CloudFront
#!/bin/bash
#
# generate static files from wordpress, deploy to S3, clear cache of CloudFront.
set -u
readonly BASE_DIR="/mnt/efs/DocumentRoot"
readonly S3_BUCKET=""
readonly CF_DISTRIBUTION_ID=""
readonly WP_UPLOAD_DIR="${BASE_DIR}/wp-content/uploads"
readonly SLACK_USERNAME=""
readonly SLACK_CHANNEL="#"
readonly SLACK_URL=""
readonly HOSTNAME=$( curl -s http://169.254.169.254/latest/meta-data/local-hostname )
readonly LOG_PREFIX_DATE=$( date "+%Y%m%d" )
readonly PREFIX_DATE=$( date "+%Y%m%d%H%M" )
debug=false
#
# Output to /tmp/deploy.sh.${LOG_PREFIX_DATE}.log
# $1: function_name: e.g check_already
# $2: state: start, finish and good
# If state is error, send messages to slack and exit 1.
function log() {
local function_name=$1
local state=$2
echo "${PREFIX_DATE}: ${state}: ${function_name}" >> /tmp/deploy.sh.${LOG_PREFIX_DATE}.log
if [ "${state}" = "error" ]; then
echo "${function_name} return non-zero status code"
slack "${function_name}" "danger"
exit 1
fi
if [ "${debug}" = true ]; then
slack "${function_name}" "good"
fi
}
function check_already() {
log "check_already" "start"
result=$( ps aux | grep deploy.sh | grep -v grep | grep -v touch | grep -v vim | wc -l )
# 2: bash deploy.sh, result and cron parent
if [ ${result} -gt 3 ]; then
echo "already running"
log "check_already->result:${result}" "error"
fi
log "check_already" "finish"
}
function generate() {
log "generate" "start"
sudo -u httpd /usr/local/bin/wp wp2static --path=/mnt/efs/DocumentRoot generate >> /tmp/generate-${LOG_PREFIX_DATE}.log 2>&1
if [ $? -ne 0 ]; then
log "generate" "error"
fi
log "generate" "finish"
}
function deploy() {
log "generate" "start"
sudo -u httpd /usr/local/bin/wp wp2static --path=/mnt/efs/DocumentRoot deploy >> /tmp/deploy-${LOG_PREFIX_DATE}.log 2>&1
if [ $? -ne 0 ]; then
log "deploy" "error"
fi
log "generate" "finish"
}
function upload() {
log "upload" "start"
aws s3 sync /mnt/efs/DocumentRoot/static s3://"${S3_BUCKET}" --delete --exact-timestamps
if [ $? -ne 0 ]; then
log "upload" "error"
fi
log "upload" "finish"
}
function invalidation() {
log "invalidation" "start"
aws cloudfront create-invalidation --distribution-id ${CF_DISTRIBUTION_ID} --paths '/*'
if [ $? -ne 0 ]; then
log "invalidation" "error"
fi
log "invalidation" "finish"
}
#
# Send messages to slack with function_name and state.
# $1: function_name e.g. check_already
# $2: state: danger or good means slack attachment color.
#
function slack() {
local function_name=$1
local state=$2
local icon_emoji=":aaw_yeah"
if [ ${state} = "good" ]; then
icon_emoji=":angel:"
fi
curl -s -X POST --data-urlencode \
"payload={
\"channel\": \"${SLACK_CHANNEL}\",
\"username\": \"${SLACK_USERNAME}\",
\"attachments\": [
{
\"color\": \"${state}\",
\"author_icon\": \":wp:\",
\"author_name\": \"${HOSTNAME}\",
\"title\": \"deploy.sh -> ${function_name}\",
}
],
\"icon_emoji\": \"${icon_emoji}\"
}" \
${SLACK_URL} > /dev/null
}
#
# $1: required lambda or cron
# lambda: send slack for progress
# cron: send slack when only error occurred via log()
#
function main() {
local from=$1
if [ ${from} = "lambda" ]; then
debug=true
fi
check_already
generate
deploy
upload
invalidation
log "main" "finish"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment