Matteo Scandolo | bea9527 | 2017-08-08 13:05:23 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 19 | /* |
| 20 | * Copyright 2012 the original author or authors. |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | * you may not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
| 33 | */ |
| 34 | |
| 35 | import org.opencord.gradle.rules.* |
| 36 | import org.yaml.snakeyaml.Yaml |
| 37 | |
| 38 | allprojects { |
| 39 | apply plugin: 'base' |
| 40 | apply plugin: 'de.gesellix.docker' |
| 41 | //apply plugin: 'com.tmiyamon.config' |
| 42 | |
| 43 | docker { |
| 44 | // dockerHost = System.env.DOCKER_HOST ?: 'unix:///var/run/docker.sock' |
| 45 | // dockerHost = System.env.DOCKER_HOST ?: 'https://192.168.99.100:2376' |
| 46 | // certPath = System.getProperty('docker.cert.path') ?: "${System.getProperty('user.home')}/.docker/machine/machines/default" |
| 47 | // authConfigPlain = [ |
| 48 | // "username" : "joe", |
| 49 | // "password" : "some-pw-as-needed", |
| 50 | // "email" : "joe@acme.com", |
| 51 | // "serveraddress" : "https://index.docker.io/v1/" |
| 52 | // ] |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | ext { |
| 57 | |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 58 | // Deployment target config file (yaml format); this can be overwritten from the command line |
| 59 | // using the -PdeployConfig=<file-path> syntax. |
alshabib | 0bf710f | 2017-02-14 11:26:38 -0800 | [diff] [blame] | 60 | deployConfig = project.hasProperty('deployConfig') ? project.getProperty('deployConfig') : '../build/config/default.yml' |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 61 | |
| 62 | println "Using deployment config: $deployConfig" |
| 63 | File configFile = new File(deployConfig) |
| 64 | def yaml = new Yaml() |
| 65 | config = yaml.load(configFile.newReader()) |
| 66 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 67 | // Upstream registry to simplify filling out the comps table below |
| 68 | upstreamReg = project.hasProperty('upstreamReg') ? project.getProperty('upstreamReg') : 'docker.io' |
| 69 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 70 | // Target registry to be used to publish docker images needed for deployment |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 71 | targetReg = project.hasProperty('targetReg') |
| 72 | ? project.getProperty('targetReg') |
| 73 | : config.docker && config.docker.registry |
| 74 | ? config.docker.registry |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 75 | : config.headnode.ip |
| 76 | ? config.headnode.ip + ":5000" |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 77 | : 'localhost:5000' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 78 | |
| 79 | // The tag used to tag the docker images push to the target registry |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 80 | targetTag = project.hasProperty('targetTag') |
| 81 | ? project.getProperty('targetTag') |
| 82 | : config.docker && config.docker.imageVersion |
| 83 | ? config.docker.imageVersion |
| 84 | : 'candidate' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 85 | } |
| 86 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 87 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 88 | task buildOnosApps (type: Exec) { |
| 89 | workingDir './apps' |
Charles Chan | cafaca5 | 2016-06-23 16:40:41 -0700 | [diff] [blame] | 90 | commandLine 'mvn', 'clean', 'install', '-U' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | task copyLocalRepo (dependsOn: buildOnosApps, type: Exec) { |
| 94 | workingDir './' |
David K. Bainbridge | 64030c4 | 2016-07-29 00:01:15 -0700 | [diff] [blame] | 95 | commandLine 'cp', '-R', System.env.HOME + '/.m2/repository', '.' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | task buildRepoImage (dependsOn: copyLocalRepo, type: Exec) { |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 99 | commandLine 'docker', 'build', '-t', 'opencord/mavenrepo', '.' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | task tagMavenRepoImage (type: Exec) { |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 103 | commandLine 'docker', 'tag', 'opencord/mavenrepo', "$targetReg/mavenrepo:$targetTag" |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | task publishMavenRepoImage (type: Exec) { |
| 107 | dependsOn tagMavenRepoImage |
| 108 | commandLine 'docker', 'push', "$targetReg/mavenrepo:$targetTag" |
| 109 | } |
| 110 | |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 111 | List.metaClass.asParam = { prefix, sep -> |
| 112 | if (delegate.size() == 0) { |
| 113 | "" |
| 114 | } |
| 115 | String result = "--" + prefix + "=" |
| 116 | String p = "" |
| 117 | delegate.each { |
| 118 | result += p + "${it}" |
| 119 | p = sep |
| 120 | } |
| 121 | result |
| 122 | } |
| 123 | |
| 124 | List.metaClass.p = { value, name -> |
| 125 | if (value != null && value != "") { |
| 126 | delegate << name + "=" + value |
| 127 | } else { |
| 128 | delegate |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | List.metaClass.p = { spec -> |
| 133 | if (spec != null && spec != "") { |
| 134 | delegate += spec |
| 135 | } else { |
| 136 | delegate |
| 137 | } |
| 138 | } |
| 139 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 140 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 141 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 142 | task fetch { |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 143 | logger.info 'Nothing to fetch for me' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 144 | } |
| 145 | |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 146 | task buildImages { |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 147 | dependsOn buildOnosApps |
| 148 | dependsOn copyLocalRepo |
| 149 | dependsOn buildRepoImage |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 150 | } |
| 151 | |
alshabib | f751d34 | 2016-06-17 14:20:30 -0700 | [diff] [blame] | 152 | task publish { |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 153 | dependsOn publishMavenRepoImage |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 154 | } |
| 155 | |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 156 | task deploy (type: Exec) { |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 157 | executable = "ansible-playbook" |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 158 | args = ["-i", config.headnode.ip + ','] |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 159 | |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 160 | if ( config.headnode.ansible_user != null && config.headnode.ansible_user != "" ) { |
| 161 | args = args << "--user=$config.headnode.ansible_user" |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | def extraVars = [] |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 165 | if (config.headnode) { |
| 166 | extraVars = extraVars.p(config.common.extraVars) |
| 167 | .p(config.headnode.ansible_ssh_pass, "ansible_ssh_pass") |
| 168 | .p(config.headnode.ansible_sudo_pass, "ansible_sudo_pass") |
| 169 | .p(config.headnode.ansible_ssh_port, "ansible_ssh_port") |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if (config.otherServers) { |
| 173 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 174 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 175 | .p(config.otherServers.role, "prov_role") |
| 176 | } |
| 177 | |
| 178 | if (config.docker) { |
| 179 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 180 | .p(config.docker.imageVersion, "docker_image_version") |
| 181 | } |
| 182 | |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 183 | def skipTags = [].p(config.common.skipTags) |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 184 | |
| 185 | args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "mavenrepo.yml" |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
alshabib | 9c7e028 | 2016-06-03 13:50:29 -0700 | [diff] [blame] | 189 | // Depending on the version of the apps this will either make a release or publish a snapshot |
| 190 | task release (type: Exec) { |
| 191 | workingDir './apps' |
| 192 | commandLine 'mvn', 'clean', 'deploy' |
| 193 | } |