Created
April 26, 2015 15:09
-
-
Save marji/83cd7722ea26a391b3a1 to your computer and use it in GitHub Desktop.
Script to update running libre presentation from Dropbox. Written for a Raspberry PI.
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 | |
# Drop And Play | |
# A script for updating a document from a dropbox URL. | |
# It checks the etag, if it has changed, it downloads and deploy the new document. | |
# Call this from Cron. | |
# | |
# Originally written for Raspberry PI running a Libre Office impress slideshow | |
# in an infinite loop (called play.odp) | |
# Marji Cermak, April 27, 2015 | |
# The shared dropbox URL: | |
DOCO_URL='https://www.dropbox.com/x/SOMEPATH/test.txt?dl=0' | |
# The file we download from dropbox must be this type: | |
PLAY_FILE_TYPE='OpenDocument Presentation' | |
# The current document (symlink to a versioned file): | |
PLAY=~/play.live | |
# Required space in the PWD. | |
# Stop if there is less to prevent filling up the disk. | |
MIN_FREE_SPACE=1000000 | |
# The directory we keep the document versions: | |
DOWNLOAD_DIR=~/drop_and_play | |
if [ ! -e $DOWNLOAD_DIR ]; then | |
echo "ERR: Directory $DOWNLOAD_DIR does not exist" | |
exit 1 | |
fi | |
# TODO: etag could be read from the currently deployed document filename. | |
ETAG_FILE="${DOWNLOAD_DIR}/etag" | |
# A semaphore preventing this script to do any changes, if it exists. | |
# Can be used for sanity - if there is an error, stop this script from | |
# downloading again and again. | |
STOP_FILE=/tmp/stop_drop_and_play | |
# If this file exists, it means we deployed a new version of the document and | |
# the application might need a restart. | |
RESTART_REQUIRED=/tmp/restart_play | |
# check we are not running already: http://mywiki.wooledge.org/BashFAQ/045 | |
exec 9>/tmp/update_lock | |
if ! flock -n 9 ; then | |
echo "Another instance of this script running."; | |
exit 1 | |
fi | |
# read the currently deployed etag: | |
if [ -f ${ETAG_FILE} ]; then | |
ETAG_LOCAL=$(<${ETAG_FILE}) | |
else | |
echo "Warn: no ETAG_FILE found." | |
fi | |
# A shared dropbox URL seems to always redirect to a one-time unique URL, | |
# but the etag stays the same if the file has not changed | |
ETAG_LINE=$(curl --silent --location --head ${DOCO_URL} | grep ^etag | tr -d '\r') | |
ETAG=`echo ${ETAG_LINE} | awk '/etag:/ { print $2 }'` | |
echo "DEBUG: local version: ${ETAG_LOCAL}, remote version: ${ETAG}." | |
if [ "${ETAG_LOCAL}" == "${ETAG}" ]; then | |
echo "DEBUG: Local and remote versions are identical. Exit." | |
exit 0 | |
fi | |
# Check free space, so we don't fill up | |
FREE_SPACE=$(df -P . | awk 'NR==2 {print $4}') | |
if [ "${FREE_SPACE}" -lt "${MIN_FREE_SPACE}" ]; then | |
echo "ERR: not enouch free space (${FREE_SPACE} free). Exit." | |
exit 1 | |
fi | |
# Check we are allowed to download | |
if [ -e "${STOP_FILE}" ]; then | |
echo "ERR: not allowed to carry on" | |
exit 1 | |
fi | |
# dropbox has different etag then the local version. | |
# Download the new version | |
# | |
NEW_PLAY="${DOWNLOAD_DIR}/play-${ETAG}.odp" | |
if [ -e "${NEW_PLAY}" ]; then | |
echo "ERR: NEW_PLAY file already exists. $NEW_PLAY. assert." | |
touch "${STOP_FILE}" | |
exit 1 | |
fi | |
echo "WARN: Downloading new version ${ETAG}." | |
curl -o "${NEW_PLAY}" --location ${DOCO_URL} | |
RV=$? | |
if [ $RV -ne 0 ]; then | |
echo "ERR: failed downloading via curl, RETVAL ${RV}" | |
touch "${STOP_FILE}" | |
exit 1 | |
fi | |
if [ ! -f "${NEW_PLAY}" ]; then | |
echo "ERR: new downloaded file does not exist. assert." | |
touch "${STOP_FILE}" | |
exit 1 | |
fi | |
FILE_TYPE=$(file -b "${NEW_PLAY}") | |
if [ "${FILE_TYPE}" != "${PLAY_FILE_TYPE}" ]; then | |
echo "ERR: retrieved unexpected file type: ${FILE_TYPE}" | |
touch "${STOP_FILE}" | |
exit 1 | |
fi | |
# TODO: check the new document size | |
# Successful download. Replace the old link with a new one. | |
ln -sf "${NEW_PLAY}" "${PLAY}" | |
echo "${ETAG}" > $ETAG_FILE | |
echo "${ETAG}" > $RESTART_REQUIRED | |
echo "DEBUG: new version installed. App restart is needed." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment