blob: 8e4a071a53437b350d805b3706052537377c2c5e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
}
}
}
|