summaryrefslogtreecommitdiffstats
path: root/build/download/clean_docker_images.py
blob: 42157490199f7f409432af079351cef0f524fe35 (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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

#   COPYRIGHT NOTICE STARTS HERE

#   Copyright 2019 © Samsung Electronics Co., Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

#   COPYRIGHT NOTICE ENDS HERE


import argparse
import docker
import logging
import sys

from downloader import AbstractDownloader
from docker_downloader import DockerDownloader


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('image_lists', nargs='+', help='Images to keep')
    parser.add_argument('--debug', '-d', action='store_true', help='Debugging messages')
    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
    else:
        logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')

    target = set()
    for lst in args.image_lists:
        target = target.union(AbstractDownloader.load_list(lst))

    target = set(map(DockerDownloader.image_registry_name, target))

    client = docker.client.DockerClient(version='auto')

    errors = 0
    for image in client.images.list():
        for tag in image.tags:
            logging.debug('Checking {}'.format(tag))
            if tag not in target:
                logging.debug('Image \'{}\' not in lists'.format(tag))
                logging.info('Removing: {}'.format(tag))
                try:
                    client.images.remove(tag)
                    logging.info('Removed: {}'.format(tag))
                except docker.errors.APIError as err:
                    errors += 1
                    logging.exception(err)
            else:
                logging.debug('Image \'{}\' found in lists.'.format(tag))
    sys.exit(errors)


if __name__ == '__main__':
    main()