Last active
February 2, 2017 14:48
-
-
Save filipposc5/4237df181be68b17a8ffc1caa81433c4 to your computer and use it in GitHub Desktop.
F.. docker
This file contains hidden or 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
| 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() | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
where
hostlistsis newline separated IP addresses for example.