From b9479d501c01e1247854d94e694da0d7030db36d Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 9 Mar 2020 10:25:45 +0100 Subject: [PATCH] implement autostop run method --- CHANGELOG.md | 81 +--------------------------------- dockertidy/Autostop.py | 18 +++++++- dockertidy/GarbageCollector.py | 2 +- docs/content/_index.md | 4 +- 4 files changed, 21 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 539486b..909b4eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,80 +1 @@ -# Change Log - -## [0.7.1](https://github.com/Yelp/docker-custodian/tree/0.7.1) (2018-03-21) -[Full Changelog](https://github.com/Yelp/docker-custodian/compare/v0.6.1...0.7.1) - -**Implemented enhancements:** - -- Remove volumes when removing containers [\#36](https://github.com/Yelp/docker-custodian/pull/36) ([pauloconnor](https://github.com/pauloconnor)) -- Add patterns support for exclude-image-file [\#35](https://github.com/Yelp/docker-custodian/pull/35) ([vshlapakov](https://github.com/vshlapakov)) - -**Fixed bugs:** - -- Fix broken filtering for images used by containers [\#43](https://github.com/Yelp/docker-custodian/pull/43) ([chekunkov](https://github.com/chekunkov)) - -**Closed issues:** - -- docker-custodian crashing when cleaning up. [\#38](https://github.com/Yelp/docker-custodian/issues/38) -- backports.ssl-match-hostname\>=3.5 error when running in Docker [\#37](https://github.com/Yelp/docker-custodian/issues/37) -- Publish on PyPI? [\#10](https://github.com/Yelp/docker-custodian/issues/10) - -**Merged pull requests:** - -- Add --exclude-container-label argument to dcgc [\#47](https://github.com/Yelp/docker-custodian/pull/47) ([ATRAN2](https://github.com/ATRAN2)) -- Port to docker lib [\#46](https://github.com/Yelp/docker-custodian/pull/46) ([samiam](https://github.com/samiam)) -- Correctly handle empty dangling volumes [\#45](https://github.com/Yelp/docker-custodian/pull/45) ([samiam](https://github.com/samiam)) -- Remove dangling volumes [\#40](https://github.com/Yelp/docker-custodian/pull/40) ([ymilki](https://github.com/ymilki)) -- Revert docker-py version change [\#39](https://github.com/Yelp/docker-custodian/pull/39) ([pauloconnor](https://github.com/pauloconnor)) - -## [v0.6.1](https://github.com/Yelp/docker-custodian/tree/v0.6.1) (2016-08-31) -[Full Changelog](https://github.com/Yelp/docker-custodian/compare/v0.5.1...v0.6.1) - -**Closed issues:** - -- Add ability to kill/delete running containers by age [\#25](https://github.com/Yelp/docker-custodian/issues/25) -- Add tag to yelp/docker-custodian on the docker hub [\#21](https://github.com/Yelp/docker-custodian/issues/21) -- Error while fetching server API version [\#20](https://github.com/Yelp/docker-custodian/issues/20) -- Could It also clear mesos tmp files? [\#19](https://github.com/Yelp/docker-custodian/issues/19) - -**Merged pull requests:** - -- Bump to python \>=2.7 [\#33](https://github.com/Yelp/docker-custodian/pull/33) ([danielhoherd](https://github.com/danielhoherd)) -- Overriding tox with /bin/true [\#32](https://github.com/Yelp/docker-custodian/pull/32) ([danielhoherd](https://github.com/danielhoherd)) -- Bump debian version to 0.6.0 [\#31](https://github.com/Yelp/docker-custodian/pull/31) ([danielhoherd](https://github.com/danielhoherd)) -- Remove py26 support, code cleanup [\#27](https://github.com/Yelp/docker-custodian/pull/27) ([kentwills](https://github.com/kentwills)) -- bump docker-py version from 1.4.0 to 1.7.2 [\#26](https://github.com/Yelp/docker-custodian/pull/26) ([oktopuz](https://github.com/oktopuz)) -- Don't remove recently created containers that were never used [\#23](https://github.com/Yelp/docker-custodian/pull/23) ([analogue](https://github.com/analogue)) - -## [v0.5.1](https://github.com/Yelp/docker-custodian/tree/v0.5.1) (2015-09-29) -[Full Changelog](https://github.com/Yelp/docker-custodian/compare/v0.4.0...v0.5.1) - -**Implemented enhancements:** - -- Use Client\(version='auto'\) [\#7](https://github.com/Yelp/docker-custodian/issues/7) - -**Fixed bugs:** - -- Automatically determining API version doesn't work? [\#17](https://github.com/Yelp/docker-custodian/issues/17) - -**Closed issues:** - -- Put on the docker hub using the yelp namespace [\#16](https://github.com/Yelp/docker-custodian/issues/16) -- Is it production ready? [\#15](https://github.com/Yelp/docker-custodian/issues/15) - -**Merged pull requests:** - -- Upgrade requirements to fix \#17 [\#18](https://github.com/Yelp/docker-custodian/pull/18) ([dnephin](https://github.com/dnephin)) -- Automatically determine the Docker API version to use [\#12](https://github.com/Yelp/docker-custodian/pull/12) ([jschrantz](https://github.com/jschrantz)) -- Add install instructions to the README [\#9](https://github.com/Yelp/docker-custodian/pull/9) ([dnephin](https://github.com/dnephin)) -- Fix readme formatting [\#8](https://github.com/Yelp/docker-custodian/pull/8) ([dnephin](https://github.com/dnephin)) -- Support excluding some images from removal [\#6](https://github.com/Yelp/docker-custodian/pull/6) ([dnephin](https://github.com/dnephin)) -- Add .travis.yml [\#5](https://github.com/Yelp/docker-custodian/pull/5) ([dnephin](https://github.com/dnephin)) - -## [v0.4.0](https://github.com/Yelp/docker-custodian/tree/v0.4.0) (2015-07-08) -**Merged pull requests:** - -- Initial rebase of docker-custodian project [\#1](https://github.com/Yelp/docker-custodian/pull/1) ([dnephin](https://github.com/dnephin)) - - - -\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file +* Initial release diff --git a/dockertidy/Autostop.py b/dockertidy/Autostop.py index e8fca7a..0226c57 100644 --- a/dockertidy/Autostop.py +++ b/dockertidy/Autostop.py @@ -31,11 +31,18 @@ class AutoStop: matcher = self._build_container_matcher(prefix) + self.logger.info( + "Stopping containers older than '{}'".format( + timedelta(config["stop"]["max_run_time"], dt_format="%Y-%m-%d, %H:%M:%S") + ) + ) for container_summary in client.containers(): container = client.inspect_container(container_summary["Id"]) name = container["Name"].lstrip("/") - if (matcher(name) and self._has_been_running_since(container, max_run_time)): + if ( + prefix and matcher(name) and self._has_been_running_since(container, max_run_time) + ) or (not prefix and self._has_been_running_since(container, max_run_time)): self.logger.info( "Stopping container %s %s: running since %s" % (container["Id"][:16], name, container["State"]["StartedAt"]) @@ -72,4 +79,11 @@ class AutoStop: def run(self): """Autostop main method.""" - print("run stop") + self.logger.info("Start autostop") + config = self.config.config + + if config["stop"]["max_run_time"]: + self.stop_containers() + + if not config["stop"]["max_run_time"]: + self.logger.info("Skipped, no arguments given") diff --git a/dockertidy/GarbageCollector.py b/dockertidy/GarbageCollector.py index 4b09ad1..139a5af 100644 --- a/dockertidy/GarbageCollector.py +++ b/dockertidy/GarbageCollector.py @@ -243,6 +243,7 @@ class GarbageCollector: """Identify old volumes and remove them.""" dangling_volumes = self._get_dangling_volumes() + self.logger.info("Removing dangling volumes") for volume in reversed(dangling_volumes): self.logger.info("Removing dangling volume %s", volume["Name"]) self._remove_volume(volume) @@ -312,7 +313,6 @@ class GarbageCollector: self.cleanup_images(exclude_set) if config["gc"]["dangling_volumes"]: - self.logger.info("Remove dangling volumes") self.cleanup_volumes() if ( diff --git a/docs/content/_index.md b/docs/content/_index.md index 501e3f9..bdce6ed 100644 --- a/docs/content/_index.md +++ b/docs/content/_index.md @@ -74,9 +74,11 @@ labels to prevent them from being removed. Stop containers that have been running for too long. `docker-tidy stop` will `docker stop` containers where the container name starts -with [\--prefix]{.title-ref} and it has been running for longer than +with [\--prefix]{.title-ref} and/or it has been running for longer than [\--max-run-time]{.title-ref}. +If no prefix is set, __all__ containers matching the `max-run-time` will be stopped! + Example: ```Shell