Skip to content

Commit

Permalink
Merge pull request #339 from yasassri/shell_execution
Browse files Browse the repository at this point in the history
Fix and improve Shell execution flow
  • Loading branch information
yasassri authored Jan 11, 2018
2 parents c3aed0b + 5d1b13d commit 2278121
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 11 deletions.
134 changes: 134 additions & 0 deletions common/src/main/java/org/wso2/testgrid/common/ShellExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.testgrid.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.testgrid.common.exception.CommandExecutionException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
* Responsible in executing given shell scripts
*
* @since 1.0
*/
public class ShellExecutor {

private static Logger logger = LoggerFactory.getLogger(ShellExecutor.class);

private File workingDirectory;

public ShellExecutor(File workingDirectory) {
this.workingDirectory = workingDirectory;
}

/**
* StreamGobbler to handle process builder output.
*
* @since 1.0
*/
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private StringBuilder stringBuilder = new StringBuilder();

public StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public void run() {

String line;
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(System.lineSeparator());
}
} catch (IOException e) {
logger.error("Error occurred while generating the output ", e);
}
}

/**
* Get the value of the output streams.
*
* @return String value of the {@link StringBuilder}
*/
public String getOutput() {
return stringBuilder.toString();
}
}

/**
* Returns current working directory.
*
* @return working directory
*/
public String getWorkingDirectory() {
return workingDirectory.getAbsolutePath();
}

/**
* Executes a shell command.
*
* @param command Command to execute
* @return boolean for successful/unsuccessful command execution (success == true)
* @throws CommandExecutionException if an {@link IOException} occurs while executing the command
*/
public boolean executeCommand(String command) throws CommandExecutionException {

if (logger.isDebugEnabled()) {
logger.debug("Running shell command : " + command + ", from directory : " + workingDirectory.getName());
}
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", command);
try {
if (workingDirectory != null && workingDirectory.exists()) {
processBuilder.directory(workingDirectory);
}
Process process = processBuilder.start();

StreamGobbler outputStreamGobbler = new StreamGobbler(process.getInputStream());
StreamGobbler errorStreamGobbler = new StreamGobbler(process.getErrorStream());
outputStreamGobbler.run();
errorStreamGobbler.run();
int status = process.waitFor();
if (status > 0) {
logger.error("Execution result : " + errorStreamGobbler.getOutput());
return false;
}
logger.info("Execution result : " + outputStreamGobbler.getOutput());
return true;
} catch (IOException e) {
throw new CommandExecutionException(
"Error occurred while executing the command '" + command + "', " + "from directory '"
+ workingDirectory.getName() + "", e);
} catch (InterruptedException e) {
throw new CommandExecutionException(
"InterruptedException occurred while executing the command '" + command + "', " + "from directory '"
+ workingDirectory.getName() + "", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import org.wso2.testgrid.common.Infrastructure;
import org.wso2.testgrid.common.InfrastructureProvider;
import org.wso2.testgrid.common.Script;
import org.wso2.testgrid.common.ShellExecutor;
import org.wso2.testgrid.common.exception.CommandExecutionException;
import org.wso2.testgrid.common.exception.TestGridInfrastructureException;
import org.wso2.testgrid.common.util.TestGridUtil;

import java.nio.file.Paths;

Expand Down Expand Up @@ -61,15 +61,16 @@ public Deployment createInfrastructure(Infrastructure infrastructure, String inf
String testPlanLocation = Paths.get(infraRepoDir, "DeploymentPatterns" ,
infrastructure.getName()).toString();

logger.info("Creating the Kubernetes cluster...");
logger.info("Executing provisioning scripts...");
try {
TestGridUtil.executeCommand("bash " + Paths.get(testPlanLocation,
getScriptToExecute(infrastructure, Script.ScriptType.INFRA_CREATE)), null);
ShellExecutor executor = new ShellExecutor(null);
executor.executeCommand("bash " + Paths
.get(testPlanLocation, getScriptToExecute(infrastructure, Script.ScriptType.INFRA_CREATE)));
} catch (CommandExecutionException e) {
throw new TestGridInfrastructureException("Exception occurred while executing the infra-create script " +
"for deployment-pattern '" + infrastructure.getName() + "'" , e);
}
return null;
return new Deployment();
}

@Override
Expand All @@ -79,16 +80,17 @@ public boolean removeInfrastructure(Infrastructure infrastructure, String infraR
infrastructure.getName()).toString();

logger.info("Destroying test environment...");
ShellExecutor executor = new ShellExecutor(null);
try {
if (TestGridUtil.executeCommand("bash " +
Paths.get(testPlanLocation, getScriptToExecute(infrastructure,
Script.ScriptType.INFRA_DESTROY)),
null)) {

if (executor.executeCommand("bash " + Paths
.get(testPlanLocation, getScriptToExecute(infrastructure, Script.ScriptType.INFRA_DESTROY)))) {
return true;
}
} catch (CommandExecutionException e) {
throw new TestGridInfrastructureException("Exception occurred while executing the infra-destroy script " +
"for deployment-pattern '" + infrastructure.getName() + "'" , e);
throw new TestGridInfrastructureException(
"Exception occurred while executing the infra-destroy script " + "for deployment-pattern '"
+ infrastructure.getName() + "'", e);
}
return false;
}
Expand Down

0 comments on commit 2278121

Please sign in to comment.