From: Holger Levsen Date: Wed, 26 Sep 2012 09:33:45 +0000 (+0200) Subject: trigger liveboot(-request) after package build X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=e66c6b3d4c6ac3588a0c64640298966953b3353b;p=profitbricks%2Fjenkins-build-scripts.git trigger liveboot(-request) after package build --- diff --git a/add_liveboot_request.py b/add_liveboot_request.py new file mode 100644 index 0000000..cfb1b0b --- /dev/null +++ b/add_liveboot_request.py @@ -0,0 +1,128 @@ +#!/usr/bin/python + +""" This script creates a new liveboot build request when a new version of + .deb package is available + + If the package is included in the default pkg list for liveboot, the + version of this pkg in the default package list will be updates. +""" + + + It updates the default pkg list for the liveboot in the database with the + create + +__author__ = "Fabian Holler " + +from cidb import * +import sys +import psycopg2 +import logging +import pwd + + +def get_default_pkg_list_id(con): + """ Returns the package_list_id for the default package_list. """ + + cur = con.cursor() + cur.execute("SELECT value FROM liveboot_settings WHERE name=%s", + ("default_package_list_id",)) + result = cur.fetchone() + if result is None: + logger.error("Error: default_package_list_id doesn't exist in" + " liveboot_settings table") + return None + return int(result[0]) + + +def get_deb_pkg_id(con, deb_package_instance_id): + """ Return the deb_package_id for a given deb_package_instance_id. """ + + cur = con.cursor() + cur.execute("SELECT dp.id FROM deb_package AS dp JOIN deb_package_instance" + " AS dpi ON (dpi.deb_package_id = dp.id) WHERE dpi.id =%s", + (deb_package_instance_id,)) + result = cur.fetchone() + if not result: + logger.error("No deb_package_instance record with the id %s exist" % + deb_package_instance_id) + return None + return int(result[0]) + + +def get_deb_pkg_instance_ids(con, deb_package_id): + """ Returns all deb_package_instance ids for a given deb_package_id. """ + + cur = con.cursor() + cur.execute("SELECT id FROM deb_package_instance AS dpi" + " JOIN deb_package AS dp ON (dpi.deb_package_id = dp.id)" + " WHERE dp.id=%s", (deb_package_id,)) + return cur.fetchall() + + +def update_default_package_list(con, def_package_list_id, + new_deb_package_instance_id, old_deb_package_instance_ids): + """ Updates a deb_package_id field in deb_package_list. + + @param def_package_list_id ID of the deb_package_list + @param new_deb_package_instance_id ID of the deb_package_list + @param old_deb_package_instance_ids List of deb_package_ids that + should be changed to new_deb_package_instance_id + @return True If at least one record was updated. + @return False If no record was updated. + """ + + cur = con.cursor() + cur.execute("UPDATE package_list_deb_package_instance AS pldpi" + " SET deb_package_instance_id = %s WHERE pldpi.package_list_id = %s" + " AND pldpi.deb_package_instance_id IN %s RETURNING package_list_id", + (new_deb_package_instance_id, def_package_list_id, + old_deb_package_instance_ids)) + + result = cur.fetchall() + return(True and result) + + +def add_liveboot_request(con, owner_uid, package_list_id): + cur = con.cursor() + cur.execute("INSERT INTO liveboot_request(owner_uid, package_list_id)" + " VALUES(%s)", (owner_uid, package_list_id)) + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("usage: %s deb_package_instance_id") + sys.exit(1) + deb_pkg_instance_id = int(sys.argv[1]) + + logging.basicConfig() + logger = logging.getLogger() + + con = db_connect() + # If the package is included in the default package list, update the list + # to include newest version of the package + deb_pkg_id = get_deb_pkg_id(con, deb_pkg_instance_id) + if not deb_pkg_id: + sys.exit(1) + + def_pkg_list_id = get_default_pkg_list(con) + if not def_pkg_list_id: + sys.exit(1) + + # This can't return None with the used db constraints + deb_pkg_instance_ids = get_deb_pkg_instance_ids(con, deb_pkg_id) + + updated = update_default_package_list(con, def_pkg_list_id, + deb_pkg_instance_id, deb_pkg_instance_ids) + con.commit() + logger.info("Updated default package list successfully") + + if not updated: + logger.info("deb_package with id %s doesn't exist in default package" + " list, no liveboot request will be created") + sys.exit(0) + + # create a new liveboot request with the updated default package list + jenkins_uid = pwd.getpwnam("jenkins").pw_uid + add_liveboot_request(con, jenkins_uid, def_pkg_list_id) + con.commit() + sys.exit(0) diff --git a/cidb.py b/cidb.py new file mode 100644 index 0000000..2238713 --- /dev/null +++ b/cidb.py @@ -0,0 +1,17 @@ +#!/usr/bin/python + +import psycopg2 +import psycopg2.extras + +DB_USER = "cidb" +DB_PASSWD = "cidb" +DB_NAME = "cidb" +DB_HOST = "jenkins" +DB_PORT = 5432 + + +def db_connect(): + """ Connects to the db and returns the Connection object. """ + + return psycopg2.connect(database=DB_NAME, user=DB_USER, password=DB_PASSWD, + host=DB_HOST, port=DB_PORT) diff --git a/debian_build.py b/debian_build.py index 7aac027..34a61c7 100755 --- a/debian_build.py +++ b/debian_build.py @@ -19,6 +19,8 @@ import time import urllib import fileinput import psycopg2 +from add_liveboot_request import * +from cidb import * from glob import glob from ftplib import FTP from lib import git_helper @@ -30,12 +32,6 @@ from ConfigParser import SafeConfigParser JOB_URL = 'http://jenkins:80/job/%(flavour)s/buildWithParameters?token=BuildIt&' JOB_DELAY = '0sec' -DB_USER = "cidb" -DB_PASSWD = "cidb" -DB_NAME = "cidb" -DB_HOST = "sagunt" -DB_PORT = 5432 - GIT = '/usr/bin/git' BIN_RM = '/bin/rm' @@ -121,10 +117,6 @@ def read_file(path): fh.close() return result -def db_connect(): - return psycopg2.connect(database=DB_NAME, user=DB_USER, password=DB_PASSWD, - host=DB_HOST, port=DB_PORT) - def db_add_job(con, name): cur = con.cursor() cur.execute("SAVEPOINT a") @@ -163,11 +155,13 @@ def db_add_package_instance(con, package_id, origin_id, build_id, version): cur = con.cursor() cur.execute("SAVEPOINT a") try: - cur.execute("INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_id, version, available) VALUES(%s, %s, %s, '%s', 'true')" % (package_id, origin_id, build_id, version)) + cur.execute("INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_id, version, available) VALUES(%s, %s, %s, '%s', 'true') RETURNING id" % (package_id, origin_id, build_id, version)) except psycopg2.DatabaseError as e: - # record already exist + # record already exist - this MUST NOT HAPPEN cur.execute("ROLLBACK TO a") # have to rollback after failed command - #logger.debug("INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_id, version, available) VALUES(%s, %s, %s, '%s', 'true')" % (package_id, origin_id, build_id, version)) + logger_error("FAILED: INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_id, version, available) VALUES(%s, %s, %s, '%s', 'true') RETURNING id" % (package_id, origin_id, build_id, version)) + exit_error() + return cur.fetchone()[0] def db_add_origin(con, origin): cur = con.cursor() @@ -181,7 +175,6 @@ def db_add_origin(con, origin): #logger.debug("INSERT INTO deb_package_origin(origin) VALUES('%s') RETURNING id" % (origin)) return cur.fetchone()[0] -#def add_package_instance("profitbricks", $BUILD_JOBNAME, $BUILD_NUMBER, changes_file) def add_package_instance(origin, job_name, build_number, changes_file, version, start, end): con = db_connect() #logger.debug("save %s to database" %(job_name)) @@ -209,11 +202,12 @@ def add_package_instance(origin, job_name, build_number, changes_file, version, #logger.debug('package: %s' %(package)) db_package_id = db_add_package (con, package) #logger.debug("INSERT success package %s" % package) - db_add_package_instance (con, db_package_id, db_origin_id, db_build_id, version) + db_package_instance_id = db_add_package_instance (con, db_package_id, db_origin_id, db_build_id, version) logger.debug("INSERT success package version %s %s" %( package,version)) #logger.debug('Cmd returned with status %d' %(cmdobj.returncode)) con.commit() logger.info("CIDB update OK.") + return db_package_instance_id def dput_package_upload(changes_path): try: @@ -690,13 +684,13 @@ if __name__ == '__main__': # cidb wise, we only care about builds from master, hotfix + develop if GIT_BRANCH_NAME == 'master' or GIT_BRANCH_NAME == 'develop' or GIT_BRANCH_NAME.startswith('hotfix/'): try: - add_package_instance("profitbricks", BUILD_JOBNAME, BUILD_NUMBER, changes_file, version, BUILD_START, BUILD_END) + package_instance_id = add_package_instance("profitbricks", BUILD_JOBNAME, BUILD_NUMBER, changes_file, version, BUILD_START, BUILD_END) except Exception, error: cmd = ['figlet-figlet', '-t', 'package instance not added to DB'] subprocess.check_call(cmd) #FIXME: this should really cause an error # exit_error() - + add_liveboot_request(package_instance_id) # finally exit_ok()