From: Holger Levsen Date: Wed, 2 Nov 2011 10:38:57 +0000 (+0100) Subject: move persistance functions outside kernel_build X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=a582708be2f8db20c21aa426b2b7fc2613201fed;p=profitbricks%2Fjenkins-build-scripts.git move persistance functions outside kernel_build --- diff --git a/kernel_build.py b/kernel_build.py index 395195f..a2ef52b 100644 --- a/kernel_build.py +++ b/kernel_build.py @@ -11,11 +11,7 @@ import logging import smtplib from glob import glob from logging import Formatter -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from sqlalchemy import Table, Column, Integer, String, MetaData, Sequence -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm.exc import NoResultFound +import kernel_persistance CWD = os.environ.get('WORKSPACE') BUILD_NUMBER = os.environ.get('BUILD_NUMBER') @@ -30,7 +26,6 @@ GIT_BRANCH_NAME = os.environ.get('GIT_BRANCH_NAME') GIT_COMMITTER_EMAIL = os.environ.get('GIT_COMMITTER_EMAIL') -PERSISTENCE_FILE = os.path.join(CWD, '..', '.persistence') SMTP_SERVER = 'roma.profitbricks.localdomain' SMTP_SUBJECT = 'Kernel build for branch %s, buildnumber %s was %s' SMTP_TEXT = ( @@ -54,10 +49,6 @@ formatter = Formatter('%(asctime)s %(name)s[%(process)d] %(levelname)s: %(messag stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) -Base = declarative_base() - -engine = create_engine('sqlite:///%s' %(PERSISTENCE_FILE)) -Session = sessionmaker(bind=engine) def send_email(result): smtp = smtplib.SMTP(SMTP_SERVER) @@ -71,66 +62,6 @@ def send_email(result): smtp.sendmail(SMTP_FROM, GIT_COMMITTER_EMAIL, msg) smtp.quit() -class KernelVersion(Base): - __tablename__ = 'branch_kernel_version' - id = Column(Integer, Sequence('branch_id'), primary_key=True) - branch_name = Column(String(255), unique=True, nullable=False) - last_version = Column(Integer, nullable=False) - - def __init__(self, branch_name, last_version): - self.branch_name = branch_name - self.last_version = last_version - - def __repr__(self): - return '' %( - self.branch_name, self.last_version - ) - -Base.metadata.create_all(engine) - -def get_last_kernel_revision_obj(): - session = Session() - logger.debug('Getting kernel revision from persistence') - if GIT_BRANCH_NAME == 'master' or GIT_BRANCH_NAME.startswith('hotfix'): - persistance_identifier = 'master' - elif GIT_BRANCH_NAME.startswith('release'): - persistance_identifier = 'release' - else: - persistance_identifier = 'develop' - - try: - answ = session.query(KernelVersion).filter( - KernelVersion.branch_name == persistance_identifier).one() - except NoResultFound: - session.add(KernelVersion(persistance_identifier, 0)) - session.commit() - answ = session.query(KernelVersion).filter( - KernelVersion.branch_name == persistance_identifier).one() - finally: - session.close() - - logger.info( - 'Got this Kernel revision for branch %s and persistance_identifier %s: %s' - %(GIT_BRANCH_NAME, persistance_identifier, answ) - ) - return answ - -def update_kernel_revision_obj(kernel_version_obj, new_revision): - session = Session() - kernel_version_obj.last_version = new_revision - session.add(kernel_version_obj) - try: - session.commit() - except Exception, error: - logger.error('Some error happend while commiting new revision') - logger.exception(error) - return False - else: - return True - finally: - session.close() - - def build_kernel_with_pbuilder(revision, pbuilder_script, dist='stable'): pbuilder_script = os.path.abspath(pbuilder_script) cmd = [ diff --git a/kernel_persistance.py b/kernel_persistance.py new file mode 100644 index 0000000..58ae796 --- /dev/null +++ b/kernel_persistance.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy import Table, Column, Integer, String, MetaData, Sequence +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm.exc import NoResultFound + +CWD = os.environ.get('WORKSPACE') +PERSISTENCE_FILE = os.path.join(CWD, '..', '.persistence') + +Base = declarative_base() + +engine = create_engine('sqlite:///%s' %(PERSISTENCE_FILE)) +Session = sessionmaker(bind=engine) + +class KernelVersion(Base): + __tablename__ = 'branch_kernel_version' + id = Column(Integer, Sequence('branch_id'), primary_key=True) + identifier = Column(String(255), unique=True, nullable=False) + branch_name = Column(String(255), unique=True, nullable=False) + last_version = Column(Integer, nullable=False) + pb_modules = Column(String(255), unique=True, nullable=False) + + def __init__(self, branch_name, last_version): + self.identifier = identifier + self.last_version = last_version + self.branch_name = branch_name + self.pb_modules = pb_modules + + def __repr__(self): + return '' %( + self.identifier, self.last_version + ) + +Base.metadata.create_all(engine) + +def get_last_kernel_revision_obj(): + session = Session() + logger.debug('Getting kernel revision from persistence') + if GIT_BRANCH_NAME == 'master' or GIT_BRANCH_NAME.startswith('hotfix'): + persistance_identifier = 'master' + elif GIT_BRANCH_NAME.startswith('release'): + persistance_identifier = 'release' + else: + # FIXME: feature branch handling + persistance_identifier = 'develop' + + try: + answ = session.query(KernelVersion).filter( + KernelVersion.identifier == persistance_identifier).one() + except NoResultFound: + session.add(KernelVersion(persistance_identifier, 0)) + session.commit() + answ = session.query(KernelVersion).filter( + KernelVersion.identifier == persistance_identifier).one() + finally: + session.close() + + logger.info( + 'Got this Kernel revision for persistance_identifier %s: %s' + %(persistance_identifier, answ) + ) + return answ + +def update_kernel_revision_obj(kernel_version_obj, new_revision): + session = Session() + kernel_version_obj.last_version = new_revision + session.add(kernel_version_obj) + try: + session.commit() + except Exception, error: + logger.error('Some error happend while commiting new revision') + logger.exception(error) + return False + else: + return True + finally: + session.close() + +