From bd30153febf4fda2e1f2872e6fd3142b0e6d84bd Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Wed, 13 Jul 2016 14:03:17 -0400 Subject: [PATCH] Move ridad into an importable location and validate module state strings. --- rida.py | 2 +- rida/database.py | 36 ++++++++++++++++++++++++++---- ridad.py => rida/scheduler/main.py | 7 +++--- 3 files changed, 36 insertions(+), 9 deletions(-) rename ridad.py => rida/scheduler/main.py (95%) diff --git a/rida.py b/rida.py index ea7c2aa6..3646ba5f 100755 --- a/rida.py +++ b/rida.py @@ -123,7 +123,7 @@ def submit_build(): build = rida.database.Build(module_id=module.id, package=pkgname, format="rpms") db.session.add(build) module.modulemd = mmd.dumps() - module.state = "wait" + module.state = rida.database.BUILD_STATES["init"] db.session.add(module) db.session.commit() # Publish to whatever bus we're configured to connect to. diff --git a/rida/database.py b/rida/database.py index ac89a529..207d490c 100644 --- a/rida/database.py +++ b/rida/database.py @@ -24,11 +24,32 @@ """Database handler functions.""" -from sqlalchemy import Column, Integer, String, ForeignKey, create_engine -from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy import ( + Column, + Integer, + String, + ForeignKey, + create_engine, +) +from sqlalchemy.orm import ( + sessionmaker, + relationship, + validates, +) from sqlalchemy.ext.declarative import declarative_base +# Just like koji.BUILD_STATES, except our own codes for modules. +BUILD_STATES = { + "init": 0, + "wait": 1, + "build": 2, + "done": 3, + "failed": 4, + "ready": 5, +} + + class RidaBase(object): # TODO -- we can implement functionality here common to all our model # classes. @@ -77,10 +98,17 @@ class Module(Base): name = Column(String, nullable=False) version = Column(String, nullable=False) release = Column(String, nullable=False) - # XXX: Consider making this a proper ENUM - state = Column(String, nullable=False) + state = Column(Integer, nullable=False) modulemd = Column(String, nullable=False) + @validates('state') + def validate_state(self, key, field): + if field in BUILD_STATES.values(): + return field + if field in BUILD_STATES: + return BUILD_STATES[field] + raise ValueError("%s: %s, not in %r" % (key, field, BUILD_STATES)) + def json(self): return { 'id': self.id, diff --git a/ridad.py b/rida/scheduler/main.py similarity index 95% rename from ridad.py rename to rida/scheduler/main.py index 7969d8d6..96294669 100755 --- a/ridad.py +++ b/rida/scheduler/main.py @@ -58,13 +58,12 @@ class Messaging(threading.Thread): for msg in rida.messaging.listen(backend=config.messaging): log.debug("Saw %r, %r" % (msg['msg_id'], msg['topic'])) if '.buildsys.build.state.change' in msg['topic']: - log.info("A build changed state in koji!!") + self.handle_build_change(msg) elif '.rida.module.state.change' in msg['topic']: - log.info("Our frontend says that a module changed state!!") + self.handle_module_change(msg) else: pass - class Polling(threading.Thread): def run(self): while True: @@ -77,7 +76,7 @@ class Polling(threading.Thread): pass -if __name__ == '__main__': +def main(): logging.basicConfig(level=logging.DEBUG) # For now logging.info("Starting ridad.") try: