blob: 79c286b469d8121f9939a37392f23ef10d49894c [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()
27
Jakub Josef79ecec32017-02-17 14:36:28 +010028 def pkg = file.split('/')[-1].split('_')[0]
29 def img = docker.image(image)
30
31 workspace = common.getWorkspace()
32 sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
33 which eatmydata || (apt-get update && apt-get install -y eatmydata) &&
34 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
35 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
36 [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list &&
37 [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || (
38 which curl || (apt-get update && apt-get install -y curl) &&
39 curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add -
40 ) &&
Filip Pytloun81c864d2017-03-21 15:19:30 +010041 apt-get update && apt-get install -y build-essential devscripts equivs sudo &&
42 groupadd -g ${jenkinsGID} jenkins &&
43 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
44 sudo -H -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
Jakub Josef79ecec32017-02-17 14:36:28 +010045 mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control
Filip Pytloun81c864d2017-03-21 15:19:30 +010046 sudo -H -u jenkins debuild --no-lintian -uc -us -b'""")
Jakub Josef79ecec32017-02-17 14:36:28 +010047}
48
49/*
50 * Build source package from directory
51 *
52 * @param dir Tree to build
53 * @param image Image name to use for build (default debian:sid)
54 * @param snapshot Generate snapshot version (default false)
55 */
56def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
57 def isGit
58 try {
59 sh("test -d ${dir}/.git")
60 isGit = true
61 } catch (Exception e) {
62 isGit = false
63 }
64
65 if (isGit == true) {
66 buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix)
67 } else {
68 buildSourceUscan(dir, image)
69 }
70}
71
72/*
73 * Build source package, fetching upstream code using uscan
74 *
75 * @param dir Tree to build
76 * @param image Image name to use for build (default debian:sid)
77 */
78def buildSourceUscan(dir, image="debian:sid") {
79 def common = new com.mirantis.mk.Common()
80 def img = docker.image(image)
81 workspace = common.getWorkspace()
82 sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
83 apt-get update && apt-get install -y build-essential devscripts &&
84 cd ${dir} && uscan --download-current-version &&
85 dpkg-buildpackage -S -nc -uc -us'""")
86}
87
88/*
89 * Build source package using git-buildpackage
90 *
91 * @param dir Tree to build
92 * @param image Image name to use for build (default debian:sid)
93 * @param snapshot Generate snapshot version (default false)
94 */
95def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
96 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +010097 def jenkinsUID = common.getJenkinsUid()
98 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +010099
100 if (! revisionPostfix) {
101 revisionPostfix = ""
102 }
103
104 def img = docker.image(image)
105 workspace = common.getWorkspace()
106 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 '
107 which eatmydata || (apt-get update && apt-get install -y eatmydata) &&
108 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
109 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
110 apt-get update && apt-get install -y build-essential git-buildpackage sudo &&
111 groupadd -g ${jenkinsGID} jenkins &&
112 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
113 cd ${dir} &&
114 sudo -H -u jenkins git config --global user.name "${gitName}" &&
115 sudo -H -u jenkins git config --global user.email "${gitEmail}" &&
116 [[ "${snapshot}" == "false" ]] || (
117 VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` &&
118 UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` &&
119 REVISION=`echo \$VERSION | cut -d "-" -f 2` &&
120 TIMESTAMP=`date +%Y%m%d%H%M` &&
121 if [[ "`cat debian/source/format`" = *quilt* ]]; then
122 UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` &&
123 UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` &&
124 NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" &&
125 NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix &&
126 echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" &&
127 sudo -H -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
128 sudo -H -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
129 else
130 NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
131 fi &&
Filip Pytloune6cfa4e2017-03-27 12:13:25 +0200132 sudo -H -u jenkins DEBFULLNAME='${gitName}' DEBEMAIL='${gitEmail}' gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100133 sudo -H -u jenkins git add -u debian/changelog &&
134 sudo -H -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
135 ) &&
chnydafa29feb2017-03-23 11:01:52 +0100136 sudo -H -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 '""")
Jakub Josef79ecec32017-02-17 14:36:28 +0100137}
138
139/*
140 * Run lintian checks
141 *
142 * @param changes Changes file to test against
143 * @param profile Lintian profile to use (default debian)
144 * @param image Image name to use for build (default debian:sid)
145 */
146def runLintian(changes, profile="debian", image="debian:sid") {
147 def common = new com.mirantis.mk.Common()
148 def img = docker.image(image)
149 workspace = common.getWorkspace()
150 sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
151 apt-get update && apt-get install -y lintian &&
152 lintian -Ii -E --pedantic --profile=${profile} ${changes}'""")
153}
chnyda4e5ac792017-03-14 15:24:18 +0100154
155/*
156 * Import gpg key
157 *
158 * @param privateKeyCredId Public key jenkins credential id
159 */
160def importGpgKey(privateKeyCredId)
161{
162 def common = new com.mirantis.mk.Common()
163 def workspace = common.getWorkspace()
164 def privKey = common.getCredentials(privateKeyCredId, "key")
165 def private_key = privKey.privateKeySource.privateKey
chnydac6846452017-03-21 16:50:43 +0100166 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
167 def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true)
168 if (retval) {
169 writeFile file:"${workspace}/private.key", text: private_key
170 sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key")
171 }
chnyda4e5ac792017-03-14 15:24:18 +0100172}
173
174/*
175 * upload source package to launchpad
176 *
177 * @param ppaRepo ppa repository on launchpad
178 * @param dirPath repository containing the source packages
179 */
180
181def uploadPpa(ppaRepo, dirPath, privateKeyCredId) {
182
183 def common = new com.mirantis.mk.Common()
184 def workspace = common.getWorkspace()
185 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
186
187 dir(dirPath)
188 {
189 def images = findFiles(glob: "*.orig*.tar.gz")
190 for (int i = 0; i < images.size(); ++i) {
191 def name = images[i].getName()
192 def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0)
193 def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0)
194 def orig_md5 = common.cutOrDie("md5sum ${name}", 0)
195 def orig_size = common.cutOrDie("ls -l ${name}", 4)
196
chnyda5d1e97f2017-03-17 15:53:47 +0100197 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 +0100198 if (retval == 0) {
199 sh("mv orig-tmp ${name}")
200 def new_sha1 = common.cutOrDie("sha1sum ${name}", 0)
201 def new_sha256 = common.cutOrDie("sha256sum ${name}", 0)
202 def new_md5 = common.cutOrDie("md5sum ${name}", 0)
203 def new_size = common.cutOrDie("ls -l ${name}", 4)
204
205 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 +0100206 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 +0100207 }
chnyda4e5ac792017-03-14 15:24:18 +0100208 }
chnyda34e5c942017-03-22 18:06:06 +0100209 sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes")
210 sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes")
chnyda4e5ac792017-03-14 15:24:18 +0100211 }
Filip Pytlounfbbd1682017-03-17 22:48:04 +0100212}