]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
update all 5 tables
authorHolger Levsen <holger@layer-acht.org>
Thu, 20 Sep 2012 14:49:13 +0000 (16:49 +0200)
committerHolger Levsen <holger@layer-acht.org>
Thu, 20 Sep 2012 14:49:13 +0000 (16:49 +0200)
debian_build.py

index 367a7c48f30a069e8f4c30524861c10a8fb9424c..4bb74082b2dcc21cbdb1eb6b41622fcc2fe28194 100755 (executable)
@@ -30,9 +30,9 @@ from ConfigParser import SafeConfigParser
 JOB_URL = 'http://jenkins:80/job/%(flavour)s/buildWithParameters?token=BuildIt&'
 JOB_DELAY = '0sec'
 
-DB_USER = "liveboot"
-DB_PASSWD = "liveboot"
-DB_NAME = "liveboot"
+DB_USER = "cidb"
+DB_PASSWD = "cidb"
+DB_NAME = "cidb"
 DB_HOST = "sagunt"
 DB_PORT = 5432
 
@@ -57,6 +57,7 @@ BUILD_ID = os.environ.get('BUILD_ID')
 BUILD_URL = os.environ.get('BUILD_URL')
 BUILD_AREA = os.path.abspath(os.path.join(os.curdir,'../build-area'))
 BUILD_JOBNAME = os.environ.get('JOB_NAME')
+BUILD_START = datetime.datetime.now()
 
 GIT_REPO_PATH = os.environ.get('GIT_REPO_PATH')
 GIT_REPO_NAME = os.path.basename(GIT_REPO_PATH)
@@ -124,31 +125,98 @@ 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")
+    try:
+        cur.execute("INSERT INTO jenkins_job(name) VALUES('%s') RETURNING id" % (name))
+    except psycopg2.DatabaseError as e:
+    # record already exist
+        cur.execute("ROLLBACK TO a")  # have to rollback after failed command
+        cur.execute("SELECT id FROM jenkins_job WHERE name='%s'" % (name))
+        #logger.debug("INSERT INTO jenkins_job(name) VALUES('%s')" % (name))
+    return cur.fetchone()[0]
 
+def db_add_build(con, number, db_id, start, end):
+    cur = con.cursor()
+    cur.execute("SAVEPOINT a")
+    try:
+        cur.execute("INSERT INTO jenkins_build(jenkins_build_number, jenkins_job_id, build_start, build_end, successful) VALUES(%s, %s, '%s', '%s', %s) RETURNING id" % (number, db_id, start, end, 'true'))
+    except psycopg2.DatabaseError as e:
+        cur.execute("ROLLBACK TO a")  # have to rollback after failed command
+        #logger.debug("INSERT INTO jenkins_build(jenkins_build_number, jenkins_job_id, build_start, build_end, successful) VALUES(%s, %s, '%s', '%s', %s) RETURNING id" % (number, db_id, start, end, 'true'))
+    return cur.fetchone()[0]
 
-def db_add_job(con, name):
+def db_add_package(con, package):
     cur = con.cursor()
     cur.execute("SAVEPOINT a")
     try:
-        cur.execute("INSERT INTO jenkins_job(name) VALUES('%s')" % name)
-        logger.debug("saved %s to databased" %(name))
+        cur.execute("INSERT INTO deb_package(name) VALUES('%s') RETURNING id" % (package))
     except psycopg2.DatabaseError as e:
     # record already exist
         cur.execute("ROLLBACK TO a")  # have to rollback after failed command
-        logger.debug("could not save %s to databased" %(name))
+        cur.execute("SELECT id FROM deb_package WHERE name='%s'" % (package))
+        #logger.debug("INSERT INTO deb_package(name) VALUES('%s') RETURNING id" % (package))
+    return cur.fetchone()[0]
+
+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)) 
+    except psycopg2.DatabaseError as e:
+    # record already exist
+        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)) 
+
+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("profitbricks", $BUILD_JOBNAME, $BUILD_NUMBER, changes_file)
-def add_package_instance(origin, job_name, job_id, changes_file, version):
+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))
-    db_add_job(con, job_name) 
-    # add jenkins_build
-    # for all packages in changes file
-      # add deb_package (if not exist)
-      # add deb_package_instance
-    # must not add development builds... or maybe?
+    #logger.debug("save %s to database" %(job_name))
+    db_origin_id = db_add_origin(con, origin) 
+    #logger.debug("INSERT success origin %s" % origin)
+    db_job_id = db_add_job(con, job_name) 
+    #logger.debug("INSERT success job %s" % job_name)
+    db_build_id = db_add_build (con, build_number, db_job_id, start, end)
+    #logger.debug("INSERT success build number %s" % build_number)
+    #logger.debug("processing %s" %(changes_file))
+    cmd = ["dcmd", '--deb', changes_file]
+    cmdobj = subprocess.Popen(
+            cmd,
+            shell=False,
+            cwd='/',
+            close_fds=True,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT,
+            env={'':''}
+    )
+    #logger.debug('Calling "%s":' %(' '.join(cmd)))
+    ret = cmdobj.wait()
+    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_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))
+    # FIXME: must not add development builds... or maybe?
        # cidb wise, we only care about builds from master
        #if GIT_BRANCH_NAME == 'master' or GIT_BRANCH_NAME.startswith('hotfix/'):
+    con.commit()
+    logger.info("CIDB update OK.")
 
 def dput_package_upload(changes_path):
     try:
@@ -620,12 +688,14 @@ if __name__ == '__main__':
     logger.info('---------------------------------------------------------------------------------------------------------')
     cmd = ['figlet-figlet', '-t', 'Success!!!']
     subprocess.check_call(cmd)
+    BUILD_END = datetime.datetime.now()
     try:
-         add_package_instance("profitbricks", BUILD_JOBNAME, BUILD_NUMBER, changes_file, version)
+         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)
-        exit_error()
+        #FIXME: this should really cause an error
+        # exit_error()
 
     # finally
     exit_ok()