blob: 92b179674b1afcf4007fcd00ddb19b87dda1abf6 [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
85 stage ("Deploy") {
86 sh "make build"
87 }
88
89 stage ("Power cycle compute nodes") {
90 for(int i=0; i < config.compute_nodes.size(); i++) {
91 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"
92 }
93 }
94
95 stage ("Wait for compute nodes to get deployed") {
96 sh "ssh-keygen -f /home/${config.dev_node.user}/.ssh/known_hosts -R ${config.head.ip}"
97 def cordApiKey = runCmd("${config.head.ip}",
98 "${config.head.user}",
99 "${config.head.pass}",
100 "sudo maas-region-admin apikey --username ${config.head.user}")
101 runCmd("${config.head.ip}",
102 "${config.head.user}",
103 "${config.head.pass}",
104 "maas login pod-maas http://${config.head.ip}/MAAS/api/1.0 ${cordApiKey}")
105 timeout(time: 45) {
106 waitUntil {
107 try {
108 num = runCmd("${config.head.ip}",
109 "${config.head.user}",
110 "${config.head.pass}",
Andy Bavier4c3e4152017-09-14 07:38:50 -0700111 "maas pod-maas nodes list | grep substatus_name | grep -i deployed | wc -l").trim()
You Wangcebbd312017-09-11 14:35:24 -0700112 return num.toInteger() == config.compute_nodes.size()
113 } catch (exception) {
114 return false
115 }
116 }
117 }
118 }
119
120 stage ("Wait for compute nodes to be provisioned") {
121 timeout(time:45) {
122 waitUntil {
123 try {
124 num = runCmd("${config.head.ip}",
125 "${config.head.user}",
126 "${config.head.pass}",
127 "cord prov list '|' grep -i node '|' grep -i complete '|' wc -l").trim()
128 return num.toInteger() == config.compute_nodes.size()
129 } catch (exception) {
130 return false
131 }
132 }
133 }
134 }
135
136 if (config.fabric_switches != null) {
137 stage("Reserve IPs for fabric switches and restart maas-dhcp service") {
138 for(int i=0; i < config.fabric_switches.size(); i++) {
139 def append = "";
140 if (i!=0) {
141 append = "-a";
142 }
143 def str = createMACIPbindingStr(i+1,
144 "${config.fabric_switches[i].mac}",
145 "${config.fabric_switches[i].ip}")
146 runCmd("${config.head.ip}",
147 "${config.head.user}",
148 "${config.head.pass}",
149 "echo -e $str '|' sudo tee $append /etc/dhcp/dhcpd.reservations > /dev/null")
150 }
151 runCmd("${config.head.ip}",
152 "${config.head.user}",
153 "${config.head.pass}",
154 "sudo restart maas-dhcpd")
155
156 runCmd("${config.head.ip}",
157 "${config.head.user}",
158 "${config.head.pass}",
159 "cord harvest go")
160 }
161
162 stage ("Wait for fabric switches to get deployed") {
163 for(int i=0; i < config.fabric_switches.size(); i++) {
164 runFabricCmd("${config.head.ip}",
165 "${config.head.user}",
166 "${config.head.pass}",
167 "${config.fabric_switches[i].ip}",
168 "${config.fabric_switches[i].user}",
169 "${config.fabric_switches[i].pass}",
170 "sudo onl-onie-boot-mode install")
171
172 runFabricCmd("${config.head.ip}",
173 "${config.head.user}",
174 "${config.head.pass}",
175 "${config.fabric_switches[i].ip}",
176 "${config.fabric_switches[i].user}",
177 "${config.fabric_switches[i].pass}",
178 "sudo reboot")
179 }
180 timeout(time: 45) {
181 waitUntil {
182 try {
183 def harvestCompleted = runCmd("${config.head.ip}",
184 "${config.head.user}",
185 "${config.head.pass}",
186 "cord harvest list '|' grep -i fabric '|' wc -l").trim()
187 return harvestCompleted.toInteger() == config.fabric_switches.size()
188 } catch (exception) {
189 return false
190 }
191 }
192 }
193 }
194
195 stage ("Wait for fabric switches to be provisioned") {
196 timeout(time:45) {
197 waitUntil {
198 try {
199 def provCompleted = 0
200 for(int i=0; i < config.fabric_switches.size(); i++) {
201 def count = runCmd("${config.head.ip}",
202 "${config.head.user}",
203 "${config.head.pass}",
204 "cord prov list '|' grep -i ${config.fabric_switches[i].ip} '|' grep -i complete '|' wc -l").trim()
205 provCompleted = provCompleted + count.toInteger()
206 }
207 return provCompleted == config.fabric_switches.size()
208 } catch (exception) {
209 return false
210 }
211 }
212 }
213 }
214 }
You Wangcebbd312017-09-11 14:35:24 -0700215 }
Luca Prete9081fcc2017-09-12 14:37:33 -0700216
217 if (config.make_release == true) {
218 stage ("Trigger Build") {
219 url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build'
220 httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201'
221 }
222 }
223
224 currentBuild.result = 'SUCCESS'
225 } catch (err) {
226 currentBuild.result = 'FAILURE'
227 step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${notificationEmail}", sendToIndividuals: false])
228 } finally {
229 sh "make -C build clean-all || true"
230 sh "rm -rf *"
231 }
232 echo "RESULT: ${currentBuild.result}"
You Wangcebbd312017-09-11 14:35:24 -0700233 }
234}
235
236/**
237 * Returns a string used to bind IPs and MAC addresses, substituting the values
238 * given.
239 *
240 * @param counter the counter used to generate the host name
241 * @param mac the MAC address to substitute
242 * @param ip the IP address to substitute
243 */
244def createMACIPbindingStr(counter, mac, ip) {
245 return """\\'host fabric${counter} {'\n'hardware ethernet ${mac}';''\n'fixed-address ${ip}';''\n'}\\'"""
246}
247
248/**
249 * Runs a command on a remote host using sshpass.
250 *
251 * @param ip the node IP address
252 * @param user the node user name
253 * @param pass the node password
254 * @param command the command to run
255 * @return the output of the command
256 */
257def runCmd(ip, user, pass, command) {
258 return sh(returnStdout: true, script: "sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}")
259}
260
261/**
262 * Runs a command on a fabric switch.
263 *
264 * @param headIp the head node IP address
265 * @param headUser the head node user name
266 * @param headPass the head node password
267 * @param ip the mgmt IP of the fabric switch, reachable from the head node
268 * @param user the mgmt user name of the fabric switch
269 * @param pass the mgmt password of the fabric switch
270 * @param command the command to run on the fabric switch
271 * @return the output of the command
272 */
273def runFabricCmd(headIp, headUser, headPass, ip, user, pass, command) {
274 return sh(returnStdout: true, script: "sshpass -p ${headPass} ssh -oStrictHostKeyChecking=no -l ${headUser} ${headIp} \"sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}\"")
275}