Last active
July 28, 2017 17:24
-
-
Save RNCTX/359489a5432937578bf5736850917d70 to your computer and use it in GitHub Desktop.
Jekyll/Amazon S3/Dropbox cron script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export PATH="$PATH" | |
############ CHANGE VARS BELOW ############## | |
BASH_ENV="/home/ubuntu/.bashrc" | |
sourcedir="/home/ubuntu/Dropbox" | |
destdir="/home/ubuntu/_site" | |
log="/home/ubuntu/jekyll_build.log" | |
s3_website_confdir="/home/ubuntu" | |
dropbox="/opt/dropbox/dropbox.py" | |
jekyll="/home/ubuntu/.rvm/gems/ruby-2.3.0/wrappers/jekyll" | |
s3_website="/home/ubuntu/.rvm/gems/ruby-2.3.0/wrappers/s3_website" | |
############# CHANGE VARS ABOVE ############# | |
# | |
# Jekyll/Amazon S3/Dropbox cron script: | |
# | |
# This script does the following: | |
# | |
# 1) stops dropbox sync | |
# 2) incrementally builds a jekyll site from the user's Dropbox | |
# 3) 'cleans' blank lines from output HTM(L) and XML files with sed | |
# 4) uploads built files to s3 via the s3_website gem | |
# 5) restarts dropbox sync | |
# 6) logs the output | |
# | |
# Assumptions: | |
# | |
# Dropbox client is running as a daemon | |
# 'jekyll --build' works for this user | |
# 's3_website push' works for this user | |
# | |
# Warnings: | |
# | |
# Error handling is rudimentary at best. Jekyll's failure | |
# methods have varying degrees of unpredictable output. If | |
# a '_config.yml' exists in your source folder and an 'index' | |
# exists in the destination after the build, this script will | |
# push its output to your s3 bucket even if the build is broken! | |
# If you run this on a BSD (including OSX) with a POSIX sed, the | |
# sed command will likely need to be modified! | |
# | |
# Don't forget to add the output log to logrotate. | |
# | |
# Optimally, run this script in a user's cron, no elevated privileges | |
# are required if the associated files are all accessible to the user. | |
# If additional paths are required, modify the PATH statement above | |
# by adding a : followed by more paths. Ex: | |
# | |
# export PATH="$PATH:/some/where/bin:/some/where/else/bin" | |
# | |
# | |
sourcecheck="$(ls "$sourcedir" | grep _config.yml)" | |
status="$($dropbox status)" | |
index="$(ls "$destdir" | grep index)" | |
s3config="$(ls "$s3_website_confdir" | grep s3_website.yml)" | |
if [[ -z "$sourcecheck" ]] || [[ -z "$s3config" ]] || [ "$status" != "Up to date" ]; then | |
prefail="$(echo -e 'Either dropbox is not idle, or a required component was not found.\nCheck config variables and try again.' && $dropbox start && echo 'Failed on' `date` && echo '---')" | |
echo "${prefail}" >> "$log" | |
exit 1; | |
else | |
build="$(echo 'Began on' `date` && $dropbox stop && $jekyll build --source "$sourcedir" --destination "$destdir" --incremental)" | |
echo "${build}" >> "$log" | |
find $destdir -type f \( -name "*.htm*" -o -name "*.xml" \) -exec sed -i '/^\s*$/d' {} \; | |
fi | |
if [[ -z "$index" ]]; then | |
buildfail="$(echo 'No index file found in destination, the jekyll build may have failed. No s3 upload was performed' && $dropbox start && echo 'Failed on' `date` && echo '---')" | |
echo "${buildfail}" >> "$log" | |
exit 1; | |
else | |
s3push="$(cd "$destdir" && cd "../" && $s3_website push --config-dir "$s3_website_confdir" && $dropbox start && echo 'Success on' `date` && echo '---')" | |
echo "${s3push}" >> "$log" | |
fi | |
$dropbox start | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment