MCP pipeline library merged with ccp-pipeline-libs repo.
Change-Id: I74080e18c5a482b7bf44b5516228b7bfe1fe3586
diff --git a/src/com/mirantis/mk/Debian.groovy b/src/com/mirantis/mk/Debian.groovy
new file mode 100644
index 0000000..acd225a
--- /dev/null
+++ b/src/com/mirantis/mk/Debian.groovy
@@ -0,0 +1,154 @@
+package com.mirantis.mk
+
+/**
+ *
+ * Debian functions
+ *
+ */
+
+def cleanup(image="debian:sid") {
+ def common = new com.mirantis.mk.Common()
+ def img = docker.image(image)
+
+ workspace = common.getWorkspace()
+ sh("docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c 'rm -rf build-area || true'")
+}
+
+/*
+ * Build binary Debian package from existing dsc
+ *
+ * @param file dsc file to build
+ * @param image Image name to use for build (default debian:sid)
+ */
+def buildBinary(file, image="debian:sid", extraRepoUrl=null, extraRepoKeyUrl=null) {
+ def common = new com.mirantis.mk.Common()
+ def pkg = file.split('/')[-1].split('_')[0]
+ def img = docker.image(image)
+
+ workspace = common.getWorkspace()
+ sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
+ which eatmydata || (apt-get update && apt-get install -y eatmydata) &&
+ export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
+ export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
+ [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list &&
+ [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || (
+ which curl || (apt-get update && apt-get install -y curl) &&
+ curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add -
+ ) &&
+ apt-get update && apt-get install -y build-essential devscripts equivs &&
+ dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
+ mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control
+ debuild --no-lintian -uc -us -b'""")
+}
+
+/*
+ * Build source package from directory
+ *
+ * @param dir Tree to build
+ * @param image Image name to use for build (default debian:sid)
+ * @param snapshot Generate snapshot version (default false)
+ */
+def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
+ def isGit
+ try {
+ sh("test -d ${dir}/.git")
+ isGit = true
+ } catch (Exception e) {
+ isGit = false
+ }
+
+ if (isGit == true) {
+ buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix)
+ } else {
+ buildSourceUscan(dir, image)
+ }
+}
+
+/*
+ * Build source package, fetching upstream code using uscan
+ *
+ * @param dir Tree to build
+ * @param image Image name to use for build (default debian:sid)
+ */
+def buildSourceUscan(dir, image="debian:sid") {
+ def common = new com.mirantis.mk.Common()
+ def img = docker.image(image)
+ workspace = common.getWorkspace()
+ sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
+ apt-get update && apt-get install -y build-essential devscripts &&
+ cd ${dir} && uscan --download-current-version &&
+ dpkg-buildpackage -S -nc -uc -us'""")
+}
+
+/*
+ * Build source package using git-buildpackage
+ *
+ * @param dir Tree to build
+ * @param image Image name to use for build (default debian:sid)
+ * @param snapshot Generate snapshot version (default false)
+ */
+def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
+ def common = new com.mirantis.mk.Common()
+ def jenkinsUID = sh (
+ script: 'id -u',
+ returnStdout: true
+ ).trim()
+ def jenkinsGID = sh (
+ script: 'id -g',
+ returnStdout: true
+ ).trim()
+
+ if (! revisionPostfix) {
+ revisionPostfix = ""
+ }
+
+ def img = docker.image(image)
+ workspace = common.getWorkspace()
+ sh("""docker run -e DEBIAN_FRONTEND=noninteractive -e DEBFULLNAME='${gitName}' -e DEBEMAIL='${gitEmail}' -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -exc '
+ which eatmydata || (apt-get update && apt-get install -y eatmydata) &&
+ export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
+ export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
+ apt-get update && apt-get install -y build-essential git-buildpackage sudo &&
+ groupadd -g ${jenkinsGID} jenkins &&
+ useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
+ cd ${dir} &&
+ sudo -H -u jenkins git config --global user.name "${gitName}" &&
+ sudo -H -u jenkins git config --global user.email "${gitEmail}" &&
+ [[ "${snapshot}" == "false" ]] || (
+ VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` &&
+ UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` &&
+ REVISION=`echo \$VERSION | cut -d "-" -f 2` &&
+ TIMESTAMP=`date +%Y%m%d%H%M` &&
+ if [[ "`cat debian/source/format`" = *quilt* ]]; then
+ UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` &&
+ UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` &&
+ NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" &&
+ NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix &&
+ echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" &&
+ sudo -H -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
+ sudo -H -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
+ else
+ NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
+ fi &&
+ sudo -H -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
+ sudo -H -u jenkins git add -u debian/changelog &&
+ sudo -H -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
+ ) &&
+ gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -S -uc -us'""")
+}
+
+/*
+ * Run lintian checks
+ *
+ * @param changes Changes file to test against
+ * @param profile Lintian profile to use (default debian)
+ * @param image Image name to use for build (default debian:sid)
+ */
+def runLintian(changes, profile="debian", image="debian:sid") {
+ def common = new com.mirantis.mk.Common()
+ def img = docker.image(image)
+ workspace = common.getWorkspace()
+ sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
+ apt-get update && apt-get install -y lintian &&
+ lintian -Ii -E --pedantic --profile=${profile} ${changes}'""")
+}