@Test public void testGenerateAndDeployWebServer() throws CommandFailureException, IOException { List<String> webServerResourceNames = new ArrayList<>(); webServerResourceNames.add("httpd.conf"); CommandOutput retSuccessExecData = new CommandOutput(new ExecReturnCode(0), "", ""); when(Config.mockWebServerControlService.controlWebServer(any(ControlWebServerRequest.class), any(User.class))).thenReturn(retSuccessExecData); when(Config.mockWebServerService.getWebServer(anyString())).thenReturn(webServer); when(Config.mockWebServerService.generateInstallServiceScript(any(WebServer.class))).thenReturn("invoke me"); when(Config.mockWebServerService.getResourceTemplateMetaData(anyString(), anyString())).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}"); when(Config.mockWebServerService.getResourceTemplateNames(anyString())).thenReturn(webServerResourceNames); when(Config.mockResourceService.generateResourceGroup()).thenReturn(new ResourceGroup()); when(Config.mockResourceService.generateResourceFile(anyString(), anyString(), any(ResourceGroup.class), any(), any(ResourceGeneratorType.class))).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}"); ResourceTemplateMetaData mockMetaData = mock(ResourceTemplateMetaData.class); when(mockMetaData.getDeployFileName()).thenReturn("httpd.conf"); when(mockMetaData.getDeployPath()).thenReturn("./anyPath"); when(mockMetaData.getContentType()).thenReturn(MediaType.TEXT_PLAIN); when(Config.mockResourceService.getTokenizedMetaData(anyString(), Matchers.anyObject(), anyString())).thenReturn(mockMetaData); Response response = null; response = webServerServiceRest.generateAndDeployWebServer(webServer.getName(), Config.mockAuthenticatedUser); assertNotNull(response); response = null; response = webServerServiceRest.generateAndDeployWebServer(webServer.getName(), Config.mockAuthenticatedUser); assertNotNull(response); }
@Override public Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser) { LOGGER.info("Generate and deploy web server {} by user {}", aWebServerName, aUser.getUser().getId()); boolean didSucceed = false; binaryDistributionLockManager.writeLock(aWebServerName); WebServer webServer = webServerService.getWebServer(aWebServerName); try { historyFacadeService.write(aWebServerName, webServer.getGroups(), "Starting to generate remote web server " + aWebServerName, EventType.USER_ACTION_INFO, aUser.getUser().getId()); verifyStopped(aWebServerName, webServer); validateResources(webServer); binaryDistributionService.distributeUnzip(webServer.getHost()); binaryDistributionService.distributeMedia(webServer.getName(), webServer.getHost(), webServer.getGroups() .toArray(new Group[webServer.getGroups().size()]), webServer.getApacheHttpdMedia()); validateHttpdConf(webServer); createScriptsDirectory(webServer); deployStartStopScripts(webServer, aUser.getUser().getId()); deleteWebServerWindowsService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.DELETE_SERVICE), aWebServerName); final List<String> templateNames = webServerService.getResourceTemplateNames(aWebServerName); for (final String templateName : templateNames) { generateAndDeployConfig(aWebServerName, templateName, aUser); } installWebServerService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE), webServer); webServerService.updateState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); simpleMessagingService.send(new WebServerState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, DateTime.now())); didSucceed = true; } catch (CommandFailureException e) { LOGGER.error("Failed for {}", aWebServerName, e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.REMOTE_COMMAND_FAILURE, e.getMessage(), e)); } finally { binaryDistributionLockManager.writeUnlock(aWebServerName); final String historyMessage = didSucceed ? "Remote generation of web server " + aWebServerName + " succeeded" : "Remote generation of web server " + aWebServerName + " failed"; historyFacadeService.write(aWebServerName, new ArrayList<>(webServer.getGroups()), historyMessage, EventType.USER_ACTION_INFO, aUser.getUser().getId()); } return ResponseBuilder.ok(webServerService.getWebServer(aWebServerName)); }
WebServerServiceRestImpl implements WebServerServiceRest { @Override public Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser) { LOGGER.info("Generate and deploy web server {} by user {}", aWebServerName, aUser.getUser().getId()); boolean didSucceed = false; binaryDistributionLockManager.writeLock(aWebServerName); WebServer webServer = webServerService.getWebServer(aWebServerName); try { historyFacadeService.write(aWebServerName, webServer.getGroups(), "Starting to generate remote web server " + aWebServerName, EventType.USER_ACTION_INFO, aUser.getUser().getId()); verifyStopped(aWebServerName, webServer); validateResources(webServer); binaryDistributionService.distributeUnzip(webServer.getHost()); binaryDistributionService.distributeMedia(webServer.getName(), webServer.getHost(), webServer.getGroups() .toArray(new Group[webServer.getGroups().size()]), webServer.getApacheHttpdMedia()); validateHttpdConf(webServer); createScriptsDirectory(webServer); deployStartStopScripts(webServer, aUser.getUser().getId()); deleteWebServerWindowsService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.DELETE_SERVICE), aWebServerName); final List<String> templateNames = webServerService.getResourceTemplateNames(aWebServerName); for (final String templateName : templateNames) { generateAndDeployConfig(aWebServerName, templateName, aUser); } installWebServerService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE), webServer); webServerService.updateState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); simpleMessagingService.send(new WebServerState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, DateTime.now())); didSucceed = true; } catch (CommandFailureException e) { LOGGER.error("Failed for {}", aWebServerName, e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.REMOTE_COMMAND_FAILURE, e.getMessage(), e)); } finally { binaryDistributionLockManager.writeUnlock(aWebServerName); final String historyMessage = didSucceed ? "Remote generation of web server " + aWebServerName + " succeeded" : "Remote generation of web server " + aWebServerName + " failed"; historyFacadeService.write(aWebServerName, new ArrayList<>(webServer.getGroups()), historyMessage, EventType.USER_ACTION_INFO, aUser.getUser().getId()); } return ResponseBuilder.ok(webServerService.getWebServer(aWebServerName)); } }
WebServerServiceRestImpl implements WebServerServiceRest { @Override public Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser) { LOGGER.info("Generate and deploy web server {} by user {}", aWebServerName, aUser.getUser().getId()); boolean didSucceed = false; binaryDistributionLockManager.writeLock(aWebServerName); WebServer webServer = webServerService.getWebServer(aWebServerName); try { historyFacadeService.write(aWebServerName, webServer.getGroups(), "Starting to generate remote web server " + aWebServerName, EventType.USER_ACTION_INFO, aUser.getUser().getId()); verifyStopped(aWebServerName, webServer); validateResources(webServer); binaryDistributionService.distributeUnzip(webServer.getHost()); binaryDistributionService.distributeMedia(webServer.getName(), webServer.getHost(), webServer.getGroups() .toArray(new Group[webServer.getGroups().size()]), webServer.getApacheHttpdMedia()); validateHttpdConf(webServer); createScriptsDirectory(webServer); deployStartStopScripts(webServer, aUser.getUser().getId()); deleteWebServerWindowsService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.DELETE_SERVICE), aWebServerName); final List<String> templateNames = webServerService.getResourceTemplateNames(aWebServerName); for (final String templateName : templateNames) { generateAndDeployConfig(aWebServerName, templateName, aUser); } installWebServerService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE), webServer); webServerService.updateState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); simpleMessagingService.send(new WebServerState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, DateTime.now())); didSucceed = true; } catch (CommandFailureException e) { LOGGER.error("Failed for {}", aWebServerName, e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.REMOTE_COMMAND_FAILURE, e.getMessage(), e)); } finally { binaryDistributionLockManager.writeUnlock(aWebServerName); final String historyMessage = didSucceed ? "Remote generation of web server " + aWebServerName + " succeeded" : "Remote generation of web server " + aWebServerName + " failed"; historyFacadeService.write(aWebServerName, new ArrayList<>(webServer.getGroups()), historyMessage, EventType.USER_ACTION_INFO, aUser.getUser().getId()); } return ResponseBuilder.ok(webServerService.getWebServer(aWebServerName)); } WebServerServiceRestImpl(final WebServerService theWebServerService,
                                    final WebServerControlService theWebServerControlService,
                                    final WebServerCommandService theWebServerCommandService,
                                    final ResourceService theResourceService, GroupService groupService,
                                    final BinaryDistributionService binaryDistributionService,
                                    final HistoryFacadeService historyFacadeService
                                    ); }
