Skip to content

Instantly share code, notes, and snippets.

@vpetersson
Last active April 29, 2016 14:57
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 vpetersson/f3c741bad8830286179873f0aa6ca681 to your computer and use it in GitHub Desktop.
Save vpetersson/f3c741bad8830286179873f0aa6ca681 to your computer and use it in GitHub Desktop.
import time
import sys
import os
import dateutil.parser
from docker import Client
"""
Helper script to determine if you should rebuild the image or use the upstream image
using the modification timestamp of the Dockerfile.
Requires that you first pull down the image from upstream.
Returns either 'build_image' or 'upstream_image'
Usage: fetch_or_build.py JohnDoe/SuperImage Dockerfile
"""
cli = Client(base_url='unix://var/run/docker.sock')
if len(sys.argv) < 2:
print 'Usage: fetch_or_build.py JohnDoe/SuperImage Dockerfile'
sys.exit(1)
else:
IMAGE = sys.argv[1]
IMAGEFILE = sys.argv[2]
def get_image_timestamp(image):
"""
Returns UNIX timestamp of image file
"""
get_image = None
try:
get_image = cli.inspect_image(image)
except:
print 'Image not found. Exiting.'
return False
dt = get_image.get('Created', False)
if dt:
parsed_date = dateutil.parser.parse(dt)
return time.mktime(parsed_date.timetuple())
else:
return False
def get_imagefile_timestamp(imagefile):
if os.path.isfile(imagefile):
return os.stat(IMAGEFILE).st_mtime
else:
return False
def __main__():
image_ts = get_image_timestamp(IMAGE)
imagefile_ts = get_imagefile_timestamp(IMAGEFILE)
if imagefile_ts > image_ts:
print 'build_image'
else:
print 'upstream_image'
__main__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment