Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for extraction of env and logs from Docker containers #2

Merged
merged 10 commits into from
Aug 27, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.troubleshooting.authenticationtroubleshooting;

import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.api.model.Image;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;


@SpringBootApplication
public class AuthenticationTroubleshootingApplication {

public static void main(String[] args) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should contain only the logic that we need to run the application. not business logic
You might put here - general spring boot configurations (DI/logging etc.)

String containerID = "8e53411614f2";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read from configurations (note that you usually want to work differently locally and in production)


DockerClient dockerClient;
dockerClient = DockerClientBuilder.getInstance().build();

// Extract Env
OutputStream outputStream = new ByteArrayOutputStream();

ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerID)
.withAttachStdout(true).withAttachStderr(true).withCmd("env").exec();

System.out.println(dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(new ExecStartResultCallback(outputStream, null)));

// Extract Logs
final List<String> loggingFrames = getDockerLogs(dockerClient, containerID);
for(int i = 0; i < loggingFrames.size(); i++) {
System.out.println(loggingFrames.get(i));
}

SpringApplication.run(AuthenticationTroubleshootingApplication.class, args);
}

public static List<String> getDockerLogs(DockerClient dockerClient, String containerID) {
Pattern p = Pattern.compile("database");

final List<String> logs = new ArrayList<>();
LogContainerCmd logContainer = dockerClient.logContainerCmd(containerID).withStdOut(true).withStdErr(true);
try {
logContainer.exec(new LogContainerResultCallback() {
@Override
public void onNext(Frame item) {
if (p.matcher(item.toString()).find()) {
logs.add(item.toString());
}
}
}).awaitCompletion();
} catch (InterruptedException e) {
e.printStackTrace();
}
return logs;
}
}


1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.troubleshooting.authenticationtroubleshooting;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AuthenticationTroubleshootingApplicationTests {

@Test
void contextLoads() {
}

}