Last active
August 29, 2015 14:23
-
-
Save choffmeister/a05be97953ecfe5cad7b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import subprocess | |
import os | |
import re | |
import sys | |
import time | |
import datetime | |
def log_info(msg): | |
ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') | |
print '%s [info ] %s' % (ts, msg) | |
def log_err(msg): | |
ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') | |
print >> sys.stderr, '%s [error] %s' % (ts, msg) | |
def run(cmd, cwd = None, stdout = subprocess.PIPE): | |
p = subprocess.Popen(cmd, cwd = cwd, env = os.environ, stdout = stdout, stderr = subprocess.PIPE) | |
stdout, err = p.communicate() | |
if p.wait() != 0: raise Exception(err) | |
return stdout | |
def run_docker(cmd, cwd = None, stdout = subprocess.PIPE): | |
docker_bin = '/usr/bin/docker' | |
return run([docker_bin] + cmd, cwd = cwd, stdout = stdout) | |
def docker_pull(name, tag): | |
return run_docker(['pull', name + ':' + tag], stdout = None) | |
def docker_images(): | |
stdout = run_docker(['images', '--no-trunc=true']) | |
res = {} | |
for l in stdout.splitlines(): | |
m = re.search('''^([^\s]+)\s+([^\s]+)\s+([0-9a-f]{64})''', l) | |
if m != None: | |
name = m.group(1) | |
tag = m.group(2) | |
id = m.group(3) | |
if name != '<none>' and tag != '<none>': | |
res[name + ':' + tag] = id | |
return res | |
def docker_image_id(name, tag): | |
images = docker_images() | |
key = name + ':' + tag | |
if key in images: | |
return images[key] | |
else: | |
return None | |
def docker_pull_and_check(name, tag): | |
id_pre = docker_image_id(name, tag) | |
log_info('Current %s:%s image is %s' % (name, tag, id_pre)) | |
docker_pull(name, tag) | |
id_post = docker_image_id(name, tag) | |
log_info('Updated %s:%s image is %s' % (name, tag, id_post)) | |
return id_pre != id_post | |
images = [ | |
{ 'name': 'docker.epages.com/unity/epages-ui', 'tag': 'develop' }, | |
{ 'name': 'docker.epages.com/unity/epages-ui-storage', 'tag': 'dev' }, | |
{ 'name': 'docker.epages.com/unity/epages-imageservice', 'tag': 'dev' } | |
] | |
log_info('Updating images') | |
updates = map(lambda x: docker_pull_and_check(x['name'], x['tag']), images) | |
if any(updates): | |
log_info('At least one image updated. Restarting instance') | |
run(['/sbin/restart', 'unity-develop']) | |
else: | |
log_info('Already running most recent images') |
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
description "Unity development" | |
author "Christian Hoffmeister" | |
start on filesystem and started docker | |
stop on runlevel [!2345] | |
respawn | |
chdir /var/instances/unitydevelop | |
script | |
/usr/local/bin/docker-compose up | |
end script | |
post-stop script | |
/usr/local/bin/docker-compose kill | |
/usr/local/bin/docker-compose rm -vf | |
end script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment