Skip to content

Commit 8957753

Browse files
Add caching for COmanage API data
Writes the osggids to project name dict to a file containing the epoch time the cache was made. If the file is found to exist and was made in the past 0.5 hours (by default), the program will read from the cache instead of making COmanage API calls. Otherwise, the program will get the project data from the API and overwrite any existing cache.
1 parent 3efe8a6 commit 8957753

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

osg-comanage-project-usermap.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import json
7+
import time
78
import getopt
89
import subprocess
910
import urllib.error
@@ -16,6 +17,8 @@
1617
MINTIMEOUT = 5
1718
MAXTIMEOUT = 625
1819
TIMEOUTMULTIPLE = 5
20+
CACHE_FILENAME = "COmanage_Projects_cache.txt"
21+
CACHE_LIFETIME_HOURS = 0.5
1922

2023
LDAP_AUTH_COMMAND = [
2124
"awk", "/ldap_default_authtok/ {print $3}", "/etc/sssd/conf.d/0060_domain_CILOGON.ORG.conf",
@@ -275,14 +278,16 @@ def get_ldap_active_users(filter_group_name):
275278
stdout=subprocess.PIPE
276279
).stdout.decode('utf-8').strip()
277280

278-
filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None else f"(&(isMemberOf={filter_group_name})(isMemberOf=CO:members:active))")
281+
filter_str = ("(isMemberOf=CO:members:active)" if filter_group_name is None
282+
else f"(&(isMemberOf={filter_group_name})(isMemberOf=CO:members:active))")
279283

280284
ldap_active_users_command = LDAP_ACTIVE_USERS_COMMAND
281285
ldap_active_users_command[LDAP_ACTIVE_USERS_COMMAND.index("{auth}")] = auth_str
282286
ldap_active_users_command[LDAP_ACTIVE_USERS_COMMAND.index("{filter}")] = filter_str
283287

284288
active_users = subprocess.run(ldap_active_users_command, stdout=subprocess.PIPE).stdout.decode('utf-8').split('\n')
285-
users = set(line.replace("voPersonApplicationUID: ", "") if re.compile("dn: voPerson*") else "" for line in active_users)
289+
users = set(line.replace("voPersonApplicationUID: ", "") if re.compile("dn: voPerson*")
290+
else "" for line in active_users)
286291
return users
287292

288293

@@ -299,9 +304,7 @@ def create_user_to_projects_map(project_to_user_map, active_users, osggids_to_na
299304
return users_to_projects_map
300305

301306

302-
def get_co_api_data():
303-
#TODO add cacheing for COManage API data
304-
307+
def get_groups_data_from_api():
305308
groups = get_osg_co_groups__map()
306309
project_osggids_to_name = dict()
307310
for id,name in groups.items():
@@ -310,6 +313,32 @@ def get_co_api_data():
310313
return project_osggids_to_name
311314

312315

316+
def get_co_api_data():
317+
try:
318+
r = open(CACHE_FILENAME, "r")
319+
lines = r.readlines()
320+
if float(lines[0]) >= (time.time() - (60 * 60 * CACHE_LIFETIME_HOURS)):
321+
entries = lines[1:len(lines)]
322+
project_osggids_to_name = dict()
323+
for entry in entries:
324+
osggid_name_pair = entry.split(":")
325+
if len(osggid_name_pair) == 2:
326+
project_osggids_to_name[osggid_name_pair[0]] = osggid_name_pair[1]
327+
else:
328+
raise OSError
329+
except OSError:
330+
with open(CACHE_FILENAME, "w") as w:
331+
project_osggids_to_name = get_groups_data_from_api()
332+
print(time.time(), file=w)
333+
for osggid, name in project_osggids_to_name.items():
334+
print(f"{osggid}:{name}", file=w)
335+
finally:
336+
if r:
337+
r.close()
338+
339+
return project_osggids_to_name
340+
341+
313342
def get_osguser_groups(filter_group_name=None):
314343
project_osggids_to_name = get_co_api_data()
315344
ldap_groups_members = get_ldap_group_members_data()

0 commit comments

Comments
 (0)