forked from jenkinsci/acceptance-test-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerinject.groovy
176 lines (157 loc) · 5.87 KB
/
dockerinject.groovy
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import org.jenkinsci.test.acceptance.docker.Docker
import static junit.framework.TestCase.fail
/*
##################################################################
### If you want to use this groovy guice injection script, ###
### then define a System Environment Variable called "CONFIG", ###
### with the path to this file as the argument! ###
##################################################################
*/
String os = System.getProperty("os.name").toLowerCase();
String version = System.getProperty("os.version").toLowerCase();
// To ugly to check for all this files use lsb_release, if files are there this command is also there!!
// /etc/centos-release
// /etc/lsb-release
// /etc/redhat-release
// /etc/system-release
// /etc/os-release
//File release = new File("/etc/lsb-release");
// Fallback if lsb_release is not available
File issue = new File("/etc/issue");
/**
* sets the named docker variable for Guice to "docker"
*/
private def dockerCmd(){
docker = "docker"
}
/**
* sets the named docker variable for Guice to "docker.io"
*/
private def dockerDotIoCmd(){
docker = "docker.io"
}
/**
* Use the lsb_release command to figure out the distribution you are using.
*
* @return StringBuffer Array with first index is Standard Output and second index is Standard ERROR
*/
private def StringBuffer[] getLSB_release(){
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = 'lsb_release -a'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
//println "out> $sout err> $serr"
def StringBuffer[] stdOutNErr = new StringBuffer[2]
stdOutNErr[0] = sout
stdOutNErr[1] = serr
return stdOutNErr
}
/**
* Use the /etc/issue file to figure out the distribution you are using.
*
* @return StringBuffer Array with first index is Standard Output and second index is Standard ERROR
*/
private def StringBuffer[] getUname(){
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = 'uname -a'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
//println "out> $sout err> $serr"
def StringBuffer[] stdOutNErr = new StringBuffer[2]
stdOutNErr[0] = sout
stdOutNErr[1] = serr
return stdOutNErr
}
/**
* Use this Funtions to handle MAC specific injection stuff for Guice
*/
private def macInjectStuff(){
// add special mac stuff if needed
}
/**
* This function tries to figure out what distribution you are running if you feed it with a all lower case string
* that you gather via command or by reading a file
*
* @param toCheck String that should be all lower case and that gets checked
* @return true if we were successful, false if we failed
*/
private def Boolean linuxDistributionStuff(String toCheck){
Boolean boolFlag = false
if (toCheck.indexOf("solydxk") >= 0 ||
toCheck.indexOf("arch linux") >= 0 ||
toCheck.indexOf("ubuntu") >= 0 ||
toCheck.indexOf("fedora") >= 0 ||
toCheck.indexOf("opensuse") >= 0
) {
dockerCmd()
boolFlag = true
} else if(toCheck.indexOf("debian") >=0 ){
dockerDotIoCmd()
boolFlag = true
} else {
boolFlag = false
}
return boolFlag
}
//MAC
if ( os.indexOf("mac") >= 0) {
//do some mac specific binding
dockerCmd()
macInjectStuff()
//Windows
} else if (os.indexOf("win") >= 0) {
fail("Sry not supported!");
//LINUX
} else if (os.indexOf("nux") >= 0 ) {
String[] outErrStrings = getLSB_release()
stdOut = outErrStrings[0]
stdErr = outErrStrings[1]
if (!stdOut.empty) {
String lowerCaseStdOut = stdOut.toLowerCase()
Boolean success = linuxDistributionStuff(lowerCaseStdOut)
if (!success){
fail("Sry, you seem to be running Linux, the lsb_release command returned this:" +
stdOut + "but your distribution is not known to us! Don't know what to inject!")
}
// backup if lsb_release is not available by default centOS, redhat, arch linux, ...
} else if (issue.exists()) {
String content = "";
issue.eachLine { line ->
//println line //Debugging
content = content + line.toLowerCase()
}
Boolean success = linuxDistributionStuff(content)
if (!success){
fail("Sry, you seem to be running Linux but your distribution is not known!" +
" Don't know what to inject!")
}
} else {
fail("Sry, you seem to be running Linux but your distribution is not known to us," +
" and no lsb_release command nor a /etc/issue file was found! Don't know what to inject!")
}
//UNIX / AIX / *BSDs
} else if( os.indexOf("nix") >= 0 || os.indexOf("aix") >= 0 || os.indexOf("bsd") >= 0 ){
// They need special treatment no lsb_release and /etc/issue
String[] outErrStrings = getUname()
stdOut = outErrStrings[0]
stdErr = outErrStrings[1]
if (!stdOut.empty) {
String lowerCaseStdOut = stdOut.toLowerCase()
if (lowerCaseStdOut.indexOf("freebsd") >= 0 ||
lowerCaseStdOut.indexOf("openbsd") >= 0 ||
lowerCaseStdOut.indexOf("pcbsd") >= 0 ||
lowerCaseStdOut.indexOf("netbsd") >= 0
) {
fail("Sry, but your distribution is not supported by docker when this was written! Don't know what to inject!")
} else {
fail("Sry, you seem to be running Linux, the lsb_release command returned this:" +
stdOut + "but your distribution is not known to us! Don't know what to inject!")
}
} else {
fail("Sry, you seem to be running some flavour of UNIX but your distribution is not known to us," +
" and no lsb_release command nor a /etc/issue file was found! Don't know what to inject!")
}
} else {
//default
fail("Sry, what System are you running? Don't know what to inject!")
}