Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 16 | import org.yaml.snakeyaml.Yaml |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 17 | |
| 18 | ext { |
| 19 | |
| 20 | // Target registry to be used to publish docker images needed for deployment |
| 21 | targetReg = project.hasProperty('targetReg') ? project.getProperty('targetReg') : 'localhost:5000' |
| 22 | |
| 23 | // The tag used to tag the docker images push to the target registry |
| 24 | targetTag = project.hasProperty('targetTag') ? project.getProperty('targetTag') : 'candidate' |
| 25 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 26 | // Deployment target config file (yaml format); this can be overwritten from the command line |
| 27 | // using the -PdeployConfig=<file-path> syntax. |
| 28 | deployConfig = project.hasProperty('deployConfig') ? project.getProperty('deployConfig') : './config/default.yml' |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 29 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 30 | dockerPath = project.hasProperty('dockerPath') ? project.getProperty('dockerPath') : '/usr/bin' |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 31 | |
| 32 | vboxUser = project.hasProperty('vboxUser') ? project.getProperty('vboxUser') : 'cord' |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 33 | } |
| 34 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 35 | // Switch Configuration Image |
| 36 | |
| 37 | task buildSwitchqImage(type: Exec) { |
| 38 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-switchq', './switchq' |
| 39 | } |
| 40 | |
| 41 | task tagSwitchqImage(type: Exec) { |
| 42 | dependsOn buildSwitchqImage |
| 43 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-switchq', "$targetReg/cord-maas-switchq:$targetTag" |
| 44 | } |
| 45 | |
| 46 | task publishSwitchqImage(type: Exec) { |
| 47 | dependsOn tagSwitchqImage |
| 48 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-switchq:$targetTag" |
| 49 | } |
| 50 | |
| 51 | // Bootstrap Image |
| 52 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 53 | task buildBootstrapImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 54 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap' |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 55 | } |
| 56 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 57 | task tagBootstrapImage(type: Exec) { |
| 58 | dependsOn buildBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 59 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 60 | } |
| 61 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 62 | task publishBootstrapImage(type: Exec) { |
| 63 | dependsOn tagBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 64 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 67 | // IP Allocator Image |
| 68 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 69 | task buildAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 70 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator' |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | task tagAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 74 | dependsOn buildAllocationImage |
| 75 | commandLine "$dockerPath/docker", 'tag', 'cord-ip-allocator', "$targetReg/cord-ip-allocator:$targetTag" |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | task publishAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 79 | dependsOn tagAllocationImage |
| 80 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag" |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 83 | // Provisioner Image |
| 84 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 85 | task buildProvisionerImage(type: Exec) { |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 86 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-provisioner', './provisioner' |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | task tagProvisionerImage(type: Exec) { |
| 90 | dependsOn buildProvisionerImage |
| 91 | commandLine "$dockerPath/docker", 'tag', 'cord-provisioner', "$targetReg/cord-provisioner:$targetTag" |
| 92 | } |
| 93 | |
| 94 | task publishProvisionerImage(type: Exec) { |
| 95 | dependsOn tagProvisionerImage |
| 96 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-provisioner:$targetTag" |
| 97 | } |
| 98 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 99 | // Automation Images |
| 100 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 101 | task buildAutomationImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 102 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | task buildAutomationImageAnsible(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 106 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | task buildAutomationImages { |
| 110 | dependsOn buildAutomationImage |
| 111 | dependsOn buildAutomationImageAnsible |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 112 | } |
| 113 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 114 | task tagAutomationImage(type: Exec) { |
| 115 | dependsOn buildAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 116 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 117 | } |
| 118 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 119 | task tagAutomationImageAnsible(type: Exec) { |
| 120 | dependsOn buildAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 121 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | task tagAutomationImages { |
| 125 | dependsOn tagAutomationImage |
| 126 | dependsOn tagAutomationImageAnsible |
| 127 | } |
| 128 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 129 | task publishAutomationImage(type: Exec) { |
| 130 | dependsOn tagAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 131 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 134 | task publishAutomationImageAnsible(type: Exec) { |
| 135 | dependsOn tagAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 136 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | task publishAutomationImages { |
| 140 | dependsOn publishAutomationImage |
| 141 | dependsOn publishAutomationImageAnsible |
| 142 | } |
| 143 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 144 | // DHCP Harvester Images |
| 145 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 146 | task buildHarvesterImage(type: Exec) { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 147 | commandLine "$dockerPath/docker", 'build', '-t', "cord-dhcp-harvester", "./harvester" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | task tagHarvesterImage(type: Exec) { |
| 151 | dependsOn buildHarvesterImage |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 152 | commandLine "$dockerPath/docker", 'tag', 'cord-dhcp-harvester', "$targetReg/cord-dhcp-harvester:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | task publishHarvesterImage(type: Exec) { |
| 156 | dependsOn tagHarvesterImage |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 157 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-dhcp-harvester:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 161 | |
| 162 | // To be used to fetch upstream binaries, clone repos, etc. |
| 163 | task fetch(type: Exec) { |
| 164 | // this is where we fetch upstream artifacts that we do not need internet for the build phase" |
| 165 | // Placeholdr example: |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 166 | commandLine "$dockerPath/docker", "pull", "golang:alpine" |
| 167 | commandLine "$dockerPath/docker", "pull", "python:2.7-alpine" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // To be used to generate all needed binaries that need to be present on the target |
| 171 | // as docker images in the local docker runner. |
| 172 | task buildImages { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 173 | dependsOn buildBootstrapImage |
| 174 | dependsOn buildHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 175 | dependsOn buildAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 176 | dependsOn buildAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 177 | dependsOn buildProvisionerImage |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 178 | dependsOn buildSwitchqImage |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | task tagImages { |
| 182 | dependsOn tagBootstrapImage |
| 183 | dependsOn tagHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 184 | dependsOn tagAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 185 | dependsOn tagAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 186 | dependsOn tagProvisionerImage |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 187 | dependsOn tagSwitchqImage |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | task publish { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 191 | dependsOn publishBootstrapImage |
| 192 | dependsOn publishHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 193 | dependsOn publishAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 194 | dependsOn publishAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 195 | dependsOn publishProvisionerImage |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 196 | dependsOn publishSwitchqImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 197 | } |
| 198 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 199 | // ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 200 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 201 | List.metaClass.asParam = { prefix, sep -> |
| 202 | if (delegate.size() == 0) { |
| 203 | "" |
| 204 | } |
| 205 | String result = "--" + prefix + "=" |
| 206 | String p = "" |
| 207 | delegate.each { |
| 208 | result += p + "${it}" |
| 209 | p = sep |
| 210 | } |
| 211 | result |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 214 | List.metaClass.p = { value, name -> |
| 215 | if (value != null && value != "") { |
| 216 | delegate << name + "=" + value |
| 217 | } else { |
| 218 | delegate |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | List.metaClass.p = { spec -> |
| 223 | if (spec != null && spec != "") { |
| 224 | delegate += spec |
| 225 | } else { |
| 226 | delegate |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | task deploy (type: Exec) { |
| 231 | println "Using deployment config: $deployConfig" |
| 232 | File configFile = new File(deployConfig) |
| 233 | def yaml = new Yaml() |
| 234 | def config = yaml.load(configFile.newReader()) |
| 235 | |
| 236 | executable = "ansible-playbook" |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 237 | args = ["-i", config.seedServer.ip + ','] |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 238 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 239 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 240 | args = args << "--user=$config.seedServer.user" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | def extraVars = [] |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 244 | if (config.seedServer) { |
| 245 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 246 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 247 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 248 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 249 | .p(config.seedServer.management_ip, "management_ip") |
David K. Bainbridge | c82a446 | 2016-06-14 12:39:01 -0700 | [diff] [blame] | 250 | .p(config.seedServer.management_network, "management_network") |
| 251 | .p(config.seedServer.management_iface, "management_iface") |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 252 | .p(config.seedServer.external_ip, "external_ip") |
David K. Bainbridge | c82a446 | 2016-06-14 12:39:01 -0700 | [diff] [blame] | 253 | .p(config.seedServer.external_network, "external_network") |
| 254 | .p(config.seedServer.external_iface, "external_iface") |
| 255 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 256 | .p(config.seedServer.fabric_network, "fabric_network") |
| 257 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 258 | .p(config.seedServer.domain, "domain") |
| 259 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 260 | .p(config.seedServer.power_helper_host, "power_helper_host") |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 261 | } |
| 262 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 263 | if (vboxUser != "") { |
| 264 | extraVars = extraVars.p(vboxUser, "power_helper_user") |
| 265 | } |
| 266 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 267 | if (config.otherServers) { |
| 268 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 269 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 270 | .p(config.otherServers.role, "prov_role") |
| 271 | } |
| 272 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 273 | if (config.docker) { |
| 274 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 275 | .p(config.docker.imageVersion, "docker_image_version") |
| 276 | } |
| 277 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 278 | def skipTags = [].p(config.seedServer.skipTags) |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 279 | |
| 280 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 281 | } |