Skip to content

Commit 26fe606

Browse files
committed
More checks
1 parent 684fcb5 commit 26fe606

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,49 @@ class BuildPlugin implements Plugin<Project> {
237237
if (rootProject.hasProperty('requiresDocker') == false) {
238238
rootProject.rootProject.ext.requiresDocker = []
239239
rootProject.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
240-
if (new File('/usr/bin/docker').exists() || new File('/usr/local/bin/docker').exists()) {
241-
return
240+
final String dockerBinary
241+
if (new File('/usr/bin/docker').exists()) {
242+
dockerBinary = '/usr/bin/docker'
243+
} else if (new File('/usr/local/bin/docker').exists()) {
244+
dockerBinary = '/usr/local/bin/docker'
245+
}
246+
final int exitCode
247+
final String dockerErrorOutput
248+
if (dockerBinary == null) {
249+
exitCode = -1
250+
dockerErrorOutput = null
251+
} else {
252+
final Process process = new ProcessBuilder(dockerBinary, "images").start()
253+
exitCode = process.waitFor()
254+
if (exitCode == 0) {
255+
return
256+
}
257+
final Scanner scanner = new Scanner(process.errorStream).useDelimiter("\\A")
258+
dockerErrorOutput = scanner.hasNext() ? scanner.next() : ""
259+
process.closeStreams()
242260
}
243261
final List<String> tasks =
244262
((List<Task>)rootProject.requiresDocker).findAll { taskGraph.hasTask(it) }.collect { " ${it.path}".toString()}
245263
if (tasks.isEmpty() == false) {
246-
throw new GradleException(
247-
String.format(
248-
Locale.ROOT,
249-
"Docker is rqeuired to run the following task%s: \n%s",
250-
tasks.size() > 1 ? "s" : "",
251-
tasks.join('\n')))
264+
if (dockerBinary == null) {
265+
throw new GradleException(
266+
String.format(
267+
Locale.ROOT,
268+
"Docker is required to run the following task%s: \n%s",
269+
tasks.size() > 1 ? "s" : "",
270+
tasks.join('\n')))
271+
} else {
272+
assert exitCode > 0 && dockerErrorOutput != null
273+
throw new GradleException(
274+
String.format(
275+
Locale.ROOT,
276+
"a problem occurred running Docker yet it is required to run the following task%s: \n%s\n" +
277+
"the problem is that Docker exited with exit code [%d] with error output [%s]",
278+
tasks.size() > 1 ? "s" : "",
279+
tasks.join('\n'),
280+
exitCode,
281+
dockerErrorOutput))
282+
}
252283
}
253284
}
254285
}

0 commit comments

Comments
 (0)