Skip to content

Commit

Permalink
Better output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
leviable committed May 27, 2018
1 parent cc73c0f commit 7dd1f03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,6 +1,11 @@
CHANGES
=======

* Better output formatting

0.2.0
-----

* Handle docker client 409 responses
* Update tests
* Add CLI options and allow \`force\`ed nuking
Expand Down
28 changes: 15 additions & 13 deletions dockernuke/main.py
Expand Up @@ -3,32 +3,34 @@


def main(force=False):
""" Finds and stops/removes all docker containers, images, and volumes """
client = docker.from_env()

for running_container in client.containers.list():
click.echo("Stopping container: {0}".format(running_container.name))
_docker_call(running_container.stop, force=force)
echo_msg = "Stopping container {0}: ".format(running_container.short_id)
_docker_call(running_container.stop, echo_msg, timeout=2)

for container in client.containers.list(all=True):
click.echo("Removing container: {0}".format(container.name))
_docker_call(container.remove, force=force)
echo_msg = "Removing container {0}: ".format(container.short_id)
_docker_call(container.remove, echo_msg, force=force)

for image in client.images.list():
click.echo("Removing image: {0}".format(image.tags))
_docker_call(client.images.remove, image.id, force=force)
echo_msg = "Removing image {0}: ".format(image.short_id.split(':')[-1])
_docker_call(client.images.remove, echo_msg, image.id, force=force)

for volume in client.volumes.list():
click.echo("Removing volume: {0}".format(volume.name))
_docker_call(volume.remove, force=force)
echo_msg = "Removing volume {0}: ".format(volume.short_id)
_docker_call(volume.remove, echo_msg, force=force)


def _docker_call(method, *args, **kwargs):
""" """
def _docker_call(method, msg, *args, **kwargs):
""" Calls `method`, echoing `msg`, and passing `*args` and `**kwargs` to the method """
click.echo(msg, nl=False)
try:
method(*args, **kwargs)
except docker.errors.APIError as exc:
if exc.status_code != 409:
raise
msg = ("Stopping/removing failed with status code 409. "
"Rerun with '--force' to force removal")
click.echo(click.style(msg, fg='red'))
click.secho('Failed (retry with "--force")', fg='red')
else:
click.secho('Succeeded', fg='green')
5 changes: 4 additions & 1 deletion tox.ini
Expand Up @@ -17,4 +17,7 @@ deps =
pytest-cov
pytest-sugar
commands =
py.test -s --cov dockernuke --cov-report term-missing:skip-covered tests []
py.test --cov dockernuke tests []

[coverage:report]
show_missing = True

0 comments on commit 7dd1f03

Please sign in to comment.