blob: 19a3a5a818e67e4ba655699ba3271477555d13f7 [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'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070029}
30
David K. Bainbridgeb5415042016-05-13 17:06:10 -070031task buildBootstrapImage(type: Exec) {
32 commandLine '/usr/bin/docker', 'build', '-t', 'cord-maas-bootstrap', './bootstrap'
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070033}
34
David K. Bainbridgeb5415042016-05-13 17:06:10 -070035task tagBootstrapImage(type: Exec) {
36 dependsOn buildBootstrapImage
37 commandLine '/usr/bin/docker', 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070038}
39
David K. Bainbridgeb5415042016-05-13 17:06:10 -070040task publishBootstrapImage(type: Exec) {
41 dependsOn tagBootstrapImage
42 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-bootstrap:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070043}
44
David K. Bainbridgeb5415042016-05-13 17:06:10 -070045task buildAutomationImage(type: Exec) {
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070046 commandLine '/usr/bin/docker', 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation"
47}
48
49task buildAutomationImageAnsible(type: Exec) {
50 commandLine '/usr/bin/docker', 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation"
51}
52
53task buildAutomationImages {
54 dependsOn buildAutomationImage
55 dependsOn buildAutomationImageAnsible
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070056}
57
David K. Bainbridgeb5415042016-05-13 17:06:10 -070058task tagAutomationImage(type: Exec) {
59 dependsOn buildAutomationImage
60 commandLine '/usr/bin/docker', 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070061}
62
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070063task tagAutomationImageAnsible(type: Exec) {
64 dependsOn buildAutomationImageAnsible
65 commandLine '/usr/bin/docker', 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible"
66}
67
68task tagAutomationImages {
69 dependsOn tagAutomationImage
70 dependsOn tagAutomationImageAnsible
71}
72
David K. Bainbridgeb5415042016-05-13 17:06:10 -070073task publishAutomationImage(type: Exec) {
74 dependsOn tagAutomationImage
75 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-automation:$targetTag"
76}
77
David K. Bainbridgeefa951d2016-05-26 10:54:25 -070078task publishAutomationImageAnsible(type: Exec) {
79 dependsOn tagAutomationImageAnsible
80 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-automation:$targetTag-ansible"
81}
82
83task publishAutomationImages {
84 dependsOn publishAutomationImage
85 dependsOn publishAutomationImageAnsible
86}
87
David K. Bainbridgeb5415042016-05-13 17:06:10 -070088task buildHarvesterImage(type: Exec) {
89 commandLine '/usr/bin/docker', 'build', '-t', "cord-maas-dhcp-harvester", "./harvester"
90}
91
92task tagHarvesterImage(type: Exec) {
93 dependsOn buildHarvesterImage
94 commandLine '/usr/bin/docker', 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
95}
96
97task publishHarvesterImage(type: Exec) {
98 dependsOn tagHarvesterImage
99 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700100}
101
102// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
103
104// To be used to fetch upstream binaries, clone repos, etc.
105task fetch(type: Exec) {
106 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
107 // Placeholdr example:
108 commandLine "/usr/bin/docker", "pull", "golang:alpine"
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700109 commandLine "/usr/bin/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700110}
111
112// To be used to generate all needed binaries that need to be present on the target
113// as docker images in the local docker runner.
114task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700115 dependsOn buildBootstrapImage
116 dependsOn buildHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700117 dependsOn buildAutomationImages
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700118}
119
120task tagImages {
121 dependsOn tagBootstrapImage
122 dependsOn tagHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700123 dependsOn tagAutomationImages
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700124}
125
126task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700127 dependsOn publishBootstrapImage
128 dependsOn publishHarvesterImage
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700129 dependsOn publishAutomationImages
130}
131
132task build {
133 dependsOn buildImages
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700134}
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700135
136// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
137
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700138List.metaClass.asParam = { prefix, sep ->
139 if (delegate.size() == 0) {
140 ""
141 }
142 String result = "--" + prefix + "="
143 String p = ""
144 delegate.each {
145 result += p + "${it}"
146 p = sep
147 }
148 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700149}
150
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700151List.metaClass.p = { value, name ->
152 if (value != null && value != "") {
153 delegate << name + "=" + value
154 } else {
155 delegate
156 }
157}
158
159List.metaClass.p = { spec ->
160 if (spec != null && spec != "") {
161 delegate += spec
162 } else {
163 delegate
164 }
165}
166
167task deploy (type: Exec) {
168 println "Using deployment config: $deployConfig"
169 File configFile = new File(deployConfig)
170 def yaml = new Yaml()
171 def config = yaml.load(configFile.newReader())
172
173 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700174 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700175
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700176 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
177 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700178 }
179
180 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700181 if (config.seedServer) {
182 extraVars = extraVars.p(config.seedServer.extraVars)
183 .p(config.seedServer.password, "ansible_ssh_pass")
184 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
185 .p(config.seedServer.fabric_ip, "fabric_ip")
186 .p(config.seedServer.management_ip, "management_ip")
187 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700188 }
189
190 if (config.otherServers) {
191 extraVars = extraVars.p(config.otherServers.location, "prov_location")
192 .p(config.otherServers.rolesPath, "prov_role_path")
193 .p(config.otherServers.role, "prov_role")
194 }
195
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700196 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700197
198 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
199 println args
200}