Andy Bavier | adea0fd | 2016-07-06 05:26:26 -0400 | [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 | */ |
| 16 | |
| 17 | import org.opencord.gradle.rules.* |
| 18 | import org.yaml.snakeyaml.Yaml |
| 19 | |
| 20 | allprojects { |
| 21 | apply plugin: 'base' |
| 22 | apply plugin: 'de.gesellix.docker' |
| 23 | //apply plugin: 'com.tmiyamon.config' |
| 24 | |
| 25 | docker { |
| 26 | // dockerHost = System.env.DOCKER_HOST ?: 'unix:///var/run/docker.sock' |
| 27 | // dockerHost = System.env.DOCKER_HOST ?: 'https://192.168.99.100:2376' |
| 28 | // certPath = System.getProperty('docker.cert.path') ?: "${System.getProperty('user.home')}/.docker/machine/machines/default" |
| 29 | // authConfigPlain = [ |
| 30 | // "username" : "joe", |
| 31 | // "password" : "some-pw-as-needed", |
| 32 | // "email" : "joe@acme.com", |
| 33 | // "serveraddress" : "https://index.docker.io/v1/" |
| 34 | // ] |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | ext { |
| 39 | |
| 40 | // Upstream registry to simplify filling out the comps table below |
| 41 | upstreamReg = project.hasProperty('upstreamReg') ? project.getProperty('upstreamReg') : 'docker.io' |
| 42 | |
| 43 | // Target registry to be used to publish docker images needed for deployment |
| 44 | targetReg = project.hasProperty('targetReg') ? project.getProperty('targetReg') : 'localhost:5000' |
| 45 | |
| 46 | // The tag used to tag the docker images push to the target registry |
| 47 | targetTag = project.hasProperty('targetTag') ? project.getProperty('targetTag') : 'candidate' |
| 48 | |
| 49 | // Component table |
| 50 | comps = [ |
| 51 | ] |
| 52 | |
| 53 | // Deployment target config file (yaml format); this can be overwritten from the command line |
| 54 | // using the -PdeployConfig=<file-path> syntax. |
| 55 | deployConfig = project.hasProperty('deployConfig') ? project.getProperty('deployConfig') : './config/default.yml' |
| 56 | |
| 57 | } |
| 58 | |
Andy Bavier | 6dbb1a3 | 2016-07-06 07:12:50 -0400 | [diff] [blame] | 59 | task copyAnsibleInventory(type: Copy) { |
| 60 | File configFile = new File(deployConfig) |
| 61 | def yaml = new Yaml() |
| 62 | def config = yaml.load(configFile.newReader()) |
| 63 | |
| 64 | from 'inventory/templates/single-prod' |
| 65 | into 'inventory' |
| 66 | expand([ |
| 67 | prod: config.seedServer.ip, |
| 68 | ]) |
| 69 | } |
| 70 | |
Andy Bavier | adea0fd | 2016-07-06 05:26:26 -0400 | [diff] [blame] | 71 | // ------------- PlaceHolders ---------------- |
| 72 | |
| 73 | task updateDocker (type: Exec) { |
| 74 | commandLine 'sudo', 'utils/enable-remote-docker-registry', "$targetReg" |
| 75 | } |
| 76 | |
| 77 | task prime { |
| 78 | // TODO this is a place-holder. |
Andy Bavier | 6dbb1a3 | 2016-07-06 07:12:50 -0400 | [diff] [blame] | 79 | // dependsOn updateDocker |
Andy Bavier | adea0fd | 2016-07-06 05:26:26 -0400 | [diff] [blame] | 80 | doFirst { |
| 81 | println "Using deployment config: $deployConfig" |
| 82 | File configFile = new File(deployConfig) |
| 83 | def yaml = new Yaml() |
| 84 | def config = yaml.load(configFile.newReader()) |
| 85 | println "Config: $config" |
| 86 | println "Access some data in config:\n\tSeed server IP=${config.seedServer.ip}" |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // ---------------- Useful tasks ---------------- |
| 91 | |
Andy Bavier | 1b8a537 | 2016-07-07 19:36:21 -0400 | [diff] [blame] | 92 | List.metaClass.asParam = { prefix, sep -> |
| 93 | if (delegate.size() == 0) { |
| 94 | "" |
| 95 | } |
| 96 | String result = "--" + prefix + "=" |
| 97 | String p = "" |
| 98 | delegate.each { |
| 99 | result += p + "${it}" |
| 100 | p = sep |
| 101 | } |
| 102 | result |
| 103 | } |
| 104 | |
| 105 | List.metaClass.p = { value, name -> |
| 106 | if (value != null && value != "") { |
| 107 | delegate << name + "=" + value |
| 108 | } else { |
| 109 | delegate |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | List.metaClass.p = { spec -> |
| 114 | if (spec != null && spec != "") { |
| 115 | delegate += spec |
| 116 | } else { |
| 117 | delegate |
| 118 | } |
| 119 | } |
| 120 | |
Andy Bavier | adea0fd | 2016-07-06 05:26:26 -0400 | [diff] [blame] | 121 | task fetchUpstreamImages { |
| 122 | comps.each { name, spec -> if (spec.type == 'image') { dependsOn "fetch" + name } } |
| 123 | } |
| 124 | |
| 125 | task fetchGitSubmodules { |
| 126 | comps.each { name, spec -> if (spec.type == 'gitmodule') { dependsOn "gitupdate" + name } } |
| 127 | } |
| 128 | |
| 129 | task fetch { |
| 130 | dependsOn fetchUpstreamImages |
| 131 | dependsOn fetchGitSubmodules |
| 132 | } |
| 133 | |
| 134 | task publishImages { |
| 135 | comps.each { name, spec -> if (spec.type == 'image') { dependsOn "publish" + name } } |
| 136 | } |
| 137 | |
| 138 | task publish { |
| 139 | dependsOn publishImages |
| 140 | } |
| 141 | |
| 142 | tasks.addRule(new DockerFetchRule(project)) |
| 143 | tasks.addRule(new DockerPublishRule(project)) |
| 144 | tasks.addRule(new DockerTagRule(project)) |
| 145 | tasks.addRule(new GitSubmoduleUpdateRule(project)) |
| 146 | |
Andy Bavier | c27a2aa | 2016-07-06 19:20:04 -0400 | [diff] [blame] | 147 | task deployPlatform (type: Exec) { |
Andy Bavier | 6dbb1a3 | 2016-07-06 07:12:50 -0400 | [diff] [blame] | 148 | dependsOn copyAnsibleInventory |
Andy Bavier | adea0fd | 2016-07-06 05:26:26 -0400 | [diff] [blame] | 149 | |
Andy Bavier | 6dbb1a3 | 2016-07-06 07:12:50 -0400 | [diff] [blame] | 150 | println "Using deployment config: $deployConfig" |
| 151 | File configFile = new File(deployConfig) |
| 152 | def yaml = new Yaml() |
| 153 | def config = yaml.load(configFile.newReader()) |
| 154 | |
| 155 | executable = "ansible-playbook" |
| 156 | args = ["-i", "inventory/single-prod"] |
| 157 | |
| 158 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 159 | args = args << "--user=$config.seedServer.user" |
| 160 | } |
| 161 | |
| 162 | def extraVars = [] |
| 163 | if (config.seedServer) { |
| 164 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 165 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 166 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 167 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 168 | .p(config.seedServer.management_ip, "management_ip") |
| 169 | .p(config.seedServer.management_network, "management_network") |
| 170 | .p(config.seedServer.management_iface, "management_iface") |
| 171 | .p(config.seedServer.external_ip, "external_ip") |
| 172 | .p(config.seedServer.external_network, "external_network") |
| 173 | .p(config.seedServer.external_iface, "external_iface") |
| 174 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 175 | .p(config.seedServer.fabric_network, "fabric_network") |
| 176 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 177 | .p(config.seedServer.domain, "domain") |
| 178 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 179 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 180 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 181 | .p(config.seedServer.port, "ansible_ssh_port") |
| 182 | } |
| 183 | |
| 184 | if (config.otherServers) { |
| 185 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 186 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 187 | .p(config.otherServers.role, "prov_role") |
| 188 | } |
| 189 | |
| 190 | if (config.docker) { |
| 191 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 192 | .p(config.docker.imageVersion, "docker_image_version") |
| 193 | } |
| 194 | |
| 195 | def skipTags = [].p(config.seedServer.skipTags) |
| 196 | |
| 197 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-head-playbook.yml" |
| 198 | } |
Andy Bavier | 1b8a537 | 2016-07-07 19:36:21 -0400 | [diff] [blame] | 199 | |
| 200 | task deploySingle (type: Exec) { |
| 201 | dependsOn copyAnsibleInventory |
| 202 | |
| 203 | println "Using deployment config: $deployConfig" |
| 204 | File configFile = new File(deployConfig) |
| 205 | def yaml = new Yaml() |
| 206 | def config = yaml.load(configFile.newReader()) |
| 207 | |
| 208 | executable = "ansible-playbook" |
| 209 | args = ["-i", "inventory/single-prod"] |
| 210 | |
| 211 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 212 | args = args << "--user=$config.seedServer.user" |
| 213 | } |
| 214 | |
| 215 | def extraVars = [] |
| 216 | if (config.seedServer) { |
| 217 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 218 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 219 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 220 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 221 | .p(config.seedServer.management_ip, "management_ip") |
| 222 | .p(config.seedServer.management_network, "management_network") |
| 223 | .p(config.seedServer.management_iface, "management_iface") |
| 224 | .p(config.seedServer.external_ip, "external_ip") |
| 225 | .p(config.seedServer.external_network, "external_network") |
| 226 | .p(config.seedServer.external_iface, "external_iface") |
| 227 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 228 | .p(config.seedServer.fabric_network, "fabric_network") |
| 229 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 230 | .p(config.seedServer.domain, "domain") |
| 231 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 232 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 233 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 234 | .p(config.seedServer.port, "ansible_ssh_port") |
| 235 | } |
| 236 | |
| 237 | if (config.otherServers) { |
| 238 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 239 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 240 | .p(config.otherServers.role, "prov_role") |
| 241 | } |
| 242 | |
| 243 | if (config.docker) { |
| 244 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 245 | .p(config.docker.imageVersion, "docker_image_version") |
| 246 | } |
| 247 | |
| 248 | def skipTags = [].p(config.seedServer.skipTags) |
| 249 | |
| 250 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-single-playbook.yml" |
| 251 | } |
| 252 | |
Andy Bavier | 2c0ec05 | 2016-07-18 19:23:26 -0400 | [diff] [blame] | 253 | task postDeployTests (type: Exec) { |
| 254 | dependsOn copyAnsibleInventory |
| 255 | |
| 256 | println "Using deployment config: $deployConfig" |
| 257 | File configFile = new File(deployConfig) |
| 258 | def yaml = new Yaml() |
| 259 | def config = yaml.load(configFile.newReader()) |
| 260 | |
| 261 | executable = "ansible-playbook" |
| 262 | args = ["-i", "inventory/single-prod"] |
| 263 | |
| 264 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 265 | args = args << "--user=$config.seedServer.user" |
| 266 | } |
| 267 | |
| 268 | def extraVars = [] |
| 269 | if (config.seedServer) { |
| 270 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 271 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 272 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 273 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 274 | .p(config.seedServer.management_ip, "management_ip") |
| 275 | .p(config.seedServer.management_network, "management_network") |
| 276 | .p(config.seedServer.management_iface, "management_iface") |
| 277 | .p(config.seedServer.external_ip, "external_ip") |
| 278 | .p(config.seedServer.external_network, "external_network") |
| 279 | .p(config.seedServer.external_iface, "external_iface") |
| 280 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 281 | .p(config.seedServer.fabric_network, "fabric_network") |
| 282 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 283 | .p(config.seedServer.domain, "domain") |
| 284 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 285 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 286 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 287 | .p(config.seedServer.port, "ansible_ssh_port") |
| 288 | } |
| 289 | |
| 290 | if (config.otherServers) { |
| 291 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 292 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 293 | .p(config.otherServers.role, "prov_role") |
| 294 | } |
| 295 | |
| 296 | if (config.docker) { |
| 297 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 298 | .p(config.docker.imageVersion, "docker_image_version") |
| 299 | } |
| 300 | |
| 301 | def skipTags = [].p(config.seedServer.skipTags) |
| 302 | |
| 303 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-post-deploy-playbook.yml" |
| 304 | } |
| 305 | |