alshabib | 676bef5 | 2016-06-02 12:18:32 -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 | |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 40 | // Deployment target config file (yaml format); this can be overwritten from the command line |
| 41 | // using the -PdeployConfig=<file-path> syntax. |
alshabib | 0bf710f | 2017-02-14 11:26:38 -0800 | [diff] [blame] | 42 | deployConfig = project.hasProperty('deployConfig') ? project.getProperty('deployConfig') : '../build/config/default.yml' |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 43 | |
| 44 | println "Using deployment config: $deployConfig" |
| 45 | File configFile = new File(deployConfig) |
| 46 | def yaml = new Yaml() |
| 47 | config = yaml.load(configFile.newReader()) |
| 48 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 49 | // Upstream registry to simplify filling out the comps table below |
| 50 | upstreamReg = project.hasProperty('upstreamReg') ? project.getProperty('upstreamReg') : 'docker.io' |
| 51 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 52 | // 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] | 53 | targetReg = project.hasProperty('targetReg') |
| 54 | ? project.getProperty('targetReg') |
| 55 | : config.docker && config.docker.registry |
| 56 | ? config.docker.registry |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 57 | : config.headnode.ip |
| 58 | ? config.headnode.ip + ":5000" |
David K. Bainbridge | 3da136f | 2016-11-15 17:22:06 -0800 | [diff] [blame] | 59 | : 'localhost:5000' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 60 | |
| 61 | // 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] | 62 | targetTag = project.hasProperty('targetTag') |
| 63 | ? project.getProperty('targetTag') |
| 64 | : config.docker && config.docker.imageVersion |
| 65 | ? config.docker.imageVersion |
| 66 | : 'candidate' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 69 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 70 | task buildOnosApps (type: Exec) { |
| 71 | workingDir './apps' |
Charles Chan | cafaca5 | 2016-06-23 16:40:41 -0700 | [diff] [blame] | 72 | commandLine 'mvn', 'clean', 'install', '-U' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | task copyLocalRepo (dependsOn: buildOnosApps, type: Exec) { |
| 76 | workingDir './' |
David K. Bainbridge | 64030c4 | 2016-07-29 00:01:15 -0700 | [diff] [blame] | 77 | commandLine 'cp', '-R', System.env.HOME + '/.m2/repository', '.' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | task buildRepoImage (dependsOn: copyLocalRepo, type: Exec) { |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 81 | commandLine 'docker', 'build', '-t', 'opencord/mavenrepo', '.' |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | task tagMavenRepoImage (type: Exec) { |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 85 | commandLine 'docker', 'tag', 'opencord/mavenrepo', "$targetReg/mavenrepo:$targetTag" |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | task publishMavenRepoImage (type: Exec) { |
| 89 | dependsOn tagMavenRepoImage |
| 90 | commandLine 'docker', 'push', "$targetReg/mavenrepo:$targetTag" |
| 91 | } |
| 92 | |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 93 | List.metaClass.asParam = { prefix, sep -> |
| 94 | if (delegate.size() == 0) { |
| 95 | "" |
| 96 | } |
| 97 | String result = "--" + prefix + "=" |
| 98 | String p = "" |
| 99 | delegate.each { |
| 100 | result += p + "${it}" |
| 101 | p = sep |
| 102 | } |
| 103 | result |
| 104 | } |
| 105 | |
| 106 | List.metaClass.p = { value, name -> |
| 107 | if (value != null && value != "") { |
| 108 | delegate << name + "=" + value |
| 109 | } else { |
| 110 | delegate |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | List.metaClass.p = { spec -> |
| 115 | if (spec != null && spec != "") { |
| 116 | delegate += spec |
| 117 | } else { |
| 118 | delegate |
| 119 | } |
| 120 | } |
| 121 | |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 122 | // ~~~~~~~~~~~~~~~~~~~ Global tasks ~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 | |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 124 | task fetch { |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 125 | logger.info 'Nothing to fetch for me' |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 126 | } |
| 127 | |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 128 | task buildImages { |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 129 | dependsOn buildOnosApps |
| 130 | dependsOn copyLocalRepo |
| 131 | dependsOn buildRepoImage |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 132 | } |
| 133 | |
alshabib | f751d34 | 2016-06-17 14:20:30 -0700 | [diff] [blame] | 134 | task publish { |
alshabib | 28a35f9 | 2016-06-03 00:16:26 -0700 | [diff] [blame] | 135 | dependsOn publishMavenRepoImage |
alshabib | 676bef5 | 2016-06-02 12:18:32 -0700 | [diff] [blame] | 136 | } |
| 137 | |
alshabib | e0b3e70 | 2016-08-29 14:43:01 -0700 | [diff] [blame] | 138 | task deploy (type: Exec) { |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 139 | executable = "ansible-playbook" |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 140 | args = ["-i", config.headnode.ip + ','] |
alshabib | 853a403 | 2016-06-16 14:54:34 -0700 | [diff] [blame] | 141 | |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 142 | if ( config.headnode.ansible_user != null && config.headnode.ansible_user != "" ) { |
| 143 | args = args << "--user=$config.headnode.ansible_user" |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | def extraVars = [] |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 147 | if (config.headnode) { |
| 148 | extraVars = extraVars.p(config.common.extraVars) |
| 149 | .p(config.headnode.ansible_ssh_pass, "ansible_ssh_pass") |
| 150 | .p(config.headnode.ansible_sudo_pass, "ansible_sudo_pass") |
| 151 | .p(config.headnode.ansible_ssh_port, "ansible_ssh_port") |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (config.otherServers) { |
| 155 | extraVars = extraVars.p(config.otherServers.location, "prov_location") |
| 156 | .p(config.otherServers.rolesPath, "prov_role_path") |
| 157 | .p(config.otherServers.role, "prov_role") |
| 158 | } |
| 159 | |
| 160 | if (config.docker) { |
| 161 | extraVars = extraVars.p(config.docker.registry, "docker_registry") |
| 162 | .p(config.docker.imageVersion, "docker_image_version") |
| 163 | } |
| 164 | |
Zack Williams | 04869e2 | 2017-02-01 13:42:18 -0700 | [diff] [blame] | 165 | def skipTags = [].p(config.common.skipTags) |
alshabib | e5e26ca | 2016-06-17 10:12:54 -0700 | [diff] [blame] | 166 | |
| 167 | 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] | 168 | } |
| 169 | |
| 170 | |
alshabib | 9c7e028 | 2016-06-03 13:50:29 -0700 | [diff] [blame] | 171 | // Depending on the version of the apps this will either make a release or publish a snapshot |
| 172 | task release (type: Exec) { |
| 173 | workingDir './apps' |
| 174 | commandLine 'mvn', 'clean', 'deploy' |
| 175 | } |