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 | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 35 | task buildBootstrapImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 36 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap' |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 37 | } |
| 38 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 39 | task tagBootstrapImage(type: Exec) { |
| 40 | dependsOn buildBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 41 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 42 | } |
| 43 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 44 | task publishBootstrapImage(type: Exec) { |
| 45 | dependsOn tagBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 46 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 47 | } |
| 48 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 49 | task buildAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 50 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator' |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | task tagAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 54 | dependsOn buildAllocationImage |
| 55 | 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] | 56 | } |
| 57 | |
| 58 | task publishAllocationImage(type: Exec) { |
David K. Bainbridge | f22dc06 | 2016-05-31 15:35:39 -0700 | [diff] [blame] | 59 | dependsOn tagAllocationImage |
| 60 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag" |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 63 | task buildProvisionerImage(type: Exec) { |
David K. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 64 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-provisioner', './provisioner' |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | task tagProvisionerImage(type: Exec) { |
| 68 | dependsOn buildProvisionerImage |
| 69 | commandLine "$dockerPath/docker", 'tag', 'cord-provisioner', "$targetReg/cord-provisioner:$targetTag" |
| 70 | } |
| 71 | |
| 72 | task publishProvisionerImage(type: Exec) { |
| 73 | dependsOn tagProvisionerImage |
| 74 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-provisioner:$targetTag" |
| 75 | } |
| 76 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 77 | task buildAutomationImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 78 | 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] | 79 | } |
| 80 | |
| 81 | task buildAutomationImageAnsible(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 82 | 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] | 83 | } |
| 84 | |
| 85 | task buildAutomationImages { |
| 86 | dependsOn buildAutomationImage |
| 87 | dependsOn buildAutomationImageAnsible |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 90 | task tagAutomationImage(type: Exec) { |
| 91 | dependsOn buildAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 92 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 95 | task tagAutomationImageAnsible(type: Exec) { |
| 96 | dependsOn buildAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 97 | 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] | 98 | } |
| 99 | |
| 100 | task tagAutomationImages { |
| 101 | dependsOn tagAutomationImage |
| 102 | dependsOn tagAutomationImageAnsible |
| 103 | } |
| 104 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 105 | task publishAutomationImage(type: Exec) { |
| 106 | dependsOn tagAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 107 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 110 | task publishAutomationImageAnsible(type: Exec) { |
| 111 | dependsOn tagAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 112 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | task publishAutomationImages { |
| 116 | dependsOn publishAutomationImage |
| 117 | dependsOn publishAutomationImageAnsible |
| 118 | } |
| 119 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 120 | task buildHarvesterImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 121 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | task tagHarvesterImage(type: Exec) { |
| 125 | dependsOn buildHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 126 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | task publishHarvesterImage(type: Exec) { |
| 130 | dependsOn tagHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 131 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 135 | |
| 136 | // To be used to fetch upstream binaries, clone repos, etc. |
| 137 | task fetch(type: Exec) { |
| 138 | // this is where we fetch upstream artifacts that we do not need internet for the build phase" |
| 139 | // Placeholdr example: |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 140 | commandLine "$dockerPath/docker", "pull", "golang:alpine" |
| 141 | commandLine "$dockerPath/docker", "pull", "python:2.7-alpine" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // To be used to generate all needed binaries that need to be present on the target |
| 145 | // as docker images in the local docker runner. |
| 146 | task buildImages { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 147 | dependsOn buildBootstrapImage |
| 148 | dependsOn buildHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 149 | dependsOn buildAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 150 | dependsOn buildAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 151 | dependsOn buildProvisionerImage |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | task tagImages { |
| 155 | dependsOn tagBootstrapImage |
| 156 | dependsOn tagHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 157 | dependsOn tagAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 158 | dependsOn tagAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 159 | dependsOn tagProvisionerImage |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | task publish { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 163 | dependsOn publishBootstrapImage |
| 164 | dependsOn publishHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 165 | dependsOn publishAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 166 | dependsOn publishAllocationImage |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 167 | dependsOn publishProvisionerImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 168 | } |
| 169 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 170 | // ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 171 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 172 | List.metaClass.asParam = { prefix, sep -> |
| 173 | if (delegate.size() == 0) { |
| 174 | "" |
| 175 | } |
| 176 | String result = "--" + prefix + "=" |
| 177 | String p = "" |
| 178 | delegate.each { |
| 179 | result += p + "${it}" |
| 180 | p = sep |
| 181 | } |
| 182 | result |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 183 | } |
| 184 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 185 | List.metaClass.p = { value, name -> |
| 186 | if (value != null && value != "") { |
| 187 | delegate << name + "=" + value |
| 188 | } else { |
| 189 | delegate |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | List.metaClass.p = { spec -> |
| 194 | if (spec != null && spec != "") { |
| 195 | delegate += spec |
| 196 | } else { |
| 197 | delegate |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | task deploy (type: Exec) { |
| 202 | println "Using deployment config: $deployConfig" |
| 203 | File configFile = new File(deployConfig) |
| 204 | def yaml = new Yaml() |
| 205 | def config = yaml.load(configFile.newReader()) |
| 206 | |
| 207 | executable = "ansible-playbook" |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 208 | args = ["-i", config.seedServer.ip + ','] |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 209 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 210 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 211 | args = args << "--user=$config.seedServer.user" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | def extraVars = [] |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 215 | if (config.seedServer) { |
| 216 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 217 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 218 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 219 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 220 | .p(config.seedServer.management_ip, "management_ip") |
| 221 | .p(config.seedServer.external_ip, "external_ip") |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 222 | } |
| 223 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame^] | 224 | if (vboxUser != "") { |
| 225 | extraVars = extraVars.p(vboxUser, "power_helper_user") |
| 226 | } |
| 227 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 228 | if (config.otherServers) { |
| 229 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 230 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 231 | .p(config.otherServers.role, "prov_role") |
| 232 | } |
| 233 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 234 | def skipTags = [].p(config.seedServer.skipTags) |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 235 | |
| 236 | 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] | 237 | } |