New Jenkinsfile YAML based for CORD-2.0

Change-Id: I11a29897b00f5b80b7bef2a3b8f6f6e729658141
diff --git a/Jenkinsfile b/Jenkinsfile
index 3aeae45..5679a47 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,112 +1,225 @@
 def filename = 'manifest-${branch}.xml'
+def manifestUrl = 'https://gerrit.opencord.org/manifest'
+def config = null;
 
 node ('master') {
-       checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: 'https://gerrit.opencord.org/manifest', quiet: true]
+    checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true]
 
-       stage 'Generate and Copy Manifest file'
-       sh returnStdout: true, script: 'repo manifest -r -o ' + filename
-       sh returnStdout: true, script: 'cp ' + filename + ' ' + env.JENKINS_HOME + '/tmp'
+    stage ("Generate and Copy Manifest file") {
+        sh returnStdout: true, script: 'repo manifest -r -o ' + filename
+        sh returnStdout: true, script: 'cp ' + filename + ' ' + env.JENKINS_HOME + '/tmp'
+    }
+
+    stage ("Parse deployment configuartion file") {
+        sh returnStdout: true, script: 'rm -rf ${configRepoBaseDir}'
+        sh returnStdout: true, script: 'git clone ${configRepoUrl}'
+        config = readYaml file: "${configRepoBaseDir}${configRepoFile}"
+    }
 }
 
