blob: 6c0babfc2505230dc43991210947698f8c6e070a [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. Bainbridgeb5415042016-05-13 17:06:10 -070033task buildBootstrapImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070034 commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070035}
36
David K. Bainbridgeb5415042016-05-13 17:06:10 -070037task tagBootstrapImage(type: Exec) {
38 dependsOn buildBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070039 commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070040}
41
David K. Bainbridgeb5415042016-05-13 17:06:10 -070042task publishBootstrapImage(type: Exec) {
43 dependsOn tagBootstrapImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070044 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070045}
46
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070047task buildAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070048 commandLine "$dockerPath/docker", 'build', '-t', 'cord-ip-allocator', './ip-allocator'
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070049}
50
51task tagAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070052 dependsOn buildAllocationImage
53 commandLine "$dockerPath/docker", 'tag', 'cord-ip-allocator', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070054}
55
56task publishAllocationImage(type: Exec) {
David K. Bainbridgef22dc062016-05-31 15:35:39 -070057 dependsOn tagAllocationImage
58 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-ip-allocator:$targetTag"
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070059}
60
David K. Bainbridgeb5415042016-05-13 17:06:10 -070061task buildAutomationImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070062 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070063}
64
65task buildAutomationImageAnsible(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070066 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070067}
68
69task buildAutomationImages {
70 dependsOn buildAutomationImage
71 dependsOn buildAutomationImageAnsible
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070072}
73
David K. Bainbridgeb5415042016-05-13 17:06:10 -070074task tagAutomationImage(type: Exec) {
75 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070076 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070077}
78
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070079task tagAutomationImageAnsible(type: Exec) {
80 dependsOn buildAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -070081 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070082}
83
84task tagAutomationImages {
85 dependsOn tagAutomationImage
86 dependsOn tagAutomationImageAnsible
87}
88
David K. Bainbridgeb5415042016-05-13 17:06:10 -070089task publishAutomationImage(type: Exec) {
90 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070091 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070092}
93
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070094task publishAutomationImageAnsible(type: Exec) {
95 dependsOn tagAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -070096 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070097}
98
99task publishAutomationImages {
100 dependsOn publishAutomationImage
101 dependsOn publishAutomationImageAnsible
102}
103
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700104task buildHarvesterImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700105 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700106}
107
108task tagHarvesterImage(type: Exec) {
109 dependsOn buildHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700110 commandLine "$dockerPath/docker", 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700111}
112
113task publishHarvesterImage(type: Exec) {
114 dependsOn tagHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700115 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700116}
117
118// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
119
120// To be used to fetch upstream binaries, clone repos, etc.
121task fetch(type: Exec) {
122 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
123 // Placeholdr example:
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700124 commandLine "$dockerPath/docker", "pull", "golang:alpine"
125 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700126}
127
128// To be used to generate all needed binaries that need to be present on the target
129// as docker images in the local docker runner.
130task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700131 dependsOn buildBootstrapImage
132 dependsOn buildHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700133 dependsOn buildAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700134 dependsOn buildAllocationImage
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700135}
136
137task tagImages {
138 dependsOn tagBootstrapImage
139 dependsOn tagHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700140 dependsOn tagAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700141 dependsOn tagAllocationImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700142}
143
144task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700145 dependsOn publishBootstrapImage
146 dependsOn publishHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700147 dependsOn publishAutomationImages
David K. Bainbridge8bc905c2016-05-31 14:07:10 -0700148 dependsOn publishAllocationImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700149}
150
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700151// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
152
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700153List.metaClass.asParam = { prefix, sep ->
154 if (delegate.size() == 0) {
155 ""
156 }
157 String result = "--" + prefix + "="
158 String p = ""
159 delegate.each {
160 result += p + "${it}"
161 p = sep
162 }
163 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700164}
165
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700166List.metaClass.p = { value, name ->
167 if (value != null && value != "") {
168 delegate << name + "=" + value
169 } else {
170 delegate
171 }
172}
173
174List.metaClass.p = { spec ->
175 if (spec != null && spec != "") {
176 delegate += spec
177 } else {
178 delegate
179 }
180}
181
182task deploy (type: Exec) {
183 println "Using deployment config: $deployConfig"
184 File configFile = new File(deployConfig)
185 def yaml = new Yaml()
186 def config = yaml.load(configFile.newReader())
187
188 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700189 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700190
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700191 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
192 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700193 }
194
195 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700196 if (config.seedServer) {
197 extraVars = extraVars.p(config.seedServer.extraVars)
198 .p(config.seedServer.password, "ansible_ssh_pass")
199 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
200 .p(config.seedServer.fabric_ip, "fabric_ip")
201 .p(config.seedServer.management_ip, "management_ip")
202 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700203 }
204
205 if (config.otherServers) {
206 extraVars = extraVars.p(config.otherServers.location, "prov_location")
207 .p(config.otherServers.rolesPath, "prov_role_path")
208 .p(config.otherServers.role, "prov_role")
209 }
210
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700211 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700212
213 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700214}