| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk | 
 | 2 |  | 
 | 3 | /** | 
 | 4 |  * | 
 | 5 |  * Debian functions | 
 | 6 |  * | 
 | 7 |  */ | 
 | 8 |  | 
 | 9 | def 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 |  */ | 
 | 23 | def buildBinary(file, image="debian:sid", extraRepoUrl=null, extraRepoKeyUrl=null) { | 
 | 24 |     def common = new com.mirantis.mk.Common() | 
| Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 25 |     def jenkinsUID = common.getJenkinsUid() | 
 | 26 |     def jenkinsGID = common.getJenkinsGid() | 
 | 27 |  | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 28 |     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 Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 41 |             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 && | 
| Jakub Josef | 6bebf16 | 2017-05-10 14:21:00 +0200 | [diff] [blame] | 44 |             [ ! -f pre_build_script.sh ] || bash ./pre_build_script.sh && | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 45 |             sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} && | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 46 |             mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 47 |             sudo -H -E -u jenkins debuild --no-lintian -uc -us -b'""") | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 48 | } | 
 | 49 |  | 
 | 50 | /* | 
 | 51 |  * Build source package from directory | 
 | 52 |  * | 
 | 53 |  * @param dir   Tree to build | 
 | 54 |  * @param image Image name to use for build (default debian:sid) | 
 | 55 |  * @param snapshot Generate snapshot version (default false) | 
 | 56 |  */ | 
 | 57 | def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") { | 
 | 58 |     def isGit | 
 | 59 |     try { | 
 | 60 |         sh("test -d ${dir}/.git") | 
 | 61 |         isGit = true | 
 | 62 |     } catch (Exception e) { | 
 | 63 |         isGit = false | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |     if (isGit == true) { | 
 | 67 |         buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix) | 
 | 68 |     } else { | 
 | 69 |         buildSourceUscan(dir, image) | 
 | 70 |     } | 
 | 71 | } | 
 | 72 |  | 
 | 73 | /* | 
 | 74 |  * Build source package, fetching upstream code using uscan | 
 | 75 |  * | 
 | 76 |  * @param dir   Tree to build | 
 | 77 |  * @param image Image name to use for build (default debian:sid) | 
 | 78 |  */ | 
 | 79 | def buildSourceUscan(dir, image="debian:sid") { | 
 | 80 |     def common = new com.mirantis.mk.Common() | 
 | 81 |     def img = docker.image(image) | 
 | 82 |     workspace = common.getWorkspace() | 
 | 83 |     sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c ' | 
 | 84 |             apt-get update && apt-get install -y build-essential devscripts && | 
 | 85 |             cd ${dir} && uscan --download-current-version && | 
 | 86 |             dpkg-buildpackage -S -nc -uc -us'""") | 
 | 87 | } | 
 | 88 |  | 
 | 89 | /* | 
 | 90 |  * Build source package using git-buildpackage | 
 | 91 |  * | 
 | 92 |  * @param dir   Tree to build | 
 | 93 |  * @param image Image name to use for build (default debian:sid) | 
 | 94 |  * @param snapshot Generate snapshot version (default false) | 
 | 95 |  */ | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 96 | def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="") { | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 97 |     def common = new com.mirantis.mk.Common() | 
| Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 98 |     def jenkinsUID = common.getJenkinsUid() | 
 | 99 |     def jenkinsGID = common.getJenkinsGid() | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 100 |  | 
 | 101 |     if (! revisionPostfix) { | 
 | 102 |         revisionPostfix = "" | 
 | 103 |     } | 
 | 104 |  | 
 | 105 |     def img = docker.image(image) | 
 | 106 |     workspace = common.getWorkspace() | 
 | 107 |     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 ' | 
 | 108 |             which eatmydata || (apt-get update && apt-get install -y eatmydata) && | 
 | 109 |             export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata && | 
 | 110 |             export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so && | 
| Jakub Josef | 5de05f6 | 2017-05-30 15:25:55 +0200 | [diff] [blame] | 111 |             apt-get update && apt-get install -y build-essential git-buildpackage dpkg-dev sudo && | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 112 |             groupadd -g ${jenkinsGID} jenkins && | 
 | 113 |             useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins && | 
 | 114 |             cd ${dir} && | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 115 |             sudo -H -E -u jenkins git config --global user.name "${gitName}" && | 
 | 116 |             sudo -H -E -u jenkins git config --global user.email "${gitEmail}" && | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 117 |             [[ "${snapshot}" == "false" ]] || ( | 
 | 118 |                 VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` && | 
 | 119 |                 UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` && | 
 | 120 |                 REVISION=`echo \$VERSION | cut -d "-" -f 2` && | 
 | 121 |                 TIMESTAMP=`date +%Y%m%d%H%M` && | 
 | 122 |                 if [[ "`cat debian/source/format`" = *quilt* ]]; then | 
 | 123 |                     UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` && | 
 | 124 |                     UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` && | 
 | 125 |                     NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" && | 
 | 126 |                     NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix && | 
 | 127 |                     echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" && | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 128 |                     sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH && | 
 | 129 |                     sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 130 |                 else | 
 | 131 |                     NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix | 
 | 132 |                 fi && | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 133 |                 sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution && | 
 | 134 |                 sudo -H -E -u jenkins git add -u debian/changelog && | 
 | 135 |                 sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION" | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 136 |             ) && | 
| Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 137 |             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 '""") | 
| Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 138 | } | 
 | 139 |  | 
 | 140 | /* | 
 | 141 |  * Run lintian checks | 
 | 142 |  * | 
 | 143 |  * @param changes   Changes file to test against | 
 | 144 |  * @param profile   Lintian profile to use (default debian) | 
 | 145 |  * @param image     Image name to use for build (default debian:sid) | 
 | 146 |  */ | 
 | 147 | def runLintian(changes, profile="debian", image="debian:sid") { | 
 | 148 |     def common = new com.mirantis.mk.Common() | 
 | 149 |     def img = docker.image(image) | 
 | 150 |     workspace = common.getWorkspace() | 
 | 151 |     sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c ' | 
 | 152 |             apt-get update && apt-get install -y lintian && | 
 | 153 |             lintian -Ii -E --pedantic --profile=${profile} ${changes}'""") | 
 | 154 | } | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 155 |  | 
 | 156 | /* | 
 | 157 |  * Import gpg key | 
 | 158 |  * | 
 | 159 |  * @param privateKeyCredId   Public key jenkins credential id | 
 | 160 |  */ | 
 | 161 | def importGpgKey(privateKeyCredId) | 
 | 162 | { | 
 | 163 |     def common = new com.mirantis.mk.Common() | 
 | 164 |     def workspace = common.getWorkspace() | 
 | 165 |     def privKey = common.getCredentials(privateKeyCredId, "key") | 
 | 166 |     def private_key = privKey.privateKeySource.privateKey | 
| chnyda | c684645 | 2017-03-21 16:50:43 +0100 | [diff] [blame] | 167 |     def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username | 
 | 168 |     def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true) | 
 | 169 |     if (retval) { | 
 | 170 |         writeFile file:"${workspace}/private.key", text: private_key | 
 | 171 |         sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key") | 
 | 172 |     } | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 173 | } | 
 | 174 |  | 
 | 175 | /* | 
 | 176 |  * upload source package to launchpad | 
 | 177 |  * | 
 | 178 |  * @param ppaRepo   ppa repository on launchpad | 
 | 179 |  * @param dirPath repository containing the source packages | 
 | 180 |  */ | 
 | 181 |  | 
 | 182 | def uploadPpa(ppaRepo, dirPath, privateKeyCredId) { | 
 | 183 |  | 
 | 184 |    def common = new com.mirantis.mk.Common() | 
 | 185 |    def workspace = common.getWorkspace() | 
 | 186 |    def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username | 
 | 187 |  | 
 | 188 |    dir(dirPath) | 
 | 189 |    { | 
 | 190 |        def images = findFiles(glob: "*.orig*.tar.gz") | 
 | 191 |        for (int i = 0; i < images.size(); ++i) { | 
 | 192 |           def name = images[i].getName() | 
 | 193 |           def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0) | 
 | 194 |           def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0) | 
 | 195 |           def orig_md5 = common.cutOrDie("md5sum ${name}", 0) | 
 | 196 |           def orig_size = common.cutOrDie("ls -l ${name}", 4) | 
 | 197 |  | 
| chnyda | 5d1e97f | 2017-03-17 15:53:47 +0100 | [diff] [blame] | 198 |           def retval = sh(script: "wget --quiet -O orig-tmp https://launchpad.net/ubuntu/+archive/primary/+files/${name}", returnStatus: true) | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 199 |           if (retval == 0) { | 
 | 200 |              sh("mv orig-tmp ${name}") | 
 | 201 |              def new_sha1 = common.cutOrDie("sha1sum ${name}", 0) | 
 | 202 |              def new_sha256 = common.cutOrDie("sha256sum ${name}", 0) | 
 | 203 |              def new_md5 = common.cutOrDie("md5sum ${name}", 0) | 
 | 204 |              def new_size = common.cutOrDie("ls -l ${name}", 4) | 
 | 205 |  | 
 | 206 |              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") | 
| chnyda | 3c93ff6 | 2017-03-23 10:11:36 +0100 | [diff] [blame] | 207 |              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") | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 208 |           } | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 209 |         } | 
| chnyda | 34e5c94 | 2017-03-22 18:06:06 +0100 | [diff] [blame] | 210 |         sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes") | 
 | 211 |         sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes") | 
| chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 212 |     } | 
| Filip Pytloun | fbbd168 | 2017-03-17 22:48:04 +0100 | [diff] [blame] | 213 | } |