summaryrefslogtreecommitdiffstats
path: root/tools/remove_runtime_images.py
blob: 67d732bbe0471f3cf94762bb57267c22c30e4fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()