f37 -> f39
[infrastructure.git] / omf / export-omf.py
1 #!/usr/bin/python /usr/bin/plcsh
2
3 # utility to store, read and diff our user base data for exporting to OMF
4
5 import sys
6 import time
7 from optparse import OptionParser
8
9 import PLC.Persons
10 import PLC.Methods.GetPersons
11
12 def getPersons(filter={}):
13     m = PLC.Methods.GetPersons
14
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])
18
19     return m.GetPersons(api).call(api, filter, return_fields)
20
21 class OmfUserBase:
22
23     def __init__ (self, options):
24         self.options=options
25             
26
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
30         # do not expose techs
31         roles=person['roles']
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]
45
46     def save(self, output):
47         """
48         Write configuration store to file.
49         """
50         
51         if output:
52             fileout = open(output, 'w')
53         else:
54             fileout=sys.stdout
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}) ] )
61         #
62         persons=getPersons({'peer_id':None})
63         persons = sorted(persons, key=lambda(person): person['email'])
64         for p in persons:
65             self.save_person(fileout, p, sites_by_id, pubkeys_by_id)
66         fileout.close()
67
68
69 def main ():
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()
77
78     if len(args)!=0:
79         parser.print_help()
80         sys.exit(1)
81
82     userbase=OmfUserBase(options)
83     userbase.save(options.output)
84
85     
86
87 if __name__ == '__main__':
88     main()