-timeout (time: 240) {
-    node ("${devNodeJenkinsName}") {
-       stage 'Checkout cord repo'
-       checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: 'https://gerrit.opencord.org/manifest', quiet: true]
+node ("${config.dev_node.name}") {
+    timeout (time: 240) {
+        stage ('Checkout cord repo') {
+            checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true]
+        }
 
-       dir('build') {
-            stage 'Redeploy head node and Build Vagrant box'
+        dir('build') {
             try {
-                parallel(
-                    maasOps: {
-                        sh "maas login maas http://${maasHeadIP}/MAAS/api/2.0 ${apiKey}"
-                        sh "maas maas machine release ${headNodeMAASSystemId}"
+                stage ("Re-deploy head node and Build Vagrant box") {
+                    parallel(
+                        maasOps: {
+                            sh "maas login maas http://${config.maas.ip}/MAAS/api/2.0 ${config.maas.api_key}"
+                            sh "maas maas machine release ${config.maas.head_system_id}"
 
-                        timeout(time: 15) {
+                            timeout(time: 15) {
+                                waitUntil {
+                                   try {
+                                        sh "maas maas machine read ${config.maas.head_system_id} | grep Ready"
+                                        return true
+                                    } catch (exception) {
+                                        return false
+                                    }
+                                }
+                            }
+
+                            sh 'maas maas machines allocate'
+                            sh "maas maas machine deploy ${config.maas.head_system_id}"
+
+                            timeout(time: 30) {
+                                waitUntil {
+                                   try {
+                                        sh "maas maas machine read ${config.maas.head_system_id} | grep Deployed"
+                                        return true
+                                    } catch (exception) {
+                                        return false
+                                    }
+                                }
+                            }
+
+                        }, vagrantOps: {
+                            sh 'vagrant up corddev'
+                        }, failFast : true
+                    )
+                }
+
+                stage ("Fetch CORD packages") {
+                    sh "vagrant ssh -c \"cd /cord/build; ./gradlew fetch\" corddev"
+                }
+
+                stage ("Build CORD Images") {
+                    sh "vagrant ssh -c \"cd /cord/build; ./gradlew buildImages\" corddev"
+                }
+
+                stage ("Downloading CORD POD configuration") {
+                    sh "vagrant ssh -c \"cd /cord/build/config; git clone ${config.pod_config.repo_url}\" corddev"
+                }
+
+                stage ("Publish to headnode") {
+                    sh "vagrant ssh -c \"cd /cord/build; ./gradlew -PtargetReg=${config.head.ip}:5000 -PdeployConfig=config/pod-configs/${config.pod_config.file_name} publish\" corddev"
+                }
+
+                stage ("Deploy") {
+                    sh "vagrant ssh -c \"cd /cord/build; ./gradlew -PtargetReg=${config.head.ip}:5000 -PdeployConfig=config/pod-configs/${config.pod_config.file_name} deploy\" corddev"
+                }
+
+                stage ("Power cycle compute nodes") {
+                    for(int i=0; i < config.compute_nodes.size(); i++) {
+                        sh "ipmitool -U ${config.compute_nodes[i].ipmi.user} -P ${config.compute_nodes[i].ipmi.pass} -H ${config.compute_nodes[i].ipmi.ip} power cycle"
+                    }
+                }
+
+                stage ("Wait for compute nodes to get deployed") {
+                    sh "ssh-keygen -f /home/${config.dev_node.user}/.ssh/known_hosts -R ${config.head.ip}"
+                    def cordApiKey = runCmd("${config.head.ip}",
+                                            "${config.head.user}",
+                                            "${config.head.pass}",
+                                            "sudo maas-region-admin apikey --username ${config.head.user}")
+                    runCmd("${config.head.ip}",
+                           "${config.head.user}",
+                           "${config.head.pass}",
+                           "maas login pod-maas http://${config.head.ip}/MAAS/api/1.0 ${cordApiKey}")
+                    timeout(time: 45) {
+                        waitUntil {
+                            try {
+                                num = runCmd("${config.head.ip}",
+                                             "${config.head.user}",
+                                             "${config.head.pass}",
+                                             "maas pod-maas nodes list | grep -i deployed | wc -l").trim()
+                                return num == '2'
+                            } catch (exception) {
+                                return false
+                            }
+                        }
+                    }
+                }
+
+                stage ("Wait for computes nodes to be provisioned") {
+                    ip = runCmd("${config.head.ip}",
+                                "${config.head.user}",
+                                "${config.head.pass}",
+                                "docker inspect --format '{{.NetworkSettings.Networks.maas_default.IPAddress}}' provisioner").trim()
+                    timeout(time:45) {
+                        waitUntil {
+                            try {
+                                out = runCmd("${config.head.ip}",
+                                             "${config.head.user}",
+                                             "${config.head.pass}",
+                                             "curl -sS http://$ip:4243/provision/ | jq -c '.[] | select(.status | contains(2))'").trim()
+                                return out != ""
+                            } catch (exception) {
+                                return false
+                            }
+                        }
+                    }
+                }
+
+                if (config.fabric_switches != null) {
+                    stage("Reserve IPs for fabric switches and restart maas-dhcp service") {
+                        for(int i=0; i < config.fabric_switches.size(); i++) {
+                            def append = "";
+                            if (i!=0) {
+                                append = "-a";
+                            }
+                            def str = createMACIPbindingStr(i+1,
+                                                           "${config.fabric_switches[i].mac}",
+                                                           "${config.fabric_switches[i].ip}")
+                            runCmd("${config.head.ip}",
+                                    "${config.head.user}",
+                                    "${config.head.pass}",
+                                    "echo -e $str '|' sudo tee $append /etc/dhcp/dhcpd.reservations > /dev/null")
+                        }
+                        runCmd("${config.head.ip}",
+                               "${config.head.user}",
+                               "${config.head.pass}",
+                               "sudo restart maas-dhcpd")
+
+                        runCmd("${config.head.ip}",
+                               "${config.head.user}",
+                               "${config.head.pass}",
+                               "cord harvest go")
+                    }
+
+                    stage ("Wait for fabric switches to get deployed") {
+                        for(int i=0; i < config.fabric_switches.size(); i++) {
+                            runFabricCmd("${config.head.ip}",
+                                         "${config.head.user}",
+                                         "${config.head.pass}",
+                                         "${config.fabric_switches[i].ip}",
+                                         "${config.fabric_switches[i].user}",
+                                         "${config.fabric_switches[i].pass}",
+                                         "sudo onl-onie-boot-mode install")
+
+                            runFabricCmd("${config.head.ip}",
+                                         "${config.head.user}",
+                                         "${config.head.pass}",
+                                         "${config.fabric_switches[i].ip}",
+                                         "${config.fabric_switches[i].user}",
+                                         "${config.fabric_switches[i].pass}",
+                                         "sudo reboot")
+                        }
+                        timeout(time: 45) {
                             waitUntil {
-                               try {
-                                    sh "maas maas machine read ${headNodeMAASSystemId} | grep Ready"
-                                    return true
+                                try {
+                                    def harvestCompleted = runCmd("${config.head.ip}",
+                                                                  "${config.head.user}",
+                                                                  "${config.head.pass}",
+                                                                  "cord harvest list '|' grep -i fabric '|' wc -l").trim()
+                                    return harvestCompleted == config.fabric_switches.size().toString()
                                 } catch (exception) {
                                     return false
                                 }
                             }
                         }
+                    }
 
-                        sh 'maas maas machines allocate'
-                        sh "maas maas machine deploy ${headNodeMAASSystemId}"
-
-                        timeout(time: 30) {
+                    stage ("Wait for fabric switches to be provisioned") {
+                        timeout(time:45) {
                             waitUntil {
-                               try {
-                                    sh "maas maas machine read ${headNodeMAASSystemId} | grep Deployed"
-                                    return true
+                                try {
+                                    def provCompleted = 0
+                                    for(int i=0; i < config.fabric_switches.size(); i++) {
+                                        def count = runCmd("${config.head.ip}",
+                                                           "${config.head.user}",
+                                                           "${config.head.pass}",
+                                                           "cord prov list '|' grep -i ${config.fabric_switches[i].ip} '|' grep -i complete '|' wc -l").trim()
+                                        provCompleted = provCompleted + count.toInteger()
+                                    }
+                                    return provCompleted == config.fabric_switches.size()
                                 } catch (exception) {
                                     return false
                                 }
                             }
                         }
-
-                    }, vagrantOps: {
-                        sh 'vagrant up corddev'
-                    }, failFast : true
-                )
-
-                stage 'Fetch CORD packages'
-                sh 'vagrant ssh -c "cd /cord/build; ./gradlew fetch" corddev'
-
-                stage 'Build CORD Images'
-                sh 'vagrant ssh -c "cd /cord/build; ./gradlew buildImages" corddev'
-
-                stage 'Downloading CORD POD configuration'
-                sh 'vagrant ssh -c "cd /cord/build/config; git clone ${podConfigRepoUrl} -b ${branch}" corddev'
-
-                stage 'Publish to headnode'
-                sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=${headNodeIP}:5000 -PdeployConfig=config/pod-configs/${podConfigFileName} publish" corddev'
-
-                stage 'Deploy'
-                sh 'vagrant ssh -c "cd /cord/build; ./gradlew -PtargetReg=${headNodeIP}:5000 -PdeployConfig=config/pod-configs/${podConfigFileName} deploy" corddev'
-
-                stage 'Power cycle compute nodes'
-                parallel(
-                    compute_1: {
-                        sh 'ipmitool -U ${computeNode1IPMIUser} -P ${computeNode1IPMIPass} -H ${computeNode1IPMIIP} power cycle'
-                    }, compute_2: {
-                        sh 'ipmitool -U ${computeNode2IPMIUser} -P ${computeNode2IPMIPass} -H ${computeNode2IPMIIP} power cycle'
-                    }, failFast : true
-                )
-
-                stage 'Wait for compute nodes to get deployed'
-                sh 'ssh-keygen -f /home/${devNodeUser}/.ssh/known_hosts -R ${headNodeIP}'
-                def cordapikey = sh(returnStdout: true, script: "sshpass -p ${headNodePass} ssh -oStrictHostKeyChecking=no -l ${headNodeUser} ${headNodeIP} sudo maas-region-admin apikey --username ${headNodeUser}")
-                sh "sshpass -p ${headNodePass} ssh -oStrictHostKeyChecking=no -l ${headNodeUser} ${headNodeIP} maas login pod-maas http://${headNodeIP}/MAAS/api/1.0 $cordapikey"
-                timeout(time: 45) {
-                    waitUntil {
-                        try {
-                            num = sh(returnStdout: true, script: "sshpass -p ${headNodePass} ssh -l ${headNodeUser} ${headNodeIP} maas pod-maas nodes list | grep Deployed | wc -l").trim()
-                            return num == '2'
-                        } catch (exception) {
-                            return false
-                        }
                     }
                 }
 
-                stage 'Wait for computes nodes to be provisioned'
-                ip = sh (returnStdout: true, script:"sshpass -p ${headNodePass} ssh -oStrictHostKeyChecking=no -l ${headNodeUser} ${headNodeIP} docker inspect --format '{{.NetworkSettings.Networks.maas_default.IPAddress}}'  provisioner").trim()
-                timeout(time:45) {
-                    waitUntil {
-                        try {
-                            out = sh (returnStdout: true, script:"sshpass -p ${headNodePass} ssh -oStrictHostKeyChecking=no -l ${headNodeUser} ${headNodeIP} curl -sS http://$ip:4243/provision/ | jq -c '.[] | select(.status | contains(2))'").trim()
-                            return out != ""
-                        } catch (exception) {
-                            return false
-                        }
+                if (config.make_release == true) {
+                    stage ("Trigger Build") {
+                        url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build'
+                        httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201'
                     }
                 }
 
-                stage 'Trigger Build'
-                url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build'
-                httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201'
-
                 currentBuild.result = 'SUCCESS'
             } catch (err) {
                 currentBuild.result = 'FAILURE'
@@ -117,6 +230,49 @@
             }
             echo "RESULT: ${currentBuild.result}"
        }
-
     }
 }
+
+/**
+ * Returns a string used to bind IPs and MAC addresses, substituting the values
+ * given.
+ *
+ * @param counter the counter used to generate the host name
+ * @param mac     the MAC address to substitute
+ * @param ip      the IP address to substitute
+ */
+def createMACIPbindingStr(counter, mac, ip) {
+    return """\\'host fabric${counter} {'\n'hardware ethernet ${mac}';''\n'fixed-address ${ip}';''\n'}\\'"""
+}
+
+/**
+ * Runs a command on a remote host using sshpass.
+ *
+ * @param ip      the node IP address
+ * @param user    the node user name
+ * @param pass    the node password
+ * @param command the command to run
+ * @return the output of the command
+ */
+def runCmd(ip, user, pass, command) {
+    return sh(returnStdout: true, script: "sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}")
+}
+
+/**
+ * Runs a command on a fabric switch.
+ *
+ * @param headIp         the head node IP address
+ * @param headUser       the head node user name
+ * @param headPass       the head node password
+ * @param ip             the mgmt IP of the fabric switch, reachable from the head node
+ * @param user           the mgmt user name of the fabric switch
+ * @param pass           the mgmt password of the fabric switch
+ * @param command        the command to run on the fabric switch
+ * @return the output of the command
+ */
+def runFabricCmd(headIp, headUser, headPass, user, pass, command) {
+    return runCmd("${haedIp}",
+                  "${headUser}",
+                  "${headPass}",
+                  "sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}")
+}
\ No newline at end of file