WebServerServiceRestImpl implements WebServerServiceRest { @Override public Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser) { LOGGER.info("Generate and deploy web server {} by user {}", aWebServerName, aUser.getUser().getId()); boolean didSucceed = false; binaryDistributionLockManager.writeLock(aWebServerName); WebServer webServer = webServerService.getWebServer(aWebServerName); try { historyFacadeService.write(aWebServerName, webServer.getGroups(), "Starting to generate remote web server " + aWebServerName, EventType.USER_ACTION_INFO, aUser.getUser().getId()); verifyStopped(aWebServerName, webServer); validateResources(webServer); binaryDistributionService.distributeUnzip(webServer.getHost()); binaryDistributionService.distributeMedia(webServer.getName(), webServer.getHost(), webServer.getGroups() .toArray(new Group[webServer.getGroups().size()]), webServer.getApacheHttpdMedia()); validateHttpdConf(webServer); createScriptsDirectory(webServer); deployStartStopScripts(webServer, aUser.getUser().getId()); deleteWebServerWindowsService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.DELETE_SERVICE), aWebServerName); final List<String> templateNames = webServerService.getResourceTemplateNames(aWebServerName); for (final String templateName : templateNames) { generateAndDeployConfig(aWebServerName, templateName, aUser); } installWebServerService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE), webServer); webServerService.updateState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); simpleMessagingService.send(new WebServerState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, DateTime.now())); didSucceed = true; } catch (CommandFailureException e) { LOGGER.error("Failed for {}", aWebServerName, e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.REMOTE_COMMAND_FAILURE, e.getMessage(), e)); } finally { binaryDistributionLockManager.writeUnlock(aWebServerName); final String historyMessage = didSucceed ? "Remote generation of web server " + aWebServerName + " succeeded" : "Remote generation of web server " + aWebServerName + " failed"; historyFacadeService.write(aWebServerName, new ArrayList<>(webServer.getGroups()), historyMessage, EventType.USER_ACTION_INFO, aUser.getUser().getId()); } return ResponseBuilder.ok(webServerService.getWebServer(aWebServerName)); } WebServerServiceRestImpl(final WebServerService theWebServerService,
                                    final WebServerControlService theWebServerControlService,
                                    final WebServerCommandService theWebServerCommandService,
                                    final ResourceService theResourceService, GroupService groupService,
                                    final BinaryDistributionService binaryDistributionService,
                                    final HistoryFacadeService historyFacadeService
                                    ); @Override Response getWebServers(final Identifier<Group> aGroupId); @Override Response getWebServer(final Identifier<WebServer> aWsId); @Override Response createWebServer(final JsonCreateWebServer aWebServerToCreate, final AuthenticatedUser aUser); @Override Response updateWebServer(final JsonUpdateWebServer aWebServerToCreate, final AuthenticatedUser aUser); @Override Response deleteWebServer(final Identifier<WebServer> id, boolean hardDelete, final AuthenticatedUser authUser); @Override Response controlWebServer(final Identifier<WebServer> aWebServerId,
                                     final JsonControlWebServer aWebServerToControl, final AuthenticatedUser aUser,
                                     final Boolean wait, Long waitTimeout); @Override Response generateConfig(final String webServerName); @Override Response generateAndDeployConfig(final String aWebServerName, final String resourceFileName, final AuthenticatedUser user); @Override Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser); @Override Response getHttpdConfig(Identifier<WebServer> aWebServerId); @Override Response getResourceNames(final String wsName); @Override Response getResourceTemplate(final String wsName, final String resourceTemplateName,
                                        final boolean tokensReplaced); @Override Response updateResourceTemplate(final String wsName, final String resourceTemplateName, final String content); @Override Response previewResourceTemplate(final String webServerName, final String fileName, final String groupName, String template); }
