diff options
author | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2020-01-24 12:57:24 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-01-24 12:57:24 +0000 |
commit | 75f35a9ee8b6fc8b79ce4db1717f615de9d97b63 (patch) | |
tree | d1d9897bc6137c95df4d90d0afcbbcec7e25d026 | |
parent | cb26bd845d1005fcfdef28e599d59d31fc226a55 (diff) | |
parent | 13aa60d4c34994c12b24c140547ed300e77fd494 (diff) |
Merge "Help script to remove runtime images from datalist"
-rwxr-xr-x | tools/remove_runtime_images.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tools/remove_runtime_images.py b/tools/remove_runtime_images.py new file mode 100755 index 00000000..67d732bb --- /dev/null +++ b/tools/remove_runtime_images.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import yaml +import sys +import argparse + + +class ModifiedParser(argparse.ArgumentParser): + """ + modified error handling to print help + """ + def error(self, message): + sys.stderr.write('error: %s\n' % message) + self.print_help() + sys.exit(2) + + +def create_blacklist(config_file): + """ + Generate a list of images which needs to be excluded from docker_image_list + :param config_file: application_configuration file where images are. + :return: + """ + with open(config_file, 'r') as f: + file = yaml.load(f, Loader=yaml.SafeLoader) + + blacklist=[] + for name, _ in file['runtime_images'].items(): + path = file['runtime_images'][name]['path'] + blacklist.append(path[1:]) + return blacklist + + +def should_remove_line(line, blacklist): + """ + Helping function to match image in blacklist + :param line: line in datalist file + :param blacklist: list of images to be removed + :return + """ + return any([image in line for image in blacklist]) + + +def update_datalist(config_file, datalist): + """ + remove local images from datalist. + :param config_file: application_configuration file where images are. + :param datalist: docker_image_list to be updated + :return: + """ + blacklist = create_blacklist(config_file) + data = [] + with open(datalist, 'r') as f: + for line in f: + if not should_remove_line(line, blacklist): + data.append(line) + with open(datalist, 'w') as f: + for line in data: + f.write(line) + + +def run_cli(): + """ + Run as cli tool + """ + + parser = ModifiedParser(description='Remove runtime images from docker_image_list') + + parser.add_argument('config_file', + help='application_configuration file where images are') + parser.add_argument('datalist', + help='docker_image_list from where images should be removed') + args = parser.parse_args() + + update_datalist(args.config_file, args.datalist) + + +if __name__ == '__main__': + run_cli() + |