blob: 5103b769b4ab03a2859c71107932df0c17d3eca2 [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
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700132// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
133
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700134List.metaClass.asParam = { prefix, sep ->
135 if (delegate.size() == 0) {
136 ""
137 }
138 String result = "--" + prefix + "="
139 String p = ""
140 delegate.each {
141 result += p + "${it}"
142 p = sep
143 }
144 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700145}
146
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700147List.metaClass.p = { value, name ->
148 if (value != null && value != "") {
149 delegate << name + "=" + value
150 } else {
151 delegate
152 }
153}
154
155List.metaClass.p = { spec ->
156 if (spec != null && spec != "") {
157 delegate += spec
158 } else {
159 delegate
160 }
161}
162
163task deploy (type: Exec) {
164 println "Using deployment config: $deployConfig"
165 File configFile = new File(deployConfig)
166 def yaml = new Yaml()
167 def config = yaml.load(configFile.newReader())
168
169 executable = "ansible-playbook"
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700170 args = ["-i", config.seedServer.ip + ',']
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700171
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700172 if ( config.seedServer.user != null && config.seedServer.user != "" ) {
173 args = args << "--user=$config.seedServer.user"
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700174 }
175
176 def extraVars = []
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700177 if (config.seedServer) {
178 extraVars = extraVars.p(config.seedServer.extraVars)
179 .p(config.seedServer.password, "ansible_ssh_pass")
180 .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
181 .p(config.seedServer.fabric_ip, "fabric_ip")
182 .p(config.seedServer.management_ip, "management_ip")
183 .p(config.seedServer.external_ip, "external_ip")
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700184 }
185
186 if (config.otherServers) {
187 extraVars = extraVars.p(config.otherServers.location, "prov_location")
188 .p(config.otherServers.rolesPath, "prov_role_path")
189 .p(config.otherServers.role, "prov_role")
190 }
191
David K. Bainbridge13c765c2016-05-26 11:24:22 -0700192 def skipTags = [].p(config.seedServer.skipTags)
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700193
194 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
195 println args
196}