blob: c7da68bfc724d1e7950983f7b65d9d6378155e6f [file] [log] [blame]
You Wangcebbd312017-09-11 14:35:24 -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
15def filename = 'manifest-${branch}.xml'
16def manifestUrl = 'https://gerrit.opencord.org/manifest'
17def config = null;
18
19node ('master') {
20 checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true]
21
22 stage ("Generate and Copy Manifest file") {
23 sh returnStdout: true, script: 'repo manifest -r -o ' + filename
24 sh returnStdout: true, script: 'cp ' + filename + ' ' + env.JENKINS_HOME + '/tmp'
25 }
26
27 stage ("Parse deployment configuration file") {
28 sh returnStdout: true, script: 'rm -rf ${configRepoBaseDir}'
29 sh returnStdout: true, script: 'git clone -b ${branch} ${configRepoUrl}'
30 config = readYaml file: "${configRepoBaseDir}${configRepoFile}"
31 }
32}
33
34node ("${config.dev_node.name}") {
35 timeout (time: 240) {
Luca Prete7b4a1e82017-09-12 10:44:48 -070036 stage ('Remove old head node from known hosts') {
You Wangcebbd312017-09-11 14:35:24 -070037 sh "ssh-keygen -R ${config.head.ip}"
38 }
39 stage ('Checkout cord repo') {
40 checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true]
41 }
42
Luca Prete9081fcc2017-09-12 14:37:33 -070043 try {
44 dir('build') {
You Wangcebbd312017-09-11 14:35:24 -070045 stage ("Re-deploy head node") {
46 maasOps: {
47 sh "maas login maas http://${config.maas.ip}/MAAS/api/2.0 ${config.maas.api_key}"
48 sh "maas maas machine release ${config.maas.head_system_id}"
49
50 timeout(time: 15) {
51 waitUntil {
52 try {
53 sh "maas maas machine read ${config.maas.head_system_id} | grep Ready"
54 return true
55 } catch (exception) {
56 return false
57 }
58 }
59 }
60
61 sh 'maas maas machines allocate'
62 sh "maas maas machine deploy ${config.maas.head_system_id}"
63
64 timeout(time: 30) {
65 waitUntil {
66 try {
67 sh "maas maas machine read ${config.maas.head_system_id} | grep Deployed"
68 return true
69 } catch (exception) {
70 return false
71 }
72 }
73 }
74 }
75 }
76
77 stage ("Download CORD POD configuration") {
78 sh "cd podconfig; git clone -b ${branch} ${config.pod_config.repo_url} automation"
79 }
80
81 stage ("Generate CORD configuration") {
82 sh "make PODCONFIG=automation/${config.pod_config.file_name} config"
83 }
84
Andy Bavier6c2f8042017-09-19 13:52:30 -070085 if (config.fabric_switches != null) {
86 stage("Reserve IPs for fabric switches") {
87 for(int i=0; i < config.fabric_switches.size(); i++) {
88 def str = createMACIPbindingStr(i+1,
89 "${config.fabric_switches[i].mac}",
90 "${config.fabric_switches[i].ip}")
91 sh "echo $str >> maas/roles/maas/files/dhcpd.reservations"
92 }
93 }
94 }
95
You Wangcebbd312017-09-11 14:35:24 -070096 stage ("Deploy") {
97 sh "make build"
98 }
99
100 stage ("Power cycle compute nodes") {
101 for(int i=0; i < config.compute_nodes.size(); i++) {
102 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"
103 }
104 }
105
106 stage ("Wait for compute nodes to get deployed") {
107 sh "ssh-keygen -f /home/${config.dev_node.user}/.ssh/known_hosts -R ${config.head.ip}"
108 def cordApiKey = runCmd("${config.head.ip}",
109 "${config.head.user}",
110 "${config.head.pass}",
111 "sudo maas-region-admin apikey --username ${config.head.user}")
112 runCmd("${config.head.ip}",
113 "${config.head.user}",
114 "${config.head.pass}",
115 "maas login pod-maas http://${config.head.ip}/MAAS/api/1.0 ${cordApiKey}")
116 timeout(time: 45) {
117 waitUntil {
118 try {
119 num = runCmd("${config.head.ip}",
120 "${config.head.user}",
121 "${config.head.pass}",
Andy Bavier4c3e4152017-09-14 07:38:50 -0700122 "maas pod-maas nodes list | grep substatus_name | grep -i deployed | wc -l").trim()
You Wangcebbd312017-09-11 14:35:24 -0700123 return num.toInteger() == config.compute_nodes.size()
124 } catch (exception) {
125 return false
126 }
127 }
128 }
129 }
130
131 stage ("Wait for compute nodes to be provisioned") {
132 timeout(time:45) {
133 waitUntil {
134 try {
135 num = runCmd("${config.head.ip}",
136 "${config.head.user}",
137 "${config.head.pass}",
138 "cord prov list '|' grep -i node '|' grep -i complete '|' wc -l").trim()
139 return num.toInteger() == config.compute_nodes.size()
140 } catch (exception) {
141 return false
142 }
143 }
144 }
145 }
146
147 if (config.fabric_switches != null) {
You Wangcebbd312017-09-11 14:35:24 -0700148
149 stage ("Wait for fabric switches to get deployed") {
150 for(int i=0; i < config.fabric_switches.size(); i++) {
151 runFabricCmd("${config.head.ip}",
152 "${config.head.user}",
153 "${config.head.pass}",
154 "${config.fabric_switches[i].ip}",
155 "${config.fabric_switches[i].user}",
156 "${config.fabric_switches[i].pass}",
157 "sudo onl-onie-boot-mode install")
158
159 runFabricCmd("${config.head.ip}",
160 "${config.head.user}",
161 "${config.head.pass}",
162 "${config.fabric_switches[i].ip}",
163 "${config.fabric_switches[i].user}",
164 "${config.fabric_switches[i].pass}",
165 "sudo reboot")
Andy Bavier6c2f8042017-09-19 13:52:30 -0700166
167 // Ensure that switches get provisioned after ONIE reinstall.
168 // Delete them if they were provisioned earlier. If the switches are not
169 // present in 'cord prov list', this command has no effect.
170 runCmd("${config.head.ip}",
171 "${config.head.user}",
172 "${config.head.pass}",
173 "cord prov delete ${config.fabric_switches[i].mac}")
You Wangcebbd312017-09-11 14:35:24 -0700174 }
175 timeout(time: 45) {
176 waitUntil {
177 try {
178 def harvestCompleted = runCmd("${config.head.ip}",
179 "${config.head.user}",
180 "${config.head.pass}",
181 "cord harvest list '|' grep -i fabric '|' wc -l").trim()
182 return harvestCompleted.toInteger() == config.fabric_switches.size()
183 } catch (exception) {
184 return false
185 }
186 }
187 }
188 }
189
190 stage ("Wait for fabric switches to be provisioned") {
191 timeout(time:45) {
192 waitUntil {
193 try {
194 def provCompleted = 0
195 for(int i=0; i < config.fabric_switches.size(); i++) {
196 def count = runCmd("${config.head.ip}",
197 "${config.head.user}",
198 "${config.head.pass}",
199 "cord prov list '|' grep -i ${config.fabric_switches[i].ip} '|' grep -i complete '|' wc -l").trim()
200 provCompleted = provCompleted + count.toInteger()
201 }
202 return provCompleted == config.fabric_switches.size()
203 } catch (exception) {
204 return false
205 }
206 }
207 }
208 }
209 }
You Wangcebbd312017-09-11 14:35:24 -0700210 }
Luca Prete9081fcc2017-09-12 14:37:33 -0700211
212 if (config.make_release == true) {
213 stage ("Trigger Build") {
214 url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build'
215 httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201'
216 }
217 }
218
219 currentBuild.result = 'SUCCESS'
220 } catch (err) {
221 currentBuild.result = 'FAILURE'
222 step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${notificationEmail}", sendToIndividuals: false])
223 } finally {
224 sh "make -C build clean-all || true"
225 sh "rm -rf *"
226 }
227 echo "RESULT: ${currentBuild.result}"
You Wangcebbd312017-09-11 14:35:24 -0700228 }
229}
230
231/**
232 * Returns a string used to bind IPs and MAC addresses, substituting the values
233 * given.
234 *
235 * @param counter the counter used to generate the host name
236 * @param mac the MAC address to substitute
237 * @param ip the IP address to substitute
238 */
239def createMACIPbindingStr(counter, mac, ip) {
Andy Bavier6c2f8042017-09-19 13:52:30 -0700240 return """host fabric${counter} {'\n'hardware ethernet ${mac}';''\n'fixed-address ${ip}';''\n'}"""
You Wangcebbd312017-09-11 14:35:24 -0700241}
242
243/**
244 * Runs a command on a remote host using sshpass.
245 *
246 * @param ip the node IP address
247 * @param user the node user name
248 * @param pass the node password
249 * @param command the command to run
250 * @return the output of the command
251 */
252def runCmd(ip, user, pass, command) {
253 return sh(returnStdout: true, script: "sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}")
254}
255
256/**
257 * Runs a command on a fabric switch.
258 *
259 * @param headIp the head node IP address
260 * @param headUser the head node user name
261 * @param headPass the head node password
262 * @param ip the mgmt IP of the fabric switch, reachable from the head node
263 * @param user the mgmt user name of the fabric switch
264 * @param pass the mgmt password of the fabric switch
265 * @param command the command to run on the fabric switch
266 * @return the output of the command
267 */
268def runFabricCmd(headIp, headUser, headPass, ip, user, pass, command) {
269 return sh(returnStdout: true, script: "sshpass -p ${headPass} ssh -oStrictHostKeyChecking=no -l ${headUser} ${headIp} \"sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}\"")
270}