Introduce cache_valid_time parameter (#34914)

this should speedup situation when several states need
refresh_db: True

In case you have several states like this, apt-get update will be
called on every step refresh is set.

Proposed solution will skip refresh in case cache_valid_time is set
unless apt-get update preceded more than cache_valid_time seconds ago.
This commit is contained in:
onorua 2016-07-28 18:24:17 +03:00 committed by Nicole Thomas
parent 42222b06fe
commit b09aacf185
2 changed files with 106 additions and 5 deletions

View File

@ -21,6 +21,7 @@ import copy
import os
import re
import logging
import time
import json
# Import third party libs
@ -235,6 +236,10 @@ def latest_version(*names, **kwargs):
A specific repo can be requested using the ``fromrepo`` keyword argument.
cache_valid_time
skip refresh the package database if refresh has already occur within
<value> seconds
CLI Example:
.. code-block:: bash
@ -257,6 +262,7 @@ def latest_version(*names, **kwargs):
fromrepo = _get_repo(**kwargs)
kwargs.pop('fromrepo', None)
kwargs.pop('repo', None)
cache_valid_time = kwargs.pop('cache_valid_time', 0)
if len(names) == 0:
return ''
@ -270,7 +276,7 @@ def latest_version(*names, **kwargs):
# Refresh before looking for the latest version available
if refresh:
refresh_db()
refresh_db(cache_valid_time)
virtpkgs = _get_virtual()
all_virt = set()
@ -343,7 +349,7 @@ def version(*names, **kwargs):
return __salt__['pkg_resource.version'](*names, **kwargs)
def refresh_db():
def refresh_db(cache_valid_time=0):
'''
Updates the APT database to latest packages based upon repositories
@ -354,13 +360,30 @@ def refresh_db():
- ``False``: Problem updating database
- ``None``: Database already up-to-date
cache_valid_time
skip refresh the package database if refresh has already occur within
<value> seconds
CLI Example:
.. code-block:: bash
salt '*' pkg.refresh_db
'''
APT_LISTS_PATH = "/var/lib/apt/lists"
ret = {}
if cache_valid_time:
try:
latest_update = os.stat(APT_LISTS_PATH).st_mtime
now = time.time()
log.debug("now: %s, last update time: %s, expire after: %s seconds", now, latest_update, cache_valid_time)
if latest_update + cache_valid_time > now:
return ret
except TypeError as exp:
log.warning("expected integer for cache_valid_time parameter, failed with: %s", exp)
except IOError as exp:
log.warning("could not stat cache directory due to: %s", exp)
cmd = ['apt-get', '-q', 'update']
call = __salt__['cmd.run_all'](cmd,
output_loglevel='trace',
@ -424,6 +447,10 @@ def install(name=None,
refresh
Whether or not to refresh the package database before installing.
cache_valid_time
skip refresh the package database if refresh has already occur within
<value> seconds
fromrepo
Specify a package repository to install from
(e.g., ``apt-get -t unstable install somepackage``)
@ -690,8 +717,9 @@ def install(name=None,
if not cmds:
return {}
cache_valid_time = kwargs.pop('cache_valid_time', 0)
if _refresh_db:
refresh_db()
refresh_db(cache_valid_time)
env = _parse_env(kwargs.get('env'))
env.update(DPKG_ENV_VARS.copy())
@ -919,6 +947,10 @@ def upgrade(refresh=True, dist_upgrade=False, **kwargs):
.. versionadded:: 2014.7.0
cache_valid_time
skip refresh the package database if refresh has already occur within
<value> seconds
force_conf_new
Always install the new version of any configuration files.
@ -935,8 +967,9 @@ def upgrade(refresh=True, dist_upgrade=False, **kwargs):
'comment': '',
}
cache_valid_time = kwargs.pop('cache_valid_time', 0)
if salt.utils.is_true(refresh):
refresh_db()
refresh_db(cache_valid_time)
old = list_pkgs()
if 'force_conf_new' in kwargs and kwargs['force_conf_new']:
@ -1304,6 +1337,10 @@ def list_upgrades(refresh=True, dist_upgrade=True, **kwargs):
Whether to refresh the package database before listing upgrades.
Default: True.
cache_valid_time
skip refresh the package database if refresh has already occur within
<value> seconds
dist_upgrade
Whether to list the upgrades using dist-upgrade vs upgrade. Default is
to use dist-upgrade.
@ -1314,8 +1351,9 @@ def list_upgrades(refresh=True, dist_upgrade=True, **kwargs):
salt '*' pkg.list_upgrades
'''
cache_valid_time = kwargs.pop('cache_valid_time', 0)
if salt.utils.is_true(refresh):
refresh_db()
refresh_db(cache_valid_time)
return _get_upgradable(dist_upgrade, **kwargs)

View File

@ -751,6 +751,34 @@ def installed(
``refresh`` to ``True``. This prevents needless additional refreshes
from slowing down the Salt run.
:param str cache_valid_time:
This parameter sets the value in seconds after which cache marked as invalid,
and cache update is necessary. This overwrite ``refresh`` parameter
default behavior.
Example:
.. code-block:: yaml
httpd:
pkg.installed:
- fromrepo: mycustomrepo
- skip_verify: True
- skip_suggestions: True
- version: 2.0.6~ubuntu3
- refresh: True
- cache_valid_time: 300
- allow_updates: True
- hold: False
In this case refresh will not take place for 5 minutes since last
``apt-get update`` executed on the system.
.. note::
This parameter available only on Debian based distributions, and
have no effect on the rest.
:param str fromrepo:
Specify a repository from which to install
@ -1630,6 +1658,28 @@ def latest(
``refresh`` to ``True``. This prevents needless additional refreshes
from slowing down the Salt run.
:param str cache_valid_time:
This parameter sets the value in seconds after which cache marked as invalid,
and cache update is necessary. This overwrite ``refresh`` parameter
default behavior.
Example:
.. code-block:: yaml
httpd:
pkg.latest:
- refresh: True
- cache_valid_time: 300
In this case refresh will not take place for 5 minutes since last
``apt-get update`` executed on the system.
.. note::
This parameter available only on Debian based distributions, and
have no effect on the rest.
Multiple Package Installation Options:
@ -2233,6 +2283,19 @@ def uptodate(name, refresh=False, **kwargs):
refresh
refresh the package database before checking for new upgrades
:param str cache_valid_time:
This parameter sets the value in seconds after which cache marked as invalid,
and cache update is necessary. This overwrite ``refresh`` parameter
default behavior.
In this case cache_valid_time is set, refresh will not take place for
amount in seconds since last ``apt-get update`` executed on the system.
.. note::
This parameter available only on Debian based distributions, and
have no effect on the rest.
kwargs
Any keyword arguments to pass through to ``pkg.upgrade``.