blob: fcc000ea1163e177765a5affb91329a6177d5a71 [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
30 dockerPath = project.hasProperty('dockerPath') ? project.getProperty('dockerPath') : '$dockerPath'
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. Bainbridgeb5415042016-05-13 17:06:10 -070047task buildAutomationImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070048 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070049}
50
51task buildAutomationImageAnsible(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070052 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070053}
54
55task buildAutomationImages {
56 dependsOn buildAutomationImage
57 dependsOn buildAutomationImageAnsible
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070058}
59
David K. Bainbridgeb5415042016-05-13 17:06:10 -070060task tagAutomationImage(type: Exec) {
61 dependsOn buildAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070062 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070063}
64
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070065task tagAutomationImageAnsible(type: Exec) {
66 dependsOn buildAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -070067 commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070068}
69
70task tagAutomationImages {
71 dependsOn tagAutomationImage
72 dependsOn tagAutomationImageAnsible
73}
74
David K. Bainbridgeb5415042016-05-13 17:06:10 -070075task publishAutomationImage(type: Exec) {
76 dependsOn tagAutomationImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070077 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070078}
79
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070080task publishAutomationImageAnsible(type: Exec) {
81 dependsOn tagAutomationImageAnsible
David K. Bainbridge19b8d272016-05-26 21:20:43 -070082 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible"
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070083}
84
85task publishAutomationImages {
86 dependsOn publishAutomationImage
87 dependsOn publishAutomationImageAnsible
88}
89
David K. Bainbridgeb5415042016-05-13 17:06:10 -070090task buildHarvesterImage(type: Exec) {
David K. Bainbridge19b8d272016-05-26 21:20:43 -070091 commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070092}
93
94task tagHarvesterImage(type: Exec) {
95 dependsOn buildHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -070096 commandLine "$dockerPath/docker", 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070097}
98
99task publishHarvesterImage(type: Exec) {
100 dependsOn tagHarvesterImage
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700101 commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700102}
103
104// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
105
106// To be used to fetch upstream binaries, clone repos, etc.
107task fetch(type: Exec) {
108 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
109 // Placeholdr example:
David K. Bainbridge19b8d272016-05-26 21:20:43 -0700110 commandLine "$dockerPath/docker", "pull", "golang:alpine"
111 commandLine "$dockerPath/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700112}
113
114// To be used to generate all needed binaries that need to be present on the target
115// as docker images in the local docker runner.
116task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700117 dependsOn buildBootstrapImage
118 dependsOn buildHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700119 dependsOn buildAutomationImages
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700120}
121
122task tagImages {
123 dependsOn tagBootstrapImage
124 dependsOn tagHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700125 dependsOn tagAutomationImages
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700126}
127
128task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700129 dependsOn publishBootstrapImage
130 dependsOn publishHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700131 dependsOn publishAutomationImages
132}
133
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700134// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
135
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700136List.metaClass.asParam = { prefix, sep ->
137 if (delegate.size() == 0) {
138 ""
139 }
140 String result = "--" + prefix + "="
141 String p = ""
142 delegate.each {
143 result += p + "${it}"
144 p = sep
145 }
146 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700147}
148
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700149List.metaClass.p = { value, name ->
150 if (value != null && value != "") {
151 delegate << name + "=" + value
152 } else {
153 delegate
154 }
155}
156
157List.metaClass.p = { spec ->
158 if (spec != null && spec != "") {
159 delegate += spec
160 } else {
161 delegate
162 }
163}
164
165task deploy (type: Exec) {
166 println "Using deployment config: $deployConfig"
167 File configFile = new File(deployConfig)
168 def yaml = new Yaml()
169 def config = yaml.load(configFile.newReader())
170
171 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700172 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700173
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700174 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
175 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700176 }
177
178 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700179 if (config.seedServer) {
180 extraVars = extraVars.p(config.seedServer.extraVars)
181 .p(config.seedServer.password, "ansible_ssh_pass")
182 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
183 .p(config.seedServer.fabric_ip, "fabric_ip")
184 .p(config.seedServer.management_ip, "management_ip")
185 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700186 }
187
188 if (config.otherServers) {
189 extraVars = extraVars.p(config.otherServers.location, "prov_location")
190 .p(config.otherServers.rolesPath, "prov_role_path")
191 .p(config.otherServers.role, "prov_role")
192 }
193
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700194 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700195
196 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700197}