blob: 1c9285a62103541517b7c381e245c656939c1a74 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
3/**
4 *
5 * Debian functions
6 *
7 */
8
9def cleanup(image="debian:sid") {
10 def common = new com.mirantis.mk.Common()
11 def img = docker.image(image)
12
13 workspace = common.getWorkspace()
14 sh("docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c 'rm -rf build-area || true'")
15}
16
17/*
18 * Build binary Debian package from existing dsc
19 *
20 * @param file dsc file to build
21 * @param image Image name to use for build (default debian:sid)
22 */
23def buildBinary(file, image="debian:sid", extraRepoUrl=null, extraRepoKeyUrl=null) {
24 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +010025 def jenkinsUID = common.getJenkinsUid()
26 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +010027 def pkg = file.split('/')[-1].split('_')[0]
chnyda1cf6f0d2017-06-02 11:01:04 +020028 def dockerLib = new com.mirantis.mk.Docker()
29 def imageArray = image.split(":")
30 def os = imageArray[0]
31 def dist = imageArray[1]
chnyda1c6c51f2017-06-02 11:26:36 +020032 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}", image)
chnyda1cf6f0d2017-06-02 11:01:04 +020033 def workspace = common.getWorkspace()
Jakub Josef79ecec32017-02-17 14:36:28 +010034
chnyda8ad962e2017-06-02 12:24:15 +020035 img.inside("-u root:root" ) {
chnydaa3603b42017-06-02 12:36:08 +020036 sh("""bash -c 'cd ${workspace} && (which eatmydata || (apt-get update && apt-get install -y eatmydata)) &&
Jakub Josef79ecec32017-02-17 14:36:28 +010037 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
38 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
39 [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list &&
40 [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || (
41 which curl || (apt-get update && apt-get install -y curl) &&
42 curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add -
43 ) &&
Filip Pytloun81c864d2017-03-21 15:19:30 +010044 apt-get update && apt-get install -y build-essential devscripts equivs sudo &&
45 groupadd -g ${jenkinsGID} jenkins &&
46 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
Jakub Josef6bebf162017-05-10 14:21:00 +020047 [ ! -f pre_build_script.sh ] || bash ./pre_build_script.sh &&
Filip Pytlounff82fc02017-03-27 12:17:05 +020048 sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
Jakub Josef79ecec32017-02-17 14:36:28 +010049 mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control
chnydaa3603b42017-06-02 12:36:08 +020050 sudo -H -E -u jenkins debuild --no-lintian -uc -us -b'""")
chnyda1cf6f0d2017-06-02 11:01:04 +020051 }
52
53
Jakub Josef79ecec32017-02-17 14:36:28 +010054}
55
56/*
57 * Build source package from directory
58 *
59 * @param dir Tree to build
60 * @param image Image name to use for build (default debian:sid)
61 * @param snapshot Generate snapshot version (default false)
62 */
63def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
64 def isGit
65 try {
66 sh("test -d ${dir}/.git")
67 isGit = true
68 } catch (Exception e) {
69 isGit = false
70 }
71
72 if (isGit == true) {
73 buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix)
74 } else {
75 buildSourceUscan(dir, image)
76 }
77}
78
79/*
80 * Build source package, fetching upstream code using uscan
81 *
82 * @param dir Tree to build
83 * @param image Image name to use for build (default debian:sid)
84 */
85def buildSourceUscan(dir, image="debian:sid") {
86 def common = new com.mirantis.mk.Common()
chnyda1cf6f0d2017-06-02 11:01:04 +020087 def dockerLib = new com.mirantis.mk.Docker()
88 def imageArray = image.split(":")
89 def os = imageArray[0]
90 def dist = imageArray[1]
chnyda1c6c51f2017-06-02 11:26:36 +020091 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}", image)
chnyda1cf6f0d2017-06-02 11:01:04 +020092 def workspace = common.getWorkspace()
93
chnyda8ad962e2017-06-02 12:24:15 +020094 img.inside("-u root:root" ) {
chnyda12f3b3f2017-06-02 12:19:38 +020095 sh("""cd ${workspace} && apt-get update && apt-get install -y build-essential devscripts &&
chnyda1cf6f0d2017-06-02 11:01:04 +020096 cd ${dir} && uscan --download-current-version &&
97 dpkg-buildpackage -S -nc -uc -us""")
98 }
Jakub Josef79ecec32017-02-17 14:36:28 +010099}
100
101/*
102 * Build source package using git-buildpackage
103 *
104 * @param dir Tree to build
105 * @param image Image name to use for build (default debian:sid)
106 * @param snapshot Generate snapshot version (default false)
107 */
Filip Pytlounff82fc02017-03-27 12:17:05 +0200108def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="") {
Jakub Josef79ecec32017-02-17 14:36:28 +0100109 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +0100110 def jenkinsUID = common.getJenkinsUid()
111 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +0100112
113 if (! revisionPostfix) {
114 revisionPostfix = ""
115 }
116
chnyda1cf6f0d2017-06-02 11:01:04 +0200117 def workspace = common.getWorkspace()
118 def dockerLib = new com.mirantis.mk.Docker()
119 def imageArray = image.split(":")
120 def os = imageArray[0]
121 def dist = imageArray[1]
chnyda1c6c51f2017-06-02 11:26:36 +0200122 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}", image)
chnyda1cf6f0d2017-06-02 11:01:04 +0200123
chnyda8ad962e2017-06-02 12:24:15 +0200124 img.inside("-u root:root") {
chnyda1cf6f0d2017-06-02 11:01:04 +0200125
126 withEnv(["DEBIAN_FRONTEND=noninteractive", "DEBFULLNAME='${gitName}'", "DEBEMAIL='${gitEmail}'"]) {
chnydaa3603b42017-06-02 12:36:08 +0200127 sh("""bash -c 'cd ${workspace} && (which eatmydata || (apt-get update && apt-get install -y eatmydata)) &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100128 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
129 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
Jakub Josef5de05f62017-05-30 15:25:55 +0200130 apt-get update && apt-get install -y build-essential git-buildpackage dpkg-dev sudo &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100131 groupadd -g ${jenkinsGID} jenkins &&
132 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
133 cd ${dir} &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200134 sudo -H -E -u jenkins git config --global user.name "${gitName}" &&
135 sudo -H -E -u jenkins git config --global user.email "${gitEmail}" &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100136 [[ "${snapshot}" == "false" ]] || (
137 VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` &&
138 UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` &&
139 REVISION=`echo \$VERSION | cut -d "-" -f 2` &&
140 TIMESTAMP=`date +%Y%m%d%H%M` &&
141 if [[ "`cat debian/source/format`" = *quilt* ]]; then
142 UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` &&
143 UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` &&
144 NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" &&
145 NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix &&
146 echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200147 sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
148 sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
Jakub Josef79ecec32017-02-17 14:36:28 +0100149 else
150 NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
151 fi &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200152 sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
153 sudo -H -E -u jenkins git add -u debian/changelog &&
154 sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
Jakub Josef79ecec32017-02-17 14:36:28 +0100155 ) &&
chnydaa3603b42017-06-02 12:36:08 +0200156 sudo -H -E -u jenkins gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -sa -S -uc -us '""")
chnyda1cf6f0d2017-06-02 11:01:04 +0200157 }
158 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100159}
160
161/*
162 * Run lintian checks
163 *
164 * @param changes Changes file to test against
165 * @param profile Lintian profile to use (default debian)
166 * @param image Image name to use for build (default debian:sid)
167 */
168def runLintian(changes, profile="debian", image="debian:sid") {
169 def common = new com.mirantis.mk.Common()
chnyda1cf6f0d2017-06-02 11:01:04 +0200170 def workspace = common.getWorkspace()
171 def dockerLib = new com.mirantis.mk.Docker()
172 def imageArray = image.split(":")
173 def os = imageArray[0]
174 def dist = imageArray[1]
chnyda1c6c51f2017-06-02 11:26:36 +0200175 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}", image)
chnyda8ad962e2017-06-02 12:24:15 +0200176 img.inside("-u root:root") {
chnyda12f3b3f2017-06-02 12:19:38 +0200177 sh("""cd ${workspace} && apt-get update && apt-get install -y lintian &&
chnyda1cf6f0d2017-06-02 11:01:04 +0200178 lintian -Ii -E --pedantic --profile=${profile} ${changes}""")
179 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100180}
chnyda4e5ac792017-03-14 15:24:18 +0100181
182/*
183 * Import gpg key
184 *
185 * @param privateKeyCredId Public key jenkins credential id
186 */
187def importGpgKey(privateKeyCredId)
188{
189 def common = new com.mirantis.mk.Common()
190 def workspace = common.getWorkspace()
191 def privKey = common.getCredentials(privateKeyCredId, "key")
192 def private_key = privKey.privateKeySource.privateKey
chnydac6846452017-03-21 16:50:43 +0100193 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
194 def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true)
195 if (retval) {
196 writeFile file:"${workspace}/private.key", text: private_key
197 sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key")
198 }
chnyda4e5ac792017-03-14 15:24:18 +0100199}
200
201/*
202 * upload source package to launchpad
203 *
204 * @param ppaRepo ppa repository on launchpad
205 * @param dirPath repository containing the source packages
206 */
207
208def uploadPpa(ppaRepo, dirPath, privateKeyCredId) {
209
210 def common = new com.mirantis.mk.Common()
211 def workspace = common.getWorkspace()
212 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
213
214 dir(dirPath)
215 {
216 def images = findFiles(glob: "*.orig*.tar.gz")
217 for (int i = 0; i < images.size(); ++i) {
218 def name = images[i].getName()
219 def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0)
220 def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0)
221 def orig_md5 = common.cutOrDie("md5sum ${name}", 0)
222 def orig_size = common.cutOrDie("ls -l ${name}", 4)
223
chnyda5d1e97f2017-03-17 15:53:47 +0100224 def retval = sh(script: "wget --quiet -O orig-tmp https://launchpad.net/ubuntu/+archive/primary/+files/${name}", returnStatus: true)
chnyda4e5ac792017-03-14 15:24:18 +0100225 if (retval == 0) {
226 sh("mv orig-tmp ${name}")
227 def new_sha1 = common.cutOrDie("sha1sum ${name}", 0)
228 def new_sha256 = common.cutOrDie("sha256sum ${name}", 0)
229 def new_md5 = common.cutOrDie("md5sum ${name}", 0)
230 def new_size = common.cutOrDie("ls -l ${name}", 4)
231
232 sh("sed -i -e s,$orig_sha1,$new_sha1,g -e s,$orig_sha256,$new_sha256,g -e s,$orig_size,$new_size,g -e s,$orig_md5,$new_md5,g *.dsc")
chnyda3c93ff62017-03-23 10:11:36 +0100233 sh("sed -i -e s,$orig_sha1,$new_sha1,g -e s,$orig_sha256,$new_sha256,g -e s,$orig_size,$new_size,g -e s,$orig_md5,$new_md5,g *_source.changes")
chnyda4e5ac792017-03-14 15:24:18 +0100234 }
chnyda4e5ac792017-03-14 15:24:18 +0100235 }
chnyda34e5c942017-03-22 18:06:06 +0100236 sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes")
237 sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes")
chnyda4e5ac792017-03-14 15:24:18 +0100238 }
Filip Pytlounfbbd1682017-03-17 22:48:04 +0100239}