From 82a9572df8057eb22a405771b43c32522f46e887 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 15 Jul 2013 17:54:36 -0400 Subject: [PATCH] Smarter removal of host/group associations means running the inventory script won't pad database rows when not needed. Similar to last change made for groups. --- .../management/commands/inventory_import.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index 2c62979d78..61166754c5 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -521,9 +521,23 @@ class Command(BaseCommand): if overwrite: LOGGER.info("purging host group memberships") db_groups = Group.objects.filter(inventory=inventory) - for g in db_groups: - g.hosts.clear() - g.save() + #for g in db_groups: + # g.hosts.clear() + # g.save() + + for db_group in db_groups: + db_hosts = db_group.hosts.all() + mem_hosts = group_names[db_group.name].hosts + mem_host_names = [ h.name for h in mem_hosts ] + removed = False + for db_host in db_hosts: + if db_host.name not in mem_host_names: + removed = True + print "DEBUG: removing non-DB host: %s" % (db_host.name) + db_group.hosts.remove(db_host) + if removed: + db_group.save() + # for each host in a mem group, add it to the parents to which it belongs # FIXME: where it does not already exist