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