Add 'cache_dir' option to api.env

`api.env` now has a `cache_dir` option, which defaults to
`os.path.join(USER_CACHE_PATH, 'ipa')`. Schema cache, server info, and
KRA key cache use `api.env.cache_dir` as base directory. The option
allows application to set a custom cache directory.

Related: https://pagure.io/freeipa/issue/9438
Related: https://bugzilla.redhat.com/show_bug.cgi?id=1513934
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
This commit is contained in:
Christian Heimes authored and Florence Blanc-Renaud committed 2023-12-01 09:46:31 +01:00
1 parent 5c8614157d
commit 5deeee31c0
6 files changed
+22 -19

No files matched your search

+3 -5
View File
@@ -550,10 +550,8 @@ class vault_mod(Local):
class _KraConfigCache:
"""The KRA config cache stores vaultconfig-show result.
"""
def __init__(self):
self._dirname = os.path.join(
constants.USER_CACHE_PATH, 'ipa', 'kra-config'
)
def __init__(self, api):
self._dirname = os.path.join(api.env.cache_dir, 'kra-config')
def _get_filename(self, domain):
basename = DNSName(domain).ToASCII() + '.json'
@@ -629,7 +627,7 @@ class _KraConfigCache:
return True
_kra_config_cache = _KraConfigCache()
_kra_config_cache = _KraConfigCache(api)
@register(override=True, no_fail=True)
+5 -7
View File
@@ -13,18 +13,16 @@ import time
from . import compat
from . import schema
from ipaclient.plugins.rpcclient import rpcclient
from ipalib.constants import USER_CACHE_PATH
from ipapython.dnsutil import DNSName
logger = logging.getLogger(__name__)
class ServerInfo(MutableMapping):
_DIR = os.path.join(USER_CACHE_PATH, 'ipa', 'servers')
def __init__(self, api):
self._dir = os.path.join(api.env.cache_dir, "servers")
hostname = DNSName(api.env.server).ToASCII()
self._path = os.path.join(self._DIR, hostname)
self._filename = os.path.join(self._dir, hostname)
self._force_check = api.env.force_schema_check
self._now = time.time()
self._dict = {}
@@ -41,7 +39,7 @@ class ServerInfo(MutableMapping):
def _read(self):
try:
with open(self._path, 'r') as sc:
with open(self._filename, 'r') as sc:
self._dict = json.load(sc)
except Exception as e:
if (isinstance(e, EnvironmentError) and
@@ -56,11 +54,11 @@ class ServerInfo(MutableMapping):
def _write(self):
try:
try:
os.makedirs(self._DIR)
os.makedirs(self._dir)
except EnvironmentError as e:
if e.errno != errno.EEXIST:
raise
with open(self._path, 'w') as sc:
with open(self._filename, 'w') as sc:
json.dump(self._dict, sc)
except EnvironmentError as e:
logger.warning('Failed to write server info: %s', e)
+6 -7
View File
@@ -18,7 +18,6 @@ import six
from ipaclient.frontend import ClientCommand, ClientMethod
from ipalib import errors, parameters, plugable
from ipalib.constants import USER_CACHE_PATH
from ipalib.errors import SchemaUpToDate
from ipalib.frontend import Object
from ipalib.output import Output
@@ -367,9 +366,9 @@ class Schema:
"""
namespaces = {'classes', 'commands', 'topics'}
_DIR = os.path.join(USER_CACHE_PATH, 'ipa', 'schema', FORMAT)
def __init__(self, client, fingerprint=None, ttl=0):
self._dir = os.path.join(client.api.env.cache_dir, 'schema', FORMAT)
self._dict = {}
self._namespaces = {}
self._help = None
@@ -409,7 +408,7 @@ class Schema:
fps = []
if not ignore_cache:
try:
fps = [fsdecode(f) for f in os.listdir(self._DIR)]
fps = [fsdecode(f) for f in os.listdir(self._dir)]
except EnvironmentError:
pass
@@ -439,7 +438,7 @@ class Schema:
def _read_schema(self, fingerprint):
# It's more efficient to read zip file members at once than to open
# the zip file a couple of times, see #6690.
filename = os.path.join(self._DIR, fingerprint)
filename = os.path.join(self._dir, fingerprint)
with zipfile.ZipFile(filename, 'r') as schema:
for name in schema.namelist():
ns, _slash, key = name.partition('/')
@@ -477,13 +476,13 @@ class Schema:
def _write_schema(self, fingerprint):
try:
os.makedirs(self._DIR)
os.makedirs(self._dir)
except EnvironmentError as e:
if e.errno != errno.EEXIST:
raise
with tempfile.NamedTemporaryFile('wb', prefix=fingerprint,
dir=self._DIR, delete=False) as f:
dir=self._dir, delete=False) as f:
try:
self._write_schema_data(f)
ipautil.flush_sync(f)
@@ -492,7 +491,7 @@ class Schema:
os.unlink(f.name)
raise
else:
os.rename(f.name, os.path.join(self._DIR, fingerprint))
os.rename(f.name, os.path.join(self._dir, fingerprint))
def _write_schema_data(self, fileobj):
with zipfile.ZipFile(fileobj, 'w', zipfile.ZIP_DEFLATED) as schema: