docker-tidy/dockertidy/Cli.py

139 lines
4.5 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-01 17:42:29 +00:00
from dockertidy.Config import SingleConfig
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()
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",
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(help="sub-command help")
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,
dest="gc.exclude_image",
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,
dest="gc.exclude_container_label",
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)
2020-03-05 22:51:21 +00:00
print(config.config)
2020-03-01 17:42:29 +00:00
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))
return config