blob: 3ab47040bf4af5cbee999682fa5b050fced81925 [file] [log] [blame]
Zsolt Haraszti2a792f62016-05-12 17:49:02 -07001/*
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. Bainbridge10b0c112016-05-24 13:17:23 -070016import org.yaml.snakeyaml.Yaml
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070017
18ext {
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. Bainbridge10b0c112016-05-24 13:17:23 -070026 // 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. Bainbridge19b8d272016-05-26 21:20:43 -070029
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070030 dockerPath = project.hasProperty('dockerPath') ? project.getProperty('dockerPath') : '/usr/bin'
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070031
32 vboxUser = project.hasProperty('vboxUser') ? project.getProperty('vboxUser') : 'cord'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070033}
34
David K. Bainbridge97ee8052016-06-14 00:52:07 -070035// Switch Configuration Image
36
37task buildSwitchqImage(type: Exec) {
38 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-switchq', './switchq'
39}
40
41task tagSwitchqImage(type: Exec) {
42 dependsOn buildSwitchqImage
43 commandLine "$dockerPath/docker", 'tag', 'cord-maas-switchq', "$targetReg/cord-maas-switchq:$targetTag"
44}
45
46task publishSwitchqImage(type: Exec) {
47 dependsOn tagSwitchqImage
48 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-switchq:$targetTag"
49}
50
51// Bootstrap Image
52
David K. Bainbridgeb5415042016-05-13 17:06:10 -070053task buildBootstrapImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070054 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070055}
56
David K. Bainbridgeb5415042016-05-13 17:06:10 -070057task tagBootstrapImage(type: Exec) {
58 dependsOn buildBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070059 commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070060}
61
David K. Bainbridgeb5415042016-05-13 17:06:10 -070062task publishBootstrapImage(type: Exec) {
63 dependsOn tagBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070064 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070065}
66
David K. Bainbridge97ee8052016-06-14 00:52:07 -070067// IP Allocator Image
68
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070069task buildAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070070 commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator'
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070071}
72
73task tagAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070074 dependsOn buildAllocationImage
75 commandLine "$dockerPath/docker", 'tag', 'cord-ip-allocator', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070076}
77
78task publishAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070079 dependsOn tagAllocationImage
80 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070081}
82
David K. Bainbridge97ee8052016-06-14 00:52:07 -070083// Provisioner Image
84
David K. Bainbridgef0da8732016-06-01 16:15:37 -070085task buildProvisionerImage(type: Exec) {
David K. Bainbridged86d96d2016-06-01 17:28:46 -070086 commandLine "$dockerPath/docker", 'build', '-t', 'cord-provisioner', './provisioner'
David K. Bainbridgef0da8732016-06-01 16:15:37 -070087}
88
89task tagProvisionerImage(type: Exec) {
90 dependsOn buildProvisionerImage
91 commandLine "$dockerPath/docker", 'tag', 'cord-provisioner', "$targetReg/cord-provisioner:$targetTag"
92}
93
94task publishProvisionerImage(type: Exec) {
95 dependsOn tagProvisionerImage
96 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-provisioner:$targetTag"
97}
98
David K. Bainbridge97ee8052016-06-14 00:52:07 -070099// Automation Images
100
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700101task buildAutomationImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700102 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700103}
104
105task buildAutomationImageAnsible(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700106 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700107}
108
109task buildAutomationImages {
110 dependsOn buildAutomationImage
111 dependsOn buildAutomationImageAnsible
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700112}
113
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700114task tagAutomationImage(type: Exec) {
115 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700116 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700117}
118
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700119task tagAutomationImageAnsible(type: Exec) {
120 dependsOn buildAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700121 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700122}
123
124task tagAutomationImages {
125 dependsOn tagAutomationImage
126 dependsOn tagAutomationImageAnsible
127}
128
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700129task publishAutomationImage(type: Exec) {
130 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700131 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700132}
133
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700134task publishAutomationImageAnsible(type: Exec) {
135 dependsOn tagAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700136 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700137}
138
139task publishAutomationImages {
140 dependsOn publishAutomationImage
141 dependsOn publishAutomationImageAnsible
142}
143
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700144// DHCP Harvester Images
145
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700146task buildHarvesterImage(type: Exec) {
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700147 commandLine "$dockerPath/docker", 'build', '-t', "cord-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700148}
149
150task tagHarvesterImage(type: Exec) {
151 dependsOn buildHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700152 commandLine "$dockerPath/docker", 'tag', 'cord-dhcp-harvester', "$targetReg/cord-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700153}
154
155task publishHarvesterImage(type: Exec) {
156 dependsOn tagHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700157 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700158}
159
160// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
161
162// To be used to fetch upstream binaries, clone repos, etc.
163task 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. Bainbridge19b8d272016-05-26 21:20:43 -0700166 commandLine "$dockerPath/docker", "pull", "golang:alpine"
167 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700168}
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.
172task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700173 dependsOn buildBootstrapImage
174 dependsOn buildHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700175 dependsOn buildAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700176 dependsOn buildAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700177 dependsOn buildProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700178 dependsOn buildSwitchqImage
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700179}
180
181task tagImages {
182 dependsOn tagBootstrapImage
183 dependsOn tagHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700184 dependsOn tagAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700185 dependsOn tagAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700186 dependsOn tagProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700187 dependsOn tagSwitchqImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700188}
189
190task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700191 dependsOn publishBootstrapImage
192 dependsOn publishHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700193 dependsOn publishAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700194 dependsOn publishAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700195 dependsOn publishProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700196 dependsOn publishSwitchqImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700197}
198
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700199// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
200
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700201List.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. Bainbridgeb5415042016-05-13 17:06:10 -0700212}
213
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700214List.metaClass.p = { value, name ->
215 if (value != null && value != "") {
216 delegate << name + "=" + value
217 } else {
218 delegate
219 }
220}
221
222List.metaClass.p = { spec ->
223 if (spec != null && spec != "") {
224 delegate += spec
225 } else {
226 delegate
227 }
228}
229
David K. Bainbridgef4181702016-06-17 14:44:03 -0700230task prime (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"
237 args = ["-i", config.seedServer.ip + ',']
238
239 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
240 args = args << "--user=$config.seedServer.user"
241 }
242
243 def extraVars = []
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")
250 .p(config.seedServer.management_network, "management_network")
251 .p(config.seedServer.management_iface, "management_iface")
252 .p(config.seedServer.external_ip, "external_ip")
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 }
260
261 if (vboxUser != "") {
262 extraVars = extraVars.p(vboxUser, "power_helper_user")
263 }
264
265 if (config.otherServers) {
266 extraVars = extraVars.p(config.otherServers.location, "prov_location")
267 .p(config.otherServers.rolesPath, "prov_role_path")
268 .p(config.otherServers.role, "prov_role")
269 }
270
271 if (config.docker) {
272 extraVars = extraVars.p(config.docker.registry, "docker_registry")
273 .p(config.docker.imageVersion, "docker_image_version")
274 }
275
276 def skipTags = [].p(config.seedServer.skipTags)
277
278 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "prime-node.yml"
279}
280
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700281task deploy (type: Exec) {
282 println "Using deployment config: $deployConfig"
283 File configFile = new File(deployConfig)
284 def yaml = new Yaml()
285 def config = yaml.load(configFile.newReader())
286
287 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700288 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700289
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700290 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
291 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700292 }
293
294 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700295 if (config.seedServer) {
296 extraVars = extraVars.p(config.seedServer.extraVars)
297 .p(config.seedServer.password, "ansible_ssh_pass")
298 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
299 .p(config.seedServer.fabric_ip, "fabric_ip")
300 .p(config.seedServer.management_ip, "management_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700301 .p(config.seedServer.management_network, "management_network")
302 .p(config.seedServer.management_iface, "management_iface")
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700303 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700304 .p(config.seedServer.external_network, "external_network")
305 .p(config.seedServer.external_iface, "external_iface")
306 .p(config.seedServer.fabric_ip, "fabric_ip")
307 .p(config.seedServer.fabric_network, "fabric_network")
308 .p(config.seedServer.fabric_iface, "fabric_iface")
309 .p(config.seedServer.domain, "domain")
310 .p(config.seedServer.power_helper_user, "power_helper_user")
311 .p(config.seedServer.power_helper_host, "power_helper_host")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700312 }
313
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700314 if (vboxUser != "") {
315 extraVars = extraVars.p(vboxUser, "power_helper_user")
316 }
317
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700318 if (config.otherServers) {
319 extraVars = extraVars.p(config.otherServers.location, "prov_location")
320 .p(config.otherServers.rolesPath, "prov_role_path")
321 .p(config.otherServers.role, "prov_role")
322 }
323
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700324 if (config.docker) {
325 extraVars = extraVars.p(config.docker.registry, "docker_registry")
326 .p(config.docker.imageVersion, "docker_image_version")
327 }
328
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700329 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700330
331 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700332}