Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.