Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 1 | /* |
| 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. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 16 | import org.yaml.snakeyaml.Yaml |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 17 | |
| 18 | ext { |
| 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. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 26 | // 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. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 29 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 30 | dockerPath = project.hasProperty('dockerPath') ? project.getProperty('dockerPath') : '/usr/bin' |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 31 | } |
| 32 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 33 | task buildBootstrapImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 34 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-bootstrap', './bootstrap' |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 35 | } |
| 36 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 37 | task tagBootstrapImage(type: Exec) { |
| 38 | dependsOn buildBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 39 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-bootstrap', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 40 | } |
| 41 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 42 | task publishBootstrapImage(type: Exec) { |
| 43 | dependsOn tagBootstrapImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 44 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-bootstrap:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 45 | } |
| 46 | |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 47 | task buildAllocationImage(type: Exec) { |
| 48 | commandLine "$dockerPath/docker", 'build', '-t', 'cord-maas-ip-allocator', './ip-allocator' |
| 49 | } |
| 50 | |
| 51 | task tagAllocationImage(type: Exec) { |
| 52 | dependsOn buildBootstrapImage |
| 53 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-ip-allocator', "$targetReg/cord-maas-ip-allocator:$targetTag" |
| 54 | } |
| 55 | |
| 56 | task publishAllocationImage(type: Exec) { |
| 57 | dependsOn tagBootstrapImage |
| 58 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-ip-allocator:$targetTag" |
| 59 | } |
| 60 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 61 | task buildAutomationImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 62 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation", "-f", "./automation/Dockerfile", "./automation" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | task buildAutomationImageAnsible(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 66 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-automation:ansible", "-f", "./automation/Dockerfile.ansible", "./automation" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | task buildAutomationImages { |
| 70 | dependsOn buildAutomationImage |
| 71 | dependsOn buildAutomationImageAnsible |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 72 | } |
| 73 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 74 | task tagAutomationImage(type: Exec) { |
| 75 | dependsOn buildAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 76 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 77 | } |
| 78 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 79 | task tagAutomationImageAnsible(type: Exec) { |
| 80 | dependsOn buildAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 81 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation:ansible', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | task tagAutomationImages { |
| 85 | dependsOn tagAutomationImage |
| 86 | dependsOn tagAutomationImageAnsible |
| 87 | } |
| 88 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 89 | task publishAutomationImage(type: Exec) { |
| 90 | dependsOn tagAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 91 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 94 | task publishAutomationImageAnsible(type: Exec) { |
| 95 | dependsOn tagAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 96 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | task publishAutomationImages { |
| 100 | dependsOn publishAutomationImage |
| 101 | dependsOn publishAutomationImageAnsible |
| 102 | } |
| 103 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 104 | task buildHarvesterImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 105 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | task tagHarvesterImage(type: Exec) { |
| 109 | dependsOn buildHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 110 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-dhcp-harvester', "$targetReg/cord-maas-dhcp-harvester:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | task publishHarvesterImage(type: Exec) { |
| 114 | dependsOn tagHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 115 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 | |
| 120 | // To be used to fetch upstream binaries, clone repos, etc. |
| 121 | task 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. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame] | 124 | commandLine "$dockerPath/docker", "pull", "golang:alpine" |
| 125 | commandLine "$dockerPath/docker", "pull", "python:2.7-alpine" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 126 | } |
| 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. |
| 130 | task buildImages { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 131 | dependsOn buildBootstrapImage |
| 132 | dependsOn buildHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 133 | dependsOn buildAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 134 | dependsOn buildAllocationImage |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | task tagImages { |
| 138 | dependsOn tagBootstrapImage |
| 139 | dependsOn tagHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 140 | dependsOn tagAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 141 | dependsOn tagAllocationImage |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | task publish { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 145 | dependsOn publishBootstrapImage |
| 146 | dependsOn publishHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 147 | dependsOn publishAutomationImages |
David K. Bainbridge | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame^] | 148 | dependsOn publishAllocationImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 149 | } |
| 150 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 151 | // ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 152 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 153 | List.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. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 164 | } |
| 165 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 166 | List.metaClass.p = { value, name -> |
| 167 | if (value != null && value != "") { |
| 168 | delegate << name + "=" + value |
| 169 | } else { |
| 170 | delegate |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | List.metaClass.p = { spec -> |
| 175 | if (spec != null && spec != "") { |
| 176 | delegate += spec |
| 177 | } else { |
| 178 | delegate |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | task 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. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 189 | args = ["-i", config.seedServer.ip + ','] |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 190 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 191 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 192 | args = args << "--user=$config.seedServer.user" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | def extraVars = [] |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 196 | 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. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 203 | } |
| 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. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 211 | def skipTags = [].p(config.seedServer.skipTags) |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 212 | |
| 213 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "head-node.yml" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 214 | } |