#!/usr/bin/env python3
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
"""arvados_docker.cleaner - Remove unused Docker images from compute nodes
Usage:
import docker
import json
+DEFAULT_CONFIG_FILE = '/etc/arvados/docker-cleaner/docker-cleaner.json'
+
SUFFIX_SIZES = {suffix: 1024 ** exp for exp, suffix in enumerate('kmgt', 1)}
logger = logging.getLogger('arvados_docker.cleaner')
args = parse_arguments(arguments)
config = default_config()
- with open(args.config, 'r') as f:
- config.update(json.load(f))
+ try:
+ with open(args.config, 'r') as f:
+ c = json.load(f)
+ config.update(c)
+ except (FileNotFoundError, IOError, ValueError) as error:
+ if (isinstance(error, FileNotFoundError) and
+ args.config == DEFAULT_CONFIG_FILE):
+ logger.warning("DEPRECATED: default config file %s not found; "
+ "relying on command line configuration",
+ repr(DEFAULT_CONFIG_FILE))
+ else:
+ sys.exit('error reading config file {}: {}'.format(
+ args.config, error))
configargs = vars(args).copy()
configargs.pop('config')
formatter_class=Formatter,
)
parser.add_argument(
- '--config', action='store', type=str, default='/etc/arvados/docker-cleaner/config.json',
+ '--config', action='store', type=str, default=DEFAULT_CONFIG_FILE,
help="configuration file")
deprecated = " (DEPRECATED -- use config file instead)"
return parser.parse_args(arguments)
-def setup_logging(config):
+def setup_logging():
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter(
'%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s',
'%Y-%m-%d %H:%M:%S'))
logger.addHandler(log_handler)
+
+
+def configure_logging(config):
logger.setLevel(logging.ERROR - (10 * config['Verbose']))
def main(arguments=sys.argv[1:]):
+ setup_logging()
config = load_config(arguments)
- setup_logging(config)
+ configure_logging(config)
try:
- run(config, docker.Client(version='1.14'))
+ run(config, docker.APIClient(version='1.35'))
except KeyboardInterrupt:
sys.exit(1)