Here’s a look at how I go about automating common workflows that I find myself repeating.


I update my website a bunch. I used to have various scripts that usually didn’t work very well that would try to handly things from end to end.

Over time I’ve given up on the giant end to end approach and have instead started depending on layers. The pre-processing script does and knows about some things that the other parts of the website generation system don’t need. Like the details about where my pictures are stored in dropbox or other sources of content that are collated togother to construct the website.

So, the lower layers seem to be fairly robust at this point in time. As long as I clean up any cached files and update the repository correctly then the existing scripts should do the rest. So I just needed a little management script that could do the higher level stuff.

That’s where this new script comes in. It starts with the obligatory shebang and license text followed by a usage method in the common format that I like to use:

#!/bin/bash

# Copyright 2015 Curtis Sand
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SCRIPT=$0

usage () {
    echo "$SCRIPT [-h|--help] WEBSITE_DIR";
    echo -e "\nUpdate Curtis' website at the call of a script."
    exit 1;
}

tryordie () {
    local msg=${1}
    local cmd="${@:2}"
    echo -e "${msg}"
    ${cmd}
    local status=$?
    if [[ "$status" != "0" ]]; then
        echo FAILED
        exit 1;
    fi
}

One important feature that you’ll notice is the tryordie method. I couldn’t think of a better name.

The idea is that I don’t want to continue trying to contstruct a broken website if one of the steps fails, so I need to check that each step along the way succeeded before moving on. This repetitive type of action is what the tryordie function was written for. It tries to run the command and it kills the whole script if the command fails. The little logged message is just a bonus feature that I liked.

Next up is the command line parsing code using the getopt tool:

ARGS=`getopt -o "h" -l "help" -n "update_website" -- "$@"`
if [ $? -ne 0 ]; then
    # bad arguments found
    exit 1;
fi
eval set -- "$ARGS"

while true ; do
    case "$1" in
        -h|--help) usage ; shift ;;
        --) shift ; break ;;
        *) echo "Internal error!" ; exit 1 ;;
    esac
done

WEBSITE_DIR=${1}
SUBMODULES="fret music"

The only strike against this script that I have is that the submodules are hard coded. I could write some code to parse the .gitmodule file in the repo but I have longer term plans to remove the submodules from the repo entirely. So instead I’ve just left them hardcoded here for now; quick and dirty.

Finally comes the meat and potatos of the whole orchestra. The content section. A mentioned above the tryordie function is used heavily to help ensure things are working okay as the script progresses:

tryordie "Entering Website Dir, \"${WEBSITE_DIR}\"..." \
    pushd ${WEBSITE_DIR} 2>/dev/null

# Clean things up
tryordie "Cleaning Up..." git clean -fd
tryordie "Updating Main Repo..." git pull --rebase

# Update the submodules
for submodule in ${submodules}; do
    tryordie "Updating Submodule, \"${submodule}\"..." \
        pushd ${submodule} 2>/dev/null && \
        git clean -fd && \
        git pull --rebase && \
        popd 2>/dev/null
done

# Build the website
tryordie "Building Website TMP Cache..." make tmp_cache
tryordie "Uploading Website..." make upload_cache

tryordie "Update Complete" popd &>/dev/null  # /WEBSITE_DIR

notify-send --expire-time 35000 "Website Updated"

The submodule update bit is a bit messy but, hey, it works just fine for me.


I don’t think I made a public anouncement about my code repo yet. This script and a bunch of others can be found in my github.com repository called code. My username is fretboardfreak so look me up!

Full update_website script:
https://github.com/fretboardfreak/code/blob/master/update_website
The code repository:
https://github.com/fretboardfreak/code
My Github Projects:
https://github.com/fretboardfreak
" "