Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pipeline {
- agent { label 'SDK Linux CI (CentOS 7)' }
- parameters { booleanParam(name: 'FORCE_BUILD', defaultValue: false, description: 'Force LDAP image build') }
- stages {
- stage('Build and run LDAP') {
- steps {
- script {
- withCredentials([usernamePassword(credentialsId: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
- sh "curl --insecure -u \"$USERNAME:$PASSWORD\" -L -o Dockerfile https://mysrever/dap/Dockerfile"
- }
- buildAndRun("ldap:latest")
- }
- }
- }
- }
- }
- def buildAndRun(String imageName, int certThresholdDays = 30) {
- def forceBuild = params.FORCE_BUILD ?: false
- // Check if image exists
- def imageExists = sh(
- script: "docker images -q ${imageName}",
- returnStdout: true
- ).trim() != ""
- // Check if cert expiring soon
- def certExpiringSoon = {
- try {
- def nowUtc = sh(script: "date +%s", returnStdout: true).trim().toLong()
- def certExpiryUtc = sh(
- script: "docker run --rm --entrypoint cat ${imageName} /artifacts/expiry_timestamp",
- returnStdout: true
- ).trim().toLong()
- def daysLeft = (certExpiryUtc - nowUtc) / 86400
- echo "Cert expires in ${daysLeft} days"
- return daysLeft < certThresholdDays
- } catch (Exception e) {
- echo "No cert info found in ${imageName}, assuming rebuild needed"
- return true
- }
- }
- def opts = [["valid", "389", "636"], ["expired", "1389", "1636"], ["invalid", "2389", "2636"]]
- if (forceBuild || !imageExists || certExpiringSoon()) {
- echo "Building image ${imageName}..."
- sh "docker build -t ${imageName} ."
- def names = opts.collect{ "ldap-${it[0]}" }
- def containers = names.join(" ")
- echo "Stopping ${containers}..."
- sh "docker stop ${containers} 2> /dev/null"
- } else {
- echo "Image ${imageName} is valid — no rebuild"
- }
- // Always start three containers with different CERT_TYPE
- opts.each { tuple ->
- def type = tuple[0]
- def port = tuple[1]
- def sslPort = tuple[2]
- def hostname = type != "valid" ? type : "ldap"
- def name = "ldap-${type}"
- echo "Ensuring container ${name} is running..."
- sh """
- docker ps --filter name=${name} --format '{{.Names}}' | grep -q "^${name}\$" || \
- docker start ${name} || \
- docker run -d --name ${name} -p ${port}:1389 -p ${sslPort}:1636 --hostname=${hostname}.sdk.local -e CERT_TYPE=${type} ${imageName}
- """
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment