Last active
November 9, 2020 09:52
-
-
Save davidohana/0d95b9f075ca0a52c887db4032c6432b 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/env python2 | |
from peasyshell import * | |
# initialize logging to stdout in colors by severity level | |
init_logging() | |
cfg = { | |
"container_name": "ELASTIC", | |
"image_name": "elasticsearch:6.8.13" | |
} | |
get_latest = yes_or_no("get latest version?") | |
if get_latest: | |
# run a formatted command, output is send to stdout, terminate on cmd failure | |
sh("docker image pull {image_name}", **cfg) | |
# run a formatted command and capture standard output stream, | |
# do not terminate on error. Shell enabled to allow piping. | |
res = sh("docker ps -a | grep {container_name}", | |
shell=True, capture_out=True, exit_on_fail=False, **cfg) | |
# grep error code 0 means search string found | |
if res.returncode == 0: | |
# process the output conveniently, no need for awk/sed | |
old_container_id = res.stdout.split()[0] | |
logger.info("removing old container with ID " + old_container_id) | |
# cmd formatting is also possible with positional arguments | |
sh("docker rm -f {}", [old_container_id]) | |
else: | |
logger.info("container does not exist") | |
sh("docker run -d --name {container_name} {image_name}", capture_out=True, **cfg) | |
# run command and stop if after 5 seconds | |
# last result always stored in shres local variable | |
sh("docker logs {} -f", [shres.stdout], timeout_sec=5) | |
logger.info("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment