blob: dc49de67c2a51f17f4bd2c610bb29333899885cc [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) {
46 commandLine '/usr/bin/docker', 'build', '-t', "cord-maas-automation", "./automation"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070047}
48
David K. Bainbridgeb5415042016-05-13 17:06:10 -070049task tagAutomationImage(type: Exec) {
50 dependsOn buildAutomationImage
51 commandLine '/usr/bin/docker', 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070052}
53
David K. Bainbridgeb5415042016-05-13 17:06:10 -070054task publishAutomationImage(type: Exec) {
55 dependsOn tagAutomationImage
56 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-automation:$targetTag"
57}
58
59task buildHarvesterImage(type: Exec) {
60 commandLine '/usr/bin/docker', 'build', '-t', "cord-maas-dhcp-harvester", "./harvester"
61}
62
63task tagHarvesterImage(type: Exec) {
64 dependsOn buildHarvesterImage
65 commandLine '/usr/bin/docker', 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
66}
67
68task publishHarvesterImage(type: Exec) {
69 dependsOn tagHarvesterImage
70 commandLine '/usr/bin/docker', 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070071}
72
73// ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~
74
75// To be used to fetch upstream binaries, clone repos, etc.
76task fetch(type: Exec) {
77 // this is where we fetch upstream artifacts that we do not need internet for the build phase"
78 // Placeholdr example:
79 commandLine "/usr/bin/docker", "pull", "golang:alpine"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070080 commandLine "/usr/bin/docker", "pull", "python:2.7-alpine"
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070081}
82
83// To be used to generate all needed binaries that need to be present on the target
84// as docker images in the local docker runner.
85task buildImages {
David K. Bainbridgeb5415042016-05-13 17:06:10 -070086 dependsOn buildBootstrapImage
87 dependsOn buildHarvesterImage
88 dependsOn buildAutomationImage
89}
90
91task tagImages {
92 dependsOn tagBootstrapImage
93 dependsOn tagHarvesterImage
94 dependsOn tagAutomationImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -070095}
96
97task publish {
David K. Bainbridgeb5415042016-05-13 17:06:10 -070098 dependsOn publishBootstrapImage
99 dependsOn publishHarvesterImage
100 dependsOn publishAutomationImage
Zsolt Haraszti2a792f62016-05-12 17:49:02 -0700101}
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700102
103// ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~
104
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700105List.metaClass.asParam = { prefix, sep ->
106 if (delegate.size() == 0) {
107 ""
108 }
109 String result = "--" + prefix + "="
110 String p = ""
111 delegate.each {
112 result += p + "${it}"
113 p = sep
114 }
115 result
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700116}
117
David K. Bainbridge10b0c112016-05-24 13:17:23 -0700118List.metaClass.p = { value, name ->
119 if (value != null && value != "") {
120 delegate << name + "=" + value
121 } else {
122 delegate
123 }
124}
125
126List.metaClass.p = { spec ->
127 if (spec != null && spec != "") {
128 delegate += spec
129 } else {
130 delegate
131 }
132}
133
134task deploy (type: Exec) {
135 println "Using deployment config: $deployConfig"
136 File configFile = new File(deployConfig)
137 def yaml = new Yaml()
138 def config = yaml.load(configFile.newReader())
139
140 executable = "ansible-playbook"
141 args = ["-i", config.seedNode.ip + ',']
142
143 if ( config.seedNode.user != null && config.seedNode.user != "" ) {
144 args = args << "--user=$config.seedNode.user"
145 }
146
147 def extraVars = []
148 if (config.seedNode) {
149 extraVars = extraVars.p(config.seedNode.extraVars)
150 .p(config.seedNode.password, "ansible_ssh_pass")
151 .p(config.seedNode.sudoPassword, "ansible_sudo_pass")
152 .p(config.seedNode.fabric_ip, "fabric_ip")
153 .p(config.seedNode.management_ip, "management_ip")
154 .p(config.seedNode.external_ip, "external_ip")
155 }
156
157 if (config.otherServers) {
158 extraVars = extraVars.p(config.otherServers.location, "prov_location")
159 .p(config.otherServers.rolesPath, "prov_role_path")
160 .p(config.otherServers.role, "prov_role")
161 }
162
163 def skipTags = [].p(config.seedNode.skipTags)
164
165 args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml"
166 println args
167}