pipeline {
    environment {
        service_name = 'metrics'
        build_file = 'docker-compose.yml'
        deployment_file = 'docker-compose-prod.yml'
        DOCKERFILE = 'Dockerfile'
        APP_NAME = 'prometheus'
        APP_PORT = '80'
        WORKDIR = 'dockerfiles/services/prometheus'
        DOCKER_HOST = 'dkhost:2376'
    }

    agent { label 'docker' }
    options {
        buildDiscarder(logRotator(numToKeepStr: '6'))
    }
    triggers {
        cron('@monthly')
    }

    stages {
        stage('prepare') {
            steps {
                checkout scm
//                 git credentialsId: 'f1f58215-c789-44a2-9b72-50e4425cb061', url: 'ssh://git@git.xai-corp.net:10022/xai-corp.net/provisioning.git'

                sh 'ls'
            }
        }

        stage('build') {
            steps {
                dir(WORKDIR) {
                    script {
                        docker.withRegistry('http://dkregistry.xai-corp.net:5000', 'b11d7f1a-81ac-4daf-8842-56afc0d2370e') {
                            def customImage = docker.build("dkregistry.xai-corp.net:5000/xaicorp/${app_name}:latest", "-f Dockerfile .")
                            customImage.push()
                        }
                        withDockerRegistry(credentialsId: 'b11d7f1a-81ac-4daf-8842-56afc0d2370e', url: 'http://dkregistry.xai-corp.net:5000') {
                            sh "docker-compose -f ${deployment_file} pull"
                        }
                    }
                }
            }
        }

        stage('deploy') {
            steps {
                dir(WORKDIR) {
                    sh """
                        export DOCKER_HOST=${DOCKER_HOST}
                        docker stack deploy --with-registry-auth -c ${deployment_file} ${service_name}
                        (cd ../ && chmod +x ./scaleout.sh && ./scaleout.sh ${service_name}_prometheus 60) &
                        (cd ../ && chmod +x ./scaleout.sh && ./scaleout.sh ${service_name}_statsd_exporter 60) &
                        (cd ../ && chmod +x ./scaleout.sh && ./scaleout.sh ${service_name}_fluentd_exporter 60) &
                        (cd ../ && chmod +x ./scaleout.sh && ./scaleout.sh ${service_name}_node_exporter 60) &
                        (cd ../ && chmod +x ./scaleout.sh && ./scaleout.sh ${service_name}_graphana 60) &

                        wait
                    """
                }
            }
        }

        stage('smoke tests') {
            steps {
                dir(WORKDIR) {
                    sleep 10
                    sh "curl -If https://metrics.xai-corp.net/"
                }
            }
            post {
                failure {
                    dir(WORKDIR) {
                        sh """
                            export DOCKER_HOST=${DOCKER_HOST}
                            docker service rollback ${service_name}_graphana
                        """
                    }
                }
            }
        }

    }
}
