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