Add environment seeking to the master file server

This commit is contained in:
Thomas S Hatch 2011-05-26 23:35:45 -06:00
parent 77c67af2cc
commit 677cac28c3
2 changed files with 25 additions and 13 deletions

View file

@ -61,7 +61,7 @@
# roots cannot match, otherwise the downloaded files will not be able to be
# reliably ensured. A base environment is required to house the top file
# Example:
# file_root:
# file_roots:
# - base:
# - /srv/salt/
# - dev:

View file

@ -325,17 +325,28 @@ class MWorker(multiprocessing.Process):
self._send_cluster()
return ret
def _find_file(self, path, env='base'):
'''
Search the environment for the relative path
'''
if not self.opts['file_roots'].has_key(env):
return False
for root in self.opts['file_roots'][env]:
full = os.path.join(root, path)
if os.path.isfile(full):
return full
return False
def _serve_file(self, load):
'''
Return a chunk from a file based on the data received
'''
if not load.has_key('path') or not load.has_key('loc'):
if not load.has_key('path')\
or not load.has_key('loc')\
or not load.has_key('env'):
return False
path = load['path']
if path.startswith('/'):
path = load['path'][1:]
path = os.path.join(self.opts['file_root'], path)
if not os.path.isfile(path):
path = self._find_file(load['path'], load['env'])
if not path:
return ''
fn_ = open(path, 'rb')
fn_.seek(load['loc'])
@ -345,14 +356,15 @@ class MWorker(multiprocessing.Process):
'''
Return a file hash, the hash type is set in the master config file
'''
if not load.has_key('path'):
return False
path = os.path.join(self.opts['file_root'], load['path'])
if not os.path.isfile(path):
if not load.has_key('path')\
or not load.has_key('env'):
return False
path = self._find_file(load['path'], load['env'])
if not path:
return ''
ret = {}
ret['hsum'] = getattr(hashlib, self.opts['hash_type'])(open(path,
'rb').read()).hexdigest()
ret['hsum'] = getattr(hashlib, self.opts['hash_type'])(
open(path, 'rb').read()).hexdigest()
ret['hash_type'] = self.opts['hash_type']
return self.crypticle.dumps(ret)