diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-10-10 17:19:04 +0100 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-10-10 17:19:04 +0100 |
commit | 3a128a0e70307ab8e7d0e2ac6b06e76e805b828f (patch) | |
tree | 223e78688dc71c2d3a8c846769665c6257cb1ce6 | |
parent | 312a0f484a688d7af4f904064c38311d58bc1c37 (diff) | |
download | lorry-controller-3a128a0e70307ab8e7d0e2ac6b06e76e805b828f.tar.gz |
Support destroying dead lorries. Also fix up dry-run a bit more
-rwxr-xr-x | lorry-controller | 53 | ||||
-rw-r--r-- | lorrycontroller/confparser.py | 2 |
2 files changed, 42 insertions, 13 deletions
diff --git a/lorry-controller b/lorry-controller index cf5dbae..e3f7d36 100755 --- a/lorry-controller +++ b/lorry-controller @@ -7,6 +7,12 @@ import cliapp import logging import os import time +import re + + +from lorrycontroller.confparser import LorryControllerConfig +from lorrycontroller.workingstate import WorkingStateManager + defaults = { 'work-area': '/home/lorry/controller-area', @@ -14,8 +20,9 @@ defaults = { 'lorry': 'lorry', } -from lorrycontroller.confparser import LorryControllerConfig -from lorrycontroller.workingstate import WorkingStateManager + +token_finder = re.compile("([0-9a-f]{40})") + class LorryController(cliapp.Application): @@ -96,9 +103,34 @@ class LorryController(cliapp.Application): else: # Could not find UUID in config, switch to 'never' should_delete = "never" - # TODO: also handle 'unchanged' - if should_delete == "always": - logging.warning("TODO: Delete from Trove") + want_destroy = (should_delete == "always") + if should_delete == "unchanged": + exit, out, err = self.maybe_runcmd( + ['git', 'ls-remote', 'ssh://git@localhost/%s.git' % + dead_lorry], dry=True) + if exit != 0: + logging.error("Unable to ls-remote to decide if " + "unchanged. Assuming it is changed.") + else: + logging.debug("TODO: Should decide if unchanged!") + + if want_destroy: + exit, out, err = self.maybe_runcmd(['ssh', 'git@localhost', + 'destroy', dead_lorry], + dry=True) + if exit != 0: + logging.error("Unable to destroy %s" % dead_lorry) + else: + token = token_finder.match(out).group(1) + exit, out, err = self.maybe_runcmd( + ['ssh', 'git@localhost', 'destroy', dead_lorry, + token]) + if exit != 0: + logging.error("Unable to destroy %s despite having" + " the token %s" % + (dead_lorry, token)) + else: + logging.debug("Destroyed") del mgr.lorry_state[dead_lorry] # 2. Handle creates for any new lorries we now want @@ -208,14 +240,11 @@ class LorryController(cliapp.Application): self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'], 'git')) - def maybe_runcmd(self, *args, **kwargs): - cmdargs = args[0] - if (not self.settings['dry-run']) or \ - (cmdargs[0] == "ssh" and \ - cmdargs[len(cmdargs)-1] == "--verbose"): - return self.runcmd_unchecked(*args, **kwargs) + def maybe_runcmd(self, cmdline, dry=False, *args, **kwargs): + if (not self.settings['dry-run']) or dry: + return self.runcmd_unchecked(cmdline, *args, **kwargs) else: - logging.debug("DRY-RUN: Not running %r" % args) + logging.debug("DRY-RUN: Not running %r" % cmdline) return 0, 'DRY-RUN', 'DRY-RUN' if __name__ == '__main__': diff --git a/lorrycontroller/confparser.py b/lorrycontroller/confparser.py index e3132e1..00a9fe1 100644 --- a/lorrycontroller/confparser.py +++ b/lorrycontroller/confparser.py @@ -194,7 +194,7 @@ class LorryControllerConfig(object): "--verbose"] state['next-vls'] = state.get('next-vls', now - 1) if state['next-vls'] < now: - exit, out, err = self.app.maybe_runcmd(listcmdargs) + exit, out, err = self.app.maybe_runcmd(listcmdargs, dry=True) if exit == 0: repo_info = {} for entry in [x for x in out.split("\n") if x != ""]: |