Add new pipeline to build telegraf

Change-Id: I7c083b0d8d45a396865572c3ade69c2082caebec
diff --git a/build-debian-packages-telegraf.groovy b/build-debian-packages-telegraf.groovy
new file mode 100644
index 0000000..5434ff3
--- /dev/null
+++ b/build-debian-packages-telegraf.groovy
@@ -0,0 +1,102 @@
+def common = new com.mirantis.mk.Common()
+def git = new com.mirantis.mk.Git()
+def artifactory = new com.mirantis.mk.Artifactory()
+def aptly = new com.mirantis.mk.Aptly()
+
+def timestamp = common.getDatetime()
+def version = timestamp
+
+node('docker') {
+    try{
+
+        stage("cleanup") {
+            sh("rm -rf * || true")
+        }
+
+        def workingDir = "src/github.com/influxdata"
+        stage("checkout") {
+            git.checkoutGitRepository(
+                "${workingDir}/telegraf",
+                "${SOURCE_URL}/telegraf",
+                SOURCE_BRANCH,
+                SOURCE_CREDENTIALS,
+                true,
+                30,
+                1
+            )
+        }
+
+        try {
+
+            def jenkinsUID = sh (
+                script: 'id -u',
+                returnStdout: true
+            ).trim()
+            def imgName = "${OS}-${DIST}-${ARCH}"
+            def img
+
+            stage("build image") {
+                img = docker.build(
+                    "${imgName}:${timestamp}",
+                    [
+                        "--build-arg uid=${jenkinsUID}",
+                        "--build-arg timestamp=${timestamp}",
+                        "-f ${workingDir}/telegraf/docker/${OS}-${DIST}-${ARCH}.Dockerfile",
+                        "docker"
+                    ].join(' ')
+                )
+            }
+            stage("build package") {
+                img.inside{
+                    sh("wget https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz &&
+                        tar xf go1.8.1.linux-amd64.tar.gz &&
+                        export GOROOT=$PWD/go &&
+                        export PATH=$PATH:$GOROOT/bin &&
+                        export GOPATH=$PWD &&
+                        cd src/github.com/influxdata/telegraf &&
+                        scripts/build.py --package --version=\"${version}\" --platform=linux --arch=amd64")
+                }
+                archiveArtifacts artifacts: "${workingDir}/*.deb"
+            }
+            lock("aptly-api") {
+                stage("upload") {
+                    def buildSteps = [:]
+                    def debFiles = sh script: "ls ${workingDir}/telegraf/build/*.deb", returnStdout: true
+                    def debFilesArray = debFiles.trim().tokenize()
+                    for (int i = 0; i < debFilesArray.size(); i++) {
+
+                        def debFile = debFiles[i];
+                        buildSteps[debFiles[i]] = aptly.uploadPackageStep(
+                            "${workingDir}/telegraf/build/"+debFile,
+                            APTLY_URL,
+                            APTLY_REPO,
+                            true
+                        )
+                    }
+                    parallel buildSteps
+                }
+                stage("publish") {
+                    aptly.snapshotRepo(APTLY_URL, APTLY_REPO, timestamp)
+                    aptly.publish(APTLY_URL)
+                }
+            }
+
+        } catch (Exception e) {
+            currentBuild.result = 'FAILURE'
+            println "Cleaning up docker images"
+            sh("docker images | grep -E '[-:\\ ]+${timestamp}[\\.\\ /\$]+' | awk '{print \$3}' | xargs docker rmi -f || true")
+            throw e
+        }
+
+    } catch (Throwable e) {
+       // If there was an exception thrown, the build failed
+       currentBuild.result = "FAILURE"
+       throw e
+    } finally {
+       common.sendNotification(currentBuild.result,"",["slack"])
+
+       if (currentBuild.result != 'FAILURE') {
+          sh("rm -rf *")
+       }
+    }
+}