WebServerServiceRestImpl implements WebServerServiceRest { @Override public Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser) { LOGGER.info("Generate and deploy web server {} by user {}", aWebServerName, aUser.getUser().getId()); boolean didSucceed = false; binaryDistributionLockManager.writeLock(aWebServerName); WebServer webServer = webServerService.getWebServer(aWebServerName); try { historyFacadeService.write(aWebServerName, webServer.getGroups(), "Starting to generate remote web server " + aWebServerName, EventType.USER_ACTION_INFO, aUser.getUser().getId()); verifyStopped(aWebServerName, webServer); validateResources(webServer); binaryDistributionService.distributeUnzip(webServer.getHost()); binaryDistributionService.distributeMedia(webServer.getName(), webServer.getHost(), webServer.getGroups() .toArray(new Group[webServer.getGroups().size()]), webServer.getApacheHttpdMedia()); validateHttpdConf(webServer); createScriptsDirectory(webServer); deployStartStopScripts(webServer, aUser.getUser().getId()); deleteWebServerWindowsService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.DELETE_SERVICE), aWebServerName); final List<String> templateNames = webServerService.getResourceTemplateNames(aWebServerName); for (final String templateName : templateNames) { generateAndDeployConfig(aWebServerName, templateName, aUser); } installWebServerService(aUser, new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE), webServer); webServerService.updateState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, StringUtils.EMPTY); simpleMessagingService.send(new WebServerState(webServer.getId(), WebServerReachableState.WS_UNREACHABLE, DateTime.now())); didSucceed = true; } catch (CommandFailureException e) { LOGGER.error("Failed for {}", aWebServerName, e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException( FaultType.REMOTE_COMMAND_FAILURE, e.getMessage(), e)); } finally { binaryDistributionLockManager.writeUnlock(aWebServerName); final String historyMessage = didSucceed ? "Remote generation of web server " + aWebServerName + " succeeded" : "Remote generation of web server " + aWebServerName + " failed"; historyFacadeService.write(aWebServerName, new ArrayList<>(webServer.getGroups()), historyMessage, EventType.USER_ACTION_INFO, aUser.getUser().getId()); } return ResponseBuilder.ok(webServerService.getWebServer(aWebServerName)); } WebServerServiceRestImpl(final WebServerService theWebServerService,
                                    final WebServerControlService theWebServerControlService,
                                    final WebServerCommandService theWebServerCommandService,
                                    final ResourceService theResourceService, GroupService groupService,
                                    final BinaryDistributionService binaryDistributionService,
                                    final HistoryFacadeService historyFacadeService
                                    ); @Override Response getWebServers(final Identifier<Group> aGroupId); @Override Response getWebServer(final Identifier<WebServer> aWsId); @Override Response createWebServer(final JsonCreateWebServer aWebServerToCreate, final AuthenticatedUser aUser); @Override Response updateWebServer(final JsonUpdateWebServer aWebServerToCreate, final AuthenticatedUser aUser); @Override Response deleteWebServer(final Identifier<WebServer> id, boolean hardDelete, final AuthenticatedUser authUser); @Override Response controlWebServer(final Identifier<WebServer> aWebServerId,
                                     final JsonControlWebServer aWebServerToControl, final AuthenticatedUser aUser,
                                     final Boolean wait, Long waitTimeout); @Override Response generateConfig(final String webServerName); @Override Response generateAndDeployConfig(final String aWebServerName, final String resourceFileName, final AuthenticatedUser user); @Override Response generateAndDeployWebServer(final String aWebServerName, final AuthenticatedUser aUser); @Override Response getHttpdConfig(Identifier<WebServer> aWebServerId); @Override Response getResourceNames(final String wsName); @Override Response getResourceTemplate(final String wsName, final String resourceTemplateName,
                                        final boolean tokensReplaced); @Override Response updateResourceTemplate(final String wsName, final String resourceTemplateName, final String content); @Override Response previewResourceTemplate(final String webServerName, final String fileName, final String groupName, String template);  }