diff options
author | Leif Johansson <leifj@sunet.se> | 2014-03-03 10:32:45 +0100 |
---|---|---|
committer | Leif Johansson <leifj@sunet.se> | 2014-03-03 10:32:45 +0100 |
commit | 60e026c0b1b2e1440e0721e634cf7c803b8e5907 (patch) | |
tree | dc441df2449b254c0260e24155e1c0e9c68747c8 /fabfile/db.py | |
parent | 45e2a1c1defacb2034c877b71fcf50e961214e72 (diff) | |
parent | 2369e391d7418fdb8a402d403caf33608c86c70a (diff) |
Merge branch 'master' of git://github.com/leifj/multiverse into multiverse
Diffstat (limited to 'fabfile/db.py')
-rw-r--r-- | fabfile/db.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/fabfile/db.py b/fabfile/db.py new file mode 100644 index 0000000..129aa50 --- /dev/null +++ b/fabfile/db.py @@ -0,0 +1,45 @@ +import os +import yaml +import re + +def _all_hosts(): + return 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() + + 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 + + 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 + + return dict(classes=classes,members=members) + +_db = None +def cosmos_db(): + global _db + if _db is None: + _db = _load_db() + return _db + +if __name__ == '__main__': + print yaml.dump(cosmos_db()) |