def common = new com.mirantis.mk.Common()
def git = new com.mirantis.mk.Git()
def aptly = new com.mirantis.mk.Aptly()
def dockerLib = new com.mirantis.mk.Docker()

def timestamp = common.getDatetime()
def version = "0.1~${timestamp}"

node('docker') {
    try{

        stage("cleanup") {
            sh("rm -rf * || true")
        }

        stage("checkout") {
            git.checkoutGitRepository(
                "libvirt-exporter-${version}",
                "${SOURCE_URL}",
                SOURCE_BRANCH,
                SOURCE_CREDENTIALS,
                true,
                30,
                1
            )
        }

        stage("build binary") {
            dir("libvirt-exporter-${version}") {
                sh("sed -i 's/VERSION/${version}/g' debian/changelog && ./build_static.sh")
            }
        }

        def img = dockerLib.getImage("tcpcloud/debian-build-ubuntu-${DIST}")
        stage("build package") {
            img.inside("-u root:root") {
                sh("apt-get update && apt-get install ruby ruby-dev && gem install fpm")
                sh("cd libvirt-exporter-${version} && scripts/build.py --package --version=\"${version}\" --platform=linux --arch=amd64")
            }
            archiveArtifacts artifacts: "libvirt-exporter-${version}/build/*.deb"
        }

        if (UPLOAD_APTLY.toBoolean()) {
            lock("aptly-api") {
                stage("upload") {
                    def buildSteps = [:]
                    def debFiles = sh(script: "ls libvirt-exporter-${version}/build/*.deb", returnStdout: true)
                    def debFilesArray = debFiles.trim().tokenize()
                    def workspace = common.getWorkspace()
                    for (int i = 0; i < debFilesArray.size(); i++) {
                        def debFile = debFilesArray[i];
                        buildSteps[debFiles[i]] = aptly.uploadPackageStep(
                            "${workspace}/"+debFile,
                            APTLY_URL,
                            APTLY_REPO,
                            true
                        )
                    }
                    parallel buildSteps
                }
                stage("publish") {
                    aptly.snapshotRepo(APTLY_URL, APTLY_REPO, timestamp)
                    aptly.publish(APTLY_URL)
                }
            }
        }

        img.inside("-u root:root") {
            sh("rm -rf * || true")
        }

    } catch (Throwable e) {
       // If there was an exception thrown, the build failed
       currentBuild.result = "FAILURE"
       currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
       throw e
    } finally {
       common.sendNotification(currentBuild.result,"",["slack"])

       if (currentBuild.result != 'FAILURE') {
          sh("rm -rf *")
       }
    }
}
