changed db.py and Makefile

This commit is contained in:
Maria Haider 2021-06-21 15:19:50 +02:00
parent 1df0d56f48
commit 8eec0eb31f
Signed by: mariah
GPG key ID: 7414A760CA747E57
2 changed files with 48 additions and 34 deletions

View file

@ -5,7 +5,7 @@ upgrade:
fab upgrade
db:
@python ./fabfile/db.py > global/overlay/etc/puppet/cosmos-db.yaml
@python3 ./fabfile/db.py > global/overlay/etc/puppet/cosmos-db.yaml
@git add global/overlay/etc/puppet/cosmos-db.yaml && git commit -m "update db" global/overlay/etc/puppet/cosmos-db.yaml
tag: db

View file

@ -1,49 +1,63 @@
import os
import sys
import yaml
import re
# disallow python2 as the output will not be correct
if sys.version_info.major != 3:
sys.stderr.write('python2 no longer supported\n')
sys.exit(1)
def _all_hosts():
return filter(lambda fn: '.' in fn and not fn.startswith('.') and os.path.isdir(fn),os.listdir("."))
return list(filter(lambda fn: '.' in fn and not fn.startswith('.') and os.path.isdir(fn), os.listdir(".")))
def _load_db():
rules = dict()
rules_file = "cosmos-rules.yaml";
if os.path.exists(rules_file):
with open(rules_file) as fd:
rules.update(yaml.load(fd))
all_hosts = _all_hosts()
rules_file = "cosmos-rules.yaml"
if not os.path.exists(rules_file):
sys.stderr.write('%s not found'.format(rules_file))
sys.exit(1)
members = dict()
for node_name in all_hosts:
for reg,cls in rules.iteritems():
if re.search(reg,node_name):
for cls_name in cls.keys():
h = members.get(cls_name,[])
h.append(node_name)
members[cls_name] = h
members['all'] = all_hosts
with open(rules_file) as fd:
rules = yaml.load(fd, Loader=yaml.SafeLoader)
classes = dict()
for node_name in all_hosts:
node_classes = dict()
for reg,cls in rules.iteritems():
if re.search(reg,node_name):
node_classes.update(cls)
classes[node_name] = node_classes
all_hosts = _all_hosts()
# Sort member lists for a more easy to read diff
for cls in members.keys():
members[cls].sort()
members = dict()
for node_name in all_hosts:
for reg, cls in rules.items():
if re.match(reg, node_name):
for cls_name in cls.keys():
h = members.get(cls_name, [])
h.append(node_name)
members[cls_name] = h
members['all'] = all_hosts
classes = dict()
for node_name in all_hosts:
node_classes = dict()
for reg, cls in rules.items():
if re.match(reg, node_name):
node_classes.update(cls)
classes[node_name] = node_classes
# Sort member lists for a more easy to read diff
for cls in members.keys():
members[cls].sort()
return dict(classes=classes, members=members)
return dict(classes=classes,members=members)
_db = None
def cosmos_db():
global _db
if _db is None:
_db = _load_db()
return _db
global _db
if _db is None:
_db = _load_db()
return _db
if __name__ == '__main__':
print yaml.dump(cosmos_db())
print(yaml.dump(cosmos_db(), default_flow_style=None))