diff options
author | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-07-12 08:58:56 +0900 |
---|---|---|
committer | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-07-12 09:00:38 +0900 |
commit | 13f987af48136ab3a3a24434782e984b7c0d25e8 (patch) | |
tree | 0dac113082d837839b0ebc9581fb50ac6bc12cc4 /saltstack-adapter/saltstack-adapter-provider/src/main | |
parent | 67c775f8adfce8b0e139ce9e6b70386047090c69 (diff) |
saltstack reqExecSlsFile API implemented
Issue-ID: CCSDK-356
Change-Id: Ib9e7a7c147992505e85d1f2f195cb7f52930057f
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
Diffstat (limited to 'saltstack-adapter/saltstack-adapter-provider/src/main')
2 files changed, 38 insertions, 9 deletions
diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java index e3046dba..84e5d4f1 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java @@ -111,11 +111,11 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { /** * This default constructor is used as a work around because the activator wasn't getting called */ - public SaltstackAdapterImpl() { + public SaltstackAdapterImpl() throws SvcLogicException{ initialize(new SaltstackAdapterPropertiesProviderImpl()); } - public SaltstackAdapterImpl(SaltstackAdapterPropertiesProvider propProvider) { + public SaltstackAdapterImpl(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException{ initialize(propProvider); } @@ -160,7 +160,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { /** * initialize the Saltstack adapter based on default and over-ride configuration data */ - private void initialize(SaltstackAdapterPropertiesProvider propProvider) { + private void initialize(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException{ Properties props = propProvider.getProperties(); @@ -204,12 +204,19 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { String sshPort = props.getProperty(SS_SERVER_PORT); logger.info("Creating ssh client with ssh KEY from " + sshKey); sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword, sshKey); + } else if ("NONE".equalsIgnoreCase(clientType)) { + logger.info("No saltstack-adapter.properties defined so reading from DG props"); + sshClient = null; } else { logger.info("No saltstack-adapter.properties defined so reading from DG props"); sshClient = null; } + } catch (NumberFormatException e) { + logger.error("Error Initializing Saltstack Adapter due to Unknown Exception", e); + throw new SvcLogicException("Saltstack Adapter Property file parsing Error = port in property file has to be an integer."); } catch (Exception e) { logger.error("Error Initializing Saltstack Adapter due to Unknown Exception", e); + throw new SvcLogicException("Saltstack Adapter Property file parsing Error = " + e.getMessage()); } logger.info("Initialized Saltstack Adapter"); @@ -231,27 +238,45 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } private String putToCommands(SvcLogicContext ctx, String slsFileName, - String reqID, String applyTo) throws SvcLogicException { + String applyTo) throws SvcLogicException { String constructedCommand = ""; try { File file = new File(slsFileName); + String slsFile = file.getName(); + if (!slsFile.substring(slsFile.lastIndexOf("."), + slsFile.length()).equalsIgnoreCase(".sls")) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input file " + + "is not of type .sls"); + } InputStream in = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; in.read(data); String str = new String(data, "UTF-8"); in.close(); - constructedCommand = "echo "+str+" > /srv/salt/"+reqID+".sls; salt '"+applyTo+"' state.apply "+reqID+" --out=json --static"; + String slsWithoutExtn = stripExtension(slsFile); + constructedCommand = "echo -e \""+str+"\" > /srv/salt/"+slsFile+"; cd /srv/salt/; salt '"+ + applyTo+"' state.apply "+slsWithoutExtn+" --out=json --static"; } catch (FileNotFoundException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input SLS file " + "not found in path : " + slsFileName+". "+ e.getMessage()); } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input SLS file " + "error in path : " + slsFileName +". "+ e.getMessage()); + } catch (StringIndexOutOfBoundsException e) { + doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input file " + + "is not of type .sls"); } logger.info("Command to be executed on server : " + constructedCommand); return constructedCommand; } + private String stripExtension (String str) { + if (str == null) return null; + int pos = str.lastIndexOf("."); + if (pos == -1) return str; + return str.substring(0, pos); + } + private String putToCommands(String slsName, String applyTo) { String constructedCommand = "cd /srv/salt/; salt '"+applyTo+"' state.apply "+slsName+" --out=json --static"; @@ -265,8 +290,8 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { // Check status of test request returned by Agent if (testResult.getStatusCode() != SaltstackResultCodes.FINAL_SUCCESS.getValue()) { - doFailure(ctx, testResult.getStatusCode(), "Request for execution of command failed. Reason = " + testResult.getStatusMessage()); ctx.setAttribute(ID_ATTRIBUTE_NAME, reqID); + doFailure(ctx, testResult.getStatusCode(), "Request for execution of command failed. Reason = " + testResult.getStatusMessage()); return; } else { logger.info(String.format("Execution of request : successful.")); @@ -338,7 +363,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { reqID = messageProcessor.reqId(params); String slsFile = messageProcessor.reqSlsFile(params); String applyTo = messageProcessor.reqApplyToDevices(params); - String commandToExecute = putToCommands(ctx, slsFile, reqID, applyTo); + String commandToExecute = putToCommands(ctx, slsFile, applyTo); testResult = execCommand(params, commandToExecute); testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); checkResponseStatus(testResult, ctx, reqID, true); diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java index 372d5657..f282a338 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java @@ -205,8 +205,12 @@ public class SaltstackMessageParser { throwIfMissingMandatoryParam(params, key); } String slsName = params.get(SaltstackMessageParser.SLS_NAME); - if(slsName.substring(slsName.lastIndexOf("."), slsName.length()).equalsIgnoreCase(".sls")) - return stripExtension(slsName); + try { + if(slsName.substring(slsName.lastIndexOf("."), slsName.length()).equalsIgnoreCase(".sls")) + return stripExtension(slsName); + } catch (StringIndexOutOfBoundsException e) { + return slsName; + } return slsName; } |