def get_cluster(self, name): """ Look up a cluster by name. @param name Cluster name. @return An ApiCluster object. """ return clusters.get_cluster(self, name)
def get_cluster(self, name): """ Look up a cluster by name. @param name: Cluster name. @return: An ApiCluster object. """ return clusters.get_cluster(self, name)
def _build_host_inventory(hostRef,inv,meta_hostvars): host = hosts.get_host(self.cm, hostRef.hostId) #print host.hostname self._add_host(inv, 'all', host.hostname) if meta_hostvars: inv['_meta']['hostvars'][host.hostname] = host.to_json_dict(preserve_ro=True) self._put_cache(host.hostname, host.to_json_dict(preserve_ro=True)) # Group by cluster if host.clusterRef: cluster = clusters.get_cluster(self.cm, host.clusterRef.clusterName) self._add_child(inv, 'all', cluster.displayName) self._add_host(inv, cluster.displayName, host.hostname) if host.roleRefs: for roleRef in host.roleRefs: role = roles.get_role(self.cm, roleRef.serviceName, roleRef.roleName, roleRef.clusterName) # Group by service service = services.get_service(self.cm, roleRef.serviceName, roleRef.clusterName) # There is no way to ensure that service display name is unique across clusters # The only simple and unique representation of the service that can be used # is the concatination of the service name and the cluster's name service_group = cluster.displayName + '-' + service.displayName self._add_child(inv, 'all', service.type) self._add_child(inv, service.type, service_group) self._add_child(inv, cluster.displayName, service_group) self._add_host(inv, service_group, host.hostname) # Group by role, roles depend on services and clusters, so the only unique and # simple representation of a Group is the concatination of the role type, service # name and the cluster name role_group = cluster.displayName + '-' + service.displayName + '-' + role.type self._add_child(inv, 'all', role.type) #self._add_child(inv, role.type, service_group) #self._add_child(inv, service_group, role_group) self._add_child(inv, role.type, role_group) self._add_host(inv, role_group, host.hostname) # Group by role Group role_group = role.roleConfigGroupRef.roleConfigGroupName self._add_child(inv, role.type, role_group) self._add_host(inv, role_group, host.hostname) # Group by role template for template in host_templates.get_all_host_templates(self.cm, host.clusterRef.clusterName): self._add_child(inv, 'all', template.name) for group in template.roleConfigGroupRefs: if role_group == group.roleConfigGroupName: self._add_child(inv, template.name, role_group) else: self._add_child(inv, 'all', 'no_role') self._add_host(inv, 'no_role', host.hostname) # Group by Rack self._add_child(inv, 'all', host.rackId) self._add_host(inv, host.rackId, host.clusterRef.clusterName) else: cluster_group = "no_cluster" self._add_child(inv, 'all', cluster_group) self._add_host(inv, cluster_group, host.hostname)
def get_inventory(self, meta_hostvars=True, n_threads=5): ''' Reads the inventory from cache or VMware API via pSphere. ''' # Use different cache names for guests only vs. all hosts. cache_name = '__inventory_all__' inv = self._get_cache(cache_name, None) if inv is not None: print "Here" return inv def _build_host_inventory(hostRef,inv,meta_hostvars): host = hosts.get_host(self.cm, hostRef.hostId) #print host.hostname self._add_host(inv, 'all', host.hostname) if meta_hostvars: inv['_meta']['hostvars'][host.hostname] = host.to_json_dict(preserve_ro=True) self._put_cache(host.hostname, host.to_json_dict(preserve_ro=True)) # Group by cluster if host.clusterRef: cluster = clusters.get_cluster(self.cm, host.clusterRef.clusterName) self._add_child(inv, 'all', cluster.displayName) self._add_host(inv, cluster.displayName, host.hostname) if host.roleRefs: for roleRef in host.roleRefs: role = roles.get_role(self.cm, roleRef.serviceName, roleRef.roleName, roleRef.clusterName) # Group by service service = services.get_service(self.cm, roleRef.serviceName, roleRef.clusterName) # There is no way to ensure that service display name is unique across clusters # The only simple and unique representation of the service that can be used # is the concatination of the service name and the cluster's name service_group = cluster.displayName + '-' + service.displayName self._add_child(inv, 'all', service.type) self._add_child(inv, service.type, service_group) self._add_child(inv, cluster.displayName, service_group) self._add_host(inv, service_group, host.hostname) # Group by role, roles depend on services and clusters, so the only unique and # simple representation of a Group is the concatination of the role type, service # name and the cluster name role_group = cluster.displayName + '-' + service.displayName + '-' + role.type self._add_child(inv, 'all', role.type) #self._add_child(inv, role.type, service_group) #self._add_child(inv, service_group, role_group) self._add_child(inv, role.type, role_group) self._add_host(inv, role_group, host.hostname) # Group by role Group role_group = role.roleConfigGroupRef.roleConfigGroupName self._add_child(inv, role.type, role_group) self._add_host(inv, role_group, host.hostname) # Group by role template for template in host_templates.get_all_host_templates(self.cm, host.clusterRef.clusterName): self._add_child(inv, 'all', template.name) for group in template.roleConfigGroupRefs: if role_group == group.roleConfigGroupName: self._add_child(inv, template.name, role_group) else: self._add_child(inv, 'all', 'no_role') self._add_host(inv, 'no_role', host.hostname) # Group by Rack self._add_child(inv, 'all', host.rackId) self._add_host(inv, host.rackId, host.clusterRef.clusterName) else: cluster_group = "no_cluster" self._add_child(inv, 'all', cluster_group) self._add_host(inv, cluster_group, host.hostname) inv = {'all': {'hosts': []}} if meta_hostvars: inv['_meta'] = {'hostvars': {}} if self.filter_clusters: # Loop through clusters and find hosts: hosts_list = [] for host in self.cm.get_all_hosts(): if host.clusterRef: if clusters.get_cluster(self.cm, host.clusterRef.clusterName).displayName in self.filter_clusters: hosts_list.append(host) else: # Get list of all hosts hosts_list = self.cm.get_all_hosts() if n_threads == 1: for hostRef in hosts_list: _build_host_inventory(inv,hostRef,meta_hostvars) else: _partial_build_host_inventory = partial(_build_host_inventory, inv=inv,meta_hostvars=meta_hostvars) pool = ThreadPool(n_threads) if sys.version_info <= (2, 6): pool.map(_partial_build_host_inventory, hosts_list) else: pool.map_async(_partial_build_host_inventory, hosts_list).get(1 << 31) self._put_cache(cache_name, inv) return inv