pipeline {
  agent {
    label 'pi501.in.thelinuxpro.net'
  }

  stages {
    stage("verify tooling") {
      steps {
	sh '''
        docker version
	docker info
        docker compose version
        '''
      }
    }


    stage("Build Containers and push to registry") {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'docker images | grep pihole | grep $(grep image docker-compose.yml  | awk -F\':\' \'{ print $4 }\')')

	  if (status != 0) {
	    sh 'docker compose build --push'
	  } else {
	    echo "Primary Image version already exists, no need to rebuild."
	  }
	}
      }
    }

    stage('Create contexts') {
      steps {
    	sh 'docker context ls | grep pi502 || docker context create pi502 --docker "host=ssh://pi502.in.thelinuxpro.net"'
    	sh 'docker context ls | grep pi503 || docker context create pi503 --docker "host=ssh://pi503.in.thelinuxpro.net"'
      }
    }

    stage('Stop pihole1') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker compose down'
	    sh 'sleep 5'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }

    stage('Start pihole1') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker compose up pihole1 -d'
	    sh 'docker compose ps'
            sh 'sleep 3'
	    sh 'docker compose logs'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }

    stage('Stop pihole2') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker --context pi502 compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker --context pi502 compose down'
	    sh 'sleep 5'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }

    stage('Start pihole2') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker --context pi502 compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker --context pi502 compose up pihole2 -d'
	    sh 'docker --context pi502 compose ps'
            sh 'sleep 3'
	    sh 'docker --context pi502 compose logs'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }

    stage('Stop pihole3') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker --context pi503 compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker --context pi503 compose down'
	    sh 'sleep 5'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }

    stage('Start pihole3') {
      steps {
	script {
	  def status = sh(returnStatus: true, script: 'grep $(docker --context pi503 compose ps | tail -n1 | awk \'{ print $2 }\') docker-compose.yml')

	  if (status != 0) {
	    sh 'docker --context pi503 compose up pihole3 -d'
	    sh 'docker --context pi503 compose ps'
            sh 'sleep 3'
	    sh 'docker --context pi503 compose logs'
	  } else {
	    echo 'skip'
	  }
	}
      }
    }
//    stage('Integration Tests') {
//      steps {
//	sh 'mkdir -p test/results/integration'
//	sh 'bats -F junit test/integration/dns/thelinux.pro/*.bat test/integration/dns/thelinux.pro/**/*.bat > test/results/integration/TEST-Integration-dns-thelinux_pro.xml'
//	sh 'bats -F junit test/integration/dns/kameronkenny.com/*.bat test/integration/dns/kameronkenny.com/**/*.bat > test/results/integration/TEST-Integration-dns-kameronkenny_com.xml'
//	sh 'bats -F junit test/integration/dns/in.thelinuxpro.net/*.bat test/integration/dns/in.thelinuxpro.net/**/*.bat > test/results/integration/TEST-Integration-dns-in_thelinuxpro_net.xml'
//     }
//   }
  }

//  post {
//    always {
//      archiveArtifacts artifacts: 'test/results/**/*.xml', fingerprint: true
//      junit 'test/results/**/*.xml'
//      sh 'rm -rf test/results'
//    }
//  }
}
