Refactor execute_docker_run function 79/15279/4
authormesaj <juanmanuel.mesamendez.ext@telefonica.com>
Mon, 14 Jul 2025 17:49:22 +0000 (19:49 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 16 Jul 2025 06:36:05 +0000 (08:36 +0200)
Change-Id: I3e73fd0d8c42722e0fb6c142f9925c08e80cae60
Signed-off-by: mesaj <juanmanuel.mesamendez.ext@telefonica.com>
jenkins/ci-pipelines/ci_stage_3.groovy

index 33cc159..4f0a55a 100644 (file)
@@ -64,26 +64,47 @@ properties([
 ])
 
 ////////////////////////////////////////////////////////////////////////////////////////
-// Helper Functions
+// Helper Classes & Functions
 ////////////////////////////////////////////////////////////////////////////////////////
-String execute_docker_run(String tagName, String osmHostname, String environmentFile,
-                        String portmappingfile, String prometheusconfigfile,
-                        String kubeconfig, String clouds,
-                        String entrypoint=null, String entrypointArgs="") {
-    try {
-        String entrypointCmd = entrypoint ? "--entrypoint ${entrypoint}" : ""
-        String output = sh(returnStdout: true, script: """docker run ${entrypointCmd} \
-            --env OSM_HOSTNAME=${osmHostname} \
-            --env-file ${environmentFile} \
-            -v ${clouds}:/etc/openstack/clouds.yaml \
-            -v ${kubeconfig}:/root/.kube/config \
-            -v ${portmappingfile}:/root/port-mapping.yaml \
-            -v ${prometheusconfigfile}:/root/etsi-vim-prometheus.json \
-            opensourcemano/tests:${tagName} ${entrypointArgs}""")
-        return output
-    } catch (Exception e) {
-        println("Something happened during the execution of the shell script: ${e.message}")
-        throw new Exception("Docker run failed for tag '${tagName}' on host '${osmHostname}' with entrypoint '${entrypoint}' and entrypointArgs '${entrypointArgs}': ${e.message}")
+/** Usage:
+ *   def dr = new DockerRunner(this)
+ *   stdout = dr.run(
+ *       image   : "opensourcemano/tests:${tag}",
+ *       entry   : "/usr/bin/osm",        // optional
+ *       envVars : [ "OSM_HOSTNAME=${host}" ],
+ *       envFile : myEnv,
+ *       mounts  : [
+ *                  "${clouds}:/etc/openstack/clouds.yaml",
+ *                  "${kubeconfig}:/root/.kube/config"
+ *                ],
+ *       cmd     : "vim-create --name osm …"
+ *   )
+ */
+class DockerRunner implements Serializable {
+    def steps                       // Jenkins DSL context (`this` from the script)
+    DockerRunner(def steps) { this.steps = steps }
+
+    /** Returns stdout (trimmed); throws Exception on non-zero exit. */
+    String run(Map args = [:]) {
+        def envFile  = args.envFile ?: ''
+        def entry    = args.entry   ? "--entrypoint ${args.entry}" : ''
+        def mounts   = (args.mounts ?: [])
+                        .findAll { it && it.trim() }  // Filter out null/empty values
+                        .collect { "-v ${it}" }.join(' ')
+        def envs      = (args.envVars ?: [])
+                        .findAll { it && it.trim() }  // Filter out null/empty values
+                        .collect { "--env ${it}" }.join(' ')
+        def image    = args.image ?: ''
+        def cmd      = args.cmd   ?: ''
+        def fullCmd  = """docker run ${entry} ${envs} ${envFile ? "--env-file ${envFile}" : ''} ${mounts} ${image} ${cmd}""".trim().replaceAll('\\s+', ' ')
+
+        try {
+            return steps.sh(returnStdout: true, script: fullCmd).trim()
+        } catch (Exception ex) {
+            throw new Exception("docker run failed → ${ex.message}")
+        } finally {
+            steps.echo("Command executed: ${fullCmd}")
+        }
     }
 }
 
@@ -110,6 +131,7 @@ void register_etsi_vim_account(
         environmentFile = "${tempdir}/env"
     }
     int attempts = 3
+    def dr = new DockerRunner(this)
     while (attempts >= 0) {
         try {
             println("Attempting to register VIM account (remaining attempts: ${attempts})")
@@ -120,7 +142,19 @@ void register_etsi_vim_account(
                         --auth_url ${OS_AUTH_URL} --account_type openstack --description vim \
                         --prometheus_config_file /root/etsi-vim-prometheus.json \
                         --config '{management_network_name: ${VIM_MGMT_NET}, dataplane_physical_net: physnet2}' || true"""
-                String createOutput = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+                String createOutput = dr.run(
+                    image   : "opensourcemano/tests:${tagName}",
+                    entry   : entrypointCmd,
+                    envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                    envFile : environmentFile,
+                    mounts  : [
+                        "${clouds}:/etc/openstack/clouds.yaml",
+                        "${kubeconfig}:/root/.kube/config",
+                        "${portmappingfile}:/root/port-mapping.yaml",
+                        "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                    ],
+                    cmd     : entrypointArgs
+                )
                 println("VIM Creation Output: ${createOutput}")
             }
 
@@ -129,7 +163,19 @@ void register_etsi_vim_account(
             while (statusChecks > 0) {
                 sleep(10)  // Wait for 10 seconds before checking status
                 entrypointArgs = """vim-list --long | grep ${VIM_TARGET}"""
-                String vimList = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+                String vimList = dr.run(
+                    image   : "opensourcemano/tests:${tagName}",
+                    entry   : entrypointCmd,
+                    envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                    envFile : environmentFile,
+                    mounts  : [
+                        "${clouds}:/etc/openstack/clouds.yaml",
+                        "${kubeconfig}:/root/.kube/config",
+                        "${portmappingfile}:/root/port-mapping.yaml",
+                        "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                    ],
+                    cmd     : entrypointArgs
+                )
                 println("VIM List output: ${vimList}")
                 if (vimList.contains("ENABLED")) {
                     println("VIM successfully registered and is ENABLED.")
@@ -141,7 +187,19 @@ void register_etsi_vim_account(
             // If stuck, delete and retry
             println("VIM stuck for more than 50 seconds, deleting and retrying...")
             entrypointArgs = """vim-delete --force ${VIM_TARGET}"""
-            String deleteOutput = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+            String vimList = dr.run(
+                image   : "opensourcemano/tests:${tagName}",
+                entry   : entrypointCmd,
+                envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                envFile : environmentFile,
+                mounts  : [
+                    "${clouds}:/etc/openstack/clouds.yaml",
+                    "${kubeconfig}:/root/.kube/config",
+                    "${portmappingfile}:/root/port-mapping.yaml",
+                    "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                ],
+                cmd     : entrypointArgs
+            )
             println("VIM Deletion Output: ${deleteOutput}")
             sleep(5)
         } catch (Exception e) {
@@ -177,13 +235,26 @@ void register_etsi_k8s_cluster(
         environmentFile = "${tempdir}/env"
     }
     int attempts = 3
+    def dr = new DockerRunner(this)
     while (attempts >= 0) {
         try {
             println("Attempting to register K8s cluster (remaining attempts: ${attempts})")
             String entrypointArgs = """k8scluster-add ${K8S_CLUSTER_TARGET} --creds ${K8S_CREDENTIALS} --version "v1" \
                         --description "Robot-cluster" --skip-jujubundle --vim ${VIM_TARGET} \
                         --k8s-nets '{net1: ${VIM_MGMT_NET}}'"""
-            String createOutput = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+            String createOutput = dr.run(
+                image   : "opensourcemano/tests:${tagName}",
+                entry   : entrypointCmd,
+                envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                envFile : environmentFile,
+                mounts  : [
+                    "${clouds}:/etc/openstack/clouds.yaml",
+                    "${kubeconfig}:/root/.kube/config",
+                    "${portmappingfile}:/root/port-mapping.yaml",
+                    "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                ],
+                cmd     : entrypointArgs
+            )
             println("K8s Cluster Addition Output: ${createOutput}")
 
             // Check if the K8s cluster is ENABLED
@@ -191,7 +262,19 @@ void register_etsi_k8s_cluster(
             while (statusChecks > 0) {
                 sleep(10)  // Wait for 10 seconds before checking status
                 entrypointArgs = """k8scluster-list | grep ${K8S_CLUSTER_TARGET}"""
-                String clusterList = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+                String clusterList = dr.run(
+                    image   : "opensourcemano/tests:${tagName}",
+                    entry   : entrypointCmd,
+                    envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                    envFile : environmentFile,
+                    mounts  : [
+                        "${clouds}:/etc/openstack/clouds.yaml",
+                        "${kubeconfig}:/root/.kube/config",
+                        "${portmappingfile}:/root/port-mapping.yaml",
+                        "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                    ],
+                    cmd     : entrypointArgs
+                )
                 println("K8s Cluster List Output: ${clusterList}")
                 if (clusterList.contains("ENABLED")) {
                     println("K8s cluster successfully registered and is ENABLED.")
@@ -203,10 +286,34 @@ void register_etsi_k8s_cluster(
             // If stuck, delete and retry
             println("K8s cluster stuck for more than 50 seconds, deleting and retrying...")
             entrypointArgs = """k8scluster-show ${K8S_CLUSTER_TARGET}"""
-            String showOutput = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+            String showOutput = dr.run(
+                image   : "opensourcemano/tests:${tagName}",
+                entry   : entrypointCmd,
+                envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                envFile : environmentFile,
+                mounts  : [
+                    "${clouds}:/etc/openstack/clouds.yaml",
+                    "${kubeconfig}:/root/.kube/config",
+                    "${portmappingfile}:/root/port-mapping.yaml",
+                    "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                ],
+                cmd     : entrypointArgs
+            )
             println("K8s Cluster Show Output: ${showOutput}")
             entrypointArgs = """k8scluster-delete ${K8S_CLUSTER_TARGET}"""
-            String deleteOutput = execute_docker_run(tagName, osmHostname, environmentFile, portmappingfile, prometheusconfigfile, kubeconfig, clouds, entrypointCmd, entrypointArgs)
+            String deleteOutput = dr.run(
+                image   : "opensourcemano/tests:${tagName}",
+                entry   : entrypointCmd,
+                envVars : [ "OSM_HOSTNAME=${osmHostname}" ],
+                envFile : environmentFile,
+                mounts  : [
+                    "${clouds}:/etc/openstack/clouds.yaml",
+                    "${kubeconfig}:/root/.kube/config",
+                    "${portmappingfile}:/root/port-mapping.yaml",
+                    "${prometheusconfigfile}:/root/etsi-vim-prometheus.json"
+                ],
+                cmd     : entrypointArgs
+            )
             println("K8s Cluster Deletion Output: ${deleteOutput}")
             sleep(5)
         } catch (Exception e) {
@@ -233,7 +340,9 @@ void run_robot_systest(String tagName,
                        String jujuPassword=null,
                        String osmRSAfile=null,
                        String passThreshold='0.0',
-                       String unstableThreshold='0.0') {
+                       String unstableThreshold='0.0',
+                       Map extraEnvVars=null,
+                       Map extraVolMounts=null) {
     tempdir = sh(returnStdout: true, script: 'mktemp -d').trim()
     String environmentFile = ''
     if (envfile) {
@@ -244,30 +353,47 @@ void run_robot_systest(String tagName,
     }
     PROMETHEUS_PORT_VAR = ''
     if (prometheusPort != null) {
-        PROMETHEUS_PORT_VAR = "--env PROMETHEUS_PORT=${prometheusPort}"
+        PROMETHEUS_PORT_VAR = "PROMETHEUS_PORT=${prometheusPort}"
     }
     hostfilemount = ''
     if (hostfile) {
-        hostfilemount = "-v ${hostfile}:/etc/hosts"
+        hostfilemount = "${hostfile}:/etc/hosts"
     }
 
     JUJU_PASSWORD_VAR = ''
     if (jujuPassword != null) {
-        JUJU_PASSWORD_VAR = "--env JUJU_PASSWORD=${jujuPassword}"
+        JUJU_PASSWORD_VAR = "JUJU_PASSWORD=${jujuPassword}"
     }
 
     try {
         withCredentials([usernamePassword(credentialsId: 'gitlab-oci-test',
                         passwordVariable: 'OCI_REGISTRY_PSW', usernameVariable: 'OCI_REGISTRY_USR')]) {
-            sh("""docker run --env OSM_HOSTNAME=${osmHostname} --env PROMETHEUS_HOSTNAME=${prometheusHostname} \
-               ${PROMETHEUS_PORT_VAR} ${JUJU_PASSWORD_VAR} --env-file ${environmentFile} \
-               --env OCI_REGISTRY_URL=${ociRegistryUrl} --env OCI_REGISTRY_USER=${OCI_REGISTRY_USR} \
-               --env OCI_REGISTRY_PASSWORD=${OCI_REGISTRY_PSW} \
-               -v ${clouds}:/etc/openstack/clouds.yaml -v ${osmRSAfile}:/root/osm_id_rsa \
-               -v ${kubeconfig}:/root/.kube/config -v ${tempdir}:/robot-systest/reports \
-               -v ${portmappingfile}:/root/port-mapping.yaml ${hostfilemount} opensourcemano/tests:${tagName} \
-               -t ${testName}""")
+            def dr = new DockerRunner(this)
+            dr.run(
+                image   : "opensourcemano/tests:${tagName}",
+                envVars : [
+                    "OSM_HOSTNAME=${osmHostname}",
+                    "PROMETHEUS_HOSTNAME=${prometheusHostname}",
+                    "${PROMETHEUS_PORT_VAR}",
+                    "${JUJU_PASSWORD_VAR}",
+                    "OCI_REGISTRY_URL=${ociRegistryUrl}",
+                    "OCI_REGISTRY_USER=${OCI_REGISTRY_USR}",
+                    "OCI_REGISTRY_PASSWORD=${OCI_REGISTRY_PSW}"
+                ],
+                envFile : "${environmentFile}",
+                mounts  : [
+                    "${clouds}:/etc/openstack/clouds.yaml",
+                    "${osmRSAfile}:/root/osm_id_rsa",
+                    "${kubeconfig}:/root/.kube/config",
+                    "${tempdir}:/robot-systest/reports",
+                    "${portmappingfile}:/root/port-mapping.yaml",
+                    "${hostfilemount}"
+                ],
+                cmd     : "-t ${testName}"
+            )
         }
+    } catch (Exception e) {
+        println("Robotest execution failed with: ${e.message}")
     } finally {
         try {
             sh("cp ${tempdir}/*.xml .")
@@ -775,8 +901,6 @@ node("${params.NODE}") {
                     """
                 } // stage("OSM Health")
             } // if ( params.DO_INSTALL )
-
-
 ///////////////////////////////////////////////////////////////////////////////////////
 // Execute Robot tests
 ///////////////////////////////////////////////////////////////////////////////////////