Don't force salt.cache to use cachedir from opts

This commit is contained in:
Joseph Hall 2017-01-10 09:10:47 -07:00
parent bf6d74c98e
commit 4489f7cac0
2 changed files with 35 additions and 22 deletions

View file

@ -47,8 +47,12 @@ class Cache(object):
Key name is a string identifier of a data container (like a file inside a
directory) which will hold the data.
'''
def __init__(self, opts):
def __init__(self, opts, cachedir=None):
self.opts = opts
if cachedir is None:
self.cachedir = opts['cachedir']
else:
self.cachedir = cachedir
self.driver = opts['cache']
self.serial = Serial(opts)
self._modules = None
@ -119,7 +123,7 @@ class Cache(object):
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'store')
return self.modules[fun](bank, key, data)
return self.modules[fun](bank, key, data, self.cachedir)
def fetch(self, bank, key):
'''
@ -143,7 +147,7 @@ class Cache(object):
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'fetch')
return self.modules[fun](bank, key)
return self.modules[fun](bank, key, self.cachedir)
def updated(self, bank, key):
'''
@ -167,7 +171,7 @@ class Cache(object):
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'updated')
return self.modules[fun](bank, key)
return self.modules[fun](bank, key, self.cachedir)
def flush(self, bank, key=None):
'''
@ -188,7 +192,7 @@ class Cache(object):
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'flush')
return self.modules[fun](bank, key=key)
return self.modules[fun](bank, key=key, cachedir=self.cachedir)
def list(self, bank):
'''
@ -206,8 +210,8 @@ class Cache(object):
Raises an exception if cache driver detected an error accessing data
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'getlist')
return self.modules[fun](bank)
fun = '{0}.{1}'.format(self.driver, 'list')
return self.modules[fun](bank, self.cachedir)
def contains(self, bank, key=None):
'''
@ -232,4 +236,4 @@ class Cache(object):
in the cache backend (auth, permissions, etc).
'''
fun = '{0}.{1}'.format(self.driver, 'contains')
return self.modules[fun](bank, key)
return self.modules[fun](bank, key, self.cachedir)

37
salt/cache/localfs.py vendored
View file

@ -18,14 +18,17 @@ from salt.exceptions import SaltCacheError
import salt.utils
import salt.utils.atomicfile
# Don't shadow built-ins
__func_alias__ = {'list_': 'list'}
log = logging.getLogger(__name__)
def store(bank, key, data):
def store(bank, key, data, cachedir):
'''
Store information in a file.
'''
base = os.path.join(__opts__['cachedir'], os.path.normpath(bank))
base = os.path.join(cachedir, os.path.normpath(bank))
if not os.path.isdir(base):
try:
os.makedirs(base)
@ -51,11 +54,11 @@ def store(bank, key, data):
)
def fetch(bank, key):
def fetch(bank, key, cachedir):
'''
Fetch information from a file.
'''
key_file = os.path.join(__opts__['cachedir'], os.path.normpath(bank), '{0}.p'.format(key))
key_file = os.path.join(cachedir, os.path.normpath(bank), '{0}.p'.format(key))
if not os.path.isfile(key_file):
log.debug('Cache file "%s" does not exist', key_file)
return None
@ -70,11 +73,11 @@ def fetch(bank, key):
)
def updated(bank, key):
def updated(bank, key, cachedir):
'''
Return the epoch of the mtime for this cache file
'''
key_file = os.path.join(__opts__['cachedir'], os.path.normpath(bank), '{0}.p'.format(key))
key_file = os.path.join(cachedir, os.path.normpath(bank), '{0}.p'.format(key))
if not os.path.isfile(key_file):
log.warning('Cache file "%s" does not exist', key_file)
return None
@ -88,18 +91,21 @@ def updated(bank, key):
)
def flush(bank, key=None):
def flush(bank, key=None, cachedir=None):
'''
Remove the key from the cache bank with all the key content.
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
if key is None:
target = os.path.join(__opts__['cachedir'], os.path.normpath(bank))
target = os.path.join(cachedir, os.path.normpath(bank))
if not os.path.isdir(target):
return False
shutil.rmtree(target)
else:
target = os.path.join(__opts__['cachedir'], os.path.normpath(bank), '{0}.p'.format(key))
target = os.path.join(cachedir, os.path.normpath(bank), '{0}.p'.format(key))
if not os.path.isfile(target):
return False
os.remove(target)
@ -112,11 +118,11 @@ def flush(bank, key=None):
return True
def getlist(bank):
def _list(bank, cachedir):
'''
Return an iterable object containing all entries stored in the specified bank.
'''
base = os.path.join(__opts__['cachedir'], os.path.normpath(bank))
base = os.path.join(cachedir, os.path.normpath(bank))
if not os.path.isdir(base):
return []
try:
@ -129,13 +135,16 @@ def getlist(bank):
)
def contains(bank, key):
getlist = _list
def contains(bank, key, cachedir):
'''
Checks if the specified bank contains the specified key.
'''
if key is None:
base = os.path.join(__opts__['cachedir'], os.path.normpath(bank))
base = os.path.join(cachedir, os.path.normpath(bank))
return os.path.isdir(base)
else:
keyfile = os.path.join(__opts__['cachedir'], os.path.normpath(bank), '{0}.p'.format(key))
keyfile = os.path.join(cachedir, os.path.normpath(bank), '{0}.p'.format(key))
return os.path.isfile(keyfile)