Summary: remove group parent association from host object
Description: - remove group back-link from host. add new method on inventory to get all host and their groups in a dict. this avoids multiple calls to recurse the groups for each host in host list. - all remove group child-to-parent links - minor clean up
This commit is contained in:
parent
1e33dc3d40
commit
8a3f42b54e
|
@ -72,27 +72,18 @@ class Host(object):
|
|||
|
||||
def __init__(self, hostname):
|
||||
self.name = hostname
|
||||
self._groups = {} # kv = groupname:group
|
||||
self.alias = ''
|
||||
self.is_mgmt = False
|
||||
self.hypervisor = ''
|
||||
self.vars = {}
|
||||
self.version = self.__class__.class_version
|
||||
|
||||
def get_vars(self):
|
||||
return self.vars.copy()
|
||||
|
||||
def upgrade(self):
|
||||
pass
|
||||
|
||||
def _add_group(self, group):
|
||||
if group.name not in self._groups:
|
||||
self._groups[group.name] = group
|
||||
|
||||
def _remove_group(self, group):
|
||||
if group.name in self._groups:
|
||||
del self._groups[group.name]
|
||||
|
||||
def get_groups(self):
|
||||
return self._groups.values()
|
||||
|
||||
def check(self):
|
||||
sshKeysExist = ssh_check_keys()
|
||||
if not sshKeysExist:
|
||||
|
@ -176,7 +167,6 @@ class Group(object):
|
|||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.parent = None
|
||||
self.children = []
|
||||
self._hosts = {} # kv = hostname:object
|
||||
self._version = 1
|
||||
|
@ -191,16 +181,26 @@ class Group(object):
|
|||
self._hosts[host.name] = host
|
||||
else:
|
||||
host = self._hosts[host.name]
|
||||
host._add_group(self)
|
||||
|
||||
def remove_host(self, host):
|
||||
if host.name in self._hosts:
|
||||
host._remove_group(self)
|
||||
del self._hosts[host.name]
|
||||
|
||||
def get_hosts(self):
|
||||
return self._hosts.values()
|
||||
|
||||
def get_hostnames(self):
|
||||
return self._hosts.keys()
|
||||
|
||||
def get_childnames(self):
|
||||
names = []
|
||||
for child in self.children:
|
||||
names.append(child.name)
|
||||
return names
|
||||
|
||||
def get_vars(self):
|
||||
return self.vars.copy()
|
||||
|
||||
|
||||
class Inventory(object):
|
||||
class_version = 1
|
||||
|
@ -272,16 +272,17 @@ class Inventory(object):
|
|||
for (service_name, container_names) in services.items():
|
||||
service_group = Group(service_name)
|
||||
deploy_group.children.append(service_group)
|
||||
service_group.parent = deploy_group
|
||||
for container_name in container_names:
|
||||
container_group = Group(container_name)
|
||||
service_group.children.append(container_group)
|
||||
container_group.parent = service_group
|
||||
self._groups[deploy_name] = deploy_group
|
||||
|
||||
def get_hosts(self):
|
||||
return self._hosts.values()
|
||||
|
||||
def get_hostnames(self):
|
||||
return self._hosts.keys()
|
||||
|
||||
def get_host(self, hostname):
|
||||
host = None
|
||||
if hostname in self._hosts:
|
||||
|
@ -305,11 +306,46 @@ class Inventory(object):
|
|||
def remove_host(self, hostname, groupname=None):
|
||||
if hostname in self._hosts:
|
||||
host = self._hosts[hostname]
|
||||
groups = host.get_groups()
|
||||
groups = self.get_groups(host)
|
||||
group_count = len(groups)
|
||||
for group in groups:
|
||||
if not groupname or groupname == group.name:
|
||||
host._remove_group(group)
|
||||
group_count -= 1
|
||||
group.remove_host(host)
|
||||
|
||||
# if host no longer exists in any group, remove it from inventory
|
||||
if not host.get_groups():
|
||||
if group_count == 0:
|
||||
del self._hosts[hostname]
|
||||
|
||||
def get_groups(self, host=None):
|
||||
"""return all groups containing host
|
||||
|
||||
if hosts is none, return all groups in inventory
|
||||
"""
|
||||
if not host:
|
||||
return self._groups.values()
|
||||
host_groups = self.get_host_groups([host])
|
||||
groupnames = host_groups[host.name]
|
||||
groups = []
|
||||
for groupname in groupnames:
|
||||
groups.append(self._groups[groupname])
|
||||
return groups
|
||||
|
||||
def get_host_groups(self, hosts):
|
||||
"""returns a dict: { hostname : [groupnames] }"""
|
||||
host_groups = self._get_host_groups(hosts, self._groups.values())
|
||||
return host_groups
|
||||
|
||||
def _get_host_groups(self, hosts, groups):
|
||||
host_groups = {}
|
||||
for group in groups:
|
||||
if group.children:
|
||||
hosts_children = self._get_host_groups(hosts, group.children)
|
||||
host_groups.update(hosts_children)
|
||||
for host in hosts:
|
||||
if host.name in group._hosts:
|
||||
if host.name not in host_groups:
|
||||
host_groups[host.name] = []
|
||||
host_groups[host.name].append(group.name)
|
||||
|
||||
return host_groups
|
||||
|
|
|
@ -15,9 +15,9 @@ import argparse
|
|||
import getpass
|
||||
import logging
|
||||
|
||||
from kollacli.ansible.inventory import Inventory
|
||||
from kollacli import exceptions
|
||||
from kollacli.i18n import _
|
||||
from kollacli.ansible.inventory import Inventory
|
||||
from kollacli.objects.zones import Zones
|
||||
|
||||
from cliff.command import Command
|
||||
|
@ -92,16 +92,15 @@ class HostList(Lister):
|
|||
inventory = Inventory.load()
|
||||
|
||||
hosts = inventory.get_hosts()
|
||||
|
||||
data = []
|
||||
if hosts:
|
||||
for host in hosts:
|
||||
groupnames = []
|
||||
for group in host.get_groups():
|
||||
groupnames.append(group.name)
|
||||
data.append((host.name, groupnames))
|
||||
host_groups = inventory.get_host_groups(hosts)
|
||||
if host_groups:
|
||||
for (hostname, groupnames) in host_groups.items():
|
||||
data.append((hostname, groupnames))
|
||||
else:
|
||||
data.append(('', ''))
|
||||
return (('Host Name', 'Groups'), data)
|
||||
return (('Host Name', 'Groups'), sorted(data))
|
||||
|
||||
|
||||
class HostSetzone(Command):
|
||||
|
|
|
@ -18,6 +18,7 @@ from common import TestHosts
|
|||
import json
|
||||
import unittest
|
||||
|
||||
|
||||
class TestFunctional(KollaCliTest):
|
||||
|
||||
def test_host_add_remove(self):
|
||||
|
|
Loading…
Reference in New Issue