/**
   * Lancer par signal de NoPublicController quand le processus sshd est démarré dans les containers
   * serveur et git
   */
  public Application updateEnv(Application application, User user) throws ServiceException {

    logger.info("--update Env of Server--");
    String command = null;
    Map<String, String> configShellModule = new HashMap<>();
    Map<String, String> configShellServer = new HashMap<>();

    Module moduleGit = moduleService.findGitModule(user.getLogin(), application);
    Server server = application.getServers().get(0);

    String rootPassword = application.getUser().getPassword();
    configShellModule.put("port", moduleGit.getSshPort());
    configShellModule.put("dockerManagerAddress", moduleGit.getApplication().getManagerIp());
    configShellModule.put("password", rootPassword);
    configShellModule.put("dockerManagerAddress", application.getManagerIp());
    logger.info("new server ip : " + server.getContainerIP());
    try {
      int counter = 0;
      while (!server.getStatus().equals(Status.START)
          || !moduleGit.getStatus().equals(Status.START)) {
        if (counter == 100) {
          break;
        }
        Thread.sleep(1000);
        logger.info(" wait git and server sshd processus start");
        logger.info(
            "SSHDSTATUS = server : " + server.getStatus() + " - module : " + moduleGit.getStatus());
        moduleGit = moduleService.findById(moduleGit.getId());
        server = serverService.findById(server.getId());
        counter++;
      }
      command = ". /cloudunit/scripts/update-env.sh " + server.getContainerIP();
      logger.info("command shell to execute [" + command + "]");

      shellUtils.executeShell(command, configShellModule);

      configShellServer.put("port", server.getSshPort());
      configShellServer.put("dockerManagerAddress", server.getApplication().getManagerIp());
      configShellServer.put("password", rootPassword);
      command = ". /cloudunit/scripts/rm-auth-keys.sh ";
      logger.info("command shell to execute [" + command + "]");

      shellUtils.executeShell(command, configShellServer);
      String cleanCommand = server.getServerAction().cleanCommand();
      if (cleanCommand != null) {
        shellUtils.executeShell(server.getServerAction().cleanCommand(), configShellServer);
      }
    } catch (Exception e) {
      moduleGit.setStatus(Status.FAIL);
      moduleGit = moduleService.saveInDB(moduleGit);
      server.setStatus(Status.FAIL);
      server = serverService.saveInDB(server);
      logger.error("Error :  Error during update Env var of GIT " + e);
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
    return application;
  }
  @Override
  @Transactional
  public Application deploy(File file, Application application)
      throws ServiceException, CheckException {

    int code = -1;
    Map<String, String> configShell = new HashMap<>();

    try {
      // get app with all its components

      for (Server server : application.getServers()) {

        // loading server ssh informations

        String rootPassword = server.getApplication().getUser().getPassword();
        configShell.put("port", server.getSshPort());
        configShell.put("dockerManagerAddress", application.getManagerIp());
        configShell.put("password", rootPassword);
        String destFile = "/cloudunit/tmp/";

        // send the file on container

        shellUtils.sendFile(
            file, rootPassword, server.getSshPort(), application.getManagerIp(), destFile);

        // call deployment script

        code =
            shellUtils.executeShell(
                "bash /cloudunit/scripts/deploy.sh "
                    + file.getName()
                    + " "
                    + application.getUser().getLogin(),
                configShell);
      }

      // if all is ok, create a new deployment tag and set app to starting

      if (code == 0) {
        deploymentService.create(application, Type.WAR);
      } else {
        throw new CheckException("No way to deploy application " + file + ", " + application);
      }

    } catch (Exception e) {
      throw new ServiceException(e.getLocalizedMessage(), e);
    }

    return application;
  }
  /**
   * Test if the user can create new applications because we limit the number per user
   *
   * @param application
   * @param serverName
   * @throws CheckException
   * @throws ServiceException
   */
  @Override
  public void checkCreate(Application application, String serverName)
      throws CheckException, ServiceException {

    logger.debug("--CHECK APP COUNT--");

    if (this.countApp(application.getUser()) >= Integer.parseInt(numberMaxApplications)) {
      throw new ServiceException(
          "You have already created your " + numberMaxApplications + " apps into the Cloud");
    }

    try {
      if (checkAppExist(application.getUser(), application.getName())) {
        throw new CheckException(messageSource.getMessage("app.exists", null, locale));
      }
      if (imageService.findByName(serverName) == null)
        throw new CheckException(messageSource.getMessage("image.not.found", null, locale));
      imageService.findByName(serverName);

    } catch (PersistenceException e) {
      logger.error("ApplicationService Error : Create Application" + e);
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
  }
  /**
   * Add git container to application associated to server
   *
   * @param application
   * @return
   * @throws ServiceException
   * @throws CheckException
   */
  private Module addGitContainer(Application application, String tagName)
      throws ServiceException, CheckException {

    Module moduleGit = ModuleFactory.getModule("git");
    // todo : externaliser la variable
    String containerGitAddress = "/cloudunit/git/.git";

    try {
      // Assign fixed host ports for forwarding git ports (22)
      Map<String, String> mapProxyPorts = portUtils.assignProxyPorts(application);
      String freeProxySshPortNumber = mapProxyPorts.get("freeProxySshPortNumber");

      // Creation of git container fo application
      moduleGit.setName("git");
      moduleGit.setImage(imageService.findByName("git"));
      moduleGit.setApplication(application);

      moduleGit.setSshPort(freeProxySshPortNumber);
      moduleGit = moduleService.initModule(application, moduleGit, tagName);

      application.getModules().add(moduleGit);
      application.setGitContainerIP(moduleGit.getContainerIP());

      application.setGitSshProxyPort(freeProxySshPortNumber);

      // Update GIT respository informations in the current application
      application.setGitAddress(
          "ssh://"
              + AlphaNumericsCharactersCheckUtils.convertToAlphaNumerics(
                  application.getUser().getLogin())
              + "@"
              + application.getName()
              + "."
              + application.getSuffixCloudUnitIO().substring(1)
              + ":"
              + application.getGitSshProxyPort()
              + containerGitAddress);

      moduleGit.setStatus(Status.START);
      moduleGit = moduleService.update(moduleGit);

    } catch (UnsupportedEncodingException e) {
      moduleGit.setStatus(Status.FAIL);
      logger.error("Error :  Error during persist git module " + e);
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
    return moduleGit;
  }
 /**
  * Method useful for Logs and Monitoring Management
  *
  * @return
  * @throws ServiceException
  */
 @Override
 public List<Application> findAll() throws ServiceException {
   try {
     logger.debug("start findAll");
     List<Application> listApplications = applicationDAO.findAll();
     for (Application application : listApplications) {
       application.setServers(serverService.findByApp(application));
       application.setModules(
           moduleService.findByAppAndUser(application.getUser(), application.getName()));
     }
     logger.debug("ApplicationService : All Applications found ");
     return listApplications;
   } catch (PersistenceException e) {
     logger.error("Error ApplicationService : error findAll Method : " + e);
     throw new ServiceException(e.getLocalizedMessage(), e);
   }
 }
  @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();
    }
  }