--- /dev/null
+#!/usr/bin/plcsh
+
+# utility to store, read and diff our user base data for exporting to OMF
+
+import sys
+import time
+from optparse import OptionParser
+
+class OmfUserBase:
+
+ def __init__ (self,filename):
+ self.filename = filename
+
+ def save_person (self, file, person, sites_by_id, pubkeys_by_id):
+ # do not expose people without a key
+ if not person['key_ids']: return
+ # do not expose techs
+ roles=person['roles']
+ if 'admin' not in roles and 'pi' not in roles and 'user' not in roles: return
+ print >>file, "--- PERSON ---"
+ #print >>file, "TMP id=%d"%person['person_id']
+ print >>file, "email=%s"%person['email']
+ for site_id in person['site_ids']:
+ print >>file, "site=%s"%sites_by_id[site_id]['name']
+ if 'pi' in person['roles']:
+ print >>file, "pi=yes"
+ # somehow the passord information does not show up here
+ #print >>file, "password=%s"%person['password']
+ for key_id in person['key_ids']:
+ print >>file, "ssh=%s"%pubkeys_by_id[key_id]
+
+ def save(self):
+ """
+ Write configuration store to file.
+ """
+
+ fileout = open(self.filename, 'w')
+ print >>fileout, "# myplc simple userbase extration tools export-omf.py"
+ print >>fileout, "# generated on %s"%time.strftime("%Y-%m-%d @ %H:%M UTC",time.gmtime())
+ # store all ssh keys by key_id - keep only the public key
+ pubkeys_by_id = dict ( [ (k['key_id'], k['key'].strip()) for k in GetKeys() if k['key_type'] == 'ssh'])
+ # idem for sites, keep whole site info
+ sites_by_id = dict ( [ (s['site_id'], s ) for s in GetSites({'peer_id':None}) ] )
+ #
+ persons=GetPersons({'peer_id':None})
+ persons = sorted(persons, key=lambda(person): person['email'])
+ for p in persons:
+ self.save_person(fileout, p, sites_by_id, pubkeys_by_id)
+ fileout.close()
+
+
+def main ():
+ output=sys.argv[1]
+ userbase=OmfUserBase(output)
+ userbase.save()
+
+if __name__ == '__main__':
+ main()