1 #!/usr/bin/python /usr/bin/plcsh
3 # utility to store, read and diff our user base data for exporting to OMF
7 from optparse import OptionParser
10 import PLC.Methods.GetPersons
12 def getPersons(filter={}):
13 m = PLC.Methods.GetPersons
15 m.hidden_fields.remove('password')
16 return_fields = PLC.Persons.Person.fields.items()
17 return_fields = dict([f for f in return_fields if f[0] not in m.hidden_fields])
19 return m.GetPersons(api).call(api, filter, return_fields)
23 def __init__ (self, options):
27 def save_person (self, file, person, sites_by_id, pubkeys_by_id):
28 # do not expose people without a key
29 if not person['key_ids']: return
32 if 'admin' not in roles and 'pi' not in roles and 'user' not in roles: return
33 print >>file, "--- PERSON ---"
34 #print >>file, "TMP id=%d"%person['person_id']
35 print >>file, "email=%s"%person['email']
36 for site_id in person['site_ids']:
37 print >>file, "site=%s"%sites_by_id[site_id]['name']
38 if 'pi' in person['roles']:
39 print >>file, "pi=yes"
40 if self.options.no_password: password='<snip>'
41 else: password=person['password']
42 print >>file, "password=%s"%password
43 for key_id in person['key_ids']:
44 print >>file, "ssh=%s"%pubkeys_by_id[key_id]
46 def save(self, output):
48 Write configuration store to file.
52 fileout = open(output, 'w')
55 print >>fileout, "# myplc simple userbase extration tools export-omf.py"
56 print >>fileout, "# generated on %s"%time.strftime("%Y-%m-%d @ %H:%M UTC",time.gmtime())
57 # store all ssh keys by key_id - keep only the public key
58 pubkeys_by_id = dict ( [ (k['key_id'], k['key'].strip()) for k in GetKeys() if k['key_type'] == 'ssh'])
59 # idem for sites, keep whole site info
60 sites_by_id = dict ( [ (s['site_id'], s ) for s in GetSites({'peer_id':None}) ] )
62 persons=getPersons({'peer_id':None})
63 persons = sorted(persons, key=lambda(person): person['email'])
65 self.save_person(fileout, p, sites_by_id, pubkeys_by_id)
70 parser = OptionParser (usage="""%prog [options]
71 Default output is on stdout""")
72 parser.add_option ("-n","--no-password", action="store_true",
73 dest="no_password", default=False, help="Don't include encrypted passwords")
74 parser.add_option ("-o","--output", action="store",
75 dest="output", default=None, help="Specify output filename")
76 options,args = parser.parse_args()
82 userbase=OmfUserBase(options)
83 userbase.save(options.output)
87 if __name__ == '__main__':