diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java index 2aeb2bc8..38927054 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java @@ -188,11 +188,13 @@ public HelmInstaller installChart( if (reuseValues) { command.append(" --reuse-values"); } - String res = - Command.executeAndGetResponseAsJson(configuration, command.toString()) - .getOutput() - .getString(); - return new ObjectMapper().readValue(res, HelmInstaller.class); + Command.ProcessResultWithError result = + Command.executeAndGetResponseAsJson(configuration, command.toString()); + if (result.getProcessResult().getExitValue() != 0) { + throw new RuntimeException(result.getError()); + } + return new ObjectMapper() + .readValue(result.getProcessResult().getOutput().getString(), HelmInstaller.class); } public int uninstaller(HelmConfiguration configuration, String name, String namespace) @@ -201,7 +203,7 @@ public int uninstaller(HelmConfiguration configuration, String name, String name safeConcat(command, name); command.append(" -n "); safeConcat(command, namespace); - return Command.execute(configuration, command.toString()).getExitValue(); + return Command.execute(configuration, command.toString()).getProcessResult().getExitValue(); } public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespace) @@ -214,6 +216,7 @@ public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespa return new ObjectMapper() .readValue( Command.executeAndGetResponseAsJson(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(), HelmLs[].class); @@ -238,7 +241,10 @@ public HelmReleaseInfo getAll(HelmConfiguration configuration, String id, String safeConcat(command, namespace); try { String unparsedReleaseInfo = - Command.execute(configuration, command.toString()).getOutput().getString(); + Command.execute(configuration, command.toString()) + .getProcessResult() + .getOutput() + .getString(); return helmReleaseInfoParser.parseReleaseInfo(unparsedReleaseInfo); } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); @@ -259,14 +265,19 @@ private String getReleaseInfo( safeConcat(command, namespace); if (infoType.equals(NOTES_INFO_TYPE)) { return Command.executeAndGetResponseAsRaw(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(); } else if (infoType.equals(VALUES_INFO_TYPE)) { return Command.executeAndGetResponseAsJson(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(); } else { - return Command.execute(configuration, command.toString()).getOutput().getString(); + return Command.execute(configuration, command.toString()) + .getProcessResult() + .getOutput() + .getString(); } } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); @@ -302,6 +313,7 @@ public HelmLs getAppById(HelmConfiguration configuration, String appId, String n .readValue( Command.executeAndGetResponseAsJson( configuration, command.toString()) + .getProcessResult() .getOutput() .getString(), HelmLs[].class); diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java index 5204149d..8d7428b9 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java @@ -13,11 +13,11 @@ public class HelmRepoService { public HelmRepo[] getHelmRepo() throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - // System.out.println(new ProcessExecutor().getDirectory().getAbsolutePath()); HelmRepo[] repo = new ObjectMapper() .readValue( Command.executeAndGetResponseAsJson("helm search repo") + .getProcessResult() .getOutput() .getString(StandardCharsets.UTF_8.name()), HelmRepo[].class); @@ -39,7 +39,7 @@ public String addHelmRepo( "--ca-file " + System.getenv("CACERTS_DIR") + "/" + caFile + " "); } command = command.concat(nomRepo + " " + url); - return Command.execute(command).getOutput().getString(); + return Command.execute(command).getProcessResult().getOutput().getString(); } public void repoUpdate() throws InterruptedException, TimeoutException, IOException { diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java index 3dccd7d2..1843e1bd 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java @@ -11,6 +11,7 @@ public class HelmVersionService { public String getVersion() throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return Command.executeAndGetResponseAsRaw("helm version --template={{.Version}}") + .getProcessResult() .getOutput() .getString(StandardCharsets.UTF_8.name()); } diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java index d0519caa..0dc26100 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java @@ -1,7 +1,10 @@ package io.github.inseefrlab.helmwrapper.utils; import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeoutException; @@ -22,9 +25,11 @@ public class Command { "^[ ]*[a-z0-9]([-a-z0-9 ]*[a-z0-9 ])?(\\.[a-z0-9 ]([-a-z0-9 ]*[a-z0-9 ])?)*[ ]*$"); private static final Logger LOGGER = LoggerFactory.getLogger(Command.class); - private static ProcessExecutor getProcessExecutor() { + private static ProcessExecutor getProcessExecutor(OutputStream errorOutputStream) { ProcessExecutor processExecutor = new ProcessExecutor(); - processExecutor.redirectError(System.err); + if (errorOutputStream != null) { + processExecutor.redirectError(errorOutputStream); + } processExecutor.readOutput(true); processExecutor.addListener( new ProcessListener() { @@ -37,47 +42,81 @@ public void afterStart(Process process, ProcessExecutor executor) { return processExecutor; } - public static ProcessResult executeAndGetResponseAsJson( + public static ProcessResultWithError executeAndGetResponseAsJson( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration) + " --output json") - .execute(); + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit( + addConfigToCommand(command, helmConfiguration) + " --output json") + .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult executeAndGetResponseAsJson(String command) + public static ProcessResultWithError executeAndGetResponseAsJson(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return executeAndGetResponseAsJson(null, command); } - public static ProcessResult executeAndGetResponseAsRaw( + public static ProcessResultWithError executeAndGetResponseAsRaw( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration)) - .execute(); + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit(addConfigToCommand(command, helmConfiguration)) + .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult executeAndGetResponseAsRaw(String command) + public static ProcessResultWithError executeAndGetResponseAsRaw(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return executeAndGetResponseAsRaw(null, command); } - public static ProcessResult execute(HelmConfiguration helmConfiguration, String command) + public static ProcessResultWithError execute( + HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration)) - .execute(); + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit(addConfigToCommand(command, helmConfiguration)) + .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult execute(String command) + public static ProcessResultWithError execute(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return execute(null, command); } + public static class ProcessResultWithError { + + private ProcessResult processResult; + private String error = null; + + public ProcessResultWithError() {} + + public ProcessResultWithError(ProcessResult processResult, ByteArrayOutputStream boas) { + this.processResult = processResult; + if (boas != null) { + error = boas.toString(Charset.defaultCharset()); + } + } + + public ProcessResult getProcessResult() { + return processResult; + } + + public String getError() { + return error; + } + } + private static Map getEnv(HelmConfiguration helmConfiguration) { Map env = new HashMap<>(); if (System.getProperty("http.proxyHost") != null) {