@Override
  @Transactional
  public Application start(Application application) throws ServiceException {
    try {
      User user = authentificationUtils.getAuthentificatedUser();
      logger.debug("start : Methods parameters : " + application);

      List<Module> modules = application.getModules();
      for (Module module : modules) {
        try {
          module = moduleService.startModule(module);
        } catch (ServiceException e) {
          logger.error("failed to start " + application.toString(), e);
        }
      }
      List<Server> servers = application.getServers();
      for (Server server : servers) {
        logger.info("old server ip : " + server.getContainerIP());
        server = serverService.startServer(server);
      }

      if (application.getAliases() != null && !application.getAliases().isEmpty()) {
        updateAliases(application);
      }
      logger.info("ApplicationService : Application successfully started ");
    } catch (PersistenceException e) {
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
    return application;
  }
  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;
  }
  /**
   * Methode qui teste la validité d'une application
   *
   * @param applicationName
   * @param serverName
   * @throws ServiceException
   * @throws CheckException
   */
  public void isValid(String applicationName, String serverName)
      throws ServiceException, CheckException {
    logger.info("--CALL APP IS VALID--");
    Application application = new Application();
    logger.info("applicationName = " + applicationName + ", serverName = " + serverName);

    User user = authentificationUtils.getAuthentificatedUser();
    if (user == null) {
      throw new CheckException("User is not authentificated");
    }

    application.setName(applicationName);
    application.setUser(user);
    application.setModules(new ArrayList<>());

    this.checkCreate(application, serverName);
  }
  @Transactional
  public void deployToContainerId(
      String applicationName, String containerId, File file, String destFile)
      throws ServiceException {

    try {
      Application application =
          this.findByNameAndUser(authentificationUtils.getAuthentificatedUser(), applicationName);

      Map<String, String> configShell = new HashMap<>();

      String sshPort = application.getSShPortByContainerId(containerId);
      String rootPassword = application.getUser().getPassword();
      configShell.put("port", sshPort);
      configShell.put("dockerManagerAddress", application.getManagerIp());
      configShell.put("password", rootPassword);

      // send the file on container
      shellUtils.sendFile(file, rootPassword, sshPort, application.getManagerIp(), destFile);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @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;
  }