|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. 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 | +import groovy.time.* |
| 19 | + |
| 20 | +/** |
| 21 | + * Utility to build docker images based in gradle projects |
| 22 | + * |
| 23 | + * This extends gradle's 'application' plugin logic with a 'distDocker' task which builds |
| 24 | + * a docker image from the Dockerfile of the project that applies this file. The image |
| 25 | + * is automatically tagged and pushed if a tag and/or a registry is given. |
| 26 | + * |
| 27 | + * Parameters that can be set on project level: |
| 28 | + * - dockerImageName (required): The name of the image to build (e.g. controller) |
| 29 | + * - dockerRegistry (optional): The registry to push to |
| 30 | + * - dockerImageTag (optional, default 'latest'): The tag for the image |
| 31 | + * - dockerImagePrefix (optional, default 'whisk'): The prefix for the image, |
| 32 | + * 'controller' becomes 'whisk/controller' per default |
| 33 | + * - dockerTimeout (optional, default 840): Timeout for docker operations in seconds |
| 34 | + * - dockerRetries (optional, default 3): How many times to retry docker operations |
| 35 | + * - dockerBinary (optional, default 'docker'): The binary to execute docker commands |
| 36 | + * - dockerBuildArgs (options, default ''): Project specific custom docker build arguments |
| 37 | + * - dockerHost (optional): The docker host to run commands on, default behaviour is |
| 38 | + * docker's own DOCKER_HOST environment variable |
| 39 | + */ |
| 40 | + |
| 41 | +ext { |
| 42 | + dockerRegistry = project.hasProperty('dockerRegistry') ? dockerRegistry + '/' : '' |
| 43 | + dockerImageTag = project.hasProperty('dockerImageTag') ? dockerImageTag : 'latest' |
| 44 | + dockerImagePrefix = project.hasProperty('dockerImagePrefix') ? dockerImagePrefix : 'whisk' |
| 45 | + dockerTimeout = project.hasProperty('dockerTimeout') ? dockerTimeout.toInteger() : 840 |
| 46 | + dockerRetries = project.hasProperty('dockerRetries') ? dockerRetries.toInteger() : 3 |
| 47 | + dockerBinary = project.hasProperty('dockerBinary') ? [dockerBinary] : ['docker'] |
| 48 | + dockerBuildArg = ['build'] |
| 49 | +} |
| 50 | +ext.dockerTaggedImageName = dockerRegistry + dockerImagePrefix + '/' + dockerImageName + ':' + dockerImageTag |
| 51 | + |
| 52 | +if(project.hasProperty('dockerHost')) { |
| 53 | + dockerBinary += ['--host', project.dockerHost] |
| 54 | +} |
| 55 | + |
| 56 | +if(project.hasProperty('dockerBuildArgs')) { |
| 57 | + dockerBuildArgs.each { arg -> |
| 58 | + dockerBuildArg += ['--build-arg', arg] |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +task distDocker { |
| 63 | + doLast { |
| 64 | + def start = new Date() |
| 65 | + def cmd = dockerBinary + dockerBuildArg + ['-t', dockerImageName, project.buildscript.sourceFile.getParentFile().getAbsolutePath()] |
| 66 | + retry(cmd, dockerRetries, dockerTimeout) |
| 67 | + println("Building '${dockerImageName}' took ${TimeCategory.minus(new Date(), start)}") |
| 68 | + } |
| 69 | +} |
| 70 | +task tagImage { |
| 71 | + doLast { |
| 72 | + def versionString = (dockerBinary + ['-v']).execute().text |
| 73 | + def matched = (versionString =~ /(\d+)\.(\d+)\.(\d+)/) |
| 74 | + |
| 75 | + def major = matched[0][1] as int |
| 76 | + def minor = matched[0][2] as int |
| 77 | + |
| 78 | + def dockerCmd = ['tag'] |
| 79 | + if(major == 1 && minor < 12) { |
| 80 | + dockerCmd += ['-f'] |
| 81 | + } |
| 82 | + retry(dockerBinary + dockerCmd + [dockerImageName, dockerTaggedImageName], dockerRetries, dockerTimeout) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +task pushImage { |
| 87 | + doLast { |
| 88 | + def cmd = dockerBinary + ['push', dockerTaggedImageName] |
| 89 | + retry(cmd, dockerRetries, dockerTimeout) |
| 90 | + } |
| 91 | +} |
| 92 | +pushImage.dependsOn tagImage |
| 93 | +pushImage.onlyIf { dockerRegistry != '' } |
| 94 | +distDocker.finalizedBy pushImage |
| 95 | + |
| 96 | +def retry(cmd, retries, timeout) { |
| 97 | + println("${new Date()}: Executing '${cmd.join(" ")}'") |
| 98 | + def proc = cmd.execute() |
| 99 | + proc.consumeProcessOutput(System.out, System.err) |
| 100 | + proc.waitForOrKill(timeout * 1000) |
| 101 | + if(proc.exitValue() != 0) { |
| 102 | + def message = "${new Date()}: Command '${cmd.join(" ")}' failed with exitCode ${proc.exitValue()}" |
| 103 | + if(proc.exitValue() == 143) { // 143 means the process was killed (SIGTERM signal) |
| 104 | + message = "${new Date()}: Command '${cmd.join(" ")}' was killed after ${timeout} seconds" |
| 105 | + } |
| 106 | + |
| 107 | + if(retries > 1) { |
| 108 | + println("${message}, ${retries-1} retries left, retrying...") |
| 109 | + retry(cmd, retries-1, timeout) |
| 110 | + } |
| 111 | + else { |
| 112 | + println("${message}, no more retries left, aborting...") |
| 113 | + throw new GradleException(message) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments