@Test(expected = AmbariException.class) public void shouldServerActionFailWhenCommandParametersAreMissing() throws Exception { Map<String, String> commandParams = new HashMap<>(); EasyMock.expect(executionCommand.getCommandParams()).andReturn(commandParams).times(2); customScriptServerAction.setExecutionCommand(executionCommand); replayAll(); CommandReport commandReport = customScriptServerAction.execute(requestSharedDataContext); }
@Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext); }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }