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, filename):
24 self.filename=filename
28 def save_person (self, file, person, sites_by_id, pubkeys_by_id):
29 # do not expose people without a key
30 if not person['key_ids']: return
33 if 'admin' not in roles and 'pi' not in roles and 'user' not in roles: return
34 print >>file, "--- PERSON ---"
35 #print >>file, "TMP id=%d"%person['person_id']
36 print >>file, "email=%s"%person['email']
37 for site_id in person['site_ids']:
38 print >>file, "site=%s"%sites_by_id[site_id]['name']
39 if 'pi' in person['roles']:
40 print >>file, "pi=yes"
41 if self.options.no_password: password='<snip>'
42 else: password=person['password']
43 print >>file, "password=%s"%password
44 for key_id in person['key_ids']:
45 print >>file, "ssh=%s"%pubkeys_by_id[key_id]
49 Write configuration store to file.
52 fileout = open(self.filename, 'w')
53 print >>fileout, "# myplc simple userbase extration tools export-omf.py"
54 print >>fileout, "# generated on %s"%time.strftime("%Y-%m-%d @ %H:%M UTC",time.gmtime())
55 # store all ssh keys by key_id - keep only the public key
56 pubkeys_by_id = dict ( [ (k['key_id'], k['key'].strip()) for k in GetKeys() if k['key_type'] == 'ssh'])
57 # idem for sites, keep whole site info
58 sites_by_id = dict ( [ (s['site_id'], s ) for s in GetSites({'peer_id':None}) ] )
60 persons=getPersons({'peer_id':None})
61 persons = sorted(persons, key=lambda(person): person['email'])
63 self.save_person(fileout, p, sites_by_id, pubkeys_by_id)
68 parser = OptionParser (usage="%prog [options] OUTPUT_FILE")
69 parser.add_option ("-n","--no-password", action="store_true",
70 dest="no_password", default=False, help="Don't include encrypted passwords")
71 options,args = parser.parse_args()
76 parser.error("no output file")
78 userbase=OmfUserBase(options, output)
81 if __name__ == '__main__':