@Test public void shouldCommandParametersBeSet() throws Exception { Map<String, Cluster> clsMap = new HashMap<>(); clsMap.put("test-cluster", clusterMock); Map<String, String> configMap = new HashMap<>(); configMap.put("hdfs_user", "hdfs-test-user"); EasyMock.expect(clusterMock.getClusterId()).andReturn(1l); EasyMock.expect(clusterMock.getClusterName()).andReturn("test-cluster"); EasyMock.expect(clusterMock.getSecurityType()).andReturn(SecurityType.NONE).times(3); EasyMock.expect(clusterMock.getDesiredConfigByType("hadoop-env")).andReturn(configMock); EasyMock.expect(configMock.getProperties()).andReturn(configMap); EasyMock.expect(actionManagerMock.getNextRequestId()).andReturn(1l); EasyMock.expect(clustersMock.getClusters()).andReturn(clsMap); EasyMock.expect(configurationMock.getServerTempDir()).andReturn("/var/lib/ambari-server/tmp").times(2); EasyMock.expect(configurationMock.getProperty(Configuration.POST_USER_CREATION_HOOK)).andReturn("/var/lib/ambari-server/resources/scripts/post-user-creation-hook.sh").anyTimes(); EasyMock.expect(objectMapperMock.writeValueAsString(((PostUserCreationHookContext) userCreatedEvent.getContext()).getUserGroups())).andReturn("{testUser=[hdfs, yarn]}"); stageMock.setStageId(-1); stageMock.addServerActionCommand(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject(Role.class), EasyMock.anyObject(RoleCommand.class), EasyMock.anyString(), EasyMock.anyObject(ServiceComponentHostServerActionEvent.class), EasyMock.<Map<String, String>>anyObject(), EasyMock.anyString(), EasyMock.<Map<String, Map<String, String>>>anyObject(), EasyMock.anyInt(), EasyMock.anyBoolean(), EasyMock.anyBoolean()); EasyMock.expect(requestFactoryMock.createNewFromStages(Arrays.asList(stageMock))).andReturn(null); EasyMock.expect(stageFactoryMock.createNew(1, "/var/lib/ambari-server/tmp:1", "test-cluster", 1, "Post user creation hook for [ 1 ] users", "{}", "{}", "{}")).andReturn(stageMock); replayAll(); hookService.onUserCreatedEvent(userCreatedEvent); }
@Subscribe public void onUserCreatedEvent(UserCreatedEvent event) throws AmbariException { LOGGER.info("Preparing hook execution for event: {}", event); try { RequestStageContainer requestStageContainer = new RequestStageContainer(actionManager.getNextRequestId(), null, requestFactory, actionManager); ClusterData clsData = getClusterData(); PostUserCreationHookContext ctx = (PostUserCreationHookContext) event.getContext(); String stageContextText = String.format(POST_USER_CREATION_REQUEST_CONTEXT, ctx.getUserGroups().size()); Stage stage = stageFactory.createNew(requestStageContainer.getId(), configuration.getServerTempDir() + File.pathSeparatorChar + requestStageContainer.getId(), clsData.getClusterName(), clsData.getClusterId(), stageContextText, "{}", "{}", "{}"); stage.setStageId(requestStageContainer.getLastStageId()); ServiceComponentHostServerActionEvent serverActionEvent = new ServiceComponentHostServerActionEvent("ambari-server-host", System.currentTimeMillis()); Map<String, String> commandParams = prepareCommandParams(ctx, clsData); stage.addServerActionCommand(PostUserCreationHookServerAction.class.getName(), "ambari", Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, clsData.getClusterName(), serverActionEvent, commandParams, stageContextText, null, null, false, false); requestStageContainer.addStages(Collections.singletonList(stage)); requestStageContainer.persist(); } catch (IOException e) { LOGGER.error("Failed to assemble stage for server action. Event: {}", event); throw new AmbariException("Failed to assemble stage for server action", e); } }
UserHookService implements HookService { @Subscribe public void onUserCreatedEvent(UserCreatedEvent event) throws AmbariException { LOGGER.info("Preparing hook execution for event: {}", event); try { RequestStageContainer requestStageContainer = new RequestStageContainer(actionManager.getNextRequestId(), null, requestFactory, actionManager); ClusterData clsData = getClusterData(); PostUserCreationHookContext ctx = (PostUserCreationHookContext) event.getContext(); String stageContextText = String.format(POST_USER_CREATION_REQUEST_CONTEXT, ctx.getUserGroups().size()); Stage stage = stageFactory.createNew(requestStageContainer.getId(), configuration.getServerTempDir() + File.pathSeparatorChar + requestStageContainer.getId(), clsData.getClusterName(), clsData.getClusterId(), stageContextText, "{}", "{}", "{}"); stage.setStageId(requestStageContainer.getLastStageId()); ServiceComponentHostServerActionEvent serverActionEvent = new ServiceComponentHostServerActionEvent("ambari-server-host", System.currentTimeMillis()); Map<String, String> commandParams = prepareCommandParams(ctx, clsData); stage.addServerActionCommand(PostUserCreationHookServerAction.class.getName(), "ambari", Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, clsData.getClusterName(), serverActionEvent, commandParams, stageContextText, null, null, false, false); requestStageContainer.addStages(Collections.singletonList(stage)); requestStageContainer.persist(); } catch (IOException e) { LOGGER.error("Failed to assemble stage for server action. Event: {}", event); throw new AmbariException("Failed to assemble stage for server action", e); } } }
UserHookService implements HookService { @Subscribe public void onUserCreatedEvent(UserCreatedEvent event) throws AmbariException { LOGGER.info("Preparing hook execution for event: {}", event); try { RequestStageContainer requestStageContainer = new RequestStageContainer(actionManager.getNextRequestId(), null, requestFactory, actionManager); ClusterData clsData = getClusterData(); PostUserCreationHookContext ctx = (PostUserCreationHookContext) event.getContext(); String stageContextText = String.format(POST_USER_CREATION_REQUEST_CONTEXT, ctx.getUserGroups().size()); Stage stage = stageFactory.createNew(requestStageContainer.getId(), configuration.getServerTempDir() + File.pathSeparatorChar + requestStageContainer.getId(), clsData.getClusterName(), clsData.getClusterId(), stageContextText, "{}", "{}", "{}"); stage.setStageId(requestStageContainer.getLastStageId()); ServiceComponentHostServerActionEvent serverActionEvent = new ServiceComponentHostServerActionEvent("ambari-server-host", System.currentTimeMillis()); Map<String, String> commandParams = prepareCommandParams(ctx, clsData); stage.addServerActionCommand(PostUserCreationHookServerAction.class.getName(), "ambari", Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, clsData.getClusterName(), serverActionEvent, commandParams, stageContextText, null, null, false, false); requestStageContainer.addStages(Collections.singletonList(stage)); requestStageContainer.persist(); } catch (IOException e) { LOGGER.error("Failed to assemble stage for server action. Event: {}", event); throw new AmbariException("Failed to assemble stage for server action", e); } }  }
UserHookService implements HookService { @Subscribe public void onUserCreatedEvent(UserCreatedEvent event) throws AmbariException { LOGGER.info("Preparing hook execution for event: {}", event); try { RequestStageContainer requestStageContainer = new RequestStageContainer(actionManager.getNextRequestId(), null, requestFactory, actionManager); ClusterData clsData = getClusterData(); PostUserCreationHookContext ctx = (PostUserCreationHookContext) event.getContext(); String stageContextText = String.format(POST_USER_CREATION_REQUEST_CONTEXT, ctx.getUserGroups().size()); Stage stage = stageFactory.createNew(requestStageContainer.getId(), configuration.getServerTempDir() + File.pathSeparatorChar + requestStageContainer.getId(), clsData.getClusterName(), clsData.getClusterId(), stageContextText, "{}", "{}", "{}"); stage.setStageId(requestStageContainer.getLastStageId()); ServiceComponentHostServerActionEvent serverActionEvent = new ServiceComponentHostServerActionEvent("ambari-server-host", System.currentTimeMillis()); Map<String, String> commandParams = prepareCommandParams(ctx, clsData); stage.addServerActionCommand(PostUserCreationHookServerAction.class.getName(), "ambari", Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, clsData.getClusterName(), serverActionEvent, commandParams, stageContextText, null, null, false, false); requestStageContainer.addStages(Collections.singletonList(stage)); requestStageContainer.persist(); } catch (IOException e) { LOGGER.error("Failed to assemble stage for server action. Event: {}", event); throw new AmbariException("Failed to assemble stage for server action", e); } }  @Override boolean execute(HookContext hookContext); @Subscribe void onUserCreatedEvent(UserCreatedEvent event); }
UserHookService implements HookService { @Subscribe public void onUserCreatedEvent(UserCreatedEvent event) throws AmbariException { LOGGER.info("Preparing hook execution for event: {}", event); try { RequestStageContainer requestStageContainer = new RequestStageContainer(actionManager.getNextRequestId(), null, requestFactory, actionManager); ClusterData clsData = getClusterData(); PostUserCreationHookContext ctx = (PostUserCreationHookContext) event.getContext(); String stageContextText = String.format(POST_USER_CREATION_REQUEST_CONTEXT, ctx.getUserGroups().size()); Stage stage = stageFactory.createNew(requestStageContainer.getId(), configuration.getServerTempDir() + File.pathSeparatorChar + requestStageContainer.getId(), clsData.getClusterName(), clsData.getClusterId(), stageContextText, "{}", "{}", "{}"); stage.setStageId(requestStageContainer.getLastStageId()); ServiceComponentHostServerActionEvent serverActionEvent = new ServiceComponentHostServerActionEvent("ambari-server-host", System.currentTimeMillis()); Map<String, String> commandParams = prepareCommandParams(ctx, clsData); stage.addServerActionCommand(PostUserCreationHookServerAction.class.getName(), "ambari", Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, clsData.getClusterName(), serverActionEvent, commandParams, stageContextText, null, null, false, false); requestStageContainer.addStages(Collections.singletonList(stage)); requestStageContainer.persist(); } catch (IOException e) { LOGGER.error("Failed to assemble stage for server action. Event: {}", event); throw new AmbariException("Failed to assemble stage for server action", e); } }  @Override boolean execute(HookContext hookContext); @Subscribe void onUserCreatedEvent(UserCreatedEvent event);  }