public List<ContainerUnit> listContainers(String applicationName, boolean withModules) throws ServiceException { List<ContainerUnit> containers = new ArrayList<>(); try { Application application = findByNameAndUser(authentificationUtils.getAuthentificatedUser(), applicationName); if (application != null) { try { // Serveurs List<Server> servers = application.getServers(); // Ajout des containers de type server for (Server server : servers) { DockerContainer dockerContainer = new DockerContainer(); dockerContainer.setName(server.getName()); dockerContainer = DockerContainer.findOne(dockerContainer, application.getManagerIp()); server = containerMapper.mapDockerContainerToServer(dockerContainer, server); ContainerUnit containerUnit = new ContainerUnit(server.getName(), server.getContainerID(), "server"); containers.add(containerUnit); } if (withModules) { // Ajout des containers de type module List<Module> modules = application.getModules(); for (Module module : modules) { // on evite de remonter les modules de type toolkit // (git, maven...) if (module.isTool()) { continue; } DockerContainer dockerContainer = new DockerContainer(); dockerContainer.setName(module.getName()); dockerContainer = DockerContainer.findOne(dockerContainer, application.getManagerIp()); module = containerMapper.mapDockerContainerToModule(dockerContainer, module); ContainerUnit containerUnit = new ContainerUnit(module.getName(), module.getContainerID(), "module"); containers.add(containerUnit); } } } catch (Exception ex) { // Si une application sort en erreur, il ne faut pas // arrĂȘter la suite des traitements logger.error(application.toString(), ex); } } } catch (Exception e) { throw new ServiceException(e.getLocalizedMessage(), e); } return containers; }
@Override @Transactional(rollbackFor = ServiceException.class) public Application create(String applicationName, String login, String serverName, String tagName) throws ServiceException, CheckException { // if tagname is null, we prefix with a ":" if (tagName != null) { tagName = ":" + tagName; } if (applicationName != null) { applicationName = applicationName.toLowerCase(); } logger.info("--CALL CREATE NEW APP--"); Application application = new Application(); logger.info("applicationName = " + applicationName + ", serverName = " + serverName); User user = authentificationUtils.getAuthentificatedUser(); // For cloning management if (tagName != null) { application.setAClone(true); } application.setName(applicationName); application.setUser(user); application.setModules(new ArrayList<>()); // verify if application exists already this.checkCreate(application, serverName); // todo : use a session flag application.setStatus(Status.PENDING); application = this.saveInDB(application); serverService.checkMaxNumberReach(application); String subdomain = System.getenv("CU_SUB_DOMAIN") == null ? "" : System.getenv("CU_SUB_DOMAIN"); List<Image> imagesEnabled = imageService.findEnabledImages(); List<String> imageNames = new ArrayList<>(); for (Image image : imagesEnabled) { imageNames.add(image.getName()); } if (!imageNames.contains(serverName)) { throw new CheckException(messageSource.getMessage("server.not.found", null, locale)); } try { // BLOC APPLICATION application.setDomainName(subdomain + suffixCloudUnitIO); application = applicationDAO.save(application); application.setManagerIp(dockerManagerIp); application.setJvmRelease(javaVersionDefault); application.setRestHost(restHost); logger.info(application.getManagerIp()); // BLOC SERVER Server server = ServerFactory.getServer(serverName); // We get image associated to server Image image = imageService.findByName(serverName); server.setImage(image); server.setApplication(application); server.setName(serverName); server = serverService.create(server, tagName); List<Server> servers = new ArrayList<>(); servers.add(server); application.setServers(servers); // BLOC MODULE Module moduleGit = this.addGitContainer(application, tagName); application.getModules().add(moduleGit); application.setGitContainerIP(moduleGit.getContainerIP()); // Persistence for Application model application = applicationDAO.save(application); // Copy the ssh key from the server to git container to be able to deploy war with gitpush // During clone processus, env variables are not updated. We must wait for a restart before // to copy the ssh keys for git push if (tagName == null) { this.sshCopyIDToServer(application, user); } } catch (DataAccessException e) { throw new ServiceException(e.getLocalizedMessage(), e); } logger.info("" + application); logger.info( "ApplicationService : Application " + application.getName() + " successfully created."); return application; }