From 696f0b9a06e415d8ef320bd8279c4911edbaca7f Mon Sep 17 00:00:00 2001
From: Jeff Peeler <jpeeler@redhat.com>
Date: Wed, 4 May 2016 23:25:53 -0400
Subject: [PATCH] Generate image dependencies on CLI

Using the new --list-dependencies flag, one can query dependencies for a
given filter. For example:

./build.py --list-dependencies heat swift
{'base': [{'openstack-base': [{'heat-base': ['heat-engine',
                                             'heat-api-cfn',
                                             'heat-api']},
                              {'swift-base': ['swift-object',
                                              'swift-proxy-server',
                                              'swift-container',
                                              'swift-rsyncd',
                                              'swift-account']}]}]}

Also added --list-images to list all available images.

Change-Id: I1797e32e32705182a763f53329eeb5c4a361abec
Implements: blueprint images-dependency-tree-cli
---
 kolla/cmd/build.py     | 40 ++++++++++++++++++++++++++++++++++++++++
 kolla/common/config.py |  4 ++++
 2 files changed, 44 insertions(+)

diff --git a/kolla/cmd/build.py b/kolla/cmd/build.py
index 5a00b1d809..b305ffd7b4 100755
--- a/kolla/cmd/build.py
+++ b/kolla/cmd/build.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 # TODO(jpeeler): Add clean up handler for SIGINT
+from __future__ import print_function
 
 import datetime
 import errno
@@ -20,6 +21,7 @@ import graphviz
 import json
 import logging
 import os
+import pprint
 import re
 import requests
 import shutil
@@ -662,6 +664,38 @@ class KollaWorker(object):
         with open(to_file, 'w') as f:
             f.write(dot.source)
 
+    def list_images(self):
+        for count, image in enumerate(self.images):
+            print(count + 1, ':', image['name'])
+
+    def list_dependencies(self):
+        match = False
+        for image in self.images:
+            if image['status'] in ['matched']:
+                match = True
+            if image['parent'] is None:
+                base = image
+        if not match:
+            print('Nothing matched!')
+            return
+
+        def list_children(images, ancestry):
+            children = ancestry.values()[0]
+            for item in images:
+                if item['status'] not in ['matched']:
+                    continue
+
+                if not item['children']:
+                    children.append(item['name'])
+                else:
+                    newparent = {item['name']: []}
+                    children.append(newparent)
+                    list_children(item['children'], newparent)
+
+        ancestry = {base['name']: []}
+        list_children(base['children'], ancestry)
+        pprint.pprint(ancestry)
+
     def find_parents(self):
         """Associate all images with parents and children"""
         sort_images = dict()
@@ -723,6 +757,12 @@ def main():
         LOG.info('Docker images dependency is saved in %s',
                  conf.save_dependency)
         return
+    if conf.list_images:
+        kolla.list_images()
+        return
+    if conf.list_dependencies:
+        kolla.list_dependencies()
+        return
 
     for x in six.moves.range(conf.threads):
         worker = WorkerThread(queue, push_queue, conf)
diff --git a/kolla/common/config.py b/kolla/common/config.py
index 6ccbdf7806..31836c4ba7 100644
--- a/kolla/common/config.py
+++ b/kolla/common/config.py
@@ -76,6 +76,10 @@ _CLI_OPTS = [
     cfg.BoolOpt('keep', default=False,
                 deprecated_group='kolla-build',
                 help='Keep failed intermediate containers'),
+    cfg.BoolOpt('list-dependencies', short='l',
+                help='Show image dependencies (filtering supported)'),
+    cfg.BoolOpt('list-images',
+                help='Show all available images'),
     cfg.StrOpt('namespace', short='n', default='kollaglue',
                deprecated_group='kolla-build',
                help='The Docker namespace name'),
-- 
GitLab