--- /dev/null
+#!/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 <fabian.holler@profitbricks.com>"
+
+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)
--- /dev/null
+#!/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)
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
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'
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")
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()
#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))
#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:
# 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()