blob: 3f515a6d122b4f76e78c7f0a8536bb28c8c26fb9 [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. Bainbridgeb5415042016-05-13 17:06:10 -070035task buildBootstrapImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070036 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070037}
38
David K. Bainbridgeb5415042016-05-13 17:06:10 -070039task tagBootstrapImage(type: Exec) {
40 dependsOn buildBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070041 commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070042}
43
David K. Bainbridgeb5415042016-05-13 17:06:10 -070044task publishBootstrapImage(type: Exec) {
45 dependsOn tagBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070046 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070047}
48
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070049task buildAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070050 commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator'
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070051}
52
53task tagAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070054 dependsOn buildAllocationImage
55 commandLine "$dockerPath/docker", 'tag', 'cord-ip-allocator', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070056}
57
58task publishAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070059 dependsOn tagAllocationImage
60 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070061}
62
David K. Bainbridgef0da8732016-06-01 16:15:37 -070063task buildProvisionerImage(type: Exec) {
David K. Bainbridged86d96d2016-06-01 17:28:46 -070064 commandLine "$dockerPath/docker", 'build', '-t', 'cord-provisioner', './provisioner'
David K. Bainbridgef0da8732016-06-01 16:15:37 -070065}
66
67task tagProvisionerImage(type: Exec) {
68 dependsOn buildProvisionerImage
69 commandLine "$dockerPath/docker", 'tag', 'cord-provisioner', "$targetReg/cord-provisioner:$targetTag"
70}
71
72task publishProvisionerImage(type: Exec) {
73 dependsOn tagProvisionerImage
74 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-provisioner:$targetTag"
75}
76
David K. Bainbridgeb5415042016-05-13 17:06:10 -070077task buildAutomationImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070078 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070079}
80
81task buildAutomationImageAnsible(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070082 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070083}
84
85task buildAutomationImages {
86 dependsOn buildAutomationImage
87 dependsOn buildAutomationImageAnsible
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070088}
89
David K. Bainbridgeb5415042016-05-13 17:06:10 -070090task tagAutomationImage(type: Exec) {
91 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070092 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070093}
94
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070095task tagAutomationImageAnsible(type: Exec) {
96 dependsOn buildAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -070097 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070098}
99
100task tagAutomationImages {
101 dependsOn tagAutomationImage
102 dependsOn tagAutomationImageAnsible
103}
104
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700105task publishAutomationImage(type: Exec) {
106 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700107 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700108}
109
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700110task publishAutomationImageAnsible(type: Exec) {
111 dependsOn tagAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700112 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700113}
114
115task publishAutomationImages {
116 dependsOn publishAutomationImage
117 dependsOn publishAutomationImageAnsible
118}
119
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700120task buildHarvesterImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700121 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700122}
123
124task tagHarvesterImage(type: Exec) {
125 dependsOn buildHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700126 commandLine "$dockerPath/docker", 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700127}
128
129task publishHarvesterImage(type: Exec) {
130 dependsOn tagHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700131 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700132}
133
134// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
135
136// To be used to fetch upstream binaries, clone repos, etc.
137task 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. Bainbridge19b8d272016-05-26 21:20:43 -0700140 commandLine "$dockerPath/docker", "pull", "golang:alpine"
141 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700142}
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.
146task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700147 dependsOn buildBootstrapImage
148 dependsOn buildHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700149 dependsOn buildAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700150 dependsOn buildAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700151 dependsOn buildProvisionerImage
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700152}
153
154task tagImages {
155 dependsOn tagBootstrapImage
156 dependsOn tagHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700157 dependsOn tagAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700158 dependsOn tagAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700159 dependsOn tagProvisionerImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700160}
161
162task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700163 dependsOn publishBootstrapImage
164 dependsOn publishHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700165 dependsOn publishAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700166 dependsOn publishAllocationImage
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700167 dependsOn publishProvisionerImage
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
201task 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. Bainbridge13c765c2016-05-26 11:24:22 -0700208 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700209
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700210 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
211 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700212 }
213
214 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700215 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. Bainbridge10b0c112016-05-24 13:17:23 -0700222 }
223
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700224 if (vboxUser != "") {
225 extraVars = extraVars.p(vboxUser, "power_helper_user")
226 }
227
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700228 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. Bainbridge13c765c2016-05-26 11:24:22 -0700234 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700235
236 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700237}