Search in sources :

Example 1 with LogConfig

use of com.github.dockerjava.api.model.LogConfig in project elastest-torm by elastest.

the class DockerService2 method getLogConfig.

public LogConfig getLogConfig(String host, int port, String tagPrefix, String tagSuffix, DockerExecution dockerExec) {
    Map<String, String> configMap = new HashMap<String, String>();
    configMap.put("syslog-address", "tcp://" + host + ":" + port);
    configMap.put("tag", tagPrefix + dockerExec.getExecutionId() + "_" + tagSuffix + "_exec");
    LogConfig logConfig = new LogConfig();
    logConfig.setType(LoggingType.SYSLOG);
    logConfig.setConfig(configMap);
    return logConfig;
}
Also used : HashMap(java.util.HashMap) LogConfig(com.github.dockerjava.api.model.LogConfig)

Example 2 with LogConfig

use of com.github.dockerjava.api.model.LogConfig in project elastest-torm by elastest.

the class DockerService2 method createContainer.

public CreateContainerResponse createContainer(DockerExecution dockerExec, String type) throws TJobStoppedException {
    TJobExecution tJobExec = dockerExec.gettJobexec();
    TJob tJob = tJobExec.getTjob();
    SutSpecification sut = tJob.getSut();
    String image = "";
    String commands = null;
    List<Parameter> parametersList = new ArrayList<Parameter>();
    String prefix = "";
    String suffix = "";
    String containerName = "";
    int logPort = 5000;
    String sutHost = null;
    String sutPath = null;
    if ("sut".equals(type.toLowerCase())) {
        parametersList = sut.getParameters();
        commands = sut.getCommands();
        image = sut.getSpecification();
        prefix = "sut_";
        if (sut.isSutInNewContainer()) {
            suffix = sut.getSutInContainerAuxLabel();
        }
        containerName = getSutName(dockerExec);
        sutPath = filesService.buildFilesPath(tJobExec, ElastestConstants.SUT_FOLDER);
        filesService.createExecFilesFolder(sutPath);
    } else if ("tjob".equals(type.toLowerCase())) {
        parametersList = tJobExec.getParameters();
        commands = tJob.getCommands();
        image = tJob.getImageName();
        prefix = "test_";
        containerName = getTestName(dockerExec);
        if (dockerExec.isWithSut()) {
            sutHost = dockerExec.getSutExec().getIp();
        }
    }
    // Environment variables (optional)
    ArrayList<String> envList = new ArrayList<>();
    String envVar;
    // Get TJob Exec Env Vars
    for (Map.Entry<String, String> entry : dockerExec.gettJobexec().getEnvVars().entrySet()) {
        envVar = entry.getKey() + "=" + entry.getValue();
        envList.add(envVar);
    }
    // Get Parameters and insert into Env Vars
    for (Parameter parameter : parametersList) {
        envVar = parameter.getName() + "=" + parameter.getValue();
        envList.add(envVar);
    }
    if (sutHost != null) {
        envList.add("ET_SUT_HOST=" + dockerExec.getSutExec().getIp());
    }
    // Commands (optional)
    ArrayList<String> cmdList = new ArrayList<>();
    ArrayList<String> entrypointList = new ArrayList<>();
    if (commands != null && !commands.isEmpty()) {
        cmdList.add("-c");
        if (sut != null) {
            if (sut.isSutInNewContainer()) {
                commands = sutPath != null ? ("cd " + sutPath + ";" + commands) : commands;
            }
        } else {
            commands = "export ET_SUT_HOST=$(" + this.getThisContainerIpCmd + ") || echo;" + commands;
        }
        cmdList.add(commands);
        entrypointList.add("/bin/sh");
    }
    // Load Log Config
    LogConfig logConfig = null;
    if (tJob.isSelectedService("ems")) {
        try {
            logConfig = getEMSLogConfig(type, prefix, suffix, dockerExec);
        } catch (Exception e) {
            logger.error("", e);
        }
    } else {
        logConfig = getDefaultLogConfig(logPort, prefix, suffix, dockerExec);
    }
    // Pull Image
    this.pullETExecImage(image, type);
    // Create docker sock volume
    Volume dockerSockVolume = new Volume(dockerSock);
    CreateContainerCmd containerCmd = dockerExec.getDockerClient().createContainerCmd(image).withEnv(envList).withLogConfig(logConfig).withName(containerName).withCmd(cmdList).withEntrypoint(entrypointList).withNetworkMode(dockerExec.getNetwork());
    Volume sharedDataVolume = null;
    if (dockerExec.gettJobexec().getTjob().getSut() != null && dockerExec.gettJobexec().getTjob().getSut().isSutInNewContainer()) {
        sharedDataVolume = new Volume(sharedFolder);
    }
    if (sharedDataVolume != null) {
        containerCmd = containerCmd.withVolumes(dockerSockVolume, sharedDataVolume).withBinds(new Bind(dockerSock, dockerSockVolume), new Bind(sharedFolder, sharedDataVolume));
    } else {
        containerCmd = containerCmd.withVolumes(dockerSockVolume).withBinds(new Bind(dockerSock, dockerSockVolume));
    }
    // Create Container
    return containerCmd.exec();
}
Also used : Bind(com.github.dockerjava.api.model.Bind) TJobExecution(io.elastest.etm.model.TJobExecution) ArrayList(java.util.ArrayList) SutSpecification(io.elastest.etm.model.SutSpecification) TJob(io.elastest.etm.model.TJob) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) Volume(com.github.dockerjava.api.model.Volume) Parameter(io.elastest.etm.model.Parameter) Map(java.util.Map) HashMap(java.util.HashMap) LogConfig(com.github.dockerjava.api.model.LogConfig)

Example 3 with LogConfig

use of com.github.dockerjava.api.model.LogConfig in project elastest-torm by elastest.

the class DockerServiceItTest method readLogInRabbit.

@Test
public void readLogInRabbit() throws Exception {
    String imageId = "alpine";
    if (!existsImage(imageId)) {
        log.info("Pulling image '{}'", imageId);
        dockerClient.pullImageCmd(imageId).exec(new PullImageResultCallback()).awaitSuccess();
    }
    String queueId = "test.default_log.1.log";
    String tag = "test_1_exec";
    WaitForMessagesHandler handler = connectToRabbitQueue(queueId);
    LogConfig logConfig = getLogConfig(tag);
    log.info("Creating container");
    CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done").withTty(false).withLogConfig(logConfig).withNetworkMode("bridge").exec();
    String containerId = container.getId();
    try {
        log.info("Created container: {}", container.toString());
        long start = System.currentTimeMillis();
        dockerClient.startContainerCmd(containerId).exec();
        log.info("Waiting for logs messages in Rabbit");
        handler.waitForCompletion(5, TimeUnit.SECONDS);
        long duration = System.currentTimeMillis() - start;
        log.info("Log received in Rabbit in {} millis", duration);
    } catch (Exception ex) {
        log.info("Log NOT received in Rabbit");
        throw ex;
    } finally {
        log.info("Cleaning up resources");
        try {
            log.info("Removing container " + containerId);
            try {
                dockerClient.stopContainerCmd(containerId).exec();
            } catch (Exception ex) {
                log.warn("Error stopping container {}", containerId, ex);
            }
            dockerClient.removeContainerCmd(containerId).exec();
        } catch (Exception ex) {
            log.warn("Error on ending test execution {}", containerId, ex);
        }
    }
}
Also used : PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler) TimeoutException(java.util.concurrent.TimeoutException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LogConfig(com.github.dockerjava.api.model.LogConfig) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with LogConfig

use of com.github.dockerjava.api.model.LogConfig in project elastest-torm by elastest.

the class DockerServiceItTest method getLogConfig.

private LogConfig getLogConfig(String tag) {
    if (logstashHost == null) {
        logstashHost = dockerClient.inspectNetworkCmd().withNetworkId("bridge").exec().getIpam().getConfig().get(0).getGateway();
    }
    log.info("Logstash IP to send logs from containers: {}", logstashHost);
    Map<String, String> configMap = new HashMap<String, String>();
    configMap.put("syslog-address", "tcp://" + logstashHost + ":" + 5000);
    configMap.put("tag", tag);
    LogConfig logConfig = new LogConfig();
    logConfig.setType(LoggingType.SYSLOG);
    logConfig.setConfig(configMap);
    return logConfig;
}
Also used : HashMap(java.util.HashMap) LogConfig(com.github.dockerjava.api.model.LogConfig)

Aggregations

LogConfig (com.github.dockerjava.api.model.LogConfig)4 HashMap (java.util.HashMap)3 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)2 IOException (java.io.IOException)2 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1 Bind (com.github.dockerjava.api.model.Bind)1 Volume (com.github.dockerjava.api.model.Volume)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 Parameter (io.elastest.etm.model.Parameter)1 SutSpecification (io.elastest.etm.model.SutSpecification)1 TJob (io.elastest.etm.model.TJob)1 TJobExecution (io.elastest.etm.model.TJobExecution)1 WaitForMessagesHandler (io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1