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 | |
| 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 ('Checkout cord repo') { |
| 37 | checkout changelog: false, poll: false, scm: [$class: 'RepoScm', currentBranch: true, manifestBranch: params.branch, manifestRepositoryUrl: "${manifestUrl}", quiet: true] |
| 38 | } |
| 39 | |
| 40 | dir('build') { |
| 41 | try { |
| 42 | stage ("Re-deploy head node") { |
| 43 | maasOps: { |
| 44 | sh "maas login maas http://${config.maas.ip}/MAAS/api/2.0 ${config.maas.api_key}" |
| 45 | sh "maas maas machine release ${config.maas.head_system_id}" |
| 46 | |
| 47 | timeout(time: 15) { |
| 48 | waitUntil { |
| 49 | try { |
| 50 | sh "maas maas machine read ${config.maas.head_system_id} | grep Ready" |
| 51 | return true |
| 52 | } catch (exception) { |
| 53 | return false |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | sh 'maas maas machines allocate' |
| 59 | sh "maas maas machine deploy ${config.maas.head_system_id}" |
| 60 | |
| 61 | timeout(time: 30) { |
| 62 | waitUntil { |
| 63 | try { |
| 64 | sh "maas maas machine read ${config.maas.head_system_id} | grep Deployed" |
| 65 | return true |
| 66 | } catch (exception) { |
| 67 | return false |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | stage ("Download CORD POD configuration") { |
| 75 | sh "cd podconfig; git clone -b ${branch} ${config.pod_config.repo_url} automation" |
| 76 | } |
| 77 | |
| 78 | stage ("Generate CORD configuration") { |
Luca Prete | 535769e | 2017-09-01 16:16:35 -0700 | [diff] [blame^] | 79 | sh "make PODCONFIG=automation/${config.pod_config.file_name} config" |
Luca Prete | e2d51fd | 2017-08-31 16:19:21 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | stage ("Deploy") { |
| 83 | sh "VAGRANT_PROVIDER=virtualbox make build" |
| 84 | } |
| 85 | |
| 86 | stage ("Power cycle compute nodes") { |
| 87 | for(int i=0; i < config.compute_nodes.size(); i++) { |
| 88 | 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" |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | stage ("Wait for compute nodes to get deployed") { |
| 93 | sh "ssh-keygen -f /home/${config.dev_node.user}/.ssh/known_hosts -R ${config.head.ip}" |
| 94 | def cordApiKey = runCmd("${config.head.ip}", |
| 95 | "${config.head.user}", |
| 96 | "${config.head.pass}", |
| 97 | "sudo maas-region-admin apikey --username ${config.head.user}") |
| 98 | runCmd("${config.head.ip}", |
| 99 | "${config.head.user}", |
| 100 | "${config.head.pass}", |
| 101 | "maas login pod-maas http://${config.head.ip}/MAAS/api/1.0 ${cordApiKey}") |
| 102 | timeout(time: 45) { |
| 103 | waitUntil { |
| 104 | try { |
| 105 | num = runCmd("${config.head.ip}", |
| 106 | "${config.head.user}", |
| 107 | "${config.head.pass}", |
| 108 | "maas pod-maas nodes list | grep -i deployed | wc -l").trim() |
| 109 | return num.toInteger() == config.compute_nodes.size() |
| 110 | } catch (exception) { |
| 111 | return false |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | stage ("Wait for computes nodes to be provisioned") { |
| 118 | ip = runCmd("${config.head.ip}", |
| 119 | "${config.head.user}", |
| 120 | "${config.head.pass}", |
| 121 | "docker inspect --format '{{.NetworkSettings.Networks.maas_default.IPAddress}}' provisioner").trim() |
| 122 | timeout(time:45) { |
| 123 | waitUntil { |
| 124 | try { |
| 125 | out = runCmd("${config.head.ip}", |
| 126 | "${config.head.user}", |
| 127 | "${config.head.pass}", |
| 128 | "curl -sS http://$ip:4243/provision/ | jq -c \".[] | select(.status | contains(${config.compute_nodes.size()}))\"".trim()) |
| 129 | return out != "" |
| 130 | } catch (exception) { |
| 131 | return false |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (config.fabric_switches != null) { |
| 138 | stage("Reserve IPs for fabric switches and restart maas-dhcp service") { |
| 139 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 140 | def append = ""; |
| 141 | if (i!=0) { |
| 142 | append = "-a"; |
| 143 | } |
| 144 | def str = createMACIPbindingStr(i+1, |
| 145 | "${config.fabric_switches[i].mac}", |
| 146 | "${config.fabric_switches[i].ip}") |
| 147 | runCmd("${config.head.ip}", |
| 148 | "${config.head.user}", |
| 149 | "${config.head.pass}", |
| 150 | "echo -e $str '|' sudo tee $append /etc/dhcp/dhcpd.reservations > /dev/null") |
| 151 | } |
| 152 | runCmd("${config.head.ip}", |
| 153 | "${config.head.user}", |
| 154 | "${config.head.pass}", |
| 155 | "sudo restart maas-dhcpd") |
| 156 | |
| 157 | runCmd("${config.head.ip}", |
| 158 | "${config.head.user}", |
| 159 | "${config.head.pass}", |
| 160 | "cord harvest go") |
| 161 | } |
| 162 | |
| 163 | stage ("Wait for fabric switches to get deployed") { |
| 164 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 165 | runFabricCmd("${config.head.ip}", |
| 166 | "${config.head.user}", |
| 167 | "${config.head.pass}", |
| 168 | "${config.fabric_switches[i].ip}", |
| 169 | "${config.fabric_switches[i].user}", |
| 170 | "${config.fabric_switches[i].pass}", |
| 171 | "sudo onl-onie-boot-mode install") |
| 172 | |
| 173 | runFabricCmd("${config.head.ip}", |
| 174 | "${config.head.user}", |
| 175 | "${config.head.pass}", |
| 176 | "${config.fabric_switches[i].ip}", |
| 177 | "${config.fabric_switches[i].user}", |
| 178 | "${config.fabric_switches[i].pass}", |
| 179 | "sudo reboot") |
| 180 | } |
| 181 | timeout(time: 45) { |
| 182 | waitUntil { |
| 183 | try { |
| 184 | def harvestCompleted = runCmd("${config.head.ip}", |
| 185 | "${config.head.user}", |
| 186 | "${config.head.pass}", |
| 187 | "cord harvest list '|' grep -i fabric '|' wc -l").trim() |
| 188 | return harvestCompleted.toInteger() == config.fabric_switches.size() |
| 189 | } catch (exception) { |
| 190 | return false |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | stage ("Wait for fabric switches to be provisioned") { |
| 197 | timeout(time:45) { |
| 198 | waitUntil { |
| 199 | try { |
| 200 | def provCompleted = 0 |
| 201 | for(int i=0; i < config.fabric_switches.size(); i++) { |
| 202 | def count = runCmd("${config.head.ip}", |
| 203 | "${config.head.user}", |
| 204 | "${config.head.pass}", |
| 205 | "cord prov list '|' grep -i ${config.fabric_switches[i].ip} '|' grep -i complete '|' wc -l").trim() |
| 206 | provCompleted = provCompleted + count.toInteger() |
| 207 | } |
| 208 | return provCompleted == config.fabric_switches.size() |
| 209 | } catch (exception) { |
| 210 | return false |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 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 'vagrant destroy -f corddev' |
| 230 | sh 'rm -rf podconfigs/automation' |
| 231 | } |
| 232 | echo "RESULT: ${currentBuild.result}" |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns a string used to bind IPs and MAC addresses, substituting the values |
| 239 | * given. |
| 240 | * |
| 241 | * @param counter the counter used to generate the host name |
| 242 | * @param mac the MAC address to substitute |
| 243 | * @param ip the IP address to substitute |
| 244 | */ |
| 245 | def createMACIPbindingStr(counter, mac, ip) { |
| 246 | return """\\'host fabric${counter} {'\n'hardware ethernet ${mac}';''\n'fixed-address ${ip}';''\n'}\\'""" |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Runs a command on a remote host using sshpass. |
| 251 | * |
| 252 | * @param ip the node IP address |
| 253 | * @param user the node user name |
| 254 | * @param pass the node password |
| 255 | * @param command the command to run |
| 256 | * @return the output of the command |
| 257 | */ |
| 258 | def runCmd(ip, user, pass, command) { |
| 259 | return sh(returnStdout: true, script: "sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}") |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Runs a command on a fabric switch. |
| 264 | * |
| 265 | * @param headIp the head node IP address |
| 266 | * @param headUser the head node user name |
| 267 | * @param headPass the head node password |
| 268 | * @param ip the mgmt IP of the fabric switch, reachable from the head node |
| 269 | * @param user the mgmt user name of the fabric switch |
| 270 | * @param pass the mgmt password of the fabric switch |
| 271 | * @param command the command to run on the fabric switch |
| 272 | * @return the output of the command |
| 273 | */ |
| 274 | def runFabricCmd(headIp, headUser, headPass, ip, user, pass, command) { |
| 275 | return sh(returnStdout: true, script: "sshpass -p ${headPass} ssh -oStrictHostKeyChecking=no -l ${headUser} ${headIp} \"sshpass -p ${pass} ssh -oStrictHostKeyChecking=no -l ${user} ${ip} ${command}\"") |
| 276 | } |