alshabib | a06a934 | 2016-08-24 17:59:45 -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 | */ |
| 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 | |
| 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 | |
| 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. |
| 79 | // dependsOn updateDocker |
| 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 | |
| 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 | |
| 121 | task fetch << { |
| 122 | logger.info 'Platform install has nothing to fetch' |
| 123 | } |
| 124 | |
| 125 | task buildImages << { |
| 126 | logger.info 'Platform install has nothing to build' |
| 127 | } |
| 128 | |
| 129 | task publishImages { |
| 130 | comps.each { name, spec -> if (spec.type == 'image') { dependsOn "publish" + name } } |
| 131 | } |
| 132 | |
| 133 | task publish { |
| 134 | dependsOn publishImages |
| 135 | } |
| 136 | |
| 137 | tasks.addRule(new DockerFetchRule(project)) |
| 138 | tasks.addRule(new DockerPublishRule(project)) |
| 139 | tasks.addRule(new DockerTagRule(project)) |
| 140 | tasks.addRule(new GitSubmoduleUpdateRule(project)) |
| 141 | |
| 142 | task deployPlatform (type: Exec) { |
| 143 | dependsOn copyAnsibleInventory |
| 144 | |
| 145 | println "Using deployment config: $deployConfig" |
| 146 | File configFile = new File(deployConfig) |
| 147 | def yaml = new Yaml() |
| 148 | def config = yaml.load(configFile.newReader()) |
| 149 | |
| 150 | executable = "ansible-playbook" |
| 151 | args = ["-i", "inventory/single-prod"] |
| 152 | |
| 153 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 154 | args = args << "--user=$config.seedServer.user" |
| 155 | } |
| 156 | |
| 157 | def extraVars = [] |
| 158 | if (config.seedServer) { |
| 159 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 160 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 161 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 162 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 163 | .p(config.seedServer.management_ip, "management_ip") |
| 164 | .p(config.seedServer.management_network, "management_network") |
| 165 | .p(config.seedServer.management_iface, "management_iface") |
| 166 | .p(config.seedServer.external_ip, "external_ip") |
| 167 | .p(config.seedServer.external_network, "external_network") |
| 168 | .p(config.seedServer.external_iface, "external_iface") |
| 169 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 170 | .p(config.seedServer.fabric_network, "fabric_network") |
| 171 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 172 | .p(config.seedServer.domain, "domain") |
| 173 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 174 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 175 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 176 | .p(config.seedServer.port, "ansible_ssh_port") |
| 177 | } |
| 178 | |
| 179 | if (config.otherServers) { |
| 180 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 181 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 182 | .p(config.otherServers.role, "prov_role") |
| 183 | } |
| 184 | |
| 185 | if (config.docker) { |
| 186 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 187 | .p(config.docker.imageVersion, "docker_image_version") |
| 188 | } |
| 189 | |
| 190 | def skipTags = [].p(config.seedServer.skipTags) |
| 191 | |
| 192 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-head-playbook.yml" |
| 193 | } |
| 194 | |
| 195 | task deploySingle (type: Exec) { |
| 196 | dependsOn copyAnsibleInventory |
| 197 | |
| 198 | println "Using deployment config: $deployConfig" |
| 199 | File configFile = new File(deployConfig) |
| 200 | def yaml = new Yaml() |
| 201 | def config = yaml.load(configFile.newReader()) |
| 202 | |
| 203 | executable = "ansible-playbook" |
| 204 | args = ["-i", "inventory/single-prod"] |
| 205 | |
| 206 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 207 | args = args << "--user=$config.seedServer.user" |
| 208 | } |
| 209 | |
| 210 | def extraVars = [] |
| 211 | if (config.seedServer) { |
| 212 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 213 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 214 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 215 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 216 | .p(config.seedServer.management_ip, "management_ip") |
| 217 | .p(config.seedServer.management_network, "management_network") |
| 218 | .p(config.seedServer.management_iface, "management_iface") |
| 219 | .p(config.seedServer.external_ip, "external_ip") |
| 220 | .p(config.seedServer.external_network, "external_network") |
| 221 | .p(config.seedServer.external_iface, "external_iface") |
| 222 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 223 | .p(config.seedServer.fabric_network, "fabric_network") |
| 224 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 225 | .p(config.seedServer.domain, "domain") |
| 226 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 227 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 228 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 229 | .p(config.seedServer.port, "ansible_ssh_port") |
| 230 | } |
| 231 | |
| 232 | if (config.otherServers) { |
| 233 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 234 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 235 | .p(config.otherServers.role, "prov_role") |
| 236 | } |
| 237 | |
| 238 | if (config.docker) { |
| 239 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 240 | .p(config.docker.imageVersion, "docker_image_version") |
| 241 | } |
| 242 | |
| 243 | def skipTags = [].p(config.seedServer.skipTags) |
| 244 | |
| 245 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-single-playbook.yml" |
| 246 | } |
| 247 | |
| 248 | task postDeployTests (type: Exec) { |
| 249 | dependsOn copyAnsibleInventory |
| 250 | |
| 251 | println "Using deployment config: $deployConfig" |
| 252 | File configFile = new File(deployConfig) |
| 253 | def yaml = new Yaml() |
| 254 | def config = yaml.load(configFile.newReader()) |
| 255 | |
| 256 | executable = "ansible-playbook" |
| 257 | args = ["-i", "inventory/single-prod"] |
| 258 | |
| 259 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 260 | args = args << "--user=$config.seedServer.user" |
| 261 | } |
| 262 | |
| 263 | def extraVars = [] |
| 264 | if (config.seedServer) { |
| 265 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 266 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 267 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 268 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 269 | .p(config.seedServer.management_ip, "management_ip") |
| 270 | .p(config.seedServer.management_network, "management_network") |
| 271 | .p(config.seedServer.management_iface, "management_iface") |
| 272 | .p(config.seedServer.external_ip, "external_ip") |
| 273 | .p(config.seedServer.external_network, "external_network") |
| 274 | .p(config.seedServer.external_iface, "external_iface") |
| 275 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 276 | .p(config.seedServer.fabric_network, "fabric_network") |
| 277 | .p(config.seedServer.fabric_iface, "fabric_iface") |
| 278 | .p(config.seedServer.domain, "domain") |
| 279 | .p(config.seedServer.virtualbox_support, "virtualbox_support") |
| 280 | .p(config.seedServer.power_helper_user, "power_helper_user") |
| 281 | .p(config.seedServer.power_helper_host, "power_helper_host") |
| 282 | .p(config.seedServer.port, "ansible_ssh_port") |
| 283 | } |
| 284 | |
| 285 | if (config.otherServers) { |
| 286 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 287 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 288 | .p(config.otherServers.role, "prov_role") |
| 289 | } |
| 290 | |
| 291 | if (config.docker) { |
| 292 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 293 | .p(config.docker.imageVersion, "docker_image_version") |
| 294 | } |
| 295 | |
| 296 | def skipTags = [].p(config.seedServer.skipTags) |
| 297 | |
| 298 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-post-deploy-playbook.yml" |
| 299 | } |
| 300 | |