#!/bin/bash
#
# Pulls a docker image.
# Returns 0 if there a change.
# Returns 2 if there is no change.
# Returns 3 if something when wrong.
#
DOCKER_IMAGE=$1

BEFORE=$(docker inspect --type image --format='{{.Id}}' ${DOCKER_IMAGE} 2>/dev/null)
<%= $docker_command %> pull ${DOCKER_IMAGE}
AFTER=$(docker inspect --type image --format='{{.Id}}' ${DOCKER_IMAGE} 2>/dev/null)

if [[ -z $AFTER ]]; then
  echo "Docker image ${DOCKER_IMAGE} failed to pull!"
  exit 3
elif [[ $BEFORE == $AFTER ]]; then
  echo "No updates to ${DOCKER_IMAGE} available. Currently on ${AFTER}."
  exit 2
else
  echo "${DOCKER_IMAGE} updated. Changed from ${BEFORE} to ${AFTER}."
  exit 0
fi
