From: Holger Levsen Date: Thu, 27 Sep 2012 14:59:13 +0000 (+0200) Subject: bugfix: add_package_instances() needs to return an array X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=7aed82d602803484f5253fa2a4fcba4dd00a4923;p=profitbricks%2Fjenkins-build-scripts.git bugfix: add_package_instances() needs to return an array --- diff --git a/db_add.py b/db_add.py index cec8255..bb40fc8 100755 --- a/db_add.py +++ b/db_add.py @@ -46,6 +46,18 @@ def db_add_build_result(con, build_id, start, end): #logger.debug("INSERT INTO jenkins_build_result(jenkins_build_id, build_start, build_end, successful) VALUES(%s, '%s', '%s', %s) RETURNING id" % (build_id, start, end, 'true')) return cur.fetchone()[0] +def db_add_origin(con, origin): + cur = con.cursor() + cur.execute("SAVEPOINT a") + try: + cur.execute("INSERT INTO deb_package_origin(origin) VALUES('%s') RETURNING id" % (origin)) + except psycopg2.DatabaseError as e: + # record already exist + cur.execute("ROLLBACK TO a") # have to rollback after failed command + cur.execute("SELECT id FROM deb_package_origin WHERE origin='%s'" % (origin)) + #logger.debug("INSERT INTO deb_package_origin(origin) VALUES('%s') RETURNING id" % (origin)) + return cur.fetchone()[0] + def db_add_package(con, package): cur = con.cursor() cur.execute("SAVEPOINT a") @@ -64,25 +76,13 @@ def db_add_package_instance(con, package_id, origin_id, build_result_id, version try: cur.execute("INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_result_id, version, available) VALUES(%s, %s, %s, '%s', 'true') RETURNING id" % (package_id, origin_id, build_result_id, version)) except psycopg2.DatabaseError as e: - # record already exist - this MUST NOT HAPPEN + # record already exist - this MUST NOT HAPPEN for jenkins builds cur.execute("ROLLBACK TO a") # have to rollback after failed command logger_error("FAILED: INSERT INTO deb_package_instance(deb_package_id, deb_package_origin_id, jenkins_build_result_id, version, available) VALUES(%s, %s, %s, '%s', 'true') RETURNING id" % (package_id, origin_id, build_result_id, version)) exit_error() return cur.fetchone()[0] -def db_add_origin(con, origin): - cur = con.cursor() - cur.execute("SAVEPOINT a") - try: - cur.execute("INSERT INTO deb_package_origin(origin) VALUES('%s') RETURNING id" % (origin)) - except psycopg2.DatabaseError as e: - # record already exist - cur.execute("ROLLBACK TO a") # have to rollback after failed command - cur.execute("SELECT id FROM deb_package_origin WHERE origin='%s'" % (origin)) - #logger.debug("INSERT INTO deb_package_origin(origin) VALUES('%s') RETURNING id" % (origin)) - return cur.fetchone()[0] - -def add_package_instance(origin, job_name, build_number, changes_file, version, start, end): +def add_package_instances(origin, job_name, build_number, changes_file, version, start, end): con = db_connect() #logger.debug("save %s to database" %(job_name)) db_origin_id = db_add_origin(con, origin) @@ -106,17 +106,18 @@ def add_package_instance(origin, job_name, build_number, changes_file, version, ) #logger.debug('Calling "%s":' %(' '.join(cmd))) ret = cmdobj.wait() + db_package_instance_ids=[] for line in cmdobj.stdout.readlines(): package = os.path.basename(line.strip()).split('_')[0] #logger.debug('package: %s' %(package)) db_package_id = db_add_package (con, package) #logger.debug("INSERT success package %s" % package) - db_package_instance_id = db_add_package_instance (con, db_package_id, db_origin_id, db_build_result_id, version) + db_package_instance_ids.append(db_add_package_instance (con, db_package_id, db_origin_id, db_build_result_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 + return db_package_instance_ids def add_package_version(origin, package, version): logger = logger_init() @@ -125,8 +126,6 @@ def add_package_version(origin, package, version): db_origin_id = db_add_origin(con, origin) db_package_id = db_add_package (con, package) db_package_instance_id = db_add_package_instance (con, db_package_id, db_origin_id, 'NULL', version) - logger.debug("INSERT success package version %s %s" %( package,version)) - #logger.debug('Cmd returned with status %d' %(cmdobj.returncode)) con.commit() return db_package_instance_id diff --git a/debian_build.py b/debian_build.py index f599a78..33b7e0d 100755 --- a/debian_build.py +++ b/debian_build.py @@ -573,9 +573,8 @@ if __name__ == '__main__': # FIXME: must not add development builds... or maybe? # 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/'): - package_instances=[] try: - package_instances.append(add_package_instance("profitbricks", BUILD_JOBNAME, BUILD_NUMBER, changes_file, version, BUILD_START, BUILD_END)) + package_instances = 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) diff --git a/debian_packages2db.py b/debian_packages2db.py index a32ad7e..0a12d12 100755 --- a/debian_packages2db.py +++ b/debian_packages2db.py @@ -47,7 +47,7 @@ if __name__ == '__main__': pkg_name = pkg_name.strip().split()[1] version = tmp.readline() version = version.strip().split()[1] - print 'would add %s %s %s' % (pkg_name,version,origin) + print 'adding %s %s from %s' % (pkg_name,version,origin) add_package_version(origin, pkg_name, version) os.remove(tmp.name)