Last active
November 18, 2022 14:56
-
-
Save cloudnull/a5d7cbe189a9fb94b47877ef9aace7b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import json | |
import re | |
import sys | |
GROUPS = dict() | |
HOSTS = dict() | |
PROTOS = dict() | |
R_WORDS = re.compile(r"\w+") | |
NODE_TYPES = [ | |
"is_combo", | |
"is_validator", | |
"is_datahub", | |
"is_fullnode", | |
"is_gbt", | |
"is_peggo", | |
"is_node", | |
"is_mixnode", | |
"is_active", | |
"is_steward", | |
"is_oracle", | |
"is_snapshot_node", | |
"is_payout_server", | |
"is_indexer" | |
] | |
def get_protos(key): | |
if key in PROTOS: | |
return PROTOS[key] | |
else: | |
proto = PROTOS[key] = collections.defaultdict(int) | |
return proto | |
def get_type_key(key): | |
res = R_WORDS.findall(key) | |
name = res[0].lower() | |
proto = get_protos(key=name) | |
if name == "dh": | |
type_key = "is_datahub" | |
_proto = get_protos(key=res[1]) | |
_proto[type_key] += 1 | |
elif name == "indexer": | |
type_key = "is_indexer" | |
_proto = get_protos(key=res[1]) | |
_proto[type_key] += 1 | |
else: | |
type_key = "is_staking" | |
proto[type_key] += 1 | |
def main(inv): | |
for key, value in inv.items(): | |
hosts = value.get("hosts", list()) | |
if not hosts: | |
hostvars = value.get("hostvars", dict()) | |
for _key, _value in hostvars.items(): | |
res = R_WORDS.findall(_key) | |
name = res[0].lower() | |
if name == "dh": | |
name = res[1].lower() | |
_value["is_datahub"] = True | |
elif name == "indexer": | |
name = res[1].lower() | |
_value["is_indexer"] = True | |
type_found = False | |
for node_type in NODE_TYPES: | |
try: | |
if _value[node_type]: | |
proto = get_protos(key=name) | |
proto[node_type] += 1 | |
except KeyError: | |
pass | |
else: | |
type_found = True | |
if not type_found: | |
proto = get_protos(key=name) | |
proto["is_unknown"] += 1 | |
hosts.append(_key) | |
hosts = set(hosts) | |
for host in hosts: | |
if host in HOSTS: | |
HOSTS[host]["groups"].append(key) | |
HOSTS[host]["groups"] = sorted(set(HOSTS[host]["groups"])) | |
else: | |
HOSTS[host] = {"groups": [key]} | |
GROUPS[key] = len(hosts) | |
print( | |
json.dumps( | |
{"protocols": PROTOS, "groups": GROUPS, "nodes": HOSTS}, | |
indent=4, | |
sort_keys=True, | |
) | |
) | |
if __name__ == "__main__": | |
with open(sys.argv[1]) as f: | |
inventory = json.loads(f.read()) | |
main(inv=inventory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment