<commit_before>/*
 * LICENCE : CloudUnit is available under the GNU Affero General Public License : https://gnu.org/licenses/agpl.html
 * but CloudUnit is licensed too under a standard commercial license.
 * Please contact our sales team if you would like to discuss the specifics of our Enterprise license.
 * If you are not sure whether the AGPL is right for you,
 * you can always test our software under the AGPL and inspect the source code before you contact us
 * about purchasing a commercial license.
 *
 * LEGAL TERMS : "CloudUnit" is a registered trademark of Treeptik and can't be used to endorse
 * or promote products derived from this project without prior written permission from Treeptik.
 * Products or services derived from this software may not be called "CloudUnit"
 * nor may "Treeptik" or similar confusing terms appear in their names without prior written permission.
 * For any questions, contact us : <EMAIL>
 */


package fr.treeptik.cloudunit.service.impl;

import com.spotify.docker.client.*;
import com.spotify.docker.client.messages.Container;
import fr.treeptik.cloudunit.dto.FileUnit;
import fr.treeptik.cloudunit.dto.SourceUnit;
import fr.treeptik.cloudunit.exception.CheckException;
import fr.treeptik.cloudunit.exception.ServiceException;
import fr.treeptik.cloudunit.filters.explorer.ExplorerFactory;
import fr.treeptik.cloudunit.filters.explorer.ExplorerFilter;
import fr.treeptik.cloudunit.model.Application;
import fr.treeptik.cloudunit.model.Module;
import fr.treeptik.cloudunit.model.Server;
import fr.treeptik.cloudunit.service.ApplicationService;
import fr.treeptik.cloudunit.service.FileService;
import fr.treeptik.cloudunit.service.ModuleService;
import fr.treeptik.cloudunit.service.ServerService;
import fr.treeptik.cloudunit.utils.AuthentificationUtils;
import fr.treeptik.cloudunit.utils.FilesUtils;
import fr.treeptik.cloudunit.utils.ShellUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

/**
 * Service for file management into container
 * Created by nicolas on 20/05/15.
 */
@Service
public class FileServiceImpl
        implements FileService {

    private Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Inject
    private AuthentificationUtils authentificationUtils;

    @Inject
    private ApplicationService applicationService;

    @Inject
    private ShellUtils shellUtils;

    @Inject
    private ModuleService moduleService;

    @Inject
    private ServerService serverService;

    @Value("${docker.manager.ip:192.168.50.4:4243}")
    private String dockerManagerIp;

    /**
     * Delete all resources (files and folders) for an application + container +
     * path.
     *
     * @param applicationName
     * @param containerId
     * @param path
     * @throws ServiceException
     */
    public void deleteFilesFromContainer(String applicationName,
                                         String containerId, String path)
            throws ServiceException {
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/home/guillaume"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }
                final String[] command = {"bash", "-c", "rm -rf " + path};
                String containerName = container.names().get(0);
                String execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);
                final LogStream output = docker.execStart(execId);
                if (output != null) {
                    output.close();
                }
            }
        } catch (DockerException | InterruptedException | DockerCertificateException e) {
            throw new ServiceException("Error in listByContainerIdAndPath", e);
        }
    }

    /**
     * Liste les fichiers de logs du répertoire de logs
     *
     * @param containerId
     * @return
     * @throws ServiceException
     */
    public List<SourceUnit> listLogsFilesByContainer(String containerId)
            throws ServiceException {

        List<SourceUnit> files = new ArrayList<>();
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/home/guillaume"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }
                String logDirectory = getLogDirectory(containerId);
                // Exec command inside running container with attached STDOUT
                // and STDERR
                final String[] command = {"bash", "-c",
                        "find " + logDirectory + " -type f ! -size 0 "};
                String execId;

                String containerName = container.names().get(0);
                execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);
                ExplorerFilter filter = ExplorerFactory.getInstance()
                        .getCustomFilter(containerName);
                final LogStream output = docker.execStart(execId);
                final String execOutput = output.readFully();
                if (execOutput != null
                        && execOutput.contains("cannot access") == false) {

                    if (logger.isDebugEnabled()) {
                        logger.debug(execOutput);
                    }

                    StringTokenizer lignes = new StringTokenizer(execOutput,
                            "\n");

                    while (lignes.hasMoreTokens()) {
                        String name = lignes.nextToken();
                        name = name.substring(name.lastIndexOf("/") + 1);
                        SourceUnit sourceUnit = new SourceUnit(name);
                        files.add(sourceUnit);
                    }
                    output.close();
                }
            }
        } catch (DockerException | InterruptedException | DockerCertificateException e) {
            throw new ServiceException("Error in listByContainerIdAndPath", e);
        }

        return files;
    }

    /**
     * List the files by Container and Path
     *
     * @param containerId
     * @param path
     * @return
     * @throws ServiceException
     */
    public List<FileUnit> listByContainerIdAndPath(String containerId,
                                                   String path)
            throws ServiceException {

        List<FileUnit> files = new ArrayList<>();
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/home/guillaume"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                // b10da42def6c1737c40b981b1692acb24e28414944b4017814713afb811ae51b
                // log.debug(container.id() + " : " + container.names());
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }

                // Exec command inside running container with attached STDOUT
                // and STDERR
                final String[] command = {"bash", "-c", "ls -laF " + path};
                String execId;

                String containerName = container.names().get(0);
                execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);<commit_msg>FiX : Change Path for certificates<commit_after>/*
 * LICENCE : CloudUnit is available under the GNU Affero General Public License : https://gnu.org/licenses/agpl.html
 * but CloudUnit is licensed too under a standard commercial license.
 * Please contact our sales team if you would like to discuss the specifics of our Enterprise license.
 * If you are not sure whether the AGPL is right for you,
 * you can always test our software under the AGPL and inspect the source code before you contact us
 * about purchasing a commercial license.
 *
 * LEGAL TERMS : "CloudUnit" is a registered trademark of Treeptik and can't be used to endorse
 * or promote products derived from this project without prior written permission from Treeptik.
 * Products or services derived from this software may not be called "CloudUnit"
 * nor may "Treeptik" or similar confusing terms appear in their names without prior written permission.
 * For any questions, contact us : <EMAIL>
 */


package fr.treeptik.cloudunit.service.impl;

import com.spotify.docker.client.*;
import com.spotify.docker.client.messages.Container;
import fr.treeptik.cloudunit.dto.FileUnit;
import fr.treeptik.cloudunit.dto.SourceUnit;
import fr.treeptik.cloudunit.exception.CheckException;
import fr.treeptik.cloudunit.exception.ServiceException;
import fr.treeptik.cloudunit.filters.explorer.ExplorerFactory;
import fr.treeptik.cloudunit.filters.explorer.ExplorerFilter;
import fr.treeptik.cloudunit.model.Application;
import fr.treeptik.cloudunit.model.Module;
import fr.treeptik.cloudunit.model.Server;
import fr.treeptik.cloudunit.service.ApplicationService;
import fr.treeptik.cloudunit.service.FileService;
import fr.treeptik.cloudunit.service.ModuleService;
import fr.treeptik.cloudunit.service.ServerService;
import fr.treeptik.cloudunit.utils.AuthentificationUtils;
import fr.treeptik.cloudunit.utils.FilesUtils;
import fr.treeptik.cloudunit.utils.ShellUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

/**
 * Service for file management into container
 * Created by nicolas on 20/05/15.
 */
@Service
public class FileServiceImpl
        implements FileService {

    private Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Inject
    private AuthentificationUtils authentificationUtils;

    @Inject
    private ApplicationService applicationService;

    @Inject
    private ShellUtils shellUtils;

    @Inject
    private ModuleService moduleService;

    @Inject
    private ServerService serverService;

    @Value("${docker.manager.ip:192.168.50.4:4243}")
    private String dockerManagerIp;

    /**
     * Delete all resources (files and folders) for an application + container +
     * path.
     *
     * @param applicationName
     * @param containerId
     * @param path
     * @throws ServiceException
     */
    public void deleteFilesFromContainer(String applicationName,
                                         String containerId, String path)
            throws ServiceException {
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/usr/local/tomcat/certificats"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }
                final String[] command = {"bash", "-c", "rm -rf " + path};
                String containerName = container.names().get(0);
                String execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);
                final LogStream output = docker.execStart(execId);
                if (output != null) {
                    output.close();
                }
            }
        } catch (DockerException | InterruptedException | DockerCertificateException e) {
            throw new ServiceException("Error in listByContainerIdAndPath", e);
        }
    }

    /**
     * Liste les fichiers de logs du répertoire de logs
     *
     * @param containerId
     * @return
     * @throws ServiceException
     */
    public List<SourceUnit> listLogsFilesByContainer(String containerId)
            throws ServiceException {

        List<SourceUnit> files = new ArrayList<>();
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/usr/local/tomcat/certificats"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }
                String logDirectory = getLogDirectory(containerId);
                // Exec command inside running container with attached STDOUT
                // and STDERR
                final String[] command = {"bash", "-c",
                        "find " + logDirectory + " -type f ! -size 0 "};
                String execId;

                String containerName = container.names().get(0);
                execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);
                ExplorerFilter filter = ExplorerFactory.getInstance()
                        .getCustomFilter(containerName);
                final LogStream output = docker.execStart(execId);
                final String execOutput = output.readFully();
                if (execOutput != null
                        && execOutput.contains("cannot access") == false) {

                    if (logger.isDebugEnabled()) {
                        logger.debug(execOutput);
                    }

                    StringTokenizer lignes = new StringTokenizer(execOutput,
                            "\n");

                    while (lignes.hasMoreTokens()) {
                        String name = lignes.nextToken();
                        name = name.substring(name.lastIndexOf("/") + 1);
                        SourceUnit sourceUnit = new SourceUnit(name);
                        files.add(sourceUnit);
                    }
                    output.close();
                }
            }
        } catch (DockerException | InterruptedException | DockerCertificateException e) {
            throw new ServiceException("Error in listByContainerIdAndPath", e);
        }

        return files;
    }

    /**
     * List the files by Container and Path
     *
     * @param containerId
     * @param path
     * @return
     * @throws ServiceException
     */
    public List<FileUnit> listByContainerIdAndPath(String containerId,
                                                   String path)
            throws ServiceException {

        List<FileUnit> files = new ArrayList<>();
        try {
            final DockerCertificates certs = new DockerCertificates(Paths.get("/usr/local/tomcat/certificats"));
            final DockerClient docker = DefaultDockerClient
                    .builder()
                    .uri("https://" + dockerManagerIp).dockerCertificates(certs).build();
            List<Container> containers = docker.listContainers();
            for (Container container : containers) {
                // <KEY>
                // log.debug(container.id() + " : " + container.names());
                if (container.id().substring(0, 12).equals(containerId) == false) {
                    continue;
                }

                // Exec command inside running container with attached STDOUT
                // and STDERR
                final String[] command = {"bash", "-c", "ls -laF " + path};
                String execId;

                String containerName = container.names().get(0);
                execId = docker.execCreate(containerName, command,
                        DockerClient.ExecParameter.STDOUT,
                        DockerClient.ExecParameter.STDERR);
{'fraction_non_alphanumeric': 0.0888104605415413, 'fraction_numerical': 0.006537838463318677, 'mean_word_length': 2.129073135409124, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4826988', 'n_tokens_mistral': 4440, 'n_tokens_neox': 4130, 'n_words': 1089}