Skip to content

Instantly share code, notes, and snippets.

@filipposc5
Last active February 2, 2017 14:48
Show Gist options
  • Select an option

  • Save filipposc5/4237df181be68b17a8ffc1caa81433c4 to your computer and use it in GitHub Desktop.

Select an option

Save filipposc5/4237df181be68b17a8ffc1caa81433c4 to your computer and use it in GitHub Desktop.
F.. docker
from fabric.api import task, sudo, warn_only
import pdb
def docker_exec(contr, cmd="true"):
return 'docker exec -ti {} bash -c "{}"'.format(contr, cmd)
def healthcheck(c):
ret = {}
for l in c:
cntr = l.split(" ")[-1].strip()
with warn_only():
ret[cntr] = sudo(docker_exec(cntr))
#pdb.set_trace()
return ret
def restart_docker(actuallyRestart=False):
if actuallyRestart:
sudo("service docker stop; service docker start")
def assert_running():
with warn_only():
sudo("initctl list | grep _container | while read x y ; do service $x start ; done")
@task
def f_docker(restart=False):
aa = sudo('docker ps | egrep -v "(tutum|stunnel)"')
newlist = aa.split('\n')
if len(newlist) < 2:
return
need_restart = []
checks = healthcheck(newlist[1:])
for c, v in checks.iteritems():
if hasattr(v, "return_code"):
if v.return_code != 0:
need_restart.append(c)
break
if len(need_restart) > 0:
print "MUST RESTART"
restart_docker(actuallyRestart=restart)
@task
def we_running():
assert_running()
@filipposc5
Copy link
Author

filipposc5 commented Feb 2, 2017

Run with

fab -H $(cat hostlists | xargs | tr ' ' , ) f_docker:restart=True -u filipposc5
fab -H $(cat hostlists | xargs | tr ' ' , ) we_running -u filipposc5

where hostlists is newline separated IP addresses for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment