INFRA-425 | Abhishek | Send proper error messages when apply fails

This commit is contained in:
Abhishek Katiyar
2020-07-28 16:02:39 +05:30
parent 4458f68420
commit f30cbc367b
2 changed files with 15 additions and 28 deletions

View File

@@ -1,12 +1,12 @@
package com.navi.infra.portal.bash;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
public class BashExecute {
@@ -16,11 +16,15 @@ public class BashExecute {
builder.command("sh", "-c", command);
try {
Process process = builder.start();
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorStream=new BufferedReader(new InputStreamReader(process.getErrorStream()));
int exitCode = process.waitFor();
List<String> result = bufferedReader.lines().collect(Collectors.toList());
List<String> errors = errorStream.lines().collect(Collectors.toList());
log.info("Process exited with code {}", exitCode);
if(exitCode!=0){
throw new RuntimeException(getErrorString(errors));
}else log.info("{}",result);
return exitCode;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
@@ -28,20 +32,9 @@ public class BashExecute {
}
}
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines()
.forEach(consumer);
}
private static String getErrorString(List<String> errors) {
StringBuffer error=new StringBuffer();
errors.forEach(err -> error.append(err));
return error.toString();
}
}

View File

@@ -186,14 +186,8 @@ public class KubernetesManifestService {
log.info("Executing command {}", command);
if (BashExecute.execute(command) > 0) {
portalEventPublisher
.publishKubeApplyEvent(KubeApplyResult.UNSUCCESSFUL, clusterName, manifestObject,
manifest.getName());
throw new RuntimeException(String.format("Unable to apply %s", manifestObject));
}
portalEventPublisher
.publishKubeApplyEvent(KubeApplyResult.SUCCESSFUL, clusterName, manifestObject,
manifest.getName());
log.info("Successfully applied {}", manifestObject);
}