Skip to content

Commit

Permalink
Merge pull request #499 from fermitools/init_scitokens_cache_loc
Browse files Browse the repository at this point in the history
move scitokens cache to /tmp
  • Loading branch information
shreyb authored Dec 4, 2023
2 parents 5d73738 + 6ec50d0 commit 5f98136
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/fake_ifdh.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,49 @@
DEFAULT_ROLE = "Analysis"


def init_scitokens() -> None:
"""
So the scitokens library by default puts a sqlite database
in $HOME/.cache/scitokens; which is a problem when your
home area is in NFS, as sqlite doesn't like sharing files
over NFS. So to tell it to use something different
we have to make a config file which points the cache area
somewhere local.
So we make a subdirectory in /tmp (or $TMPDIR)
and put the config file in there, which tells scitokens
to put the cache file in there as well.
"""
uid = os.getuid()
tdir = os.environ.get("TMPDIR", "/tmp")
jstmpdir = f"{tdir}/js_scitok_{uid}"
cfgfile = f"{jstmpdir}/.scitokens.cfg"

# make sure we have the directory
if not os.access(jstmpdir, os.W_OK):
os.makedirs(jstmpdir)

# always update the config file, so /tmp scrubbers do not delete it
# out from under us
if os.access(cfgfile, os.W_OK):
os.utime(cfgfile)
else:
with open(cfgfile, "w") as cff:
cff.write(f"[scitokens]\ncache_location: {jstmpdir}\n")

# in case moving it to /tmp doesn't fix the bug, check for zero length cache file
# and remove it if zero length
cachefile = f"{jstmpdir}/scitokens/scitokens_keycache.sqllite"
if os.access(cachefile, os.R_OK):
si = os.stat(f"{jstmpdir}/scitokens/scitokens_keycache.sqllite")
if si.st_size == 0:
os.unlink(cachefile)

scitokens.set_config(cfgfile)


init_scitokens()


def getTmp() -> str:
"""return temp directory path"""
return os.environ.get("TMPDIR", "/tmp")
Expand Down

0 comments on commit 5f98136

Please sign in to comment.