3
3
import re
4
4
import os
5
5
import sys
6
+ import json
7
+ import time
6
8
import getopt
7
9
import subprocess
8
10
import urllib .error
13
15
SCRIPT = os .path .basename (__file__ )
14
16
ENDPOINT = "https://registry-test.cilogon.org/registry/"
15
17
OSG_CO_ID = 8
18
+ MINTIMEOUT = 5
19
+ MAXTIMEOUT = 625
20
+ TIMEOUTMULTIPLE = 5
21
+ CACHE_FILENAME = "COmanage_Projects_cache.txt"
22
+ CACHE_LIFETIME_HOURS = 0.5
16
23
17
24
LDAP_AUTH_COMMAND = [
18
25
"awk" , "/ldap_default_authtok/ {print $3}" , "/etc/sssd/conf.d/0060_domain_CILOGON.ORG.conf" ,
@@ -198,14 +205,16 @@ def get_ldap_active_users(filter_group_name):
198
205
stdout = subprocess .PIPE
199
206
).stdout .decode ('utf-8' ).strip ()
200
207
201
- filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None else f"(&(isMemberOf={ filter_group_name } )(isMemberOf=CO:members:active))" )
208
+ filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None
209
+ else f"(&(isMemberOf={ filter_group_name } )(isMemberOf=CO:members:active))" )
202
210
203
211
ldap_active_users_command = LDAP_ACTIVE_USERS_COMMAND
204
212
ldap_active_users_command [LDAP_ACTIVE_USERS_COMMAND .index ("{auth}" )] = auth_str
205
213
ldap_active_users_command [LDAP_ACTIVE_USERS_COMMAND .index ("{filter}" )] = filter_str
206
214
207
215
active_users = subprocess .run (ldap_active_users_command , stdout = subprocess .PIPE ).stdout .decode ('utf-8' ).split ('\n ' )
208
- users = set (line .replace ("voPersonApplicationUID: " , "" ) if re .compile ("dn: voPerson*" ) else "" for line in active_users )
216
+ users = set (line .replace ("voPersonApplicationUID: " , "" ) if re .compile ("dn: voPerson*" )
217
+ else "" for line in active_users )
209
218
return users
210
219
211
220
@@ -222,9 +231,7 @@ def create_user_to_projects_map(project_to_user_map, active_users, osggids_to_na
222
231
return users_to_projects_map
223
232
224
233
225
- def get_co_api_data ():
226
- #TODO add cacheing for COManage API data
227
-
234
+ def get_groups_data_from_api ():
228
235
groups = get_osg_co_groups__map ()
229
236
project_osggids_to_name = dict ()
230
237
for id ,name in groups .items ():
@@ -233,6 +240,32 @@ def get_co_api_data():
233
240
return project_osggids_to_name
234
241
235
242
243
+ def get_co_api_data ():
244
+ try :
245
+ r = open (CACHE_FILENAME , "r" )
246
+ lines = r .readlines ()
247
+ if float (lines [0 ]) >= (time .time () - (60 * 60 * CACHE_LIFETIME_HOURS )):
248
+ entries = lines [1 :len (lines )]
249
+ project_osggids_to_name = dict ()
250
+ for entry in entries :
251
+ osggid_name_pair = entry .split (":" )
252
+ if len (osggid_name_pair ) == 2 :
253
+ project_osggids_to_name [osggid_name_pair [0 ]] = osggid_name_pair [1 ]
254
+ else :
255
+ raise OSError
256
+ except OSError :
257
+ with open (CACHE_FILENAME , "w" ) as w :
258
+ project_osggids_to_name = get_groups_data_from_api ()
259
+ print (time .time (), file = w )
260
+ for osggid , name in project_osggids_to_name .items ():
261
+ print (f"{ osggid } :{ name } " , file = w )
262
+ finally :
263
+ if r :
264
+ r .close ()
265
+
266
+ return project_osggids_to_name
267
+
268
+
236
269
def get_osguser_groups (filter_group_name = None ):
237
270
project_osggids_to_name = get_co_api_data ()
238
271
ldap_groups_members = get_ldap_group_members_data ()
0 commit comments