You Wang | cebbd31 | 2017-09-11 14:35:24 -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 | |
| 15 | def filename = 'manifest-${branch}.xml' |
| 16 | def manifestUrl = 'https://gerrit.opencord.org/manifest' |
| 17 | def config = null; |
| 18 | |
| 19 | node ('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 | |
| 34 | node ("${config.dev_node.name}") { |
| 35 | timeout (time: 240) { |
| 36 | stage ('Clean up old build') { |
| 37 | sh "make -C build clean-all || true" |
| 38 | sh "rm -rf *" |
| 39 | sh "ssh-keygen -R ${config.head.ip}" |
| 40 | } |
| 41 | stage ('Checkout cord repo') { |
| 42 | checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true] |
| 43 | } |
| 44 | |
| 45 | dir('build') { |
| 46 | try { |
| 47 | stage ("Re-deploy head node") { |
| 48 | maasOps: { |
| 49 | sh "maas login maas http://${config.maas.ip}/MAAS/api/2.0 ${config.maas.api_key}" |
| 50 | sh "maas maas machine release ${config.maas.head_system_id}" |
| 51 | |
| 52 | timeout(time: 15) { |
| 53 | waitUntil { |
| 54 | try { |
| 55 | sh "maas maas machine read ${config.maas.head_system_id} | grep Ready" |
| 56 | return true |
| 57 | } catch (exception) { |
| 58 | return false |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | sh 'maas maas machines allocate' |
| 64 | sh "maas maas machine deploy ${config.maas.head_system_id}" |
| 65 | |
| 66 | timeout(time: 30) { |
| 67 | waitUntil { |
| 68 | try { |
| 69 | sh "maas maas machine read ${config.maas.head_system_id} | grep Deployed" |
| 70 | return true |
| 71 | } catch (exception) { |
| 72 | return false |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | stage ("Download CORD POD configuration") { |
| 80 | sh "cd podconfig; git clone -b ${branch} ${config.pod_config.repo_url} automation" |
| 81 | } |
| 82 | |
| 83 | stage ("Generate CORD configuration") { |
| 84 | sh "make PODCONFIG=automation/${config.pod_config.file_name} config" |
| 85 | } |
| 86 | |
| 87 | stage ("Deploy") { |
| 88 | sh "make build" |
| 89 | } |
| 90 | |
| 91 | stage ("Power cycle compute nodes") { |
| 92 | for(int i=0; i < config.compute_nodes.size(); i++) { |
| 93 | 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" |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | stage ("Wait for compute nodes to get deployed") { |
| 98 | sh "ssh-keygen -f /home/${config.dev_node.user}/.ssh/known_hosts -R ${config.head.ip}" |
| 99 | def cordApiKey = runCmd("${config.head.ip}", |
| 100 | "${config.head.user}", |
| 101 | "${config.head.pass}", |
| 102 | "sudo maas-region-admin apikey --username ${config.head.user}") |
| 103 | runCmd("${config.head.ip}", |
| 104 | "${config.head.user}", |
| 105 | "${config.head.pass}", |
| 106 | "maas login pod-maas http://${config.head.ip}/MAAS/api/1.0 ${cordApiKey}") |
| 107 | timeout(time: 45) { |
| 108 | waitUntil { |
| 109 | try { |
| 110 | num = runCmd("${config.head.ip}", |
| 111 | "${config.head.user}", |
| 112 | "${config.head.pass}", |
| 113 | "maas pod-maas nodes list | grep -i deployed | wc -l").trim() |
| 114 | return num.toInteger() == config.compute_nodes.size() |
| 115 | } catch (exception) { |
| 116 | return false |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | stage ("Wait for compute nodes to be provisioned") { |
| 123 | timeout(time:45) { |
| 124 | waitUntil { |
| 125 | try { |
| 126 | num = runCmd("${config.head.ip}", |
| 127 | "${config.head.user}", |
| 128 | "${config.head.pass}", |
| 129 | "cord prov list '|' grep -i node '|' grep -i complete '|' wc -l").trim() |
| 130 | return num.toInteger() == config.compute_nodes.size() |
| 131 | } catch (exception) { |
| 132 | return false |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (config.fabric_switches != null) { |
| 139 | stage("Reserve IPs for fabric switches and restart maas-dhcp service") { |
| 140 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 141 | def append = ""; |
| 142 | if (i!=0) { |
| 143 | append = "-a"; |
| 144 | } |
| 145 | def str = createMACIPbindingStr(i+1, |
| 146 | "${config.fabric_switches[i].mac}", |
| 147 | "${config.fabric_switches[i].ip}") |
| 148 | runCmd("${config.head.ip}", |
| 149 | "${config.head.user}", |
| 150 | "${config.head.pass}", |
| 151 | "echo -e $str '|' sudo tee $append /etc/dhcp/dhcpd.reservations > /dev/null") |
| 152 | } |
| 153 | runCmd("${config.head.ip}", |
| 154 | "${config.head.user}", |
| 155 | "${config.head.pass}", |
| 156 | "sudo restart maas-dhcpd") |
| 157 | |
| 158 | runCmd("${config.head.ip}", |
| 159 | "${config.head.user}", |
| 160 | "${config.head.pass}", |
| 161 | "cord harvest go") |
| 162 | } |
| 163 | |
| 164 | stage ("Wait for fabric switches to get deployed") { |
| 165 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 166 | runFabricCmd("${config.head.ip}", |
| 167 | "${config.head.user}", |
| 168 | "${config.head.pass}", |
| 169 | "${config.fabric_switches[i].ip}", |
| 170 | "${config.fabric_switches[i].user}", |
| 171 | "${config.fabric_switches[i].pass}", |
| 172 | "sudo onl-onie-boot-mode install") |
| 173 | |
| 174 | runFabricCmd("${config.head.ip}", |
| 175 | "${config.head.user}", |
| 176 | "${config.head.pass}", |
| 177 | "${config.fabric_switches[i].ip}", |
| 178 | "${config.fabric_switches[i].user}", |
| 179 | "${config.fabric_switches[i].pass}", |
| 180 | "sudo reboot") |
| 181 | } |
| 182 | timeout(time: 45) { |
| 183 | waitUntil { |
| 184 | try { |
| 185 | def harvestCompleted = runCmd("${config.head.ip}", |
| 186 | "${config.head.user}", |
| 187 | "${config.head.pass}", |
| 188 | "cord harvest list '|' grep -i fabric '|' wc -l").trim() |
| 189 | return harvestCompleted.toInteger() == config.fabric_switches.size() |
| 190 | } catch (exception) { |
| 191 | return false |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | stage ("Wait for fabric switches to be provisioned") { |
| 198 | timeout(time:45) { |
| 199 | waitUntil { |
| 200 | try { |
| 201 | def provCompleted = 0 |
| 202 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 203 | def count = runCmd("${config.head.ip}", |
| 204 | "${config.head.user}", |
| 205 | "${config.head.pass}", |
| 206 | "cord prov list '|' grep -i ${config.fabric_switches[i].ip} '|' grep -i complete '|' wc -l").trim() |
| 207 | provCompleted = provCompleted + count.toInteger() |
| 208 | } |
| 209 | return provCompleted == config.fabric_switches.size() |
| 210 | } catch (exception) { |
| 211 | return false |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (config.make_release == true) { |
| 219 | stage ("Trigger Build") { |
| 220 | url = 'https://jenkins.opencord.org/job/release-build/job/' + params.branch + '/build' |
| 221 | httpRequest authentication: 'auto-release', httpMode: 'POST', url: url, validResponseCodes: '201' |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | currentBuild.result = 'SUCCESS' |
| 226 | } catch (err) { |
| 227 | currentBuild.result = 'FAILURE' |
| 228 | step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: "${notificationEmail}", sendToIndividuals: false]) |
| 229 | } finally { |
| 230 | } |
| 231 | echo "RESULT: ${currentBuild.result}" |
| 232 | } |
| 233 | } |
| 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 | */ |
| 244 | def 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 | */ |
| 257 | def 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 | */ |
| 273 | def 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 | } |