#!/usr/bin/python /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 import PLC.Persons import PLC.Methods.GetPersons def getPersons(filter={}): m = PLC.Methods.GetPersons m.hidden_fields.remove('password') return_fields = PLC.Persons.Person.fields.items() return_fields = dict([f for f in return_fields if f[0] not in m.hidden_fields]) return m.GetPersons(api).call(api, filter, return_fields) class OmfUserBase: def __init__ (self, options): self.options=options 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" if self.options.no_password: password='' else: password=person['password'] print >>file, "password=%s"%password for key_id in person['key_ids']: print >>file, "ssh=%s"%pubkeys_by_id[key_id] def save(self, output): """ Write configuration store to file. """ if output: fileout = open(output, 'w') else: fileout=sys.stdout 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 (): parser = OptionParser (usage="""%prog [options] Default output is on stdout""") parser.add_option ("-n","--no-password", action="store_true", dest="no_password", default=False, help="Don't include encrypted passwords") parser.add_option ("-o","--output", action="store", dest="output", default=None, help="Specify output filename") options,args = parser.parse_args() if len(args)!=0: parser.print_help() sys.exit(1) userbase=OmfUserBase(options) userbase.save(options.output) if __name__ == '__main__': main()