forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make DockerSupportService configuration cache compatible
Allow filtering docker command output for better cc compatibility
- Loading branch information
Showing
3 changed files
with
171 additions
and
55 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...d-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.docker; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* This class models the result of running a command. It captures the exit code, standard output and standard error and allows | ||
* applying String filter for stdout as this is intended to create configuration cache compatible output which | ||
* aims to be agnostic. | ||
*/ | ||
public class DockerResult { | ||
|
||
private int exitCode; | ||
private String stdout; | ||
private String stderr; | ||
|
||
public DockerResult(int exitCode, String stdout, String stderr) { | ||
this.exitCode = exitCode; | ||
this.stdout = stdout; | ||
this.stderr = stderr; | ||
} | ||
|
||
public int getExitCode() { | ||
return exitCode; | ||
} | ||
|
||
public String getStdout() { | ||
return stdout; | ||
} | ||
|
||
public String getStderr() { | ||
return stderr; | ||
} | ||
|
||
public void setExitCode(int exitCode) { | ||
this.exitCode = exitCode; | ||
} | ||
|
||
public void setStdout(String stdout) { | ||
this.stdout = stdout; | ||
} | ||
|
||
public void setStderr(String stderr) { | ||
this.stderr = stderr; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return exitCode == 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DockerResult that = (DockerResult) o; | ||
return exitCode == that.exitCode && Objects.equals(stdout, that.stdout) && Objects.equals(stderr, that.stderr); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(exitCode, stdout, stderr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ls-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerValueSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.docker; | ||
|
||
import org.gradle.api.provider.ListProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.provider.ValueSource; | ||
import org.gradle.api.provider.ValueSourceParameters; | ||
import org.gradle.process.ExecOperations; | ||
import org.gradle.process.ExecResult; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
|
||
public abstract class DockerValueSource implements ValueSource<DockerResult, DockerValueSource.Parameters> { | ||
public interface OutputFilter { | ||
String filter(String input); | ||
} | ||
|
||
interface Parameters extends ValueSourceParameters { | ||
ListProperty<String> getArgs(); | ||
|
||
Property<OutputFilter> getOutputFilter(); | ||
} | ||
|
||
@Inject | ||
abstract protected ExecOperations getExecOperations(); | ||
|
||
@Override | ||
public DockerResult obtain() { | ||
return runCommand(getParameters().getArgs().get()); | ||
} | ||
|
||
/** | ||
* Runs a command and captures the exit code, standard output and standard error. | ||
* | ||
* @param args the command and any arguments to execute | ||
* @return a object that captures the result of running the command. If an exception occurring | ||
* while running the command, or the process was killed after reaching the 10s timeout, | ||
* then the exit code will be -1. | ||
*/ | ||
private DockerResult runCommand(List args) { | ||
if (args.size() == 0) { | ||
throw new IllegalArgumentException("Cannot execute with no command"); | ||
} | ||
|
||
ByteArrayOutputStream stdout = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream stderr = new ByteArrayOutputStream(); | ||
|
||
final ExecResult execResult = getExecOperations().exec(spec -> { | ||
// The redundant cast is to silence a compiler warning. | ||
spec.setCommandLine(args); | ||
spec.setStandardOutput(stdout); | ||
spec.setErrorOutput(stderr); | ||
spec.setIgnoreExitValue(true); | ||
}); | ||
return new DockerResult(execResult.getExitValue(), filtered(stdout.toString()), stderr.toString()); | ||
} | ||
|
||
private String filtered(String input) { | ||
return getParameters().getOutputFilter().get().filter(input); | ||
} | ||
|
||
} |