blob: ed2d75708d92e6d91b37842839d37dd61530ac3b [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'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070031}
32
David K. Bainbridge97ee8052016-06-14 00:52:07 -070033// Switch Configuration Image
34
35task buildSwitchqImage(type: Exec) {
36 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-switchq', './switchq'
37}
38
39task tagSwitchqImage(type: Exec) {
40 dependsOn buildSwitchqImage
41 commandLine "$dockerPath/docker", 'tag', 'cord-maas-switchq', "$targetReg/cord-maas-switchq:$targetTag"
42}
43
44task publishSwitchqImage(type: Exec) {
45 dependsOn tagSwitchqImage
46 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-switchq:$targetTag"
47}
48
49// Bootstrap Image
50
David K. Bainbridgeb5415042016-05-13 17:06:10 -070051task buildBootstrapImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070052 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070053}
54
David K. Bainbridgeb5415042016-05-13 17:06:10 -070055task tagBootstrapImage(type: Exec) {
56 dependsOn buildBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070057 commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070058}
59
David K. Bainbridgeb5415042016-05-13 17:06:10 -070060task publishBootstrapImage(type: Exec) {
61 dependsOn tagBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070062 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070063}
64
David K. Bainbridge97ee8052016-06-14 00:52:07 -070065// IP Allocator Image
66
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070067task buildAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070068 commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator'
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070069}
70
71task tagAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070072 dependsOn buildAllocationImage
73 commandLine "$dockerPath/docker", 'tag', 'cord-ip-allocator', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070074}
75
76task publishAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070077 dependsOn tagAllocationImage
78 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070079}
80
David K. Bainbridge97ee8052016-06-14 00:52:07 -070081// Provisioner Image
82
David K. Bainbridgef0da8732016-06-01 16:15:37 -070083task buildProvisionerImage(type: Exec) {
David K. Bainbridged86d96d2016-06-01 17:28:46 -070084 commandLine "$dockerPath/docker", 'build', '-t', 'cord-provisioner', './provisioner'
David K. Bainbridgef0da8732016-06-01 16:15:37 -070085}
86
87task tagProvisionerImage(type: Exec) {
88 dependsOn buildProvisionerImage
89 commandLine "$dockerPath/docker", 'tag', 'cord-provisioner', "$targetReg/cord-provisioner:$targetTag"
90}
91
92task publishProvisionerImage(type: Exec) {
93 dependsOn tagProvisionerImage
94 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-provisioner:$targetTag"
95}
96
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -070097// Automation Image
David K. Bainbridge97ee8052016-06-14 00:52:07 -070098
David K. Bainbridgeb5415042016-05-13 17:06:10 -070099task buildAutomationImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700100 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700101}
102
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700103task tagAutomationImage(type: Exec) {
104 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700105 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700106}
107
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700108task publishAutomationImage(type: Exec) {
109 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700110 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700111}
112
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700113// DHCP Harvester Images
114
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700115task buildHarvesterImage(type: Exec) {
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700116 commandLine "$dockerPath/docker", 'build', '-t', "cord-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700117}
118
119task tagHarvesterImage(type: Exec) {
120 dependsOn buildHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700121 commandLine "$dockerPath/docker", 'tag', 'cord-dhcp-harvester', "$targetReg/cord-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700122}
123
124task publishHarvesterImage(type: Exec) {
125 dependsOn tagHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700126 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700127}
128
129// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
130
131// To be used to fetch upstream binaries, clone repos, etc.
132task fetch(type: Exec) {
133 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
134 // Placeholdr example:
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700135 commandLine "$dockerPath/docker", "pull", "golang:alpine"
136 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700137}
138
139// To be used to generate all needed binaries that need to be present on the target
140// as docker images in the local docker runner.
141task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700142 dependsOn buildBootstrapImage
143 dependsOn buildHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700144 dependsOn buildAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700145 dependsOn buildAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700146 dependsOn buildProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700147 dependsOn buildSwitchqImage
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700148}
149
150task tagImages {
151 dependsOn tagBootstrapImage
152 dependsOn tagHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700153 dependsOn tagAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700154 dependsOn tagAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700155 dependsOn tagProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700156 dependsOn tagSwitchqImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700157}
158
159task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700160 dependsOn publishBootstrapImage
161 dependsOn publishHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700162 dependsOn publishAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700163 dependsOn publishAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700164 dependsOn publishProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700165 dependsOn publishSwitchqImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700166}
167
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700168// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
169
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700170List.metaClass.asParam = { prefix, sep ->
171 if (delegate.size() == 0) {
172 ""
173 }
174 String result = "--" + prefix + "="
175 String p = ""
176 delegate.each {
177 result += p + "${it}"
178 p = sep
179 }
180 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700181}
182
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700183List.metaClass.p = { value, name ->
184 if (value != null && value != "") {
185 delegate << name + "=" + value
186 } else {
187 delegate
188 }
189}
190
191List.metaClass.p = { spec ->
192 if (spec != null && spec != "") {
193 delegate += spec
194 } else {
195 delegate
196 }
197}
198
David K. Bainbridgef4181702016-06-17 14:44:03 -0700199task prime (type: Exec) {
200 println "Using deployment config: $deployConfig"
201 File configFile = new File(deployConfig)
202 def yaml = new Yaml()
203 def config = yaml.load(configFile.newReader())
204
205 executable = "ansible-playbook"
206 args = ["-i", config.seedServer.ip + ',']
207
208 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
209 args = args << "--user=$config.seedServer.user"
210 }
211
212 def extraVars = []
213 if (config.seedServer) {
214 extraVars = extraVars.p(config.seedServer.extraVars)
215 .p(config.seedServer.password, "ansible_ssh_pass")
216 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
217 .p(config.seedServer.fabric_ip, "fabric_ip")
218 .p(config.seedServer.management_ip, "management_ip")
219 .p(config.seedServer.management_network, "management_network")
220 .p(config.seedServer.management_iface, "management_iface")
221 .p(config.seedServer.external_ip, "external_ip")
222 .p(config.seedServer.external_network, "external_network")
223 .p(config.seedServer.external_iface, "external_iface")
224 .p(config.seedServer.fabric_ip, "fabric_ip")
225 .p(config.seedServer.fabric_network, "fabric_network")
226 .p(config.seedServer.fabric_iface, "fabric_iface")
227 .p(config.seedServer.domain, "domain")
David K. Bainbridgebe58a0d2016-06-22 15:43:02 -0700228 .p(config.seedServer.virtualbox_support, "virtualbox_support")
229 .p(config.seedServer.power_helper_user, "power_helper_user")
230 .p(config.seedServer.power_helper_host, "power_helper_host")
231 .p(config.seedServer.port, "ansible_ssh_port")
David K. Bainbridgef4181702016-06-17 14:44:03 -0700232 }
233
234 if (config.otherServers) {
235 extraVars = extraVars.p(config.otherServers.location, "prov_location")
236 .p(config.otherServers.rolesPath, "prov_role_path")
237 .p(config.otherServers.role, "prov_role")
238 }
239
240 if (config.docker) {
241 extraVars = extraVars.p(config.docker.registry, "docker_registry")
242 .p(config.docker.imageVersion, "docker_image_version")
243 }
244
245 def skipTags = [].p(config.seedServer.skipTags)
246
247 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "prime-node.yml"
248}
249
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700250task deploy (type: Exec) {
251 println "Using deployment config: $deployConfig"
252 File configFile = new File(deployConfig)
253 def yaml = new Yaml()
254 def config = yaml.load(configFile.newReader())
255
256 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700257 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700258
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700259 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
260 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700261 }
262
263 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700264 if (config.seedServer) {
265 extraVars = extraVars.p(config.seedServer.extraVars)
266 .p(config.seedServer.password, "ansible_ssh_pass")
267 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
268 .p(config.seedServer.fabric_ip, "fabric_ip")
269 .p(config.seedServer.management_ip, "management_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700270 .p(config.seedServer.management_network, "management_network")
271 .p(config.seedServer.management_iface, "management_iface")
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700272 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700273 .p(config.seedServer.external_network, "external_network")
274 .p(config.seedServer.external_iface, "external_iface")
275 .p(config.seedServer.fabric_ip, "fabric_ip")
276 .p(config.seedServer.fabric_network, "fabric_network")
277 .p(config.seedServer.fabric_iface, "fabric_iface")
278 .p(config.seedServer.domain, "domain")
David K. Bainbridgebe58a0d2016-06-22 15:43:02 -0700279 .p(config.seedServer.virtualbox_support, "virtualbox_support")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700280 .p(config.seedServer.power_helper_user, "power_helper_user")
281 .p(config.seedServer.power_helper_host, "power_helper_host")
David K. Bainbridgebe58a0d2016-06-22 15:43:02 -0700282 .p(config.seedServer.port, "ansible_ssh_port")
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700283 }
284
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700285 if (config.otherServers) {
286 extraVars = extraVars.p(config.otherServers.location, "prov_location")
287 .p(config.otherServers.rolesPath, "prov_role_path")
288 .p(config.otherServers.role, "prov_role")
289 }
290
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700291 if (config.docker) {
292 extraVars = extraVars.p(config.docker.registry, "docker_registry")
293 .p(config.docker.imageVersion, "docker_image_version")
294 }
295
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700296 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700297
298 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700299}