freeipa/ipaclient/remote_plugins/compat.py
Rob Crittenden b842b825ab Make the schema cache TTL user-configurable
The API schema is not checked for changes until after a TTL
is expired. A one-hour TTL was hardcoded which makes development
tedious because the only way to force a schema update is to
remember to remove files between invocations.

This adds a new environment variable, schema_ttl, to configure
the TTL returned by the server to schema() calls. This can be
set low to ensure a frequent refresh during development.

If the client is in compat mode, that is if client is working
against a server that doesn't support the schema() command,
then use the client's schema_ttl instead so that the user still
has control.

Re-check validity before writing the cache. This saves us both
a disk write and the possibility of updating the expiration
with a ttl of 0. This can happen if the fingerprint is still
valid (not expired, no language change) the schema check is
skipped so we have no server-provided ttl.

https://pagure.io/freeipa/issue/8492

Signed-off-by: Rob Crittenden <rcritten@redhat.com>
Reviewed-By: Stanislav Levin <slev@altlinux.org>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
2021-11-03 10:59:10 +01:00

91 lines
2.4 KiB
Python

#
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
#
from __future__ import unicode_literals
import importlib
import os
import re
import sys
import six
from ipaclient.frontend import ClientCommand, ClientMethod
from ipalib.frontend import Object
from ipapython.ipautil import APIVersion
if six.PY3:
unicode = str
class CompatCommand(ClientCommand):
@property
def forwarded_name(self):
return self.name
class CompatMethod(ClientMethod, CompatCommand):
pass
class CompatObject(Object):
pass
def get_package(server_info, client):
try:
server_version = server_info['version']
except KeyError:
is_valid = False
else:
is_valid = server_info.is_valid()
if not is_valid:
if not client.isconnected():
client.connect(verbose=False)
env = client.forward('env', 'api_version', version='2.0')
try:
server_version = env['result']['api_version']
except KeyError:
ping = client.forward('ping', version='2.0')
try:
match = re.search(r'API version (2\.[0-9]+)', ping['summary'])
except KeyError:
match = None
if match is not None:
server_version = match.group(1)
else:
server_version = '2.0'
server_info['version'] = server_version
# in compat mode we don't get the schema TTL from the server
# so use the client context value.
server_info.update_validity(client.api.env.schema_ttl)
server_version = APIVersion(server_version)
package_names = {}
base_name = __name__.rpartition('.')[0]
base_dir = os.path.dirname(__file__)
for name in os.listdir(base_dir):
package_dir = os.path.join(base_dir, name)
if name.startswith('2_') and os.path.isdir(package_dir):
package_version = APIVersion(name.replace('_', '.'))
package_names[package_version] = '{}.{}'.format(base_name, name)
package_version = None
for version in sorted(package_names):
if package_version is None or package_version < version:
package_version = version
if version >= server_version:
break
package_name = package_names[package_version]
try:
package = sys.modules[package_name]
except KeyError:
package = importlib.import_module(package_name)
return package