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 | |
| 30 | dockerPath = project.hasProperty('dockerPath') ? project.getProperty('dockerPath') : '$dockerPath' |
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 | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 47 | task buildAutomationImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 48 | 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] | 49 | } |
| 50 | |
| 51 | task buildAutomationImageAnsible(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 52 | 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] | 53 | } |
| 54 | |
| 55 | task buildAutomationImages { |
| 56 | dependsOn buildAutomationImage |
| 57 | dependsOn buildAutomationImageAnsible |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 58 | } |
| 59 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 60 | task tagAutomationImage(type: Exec) { |
| 61 | dependsOn buildAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 62 | commandLine "$dockerPath/docker", 'tag', 'cord-maas-automation', "$targetReg/cord-maas-automation:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 63 | } |
| 64 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 65 | task tagAutomationImageAnsible(type: Exec) { |
| 66 | dependsOn buildAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 67 | 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] | 68 | } |
| 69 | |
| 70 | task tagAutomationImages { |
| 71 | dependsOn tagAutomationImage |
| 72 | dependsOn tagAutomationImageAnsible |
| 73 | } |
| 74 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 75 | task publishAutomationImage(type: Exec) { |
| 76 | dependsOn tagAutomationImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 77 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 78 | } |
| 79 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 80 | task publishAutomationImageAnsible(type: Exec) { |
| 81 | dependsOn tagAutomationImageAnsible |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 82 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-automation:$targetTag-ansible" |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | task publishAutomationImages { |
| 86 | dependsOn publishAutomationImage |
| 87 | dependsOn publishAutomationImageAnsible |
| 88 | } |
| 89 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 90 | task buildHarvesterImage(type: Exec) { |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 91 | commandLine "$dockerPath/docker", 'build', '-t', "cord-maas-dhcp-harvester", "./harvester" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | task tagHarvesterImage(type: Exec) { |
| 95 | dependsOn buildHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 96 | 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] | 97 | } |
| 98 | |
| 99 | task publishHarvesterImage(type: Exec) { |
| 100 | dependsOn tagHarvesterImage |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 101 | commandLine "$dockerPath/docker", 'push', "$targetReg/cord-maas-dhcp-harvester:$targetTag" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | |
| 106 | // To be used to fetch upstream binaries, clone repos, etc. |
| 107 | task fetch(type: Exec) { |
| 108 | // this is where we fetch upstream artifacts that we do not need internet for the build phase" |
| 109 | // Placeholdr example: |
David K. Bainbridge | 19b8d27 | 2016-05-26 21:20:43 -0700 | [diff] [blame^] | 110 | commandLine "$dockerPath/docker", "pull", "golang:alpine" |
| 111 | commandLine "$dockerPath/docker", "pull", "python:2.7-alpine" |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // To be used to generate all needed binaries that need to be present on the target |
| 115 | // as docker images in the local docker runner. |
| 116 | task buildImages { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 117 | dependsOn buildBootstrapImage |
| 118 | dependsOn buildHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 119 | dependsOn buildAutomationImages |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | task tagImages { |
| 123 | dependsOn tagBootstrapImage |
| 124 | dependsOn tagHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 125 | dependsOn tagAutomationImages |
Zsolt Haraszti | 2a792f6 | 2016-05-12 17:49:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | task publish { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 129 | dependsOn publishBootstrapImage |
| 130 | dependsOn publishHarvesterImage |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 131 | dependsOn publishAutomationImages |
| 132 | } |
| 133 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 134 | // ~~~~~~~~~~~~~~~~~~~ Deployment / Test Tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 135 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 136 | List.metaClass.asParam = { prefix, sep -> |
| 137 | if (delegate.size() == 0) { |
| 138 | "" |
| 139 | } |
| 140 | String result = "--" + prefix + "=" |
| 141 | String p = "" |
| 142 | delegate.each { |
| 143 | result += p + "${it}" |
| 144 | p = sep |
| 145 | } |
| 146 | result |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 149 | List.metaClass.p = { value, name -> |
| 150 | if (value != null && value != "") { |
| 151 | delegate << name + "=" + value |
| 152 | } else { |
| 153 | delegate |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | List.metaClass.p = { spec -> |
| 158 | if (spec != null && spec != "") { |
| 159 | delegate += spec |
| 160 | } else { |
| 161 | delegate |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | task deploy (type: Exec) { |
| 166 | println "Using deployment config: $deployConfig" |
| 167 | File configFile = new File(deployConfig) |
| 168 | def yaml = new Yaml() |
| 169 | def config = yaml.load(configFile.newReader()) |
| 170 | |
| 171 | executable = "ansible-playbook" |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 172 | args = ["-i", config.seedServer.ip + ','] |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 173 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 174 | if ( config.seedServer.user != null && config.seedServer.user != "" ) { |
| 175 | args = args << "--user=$config.seedServer.user" |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | def extraVars = [] |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 179 | if (config.seedServer) { |
| 180 | extraVars = extraVars.p(config.seedServer.extraVars) |
| 181 | .p(config.seedServer.password, "ansible_ssh_pass") |
| 182 | .p(config.seedServer.sudoPassword, "ansible_sudo_pass") |
| 183 | .p(config.seedServer.fabric_ip, "fabric_ip") |
| 184 | .p(config.seedServer.management_ip, "management_ip") |
| 185 | .p(config.seedServer.external_ip, "external_ip") |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (config.otherServers) { |
| 189 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 190 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 191 | .p(config.otherServers.role, "prov_role") |
| 192 | } |
| 193 | |
David K. Bainbridge | 13c765c | 2016-05-26 11:24:22 -0700 | [diff] [blame] | 194 | def skipTags = [].p(config.seedServer.skipTags) |
David K. Bainbridge | 10b0c11 | 2016-05-24 13:17:23 -0700 | [diff] [blame] | 195 | |
| 196 | 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] | 197 | } |