Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 1 | // Copyright 2017-present Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 15 | import groovy.json.JsonSlurperClassic |
| 16 | |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 17 | def filename = 'manifest-${branch}.xml' |
| 18 | def manifestUrl = 'https://gerrit.opencord.org/manifest' |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 19 | deployment_config = null; |
| 20 | pod_config = null; |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 21 | |
| 22 | node ('master') { |
| 23 | checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true] |
| 24 | |
| 25 | stage ("Generate and Copy Manifest file") { |
| 26 | sh returnStdout: true, script: 'repo manifest -r -o ' + filename |
| 27 | sh returnStdout: true, script: 'cp ' + filename + ' ' + env.JENKINS_HOME + '/tmp' |
| 28 | } |
| 29 | |
| 30 | stage ("Parse deployment configuration file") { |
| 31 | sh returnStdout: true, script: 'rm -rf ${configRepoBaseDir}' |
| 32 | sh returnStdout: true, script: 'git clone -b ${branch} ${configRepoUrl}' |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 33 | deployment_config = readYaml file: "${configRepoBaseDir}${configRepoFile}" |
| 34 | pod_config = readYaml file: "${configRepoBaseDir}${deployment_config.pod_config.file_name}" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 38 | node ("${deployment_config.dev_node.name}") { |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 39 | timeout (time: 240) { |
Luca Prete | 225d965 | 2017-09-12 10:44:48 -0700 | [diff] [blame] | 40 | stage ('Remove old head node from known hosts') { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 41 | sh "ssh-keygen -R ${deployment_config.head.ip}" |
You Wang | f9cb2f3 | 2017-09-11 13:26:42 -0700 | [diff] [blame] | 42 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 43 | stage ('Checkout cord repo') { |
| 44 | checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true] |
| 45 | } |
| 46 | |
Luca Prete | fcbc54b | 2017-09-12 14:37:33 -0700 | [diff] [blame] | 47 | try { |
| 48 | dir('build') { |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 49 | stage ("Re-deploy head node") { |
| 50 | maasOps: { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 51 | sh "maas login maas http://${deployment_config.maas.ip}/MAAS/api/2.0 ${deployment_config.maas.api_key}" |
| 52 | sh "maas maas machine release ${deployment_config.maas.head_system_id}" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 53 | |
| 54 | timeout(time: 15) { |
| 55 | waitUntil { |
| 56 | try { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 57 | sh "maas maas machine read ${deployment_config.maas.head_system_id} | grep Ready" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 58 | return true |
| 59 | } catch (exception) { |
| 60 | return false |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | sh 'maas maas machines allocate' |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 66 | sh "maas maas machine deploy ${deployment_config.maas.head_system_id}" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 67 | |
| 68 | timeout(time: 30) { |
| 69 | waitUntil { |
| 70 | try { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 71 | sh "maas maas machine read ${deployment_config.maas.head_system_id} | grep Deployed" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 72 | return true |
| 73 | } catch (exception) { |
| 74 | return false |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | stage ("Download CORD POD configuration") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 82 | sh "cd podconfig; git clone -b ${branch} ${deployment_config.pod_config.repo_url} automation" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | stage ("Generate CORD configuration") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 86 | sh "make PODCONFIG=automation/${deployment_config.pod_config.file_name} config" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 87 | } |
| 88 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 89 | if (deployment_config.fabric_switches != null) { |
Andy Bavier | 700f266 | 2017-09-19 13:52:30 -0700 | [diff] [blame] | 90 | stage("Reserve IPs for fabric switches") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 91 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 92 | def str = createMACIPbindingStr("fabric", i+1, |
| 93 | "${deployment_config.fabric_switches[i].mac}", |
| 94 | "${deployment_config.fabric_switches[i].ip}") |
Andy Bavier | 700f266 | 2017-09-19 13:52:30 -0700 | [diff] [blame] | 95 | sh "echo $str >> maas/roles/maas/files/dhcpd.reservations" |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 100 | stage ("Deploy") { |
Luca Prete | 614d670 | 2017-10-18 11:52:55 -0700 | [diff] [blame] | 101 | sh "make build" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 102 | } |
| 103 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 104 | if (deployment_config.compute_nodes != null) { |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 105 | stage ("Power cycle compute nodes") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 106 | for(int i=0; i < deployment_config.compute_nodes.size(); i++) { |
| 107 | sh "ipmitool -U ${deployment_config.compute_nodes[i].ipmi.user} -P ${deployment_config.compute_nodes[i].ipmi.pass} -H ${deployment_config.compute_nodes[i].ipmi.ip} power cycle" |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | stage ("Wait for compute nodes to get deployed") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 112 | sh "ssh-keygen -f /home/${deployment_config.dev_node.user}/.ssh/known_hosts -R ${deployment_config.head.ip}" |
| 113 | def cordApiKey = runHeadNodeCmd("sudo maas-region-admin apikey --username ${deployment_config.head.user}") |
| 114 | runHeadNodeCmd("maas login pod-maas http://${deployment_config.head.ip}/MAAS/api/1.0 ${cordApiKey}") |
Luca Prete | 49c818c | 2017-10-24 17:22:09 -0700 | [diff] [blame] | 115 | timeout(time: 90) { |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 116 | waitUntil { |
| 117 | try { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 118 | num = runHeadNodeCmd("maas pod-maas nodes list | grep substatus_name | grep -i deployed | wc -l").trim() |
| 119 | return num.toInteger() == deployment_config.compute_nodes.size() |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 120 | } catch (exception) { |
| 121 | return false |
| 122 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 126 | |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 127 | stage ("Wait for compute nodes to be provisioned") { |
| 128 | timeout(time:45) { |
| 129 | waitUntil { |
| 130 | try { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 131 | num = runHeadNodeCmd("cord prov list | grep -i node | grep -i complete | wc -l").trim() |
| 132 | return num.toInteger() == deployment_config.compute_nodes.size() |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 133 | } catch (exception) { |
| 134 | return false |
| 135 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Luca Prete | e209527 | 2017-10-17 11:47:26 -0700 | [diff] [blame] | 139 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 140 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 141 | if (deployment_config.fabric_switches != null) { |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 142 | stage ("Wait for fabric switches to get deployed") { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 143 | runFabricSwitchCmdAll("sudo onl-onie-boot-mode install") |
| 144 | runFabricSwitchCmdAll("sudo reboot") |
Andy Bavier | 700f266 | 2017-09-19 13:52:30 -0700 | [diff] [blame] | 145 | // Ensure that switches get provisioned after ONIE reinstall. |
| 146 | // Delete them if they were provisioned earlier. If the switches are not |
| 147 | // present in 'cord prov list', this command has no effect. |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 148 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 149 | runHeadNodeCmd("cord prov delete ${deployment_config.fabric_switches[i].mac}") |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 150 | } |
| 151 | timeout(time: 45) { |
| 152 | waitUntil { |
| 153 | try { |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 154 | def harvestCompleted = runHeadNodeCmd("cord harvest list | grep -i fabric | wc -l").trim() |
| 155 | return harvestCompleted.toInteger() == deployment_config.fabric_switches.size() |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 156 | } catch (exception) { |
| 157 | return false |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | stage ("Wait for fabric switches to be provisioned") { |
| 164 | timeout(time:45) { |
| 165 | waitUntil { |
| 166 | try { |
| 167 | def provCompleted = 0 |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 168 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 169 | def count = runHeadNodeCmd("cord prov list | grep -i ${deployment_config.fabric_switches[i].ip} | grep -i complete | wc -l").trim() |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 170 | provCompleted = provCompleted + count.toInteger() |
| 171 | } |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 172 | return provCompleted == deployment_config.fabric_switches.size() |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 173 | } catch (exception) { |
| 174 | return false |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
Marc De Leenheer | 672afa8 | 2017-11-27 17:25:09 -0800 | [diff] [blame] | 179 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 180 | // Post installation configuration starts here |
| 181 | fabricIpPrefix = pod_config.fabric_ip.split(/\.\d+\.\d+\/24/)[0] |
| 182 | xosUser = "xosadmin@opencord.org" |
| 183 | xosPass = runHeadNodeCmd("cat /opt/credentials/xosadmin@opencord.org").trim() |
| 184 | stage ("Connect fabric switches and compute nodes to ONOS") { |
| 185 | // Configure breakout ports |
| 186 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 187 | if (deployment_config.fabric_switches[i].breakout_ports != null) { |
| 188 | for(int j=0; j < deployment_config.fabric_switches[i].breakout_ports.size(); j++) { |
| 189 | runFabricSwitchCmd("${deployment_config.fabric_switches[i].ip}", |
| 190 | "${deployment_config.fabric_switches[i].user}", |
| 191 | "${deployment_config.fabric_switches[i].pass}", |
You Wang | d1d67aa | 2017-12-13 11:18:54 -0800 | [diff] [blame^] | 192 | "sed -i -e 's/#port_mode_${deployment_config.fabric_switches[i].breakout_ports[j]}=/port_mode_${deployment_config.fabric_switches[i].breakout_ports[j]}=/g' /etc/accton/ofdpa.conf") |
Marc De Leenheer | 672afa8 | 2017-11-27 17:25:09 -0800 | [diff] [blame] | 193 | } |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | connectFabricAndComputeToOnos() |
| 197 | } |
| 198 | stage ("Configure the compute nodes") { |
| 199 | leafSwitchNum = 0 |
| 200 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 201 | if(deployment_config.fabric_switches[i].role.toLowerCase().contains("leaf")) { |
| 202 | leafSwitchNum += 1 |
| 203 | } |
| 204 | } |
| 205 | // Configure head-node |
| 206 | // Add routes to fabric subnets |
| 207 | for(int i=2; i<=leafSwitchNum; i++) { |
| 208 | runHeadNodeCmd("sudo ip route add ${fabricIpPrefix}." + i.toString() + ".0/24 via ${fabricIpPrefix}.1.254 || echo route already exists") |
| 209 | } |
| 210 | for(int i=1; i<=leafSwitchNum; i++) { |
| 211 | // Figure out which compute node connects to which switch |
| 212 | leafName = "leaf-" + i.toString() |
| 213 | computeNames = getComputeNames(leafName) |
| 214 | echo "Compute nodes connnected to " + leafName + " switch:" |
| 215 | for(name in computeNames) { echo "${name}" } |
| 216 | index = 1 |
| 217 | for(name in computeNames) { |
| 218 | if(i>1) { |
| 219 | //Update fabric IP of compute nodes |
| 220 | index += 1 |
| 221 | fabricIp = runComputeNodeCmd("${name}", "ip a | grep -o '${fabricIpPrefix}.[1-9][0-9]*.[0-9]*/24'").trim() |
| 222 | if (fabricIp != "") { |
| 223 | nodeId = sh(returnStdout: true, script: "curl -u ${xosUser}:${xosPass} -X GET http://${deployment_config.head.ip}/xosapi/v1/core/nodes | jq '.[\"items\"][] | select(.dataPlaneIp==\"${fabricIp}\") | .id'").trim() |
| 224 | newFabricIp = "${fabricIpPrefix}." + i.toString() + "." + index.toString() + "/24" |
| 225 | out = sh(returnStdout: true, script: "curl -u ${xosUser}:${xosPass} -X PUT -d '{\"dataPlaneIp\":\"${newFabricIp}\"}' http://${deployment_config.head.ip}/xosapi/v1/core/nodes/${nodeId}").trim() |
| 226 | // Wait until the new fabric IP gets configured |
| 227 | timeout(time: 5) { |
| 228 | waitUntil { |
| 229 | try { |
| 230 | num = runComputeNodeCmd("${name}", "ip a | grep " + newFabricIp + " | wc -l").trim() |
| 231 | return num.toInteger() == 1 |
| 232 | } catch (exception) { |
| 233 | return false |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | else echo "Cannot find fabric IP matching pattern ${fabricIpPrefix}.[1-9][0-9]*.[0-9]*" |
| 239 | } |
| 240 | //Add routes to fabric subnets |
| 241 | for(int j=1; j<=leafSwitchNum; j++) { |
| 242 | if(j!=i) { |
| 243 | runComputeNodeCmd("${name}", "sudo ip route add ${fabricIpPrefix}." + j.toString() + ".0/24 via ${fabricIpPrefix}." + i.toString() + ".254 || echo route already exists") |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | stage ("Generate and load network configuration") { |
| 250 | // Reconnect compute nodes to update the fabric IP in ONOS |
| 251 | connectFabricAndComputeToOnos() |
| 252 | // Generate network configuration |
| 253 | runHeadNodeCmd(""" |
| 254 | cd /opt/cord_profile/ |
| 255 | cp fabric-network-cfg.json fabric-network-cfg.json.\$(date +%Y%m%d-%H%M%S) |
| 256 | cord generate > fabric-network-cfg.json |
| 257 | """) |
| 258 | // Install httpie on the head-node |
| 259 | runHeadNodeCmd("sudo pip install httpie") |
| 260 | // Delete old ONOS netcfg |
| 261 | runHeadNodeCmd("http -a onos:rocks DELETE http://onos-fabric:8181/onos/v1/network/configuration/") |
| 262 | // Load new configuration |
| 263 | runHeadNodeCmd(""" |
| 264 | cd /opt/cord_profile |
| 265 | docker-compose -p ${pod_config.cord_profile} exec -T xos_ui python /opt/xos/tosca/run.py xosadmin@opencord.org /opt/cord_profile/fabric-service.yaml |
| 266 | """) |
| 267 | // Restart ONOS apps |
| 268 | runHeadNodeCmd(""" |
| 269 | http -a onos:rocks DELETE http://onos-fabric:8181/onos/v1/applications/org.onosproject.vrouter/active; sleep 5 |
| 270 | http -a onos:rocks POST http://onos-fabric:8181/onos/v1/applications/org.onosproject.vrouter/active; sleep 5 |
| 271 | http -a onos:rocks DELETE http://onos-fabric:8181/onos/v1/applications/org.onosproject.segmentrouting/active; sleep 5 |
| 272 | http -a onos:rocks POST http://onos-fabric:8181/onos/v1/applications/org.onosproject.segmentrouting/active; sleep 5 |
| 273 | """) |
Marc De Leenheer | 672afa8 | 2017-11-27 17:25:09 -0800 | [diff] [blame] | 274 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 275 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 276 | } |
Luca Prete | fcbc54b | 2017-09-12 14:37:33 -0700 | [diff] [blame] | 277 | |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 278 | if (deployment_config.make_release == true) { |
Luca Prete | fcbc54b | 2017-09-12 14:37:33 -0700 | [diff] [blame] | 279 | stage ("Trigger Build") { |
| 280 | url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build' |
| 281 | httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201' |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | currentBuild.result = 'SUCCESS' |
| 286 | } catch (err) { |
| 287 | currentBuild.result = 'FAILURE' |
| 288 | step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${notificationEmail}", sendToIndividuals: false]) |
| 289 | } finally { |
| 290 | sh "make -C build clean-all || true" |
| 291 | sh "rm -rf *" |
| 292 | } |
| 293 | echo "RESULT: ${currentBuild.result}" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Returns a string used to bind IPs and MAC addresses, substituting the values |
| 299 | * given. |
| 300 | * |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 301 | * @param word the word used to generate the host name |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 302 | * @param counter the counter used to generate the host name |
| 303 | * @param mac the MAC address to substitute |
| 304 | * @param ip the IP address to substitute |
| 305 | */ |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 306 | def createMACIPbindingStr(word, counter, mac, ip) { |
| 307 | return """host ${word}-${counter} {'\n'hardware ethernet ${mac}';''\n'fixed-address ${ip}';''\n'}""" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Runs a command on a remote host using sshpass. |
| 312 | * |
| 313 | * @param ip the node IP address |
| 314 | * @param user the node user name |
| 315 | * @param pass the node password |
| 316 | * @param command the command to run |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 317 | * @param sshArgs arguments for the ssh command |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 318 | * @return the output of the command |
| 319 | */ |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 320 | def runCmd(ip, user, pass, command, sshArgs="") { |
| 321 | return sh(returnStdout: true, script: "sshpass -p ${pass} ssh ${sshArgs} -oStrictHostKeyChecking=no -l ${user} ${ip} \"${command}\"") |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Runs a command on the head node. |
| 326 | * |
| 327 | * @param command the command to run |
| 328 | * @param sshArgs arguments for the ssh command |
| 329 | * @return the output of the command |
| 330 | */ |
| 331 | def runHeadNodeCmd(command, sshArgs="") { |
| 332 | return sh(returnStdout: true, script: "sshpass -p ${deployment_config.head.pass} ssh ${sshArgs} -oStrictHostKeyChecking=no -l ${deployment_config.head.user} ${deployment_config.head.ip} \"${command}\"") |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Runs a command on a fabric switch. |
| 337 | * |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 338 | * @param ip the mgmt IP of the fabric switch, reachable from the head node |
| 339 | * @param user the mgmt user name of the fabric switch |
| 340 | * @param pass the mgmt password of the fabric switch |
| 341 | * @param command the command to run on the fabric switch |
| 342 | * @param ssgArgs arguments for the ssh command |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 343 | * @return the output of the command |
| 344 | */ |
You Wang | 1abf2bf | 2017-12-12 15:21:07 -0800 | [diff] [blame] | 345 | def runFabricSwitchCmd(ip, user, pass, command, sshArgs="") { |
| 346 | return sh(returnStdout: true, script: "sshpass -p ${deployment_config.head.pass} ssh ${sshArgs} -oStrictHostKeyChecking=no -l ${deployment_config.head.user} ${deployment_config.head.ip} \"sshpass -p ${pass} ssh ${sshArgs} -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}\"") |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Runs a command on all fabric switches |
| 351 | * |
| 352 | * @param command the command to run on the fabric switches |
| 353 | * @param ssgArgs arguments for the ssh command |
| 354 | */ |
| 355 | def runFabricSwitchCmdAll(command, sshArgs="") { |
| 356 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 357 | runFabricSwitchCmd("${deployment_config.fabric_switches[i].ip}", |
| 358 | "${deployment_config.fabric_switches[i].user}", |
| 359 | "${deployment_config.fabric_switches[i].pass}", |
| 360 | "${command}", |
| 361 | "${sshArgs}") |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Runs a command on a compute node. |
| 367 | * |
| 368 | * @param name the name of the compute node |
| 369 | * @param command the command to run on the compute node |
| 370 | * @param ssgArgs arguments for the ssh command |
| 371 | * @return the output of the command |
| 372 | */ |
| 373 | def runComputeNodeCmd(name, command, sshArgs="") { |
| 374 | return sh(returnStdout: true, script: "sshpass -p ${deployment_config.head.pass} ssh ${sshArgs} -oStrictHostKeyChecking=no -l ${deployment_config.head.user} ${deployment_config.head.ip} \"ssh ${sshArgs} ubuntu@${name} ${command}\"") |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Runs a command on all compute nodes |
| 379 | * |
| 380 | * @param command the command to run on the compute nodes |
| 381 | * @param ssgArgs arguments for the ssh command |
| 382 | */ |
| 383 | def runComputeNodeCmdAll(command, sshArgs="") { |
| 384 | computeNamesAll = getComputeNames() |
| 385 | for (name in computeNamesAll) { |
| 386 | runComputeNodeCmd("${name}", "${command}", "${sshArgs}") |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Runs an ONOS CLI command |
| 392 | * |
| 393 | * @param name the onos node name, reachable from the head node |
| 394 | * @param port the port used to login to ONOS CLI |
| 395 | * @param user the user name to login to ONOS CLI |
| 396 | * @param pass the password to login to ONOS CLI |
| 397 | * @param command the command to run in ONOS CLI |
| 398 | * @return the output of the command |
| 399 | */ |
| 400 | def runOnosCliCmd(name, port, user, pass, command) { |
| 401 | return sh(returnStdout: true, script: "sshpass -p ${deployment_config.head.pass} ssh -oStrictHostKeyChecking=no -l ${deployment_config.head.user} ${deployment_config.head.ip} \"sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} -p ${port} ${name} ${command}\"") |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Returns a list of compute node names. When "role" is specified, returns only |
| 406 | * names of compute nodes connected to the switch |
| 407 | * |
| 408 | * @param role the switch role, i.e. "leaf-1" |
| 409 | */ |
| 410 | def getComputeNames(role="") { |
| 411 | computeNamesAll = runHeadNodeCmd("cord prov list | grep node | awk '{print \\\$2}' | sed -e \\\"s/.*/'&'/\\\"").trim() |
| 412 | computeNamesAll = "${computeNamesAll}".split() |
| 413 | computeNamesAll = "${computeNamesAll}".replaceAll("'", "\"") |
| 414 | computeNamesAll = new JsonSlurperClassic().parseText("${computeNamesAll}") |
| 415 | if ("${role}" == "") return computeNamesAll |
| 416 | computeNames = [] |
| 417 | switchMac = "" |
| 418 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 419 | if ("${deployment_config.fabric_switches[i].role}" == "${role}") |
| 420 | switchMac = "${deployment_config.fabric_switches[i].mac}" |
| 421 | } |
| 422 | if ("${switchMac}" != "") { |
| 423 | switchMac = switchMac.toLowerCase().replaceAll(':','') |
| 424 | // Get fabric IPs of compute nodes connected to the switch |
| 425 | computeFabricIps = runHeadNodeCmd("sshpass -p rocks ssh -q -oStrictHostKeyChecking=no -l onos -p 8101 onos-fabric hosts -j | jq '.[] | select(.location.elementId | contains(\\\"${switchMac}\\\")) | .ipAddresses' | grep -o '\\\"${fabricIpPrefix}.[1-9][0-9]*.[0-9]*\\\"'", |
| 426 | "-q").trim() |
| 427 | computeFabricIps = "${computeFabricIps}".split() |
| 428 | computeFabricIps = new JsonSlurperClassic().parseText("${computeFabricIps}") |
| 429 | // Figure out which compute node connects to the switch |
| 430 | for (name in computeNamesAll) { |
| 431 | fabricIp = runComputeNodeCmd("${name}", "ip a | grep -o '${fabricIpPrefix}.[1-9][0-9]*.[0-9]*'").trim() |
| 432 | if (fabricIp in computeFabricIps) { |
| 433 | computeNames.add("${name}") |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | return computeNames |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Connects all fabric switches and compute nodes to ONOS |
| 442 | */ |
| 443 | def connectFabricAndComputeToOnos() { |
| 444 | // Kill existing switch connections |
| 445 | runFabricSwitchCmdAll("./killit || echo no ofagentapp running") |
| 446 | // Clean stale ONOS data |
| 447 | runOnosCliCmd("onos-fabric", "8101", "onos", "rocks", "wipe-out -r -j please") |
| 448 | // Connect switches to ONOS |
| 449 | for(int i=0; i < deployment_config.fabric_switches.size(); i++) { |
| 450 | runFabricSwitchCmd("${deployment_config.fabric_switches[i].ip}", |
| 451 | "${deployment_config.fabric_switches[i].user}", |
| 452 | "${deployment_config.fabric_switches[i].pass}", |
| 453 | "./connect -bg 2>&1 > ${deployment_config.fabric_switches[i].ip}.log", |
| 454 | "-qftn") |
| 455 | } |
| 456 | // Verify ONOS has recognized the switches |
| 457 | timeout(time: 5) { |
| 458 | waitUntil { |
| 459 | try { |
| 460 | num = runHeadNodeCmd("\"sshpass -p rocks ssh -q -oStrictHostKeyChecking=no -l onos -p 8101 onos-fabric devices | grep available=true | wc -l\"").trim() |
| 461 | return num.toInteger() == deployment_config.fabric_switches.size() |
| 462 | } catch (exception) { |
| 463 | return false |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | // Connect compute nodes to ONOS |
| 468 | runHeadNodeCmd("ping -c 1 ${fabricIpPrefix}.1.254", "-qftn") |
| 469 | runComputeNodeCmdAll("ping -c 1 ${fabricIpPrefix}.1.254", "-qftn") |
| 470 | // Verify ONOS has recognized the hosts |
| 471 | timeout(time: 5) { |
| 472 | waitUntil { |
| 473 | try { |
| 474 | num = runHeadNodeCmd("\"sshpass -p rocks ssh -q -oStrictHostKeyChecking=no -l onos -p 8101 onos-fabric hosts | grep id= | wc -l\"").trim() |
| 475 | return num.toInteger() == deployment_config.compute_nodes.size() + 1 |
| 476 | } catch (exception) { |
| 477 | return false |
| 478 | } |
| 479 | } |
| 480 | } |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 481 | } |