docker-tidy/dockertidy/cli.py

150 lines
4.8 KiB
Python
Raw Normal View History

2020-03-01 17:42:29 +00:00
#!/usr/bin/env python3
"""Entrypoint and CLI handler."""
import argparse
2020-03-01 17:55:30 +00:00
import dockertidy.exception
2020-03-01 17:55:30 +00:00
from dockertidy import __version__
from dockertidy.autostop import AutoStop
from dockertidy.config import SingleConfig
from dockertidy.garbage_collector import GarbageCollector
from dockertidy.logger import SingleLog
from dockertidy.parser import timedelta_validator
2020-03-01 17:42:29 +00:00
class DockerTidy:
2020-03-06 21:39:32 +00:00
"""Cli entrypoint to handle command arguments."""
2020-03-01 17:42:29 +00:00
def __init__(self):
self.log = SingleLog()
self.logger = self.log.logger
self.args = self._cli_args()
self.config = self._get_config()
2020-03-09 00:05:17 +00:00
self.gc = GarbageCollector()
self.stop = AutoStop()
self.run()
2020-03-01 17:42:29 +00:00
def _cli_args(self):
"""
Use argparse for parsing CLI arguments.
:return: args objec
"""
2020-03-15 13:50:58 +00:00
parser = argparse.ArgumentParser(description="keep docker hosts tidy")
2020-03-05 22:51:21 +00:00
parser.add_argument(
"--dry-run",
action="store_true",
default=None,
2020-03-05 22:51:21 +00:00
dest="dry_run",
2020-03-15 13:50:58 +00:00
help="only log actions, don't stop anything"
2020-03-05 22:51:21 +00:00
)
parser.add_argument(
"-t",
"--timeout",
type=int,
dest="http_timeout",
metavar="HTTP_TIMEOUT",
2020-03-15 13:50:58 +00:00
help="HTTP timeout in seconds for making docker API calls"
2020-03-05 22:51:21 +00:00
)
parser.add_argument(
"-v", dest="logging.level", action="append_const", const=-1, help="increase log level"
)
parser.add_argument(
"-q", dest="logging.level", action="append_const", const=1, help="decrease log level"
)
parser.add_argument(
"--version", action="version", version="%(prog)s {}".format(__version__)
)
2020-03-01 17:42:29 +00:00
subparsers = parser.add_subparsers(dest="command", help="sub-command help")
2020-03-09 00:05:17 +00:00
subparsers.required = True
2020-03-01 17:42:29 +00:00
2020-03-15 13:50:58 +00:00
parser_gc = subparsers.add_parser("gc", help="run docker garbage collector")
2020-03-01 17:42:29 +00:00
parser_gc.add_argument(
"--max-container-age",
2020-03-05 22:51:21 +00:00
type=timedelta_validator,
dest="gc.max_container_age",
metavar="MAX_CONTAINER_AGE",
2020-03-15 13:50:58 +00:00
help="maximum age for a container, containers older than this age "
"will be removed (dateparser value)"
2020-03-05 22:51:21 +00:00
)
2020-03-01 17:42:29 +00:00
parser_gc.add_argument(
"--max-image-age",
2020-03-05 22:51:21 +00:00
type=timedelta_validator,
dest="gc.max_image_age",
metavar="MAX_IMAGE_AGE",
2020-03-15 13:50:58 +00:00
help="maxium age for an image, images older than this age will be "
"removed (dateparser value)"
2020-03-05 22:51:21 +00:00
)
2020-03-01 17:42:29 +00:00
parser_gc.add_argument(
"--dangling-volumes",
action="store_true",
2020-03-05 22:51:21 +00:00
dest="gc.dangling_volumes",
2020-03-15 13:50:58 +00:00
help="dangling volumes will be removed"
2020-03-05 22:51:21 +00:00
)
2020-03-01 17:42:29 +00:00
parser_gc.add_argument(
"--exclude-image",
action="append",
2020-03-05 22:51:21 +00:00
type=str,
2020-03-09 00:05:17 +00:00
dest="gc.exclude_images",
2020-03-05 22:51:21 +00:00
metavar="EXCLUDE_IMAGE",
2020-03-15 13:50:58 +00:00
help="never remove images with this tag"
2020-03-05 22:51:21 +00:00
)
2020-03-01 17:42:29 +00:00
parser_gc.add_argument(
"--exclude-container-label",
2020-03-05 22:51:21 +00:00
action="append",
type=str,
2020-03-09 00:05:17 +00:00
dest="gc.exclude_container_labels",
2020-03-05 22:51:21 +00:00
metavar="EXCLUDE_CONTAINER_LABEL",
2020-03-15 13:50:58 +00:00
help="never remove containers with this label key "
2020-03-05 22:51:21 +00:00
"or label key=value"
)
parser_stop = subparsers.add_parser(
2020-03-15 13:50:58 +00:00
"stop", help="stop containers that have been running for too long"
2020-03-05 22:51:21 +00:00
)
parser_stop.add_argument(
"--max-run-time",
type=timedelta_validator,
dest="stop.max_run_time",
metavar="MAX_RUN_TIME",
2020-03-15 13:50:58 +00:00
help="maximum time a container is allows to run (dateparser value)"
2020-03-05 22:51:21 +00:00
)
parser_stop.add_argument(
"--prefix",
action="append",
type=str,
dest="stop.prefix",
metavar="PREFIX",
2020-03-15 13:50:58 +00:00
help="only stop containers which match one of the prefix"
2020-03-05 22:51:21 +00:00
)
2020-03-01 17:42:29 +00:00
return parser.parse_args().__dict__
def _get_config(self):
try:
config = SingleConfig(args=self.args)
except dockertidy.exception.ConfigError as e:
2020-03-01 17:42:29 +00:00
self.log.sysexit_with_message(e)
try:
self.log.set_level(config.config["logging"]["level"])
except ValueError as e:
2020-03-05 22:51:21 +00:00
self.log.sysexit_with_message("Can not set log level.\n{}".format(str(e)))
2020-03-01 17:42:29 +00:00
self.logger.info("Using config file {}".format(config.config_file))
2020-03-09 00:05:17 +00:00
self.logger.debug("Config dump: {}".format(config.config))
2020-03-01 17:42:29 +00:00
return config
2020-03-09 00:05:17 +00:00
def run(self):
"""Cli main method."""
if self.config.config["command"] == "gc":
self.gc.run()
elif self.config.config["command"] == "stop":
self.stop.run()
2020-11-15 19:51:26 +00:00
def main():
DockerTidy()