docker-tidy/dockertidy/Cli.py

152 lines
5.0 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
2020-03-01 17:42:29 +00:00
import dockertidy.Exception
2020-03-01 17:55:30 +00:00
from dockertidy import __version__
2020-03-09 00:05:17 +00:00
from dockertidy.Autostop import AutoStop
2020-03-01 17:42:29 +00:00
from dockertidy.Config import SingleConfig
2020-03-09 00:05:17 +00:00
from dockertidy.GarbageCollector import GarbageCollector
2020-03-05 22:51:21 +00:00
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
"""
parser = argparse.ArgumentParser(
2020-03-05 22:51:21 +00:00
description="Generate documentation from annotated Ansible roles using templates"
)
parser.add_argument(
"--dry-run",
action="store_true",
default=None,
2020-03-05 22:51:21 +00:00
dest="dry_run",
help="Only log actions, don't stop anything."
)
parser.add_argument(
"-t",
"--timeout",
type=int,
dest="http_timeout",
metavar="HTTP_TIMEOUT",
help="HTTP timeout in seconds for making docker API calls."
)
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-05 22:51:21 +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-01 17:42:29 +00:00
help="Maximum age for a container. Containers older than this age "
"will be removed. Age can be specified in any pytimeparse "
2020-03-05 22:51:21 +00:00
"supported format."
)
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-01 17:42:29 +00:00
help="Maxium age for an image. Images older than this age will be "
"removed. Age can be specified in any pytimeparse supported "
2020-03-05 22:51:21 +00:00
"format."
)
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",
help="Dangling volumes will be removed."
)
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",
help="Never remove images with this tag."
)
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",
help="Never remove containers with this label key "
"or label key=value"
)
parser_stop = subparsers.add_parser(
"stop", help="Stop containers that have been running for too long."
)
parser_stop.add_argument(
"--max-run-time",
type=timedelta_validator,
dest="stop.max_run_time",
metavar="MAX_RUN_TIME",
help="Maximum time a container is allows to run. Time may "
"be specified in any pytimeparse supported format."
)
parser_stop.add_argument(
"--prefix",
action="append",
type=str,
dest="stop.prefix",
metavar="PREFIX",
help="Only stop containers which match one of the "
"prefix."
)
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:
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()