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