From 537a78ec5c536b8eb7268a49addf42de86f0e8f8 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Sat, 21 Feb 2015 22:26:01 -0600 Subject: [PATCH] Add salt.fileserver.svnfs.clear_lock() --- salt/fileserver/svnfs.py | 58 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/salt/fileserver/svnfs.py b/salt/fileserver/svnfs.py index 2dccd2547d..0daa8241ea 100644 --- a/salt/fileserver/svnfs.py +++ b/salt/fileserver/svnfs.py @@ -257,6 +257,52 @@ def clear_cache(): return errors +def clear_lock(remote=None): + ''' + Clear update.lk + ''' + def _add_error(errlist, url, lk_fn, exc): + errlist.append('Unable to remove update lock for {0} ({1}): {2} ' + .format(url, lk_fn, exc)) + + cleared = [] + errors = [] + for repo in init(): + if remote: + try: + if remote not in repo['url']: + continue + except TypeError: + # remote was non-string, try again + if _text_type(remote) not in repo['url']: + continue + lk_fn = _update_lockfile(repo) + if os.path.exists(lk_fn): + try: + os.remove(lk_fn) + except OSError as exc: + if exc.errno == errno.EISDIR: + # Somehow this path is a directory. Should never happen + # unless some wiseguy manually creates a directory at this + # path, but just in case, handle it. + try: + shutil.rmtree(lk_fn) + except OSError as exc: + _add_error(errors, repo['url'], lk_fn, exc) + else: + _add_error(errors, repo['url'], lk_fn, exc) + else: + cleared.append('Removed lock for {0}'.format(repo['url'])) + return cleared, errors + + +def _update_lockfile(repo): + ''' + Return the filename of the update lock + ''' + return os.path.join(repo['repo'], 'update.lk') + + def update(): ''' Execute an svn update on all of the repos @@ -267,7 +313,17 @@ def update(): pid = os.getpid() data['changed'] = _clear_old_remotes() for repo in init(): - lk_fn = os.path.join(repo['repo'], 'update.lk') + lk_fn = _update_lockfile(repo) + if os.path.exists(lk_fn): + log.warning( + 'Update lockfile is present for svn remote {0}, skipping. ' + 'If this warning persists, it is possible that the update ' + 'process was interrupted. Removing {1} or running ' + '\'salt-run fileserver.clear_lock backend=svn\' will allow ' + 'updates to continue for this remote.' + .format(repo['url'], lk_fn) + ) + continue with salt.utils.fopen(lk_fn, 'w+') as fp_: fp_.write(str(pid)) old_rev = _rev(repo)