Use jenkins-cli to add foundation agents to jenkins

PROD-37197

Change-Id: I267d47f98a02244a571f90593b4a9839c228bc18
diff --git a/jobs/pipelines/swarm-bootstrap-salt-cluster-heat.groovy b/jobs/pipelines/swarm-bootstrap-salt-cluster-heat.groovy
index 7086240..5a74fea 100644
--- a/jobs/pipelines/swarm-bootstrap-salt-cluster-heat.groovy
+++ b/jobs/pipelines/swarm-bootstrap-salt-cluster-heat.groovy
@@ -176,81 +176,42 @@
                         common.printMsg("JENKINS_SLAVE_NODE_NAME=${JENKINS_SLAVE_NODE_NAME}", "green")
                         common.printMsg("JENKINS_SLAVE_IP=${jenkins_slave_ip}", "green")
 
-                withCredentials([
-                   [$class          : 'UsernamePasswordMultiBinding',
-                   credentialsId   : "${CREATE_JENKINS_NODE_CREDENTIALS}",
-                   passwordVariable: 'JENKINS_PASS',
-                   usernameVariable: 'JENKINS_USER']
-                ]) {
+                        withCredentials([[$class          : 'UsernamePasswordMultiBinding',
+                                          credentialsId   : "${CREATE_JENKINS_NODE_CREDENTIALS}",
+                                          passwordVariable: 'JENKINS_PASS',
+                                          usernameVariable: 'JENKINS_USER']]) {
+                            sh "curl ${JENKINS_URL}/jnlpJars/jenkins-cli.jar --output jenkins-cli.jar"
 
-                        script_delete_agent = ("""\
-                            CRUMB=\$(curl --fail -0 -u \"\${JENKINS_USER}:\${JENKINS_PASS}\" \${JENKINS_URL}\'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)\' 2>/dev/null)
-                            curl -w '%{http_code}' -o /dev/null \
-                                -u \"\${JENKINS_USER}:\${JENKINS_PASS}\" \
-                                -H \"Content-Type:application/x-www-form-urlencoded\" \
-                                -H \"\$CRUMB\" \
-                                \"\${JENKINS_URL}/computer/\${JENKINS_SLAVE_NODE_NAME}/doDelete\" \
-                                --request \'POST\' --data \'\'
-                            sleep 10
-                        """)
+                            sh "java -jar jenkins-cli.jar -s ${JENKINS_URL} -auth ${JENKINS_USER}:${JENKINS_PASS} delete-node ${JENKINS_SLAVE_NODE_NAME} || true"
+                            create_node_xml="""\
+                                            <slave>
+                                            <name>${JENKINS_SLAVE_NODE_NAME}</name>
+                                            <description>${ENV_NAME}</description>
+                                            <remoteFS>/home/jenkins/workspace</remoteFS>
+                                            <numExecutors>${jenkins_slave_executors}</numExecutors>
+                                            <mode>EXCLUSIVE</mode>
+                                            <retentionStrategy class="hudson.slaves.RetentionStrategy\$Always"/>
+                                            <launcher class="hudson.plugins.sshslaves.SSHLauncher">
+                                              <host>${jenkins_slave_ip}</host>
+                                              <port>22</port>
+                                              <credentialsId>${ACCESS_JENKINS_NODE_CREDENTIALS}</credentialsId>
+                                            </launcher>
+                                            <label>${ENV_NAME}</label>
+                                            <nodeProperties/>
+                                            </slave>""".stripIndent().replaceAll("\\n", "")
+                            sh "echo '${create_node_xml}' | java -jar jenkins-cli.jar -s ${JENKINS_URL} -auth ${JENKINS_USER}:${JENKINS_PASS} create-node ${JENKINS_SLAVE_NODE_NAME}"
 
-                        script_create_agent = ("""\
-                            CRUMB=\$(curl --fail -0 -u \"\${JENKINS_USER}:\${JENKINS_PASS}\" \${JENKINS_URL}\'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)\' 2>/dev/null)
-
-                            curl -L -sS -w '%{http_code}' -o /dev/null \
-                                -u \"\${JENKINS_USER}:\${JENKINS_PASS}\" \
-                                -H \"Content-Type:application/x-www-form-urlencoded\" \
-                                -H \"\$CRUMB\" \
-                                -X POST -d 'json={\
-                                    \"name\": \"'\"\$JENKINS_SLAVE_NODE_NAME\"'\", \
-                                    \"nodeDescription\": \"'\"\$ENV_NAME\"'\", \
-                                    \"numExecutors\": \"'\"${jenkins_slave_executors}\"'\", \
-                                    \"remoteFS\": \"'\"/home/jenkins/workspace\"'\", \
-                                    \"labelString\": \"'\"\$ENV_NAME\"'\", \
-                                    \"mode\": \"EXCLUSIVE\", \
-                                    \"\": [\"hudson.plugins.sshslaves.SSHLauncher\", \"hudson.slaves.RetentionStrategy\$Always\"], \
-                                    \"launcher\": {\
-                                        \"stapler-class\": \"hudson.plugins.sshslaves.SSHLauncher\", \
-                                        \"\$class\": \"hudson.plugins.sshslaves.SSHLauncher\", \
-                                        \"host\": \"'\"${jenkins_slave_ip}\"'\", \
-                                        \"credentialsId\": \"'\"\$ACCESS_JENKINS_NODE_CREDENTIALS\"'\", \
-                                        \"port\": \"'\"22\"'\", \
-                                        \"javaPath\": \"\", \
-                                        \"jvmOptions\": \"\", \
-                                        \"prefixStartSlaveCmd\": \"\", \
-                                        \"suffixStartSlaveCmd\": \"\", \
-                                        \"launchTimeoutSeconds\": \"\", \
-                                        \"maxNumRetries\": \"\", \
-                                        \"retryWaitTime\": \"\", \
-                                        \"sshHostKeyVerificationStrategy\": {\
-                                            \"\$class\": \"hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy\" \
-                                        }, \
-                                        \"tcpNoDelay\": \"true\"\
-                                    }, \
-                                    \"retentionStrategy\": {\
-                                        \"stapler-class\": \"hudson.slaves.RetentionStrategy\$Always\", \
-                                        \"\$class\": \"hudson.slaves.RetentionStrategy\$Always\"\
-                                    }, \
-                                    \"nodeProperties\": {\
-                                        \"stapler-class-bag\": \"true\"\
-                                    }, \
-                                    \"type\": \"hudson.slaves.DumbSlave\", \
-                                    \"crumb\": \"'\"\$CRUMB\"'\"}' \
-                                \"\${JENKINS_URL}/computer/doCreateItem?name=\${JENKINS_SLAVE_NODE_NAME}&type=hudson.slaves.DumbSlave\"
-                        """)
-                        shared.verbose_sh(script_delete_agent, true, false, true)
-                        shared.verbose_sh(script_create_agent, true, false, true)
-                        timeout(time: 30, unit: 'MINUTES') {
-                            node("${JENKINS_SLAVE_NODE_NAME}") {
-                                sh "echo 'ok'"
-                                println "Jenkins agent is available now and can executes commands"
+                            timeout(time: 30, unit: 'MINUTES') {
+                                node("${JENKINS_SLAVE_NODE_NAME}") {
+                                    sh "echo 'ok'"
+                                    println "Jenkins agent is available now and can executes commands"
+                                }
                             }
-                        }
-                        // Store jenkins agent IP address
-                        jenkins_agent_description = "ssh jenkins@${jenkins_slave_ip}  # foundation node with Jenkins agent <a href=${JENKINS_URL}/computer/${JENKINS_SLAVE_NODE_NAME}>${JENKINS_SLAVE_NODE_NAME}</a><br>ssh root@${SALT_MASTER_IP}  # cfg01 node<br>"
-                        writeFile(file: "jenkins_agent_description.txt", text: jenkins_agent_description, encoding: "UTF-8")
+                            // Store jenkins agent IP address
+                            jenkins_agent_description = "ssh jenkins@${jenkins_slave_ip}  # foundation node with Jenkins agent <a href=${JENKINS_URL}/computer/${JENKINS_SLAVE_NODE_NAME}>${JENKINS_SLAVE_NODE_NAME}</a><br>ssh root@${SALT_MASTER_IP}  # cfg01 node<br>"
+                            writeFile(file: "jenkins_agent_description.txt", text: jenkins_agent_description, encoding: "UTF-8")
 
-                } // withCredentials
+                        } // withCredentials
 
                     }// stage
 
diff --git a/tcp_tests/templates/_heat_environments/eu-cloud-low-flavors.env b/tcp_tests/templates/_heat_environments/eu-cloud-low-flavors.env
index 8e984e0..7f20da3 100644
--- a/tcp_tests/templates/_heat_environments/eu-cloud-low-flavors.env
+++ b/tcp_tests/templates/_heat_environments/eu-cloud-low-flavors.env
@@ -44,5 +44,5 @@
   vm_availability_zone: nova
   net_public: public
 
-  foundation_image: system.foundation02
+  foundation_image: system.foundation
   nameservers: 172.18.176.6,172.18.224.6
diff --git a/tcp_tests/templates/_heat_environments/eu-cloud.env b/tcp_tests/templates/_heat_environments/eu-cloud.env
index d2c6dac..9eb529b 100644
--- a/tcp_tests/templates/_heat_environments/eu-cloud.env
+++ b/tcp_tests/templates/_heat_environments/eu-cloud.env
@@ -48,5 +48,5 @@
   vm_availability_zone: nova
   net_public: public
 
-  foundation_image: system.foundation02
+  foundation_image: system.foundation
   nameservers: 172.18.176.6,172.18.224.6