Skip to content

Commit 2055580

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 da9f34f commit 2055580

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

osg-comanage-project-usermap.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import re
44
import os
55
import sys
6+
import json
7+
import time
68
import getopt
79
import subprocess
810
import urllib.error
@@ -13,6 +15,11 @@
1315
SCRIPT = os.path.basename(__file__)
1416
ENDPOINT = "https://registry-test.cilogon.org/registry/"
1517
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
1623

1724
LDAP_AUTH_COMMAND = [
1825
"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):
198205
stdout=subprocess.PIPE
199206
).stdout.decode('utf-8').strip()
200207

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))")
202210

203211
ldap_active_users_command = LDAP_ACTIVE_USERS_COMMAND
204212
ldap_active_users_command[LDAP_ACTIVE_USERS_COMMAND.index("{auth}")] = auth_str
205213
ldap_active_users_command[LDAP_ACTIVE_USERS_COMMAND.index("{filter}")] = filter_str
206214

207215
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)
209218
return users
210219

211220

@@ -222,9 +231,7 @@ def create_user_to_projects_map(project_to_user_map, active_users, osggids_to_na
222231
return users_to_projects_map
223232

224233

225-
def get_co_api_data():
226-
#TODO add cacheing for COManage API data
227-
234+
def get_groups_data_from_api():
228235
groups = get_osg_co_groups__map()
229236
project_osggids_to_name = dict()
230237
for id,name in groups.items():
@@ -233,6 +240,32 @@ def get_co_api_data():
233240
return project_osggids_to_name
234241

235242

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+
236269
def get_osguser_groups(filter_group_name=None):
237270
project_osggids_to_name = get_co_api_data()
238271
ldap_groups_members = get_ldap_group_members_data()

0 commit comments

Comments
 (0)