diff options
author | Florian Zschocke <f.zschocke+git@gmail.com> | 2021-12-10 00:10:04 +0100 |
---|---|---|
committer | Florian Zschocke <f.zschocke+git@gmail.com> | 2021-12-10 00:10:04 +0100 |
commit | 109d6479b411d2ee1439ebf70590030981cbcac7 (patch) | |
tree | 7e1397bf6de47cbe20c7a39f989e60cc148a3ecb | |
parent | 14e91e8a1ca3b4b4b0ea54f518805e6409f8705d (diff) | |
download | gitblit-109d6479b411d2ee1439ebf70590030981cbcac7.tar.gz gitblit-109d6479b411d2ee1439ebf70590030981cbcac7.zip |
Detect and report if running in container
To help with analysis, try to detect if the instance is running inside
a container. Some containers are detected, but this is probably not
exhaustive. At least a Docker container should be detectable.
Report in the runtime manager to the log if a container was detected.
-rw-r--r-- | src/main/java/com/gitblit/manager/RuntimeManager.java | 2 | ||||
-rw-r--r-- | src/main/java/com/gitblit/utils/ContainerDetector.java | 74 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/main/java/com/gitblit/manager/RuntimeManager.java b/src/main/java/com/gitblit/manager/RuntimeManager.java index 18d6b9c2..f0cca514 100644 --- a/src/main/java/com/gitblit/manager/RuntimeManager.java +++ b/src/main/java/com/gitblit/manager/RuntimeManager.java @@ -22,6 +22,7 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; +import com.gitblit.utils.ContainerDetector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,6 +79,7 @@ public class RuntimeManager implements IRuntimeManager { logTimezone("App timezone: ", getTimezone()); logger.info("JVM locale : " + Locale.getDefault()); logger.info("App locale : " + (getLocale() == null ? "<client>" : getLocale())); + ContainerDetector.report(logger, true); return this; } diff --git a/src/main/java/com/gitblit/utils/ContainerDetector.java b/src/main/java/com/gitblit/utils/ContainerDetector.java new file mode 100644 index 00000000..8e4a071a --- /dev/null +++ b/src/main/java/com/gitblit/utils/ContainerDetector.java @@ -0,0 +1,74 @@ +package com.gitblit.utils; + +import org.slf4j.Logger; + +import java.io.File; + +/** + * The ContainerDetector tries to detect if the Gitblit instance + * is running in a container, or directly on the (virtualized) OS. + */ +public class ContainerDetector +{ + private static Boolean inContainer; + private static String detectedType = ""; + + /** + * Detect if this instance in running inside a container. + * + * @return true - if a container could be detected + * false - otherwise + */ + public static boolean detect() + { + if (inContainer == null) { + File proc = new File("/proc/1/cgroup"); + if (! proc.exists()) inContainer = Boolean.FALSE; + else { + String cgroups = FileUtils.readContent(proc, null); + if (cgroups.contains("/docker")) { + inContainer = Boolean.TRUE; + detectedType = "Docker container"; + } + else if (cgroups.contains("/ecs")) { + inContainer = Boolean.TRUE; + detectedType = "ECS container"; + } + else if (cgroups.contains("/kubepod") || cgroups.contains("/kubepods")) { + inContainer = Boolean.TRUE; + detectedType = "Kubernetes pod"; + } + } + + // Finally, if we still haven't found proof, it is probably not a container + if (inContainer == null) inContainer = Boolean.FALSE; + } + + return inContainer; + } + + + /** + * Report to some output if a container was detected. + * + */ + public static void report(Logger logger, boolean onlyIfInContainer) + { + if (detect()) { + String msg = "Running in a " + detectedType; + if (logger == null) { + System.out.println(msg); + } + else logger.info(msg); + } + else if (!onlyIfInContainer) { + String msg = "Not detected to be running in a container"; + if (logger == null) { + System.out.println(msg); + } + else logger.info(msg); + + } + + } +} |