diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-21 14:53:45 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-21 14:53:45 +0000 |
commit | 5a5f9d6b84a4c3bbb11a8caa93c42cb7a15feb3e (patch) | |
tree | a86321744d3f06edca9bce3bfd4ba3105a8f1f9e | |
parent | 0a41b7d0c4f3773a871e7f2d8aa9aa300eec296c (diff) | |
download | lorry-controller-5a5f9d6b84a4c3bbb11a8caa93c42cb7a15feb3e.tar.gz |
Remove lorries from STATEDB if gone from CONFGIT
-rwxr-xr-x | lorry-controller-webapp | 34 | ||||
-rw-r--r-- | yarns.webapp/030-queue-management.yarn | 7 | ||||
-rw-r--r-- | yarns.webapp/900-implementations.yarn | 6 |
3 files changed, 44 insertions, 3 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index edad578..759b0a9 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -101,9 +101,15 @@ class StateDB(object): c = self._conn.cursor() return c.execute('SELECT path, text, generated FROM lorries') + def get_lorries_paths(self): + logging.debug('StateDB.get_lorries_paths called') + self._open() + c = self._conn.cursor() + return [row[0] for row in c.execute('SELECT path FROM lorries')] + def add_to_lorries(self, path=None, text=None, generated=None): logging.debug( - 'StateDB.add_to_lorries(path=%r, text=%, generated=%r called', + 'StateDB.add_to_lorries(path=%r, text=%r, generated=%r called', path, text, generated) @@ -121,6 +127,14 @@ class StateDB(object): (path, text, generated)) self._conn.commit() + def remove_lorry(self, path): + logging.debug('StateDB.remove_lorry(%r) called', path) + self._open() + c = self._conn.cursor() + c.execute('BEGIN TRANSACTION') + c.execute('DELETE FROM lorries WHERE path IS ?', (path,)) + self._conn.commit() + class LorryControllerRoute(object): @@ -221,9 +235,18 @@ class ReadConfiguration(LorryControllerRoute): conf_obj = self.read_config_file() + existing = set(self.statedb.get_lorries_paths()) + logging.debug('existing at beginning: %r', existing) + for section in conf_obj: if section['type'] == 'lorries': - self.add_matching_lorries_to_statedb(section) + added = self.add_matching_lorries_to_statedb(section) + logging.debug('added: %r', added) + existing = existing.difference(added) + + logging.debug('existing at end: %r', existing) + for path in existing: + self.statedb.remove_lorry(path) return 'Configuration has been updated.' @@ -251,6 +274,8 @@ class ReadConfiguration(LorryControllerRoute): bottle.abort(500, 'Error reading %s: %s' % (filename, e)) def add_matching_lorries_to_statedb(self, section): + added_paths = set() + filenames = self.find_lorry_files_for_section(section) for filename in filenames: for lorry_spec in self.get_lorry_specs(filename): @@ -258,8 +283,11 @@ class ReadConfiguration(LorryControllerRoute): text = self.serialise_lorry_spec(lorry_spec) logging.debug( '%s %s: adding lorry spec %r: %r', - path, text) + self.http_method, self.path, path, text) self.statedb.add_to_lorries(path=path, text=text, generated=0) + added_paths.add(path) + + return added_paths def find_lorry_files_for_section(self, section): result = [] diff --git a/yarns.webapp/030-queue-management.yarn b/yarns.webapp/030-queue-management.yarn index 4404e20..5ee45e1 100644 --- a/yarns.webapp/030-queue-management.yarn +++ b/yarns.webapp/030-queue-management.yarn @@ -72,6 +72,13 @@ configuration makes `/list-queue` report it. AND admin makes request GET /1.0/list-queue THEN response has queue set to ["upstream/foo"] +If the `.lorry` file is removed, the queue should again become empty. + + GIVEN file CONFGIT/foo.lorry is removed + WHEN admin makes request GET /1.0/read-configuration + AND admin makes request GET /1.0/list-queue + THEN response has queue set to [] + Finally, clean up. FINALLY WEBAPP terminates diff --git a/yarns.webapp/900-implementations.yarn b/yarns.webapp/900-implementations.yarn index 4c1ddfe..9fb2786 100644 --- a/yarns.webapp/900-implementations.yarn +++ b/yarns.webapp/900-implementations.yarn @@ -87,6 +87,12 @@ Add a `.lorry` file to be used by a `lorry-controller.conf`. IMPLEMENTS GIVEN Lorry file (\S+) with (.*) printf '%s\n' "$MATCH_2" > "$DATADIR/$MATCH_1" +Remove a file. This is actually quite generic, but it's relevant to us +for `.lorry` files only (when this is being written). + + IMPLEMENTS GIVEN file (\S+) is removed + rm "$DATADIR/$MATCH_1" + Add a `lorries` section to a `lorry-controller.conf`. This hardcodes most of the configuration. |