Merge pull request #909 from bcoca/apt_return_cacheinfo

added updated cache time to apt, also started documenting return values
This commit is contained in:
Brian Coca 2015-05-26 11:54:03 -04:00
commit 0d41af23c2

View file

@ -138,6 +138,28 @@ EXAMPLES = '''
- apt: pkg=foo state=build-dep - apt: pkg=foo state=build-dep
''' '''
RETURN = '''
cache_updated:
description: if the cache was updated or not
returned: success, in some cases
type: boolean
sample: True
cache_update_time:
description: time of the last cache update (0 if unknown)
returned: success, in some cases
type: datetime
sample: 1425828348000
stdout:
description: output from apt
returned: success, when needed
type: string
sample: "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following extra packages will be installed:\n apache2-bin ..."
stderr:
description: error output from apt
returned: success, when needed
type: string
sample: "AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to ..."
'''
import traceback import traceback
# added to stave off future warnings about apt api # added to stave off future warnings about apt api
@ -545,6 +567,8 @@ def main():
if not APTITUDE_CMD and p.get('upgrade', None) in [ 'full', 'safe', 'yes' ]: if not APTITUDE_CMD and p.get('upgrade', None) in [ 'full', 'safe', 'yes' ]:
module.fail_json(msg="Could not find aptitude. Please ensure it is installed.") module.fail_json(msg="Could not find aptitude. Please ensure it is installed.")
updated_cache = False
updated_cache_time = 0
install_recommends = p['install_recommends'] install_recommends = p['install_recommends']
dpkg_options = expand_dpkg_options(p['dpkg_options']) dpkg_options = expand_dpkg_options(p['dpkg_options'])
@ -567,41 +591,41 @@ def main():
if p['update_cache']: if p['update_cache']:
# Default is: always update the cache # Default is: always update the cache
cache_valid = False cache_valid = False
if p['cache_valid_time']: now = datetime.datetime.now()
tdelta = datetime.timedelta(seconds=p['cache_valid_time']) if p.get('cache_valid_time', False):
try: try:
mtime = os.stat(APT_UPDATE_SUCCESS_STAMP_PATH).st_mtime mtime = os.stat(APT_UPDATE_SUCCESS_STAMP_PATH).st_mtime
except: except:
mtime = False
if mtime is False:
# Looks like the update-success-stamp is not available # Looks like the update-success-stamp is not available
# Fallback: Checking the mtime of the lists # Fallback: Checking the mtime of the lists
try: try:
mtime = os.stat(APT_LISTS_PATH).st_mtime mtime = os.stat(APT_LISTS_PATH).st_mtime
except: except:
# No mtime could be read. We update the cache to be safe
mtime = False mtime = False
if mtime is False:
# No mtime could be read - looks like lists are not there if mtime:
# We update the cache to be safe tdelta = datetime.timedelta(seconds=p['cache_valid_time'])
cache_valid = False
else:
mtimestamp = datetime.datetime.fromtimestamp(mtime) mtimestamp = datetime.datetime.fromtimestamp(mtime)
if mtimestamp + tdelta >= datetime.datetime.now(): if mtimestamp + tdelta >= now:
# dont update the cache
# the old cache is less than cache_valid_time seconds old - so still valid
cache_valid = True cache_valid = True
updated_cache_time = int(time.mktime(mtimestamp.timetuple()))
if cache_valid is not True: if cache_valid is not True:
cache.update() cache.update()
cache.open(progress=None) cache.open(progress=None)
updated_cache = True
updated_cache_time = int(time.mktime(now.timetuple()))
if not p['package'] and not p['upgrade'] and not p['deb']: if not p['package'] and not p['upgrade'] and not p['deb']:
module.exit_json(changed=False) module.exit_json(changed=False, cache_updated=updated_cache, cache_update_time=updated_cache_time)
else:
updated_cache = False
updated_cache_time = 0
force_yes = p['force'] force_yes = p['force']
if p['upgrade']: if p['upgrade']:
upgrade(module, p['upgrade'], force_yes, upgrade(module, p['upgrade'], force_yes, p['default_release'], dpkg_options)
p['default_release'], dpkg_options)
if p['deb']: if p['deb']:
if p['state'] != 'present': if p['state'] != 'present':
@ -631,6 +655,8 @@ def main():
force=force_yes, dpkg_options=dpkg_options, force=force_yes, dpkg_options=dpkg_options,
build_dep=state_builddep) build_dep=state_builddep)
(success, retvals) = result (success, retvals) = result
retvals['cache_updated']=updated_cache
retvals['cache_update_time']=updated_cache_time
if success: if success:
module.exit_json(**retvals) module.exit_json(**retvals)
else: else: