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;
}
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();
}
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);
}
}
}
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;
}
Aggregations