summaryrefslogtreecommitdiff
path: root/lorrycontroller/workingstate.py
diff options
context:
space:
mode:
Diffstat (limited to 'lorrycontroller/workingstate.py')
-rw-r--r--lorrycontroller/workingstate.py127
1 files changed, 0 insertions, 127 deletions
diff --git a/lorrycontroller/workingstate.py b/lorrycontroller/workingstate.py
deleted file mode 100644
index b8dc751..0000000
--- a/lorrycontroller/workingstate.py
+++ /dev/null
@@ -1,127 +0,0 @@
-# Copyright (C) 2013 Codethink Limited
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; version 2 of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-
-import json
-import os
-import logging
-import string
-
-class LorryFileRunner(object):
- def __init__(self, mgr, lorryname):
- self.mgr = mgr
- self.lorryname = lorryname
- self.lorryfile = os.path.join(self.mgr.workdir,
- self._esc(lorryname) + ".lorry")
-
- def _esc(self, name):
- valid_chars = string.digits + string.letters + '%_'
- transl = lambda x: x if x in valid_chars else '_'
- return ''.join([transl(x) for x in name])
-
- def __enter__(self):
- lorry_obj = { self.lorryname:
- self.mgr.lorry_state[self.lorryname]['lorry'] }
- with open(self.lorryfile, "w") as fh:
- json.dump(lorry_obj, fh)
- fh.write("\n")
- return self
-
- def __exit__(self, exctype, excvalue, exctraceback):
- os.unlink(self.lorryfile)
-
- def run_lorry(self, *args):
- cmdargs = list(args)
- cmdargs.append(self.lorryfile)
- conf_uuid = self.mgr.lorry_state[self.lorryname]['conf']
- conf = self.mgr.app.conf.configs[conf_uuid]
- cmdargs.append("--tarball=%s" % conf['tarball'])
- exit, out, err = self.mgr.app.maybe_runcmd(cmdargs)
- if exit == 0:
- logging.debug("Lorry of %s succeeded: %s" % (self.lorryname, out))
- self.mgr.lorry_state[self.lorryname]['result'] = "OK"
- else:
- logging.warn("Lorry of %s failed: %s" % (self.lorryname, err))
- self.mgr.lorry_state[self.lorryname]['result'] = err
-
-class WorkingStateManager(object):
- '''Manage the working state of lorry-controller'''
-
- def __init__(self, app):
- self.app = app
- self.workdir = os.path.join(self.app.settings['work-area'], 'work')
-
- def __enter__(self):
- self._load_state()
- return self
-
- def __exit__(self, exctype, excvalue, exctraceback):
- self.purge_dead_troves()
- if not self.app.settings['dry-run']:
- self.save_state()
- else:
- logging.debug("DRY-RUN: Not saving state again")
-
- def purge_dead_troves(self):
- old_trove_count = len(self.trove_state.keys())
- all_troves = self.trove_state
- self.trove_state = {}
- new_trove_count = 0
- for uuid, trove in all_troves.iteritems():
- self.trove_state[uuid] = trove
- new_trove_count += 1
- if old_trove_count != new_trove_count:
- trove_diff = old_trove_count - new_trove_count
- logging.info("Purged %d dead trove entr%s from the state file" % (
- trove_diff, ("y" if trove_diff == 1 else "ies")))
-
- def _load_state(self):
- self.lorry_state_file = os.path.join(self.workdir,
- "last-lorry-state.json")
- self.trove_state_file = os.path.join(self.workdir,
- "last-trove-state.json")
- if os.path.exists(self.lorry_state_file):
- logging.info("Loading lorry state file: %s" %
- self.lorry_state_file)
- with open(self.lorry_state_file, "r") as fh:
- self.lorry_state = json.load(fh)
- else:
- self.lorry_state = dict()
-
- if os.path.exists(self.trove_state_file):
- logging.info("Loading trove state file: %s" %
- self.trove_state_file)
- with open(self.trove_state_file, "r") as fh:
- self.trove_state = json.load(fh)
- else:
- self.trove_state = dict()
-
- def save_state(self):
- logging.info("Serialising lorry state: %s" % self.lorry_state_file)
- with open(self.lorry_state_file, "w") as fh:
- json.dump(self.lorry_state, fh, sort_keys=True, indent=4)
- fh.write("\n")
- logging.info("Serialising trove state: %s" % self.trove_state_file)
- with open(self.trove_state_file, "w") as fh:
- json.dump(self.trove_state, fh, sort_keys=True, indent=4)
- fh.write("\n")
-
- def get_trove(self, troveuuid):
- if troveuuid not in self.trove_state:
- self.trove_state[troveuuid] = {}
- return self.trove_state[troveuuid]
-
- def runner(self, lorryname):
- return LorryFileRunner(self, lorryname)