blob: 86c0888bf4cb6d191389adc74667ef55e3031f5a [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. Bainbridge9d1e02d2016-06-22 09:22:16 -070099// Automation Image
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700100
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
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700105task tagAutomationImage(type: Exec) {
106 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700107 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700108}
109
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700110task publishAutomationImage(type: Exec) {
111 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700112 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700113}
114
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700115// DHCP Harvester Images
116
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700117task buildHarvesterImage(type: Exec) {
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700118 commandLine "$dockerPath/docker", 'build', '-t', "cord-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700119}
120
121task tagHarvesterImage(type: Exec) {
122 dependsOn buildHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700123 commandLine "$dockerPath/docker", 'tag', 'cord-dhcp-harvester', "$targetReg/cord-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700124}
125
126task publishHarvesterImage(type: Exec) {
127 dependsOn tagHarvesterImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700128 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700129}
130
131// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
132
133// To be used to fetch upstream binaries, clone repos, etc.
134task fetch(type: Exec) {
135 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
136 // Placeholdr example:
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700137 commandLine "$dockerPath/docker", "pull", "golang:alpine"
138 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700139}
140
141// To be used to generate all needed binaries that need to be present on the target
142// as docker images in the local docker runner.
143task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700144 dependsOn buildBootstrapImage
145 dependsOn buildHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700146 dependsOn buildAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700147 dependsOn buildAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700148 dependsOn buildProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700149 dependsOn buildSwitchqImage
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700150}
151
152task tagImages {
153 dependsOn tagBootstrapImage
154 dependsOn tagHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700155 dependsOn tagAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700156 dependsOn tagAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700157 dependsOn tagProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700158 dependsOn tagSwitchqImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700159}
160
161task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700162 dependsOn publishBootstrapImage
163 dependsOn publishHarvesterImage
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700164 dependsOn publishAutomationImage
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700165 dependsOn publishAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700166 dependsOn publishProvisionerImage
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700167 dependsOn publishSwitchqImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700168}
169
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700170// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
171
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700172List.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. Bainbridgeb5415042016-05-13 17:06:10 -0700183}
184
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700185List.metaClass.p = { value, name ->
186 if (value != null && value != "") {
187 delegate << name + "=" + value
188 } else {
189 delegate
190 }
191}
192
193List.metaClass.p = { spec ->
194 if (spec != null && spec != "") {
195 delegate += spec
196 } else {
197 delegate
198 }
199}
200
David K. Bainbridgef4181702016-06-17 14:44:03 -0700201task prime (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"
208 args = ["-i", config.seedServer.ip + ',']
209
210 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
211 args = args << "--user=$config.seedServer.user"
212 }
213
214 def extraVars = []
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.management_network, "management_network")
222 .p(config.seedServer.management_iface, "management_iface")
223 .p(config.seedServer.external_ip, "external_ip")
224 .p(config.seedServer.external_network, "external_network")
225 .p(config.seedServer.external_iface, "external_iface")
226 .p(config.seedServer.fabric_ip, "fabric_ip")
227 .p(config.seedServer.fabric_network, "fabric_network")
228 .p(config.seedServer.fabric_iface, "fabric_iface")
229 .p(config.seedServer.domain, "domain")
230 }
231
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700232 if (config.seedServer.port) {
233 extraVars = extraVars.p(config.seedServer.port, "ansible_ssh_port")
234 }
235
David K. Bainbridgef4181702016-06-17 14:44:03 -0700236 if (vboxUser != "") {
237 extraVars = extraVars.p(vboxUser, "power_helper_user")
238 }
239
240 if (config.otherServers) {
241 extraVars = extraVars.p(config.otherServers.location, "prov_location")
242 .p(config.otherServers.rolesPath, "prov_role_path")
243 .p(config.otherServers.role, "prov_role")
244 }
245
246 if (config.docker) {
247 extraVars = extraVars.p(config.docker.registry, "docker_registry")
248 .p(config.docker.imageVersion, "docker_image_version")
249 }
250
251 def skipTags = [].p(config.seedServer.skipTags)
252
253 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "prime-node.yml"
254}
255
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700256task deploy (type: Exec) {
257 println "Using deployment config: $deployConfig"
258 File configFile = new File(deployConfig)
259 def yaml = new Yaml()
260 def config = yaml.load(configFile.newReader())
261
262 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700263 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700264
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700265 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
266 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700267 }
268
269 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700270 if (config.seedServer) {
271 extraVars = extraVars.p(config.seedServer.extraVars)
272 .p(config.seedServer.password, "ansible_ssh_pass")
273 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
274 .p(config.seedServer.fabric_ip, "fabric_ip")
275 .p(config.seedServer.management_ip, "management_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700276 .p(config.seedServer.management_network, "management_network")
277 .p(config.seedServer.management_iface, "management_iface")
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700278 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridgec82a4462016-06-14 12:39:01 -0700279 .p(config.seedServer.external_network, "external_network")
280 .p(config.seedServer.external_iface, "external_iface")
281 .p(config.seedServer.fabric_ip, "fabric_ip")
282 .p(config.seedServer.fabric_network, "fabric_network")
283 .p(config.seedServer.fabric_iface, "fabric_iface")
284 .p(config.seedServer.domain, "domain")
285 .p(config.seedServer.power_helper_user, "power_helper_user")
286 .p(config.seedServer.power_helper_host, "power_helper_host")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700287 }
288
David K. Bainbridge9d1e02d2016-06-22 09:22:16 -0700289 if (config.seedServer.port) {
290 extraVars = extraVars.p(config.seedServer.port, "ansible_ssh_port")
291 }
292
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700293 if (vboxUser != "") {
294 extraVars = extraVars.p(vboxUser, "power_helper_user")
295 }
296
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700297 if (config.otherServers) {
298 extraVars = extraVars.p(config.otherServers.location, "prov_location")
299 .p(config.otherServers.rolesPath, "prov_role_path")
300 .p(config.otherServers.role, "prov_role")
301 }
302
David K. Bainbridge97ee8052016-06-14 00:52:07 -0700303 if (config.docker) {
304 extraVars = extraVars.p(config.docker.registry, "docker_registry")
305 .p(config.docker.imageVersion, "docker_image_version")
306 }
307
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700308 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700309
310 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700311}