Skip to content

Commit 5e59e0e

Browse files
authored
DoodContainer.groovy (#28)
* Added changeDockerSockOwner() GroovyDoodContainer.groovy * New container intended to be used when groovy needs access to docker engine UbuntuContainer.groovy * A new basic ubuntu container Container.groovy * Improved documentation * Added createSleepyContainer() pom.xml * Bumped to 2.3.8 * Bumped docker-client 2023-05-07T23-22-00 -> 2023-08-16T08-25-00 * Tweaked maven-shade to work better both as standalone as "normal"
1 parent aa8716a commit 5e59e0e

File tree

6 files changed

+94
-40
lines changed

6 files changed

+94
-40
lines changed

pom.xml

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66

77
<groupId>com.eficode</groupId>
88
<artifactId>devstack</artifactId>
9-
<version>2.3.7-SNAPSHOT</version>
9+
<version>2.3.8-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>DevStack</name>
1313

1414
<description>A series of scripts for setting up common developer application suites</description>
1515

16+
<properties>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
<groovy.major.version>3.0</groovy.major.version>
20+
<groovy.version>[3.0,4.0)</groovy.version>
21+
<spock-core.version>2.4-M1-groovy-${groovy.major.version}</spock-core.version>
22+
<jiraShortcuts.version>2.0.3-SNAPSHOT-groovy-3.0</jiraShortcuts.version>
23+
<bitbucketinstancemanager.version>0.0.3-SNAPSHOT-groovy-3.0</bitbucketinstancemanager.version>
24+
</properties>
1625

1726

1827
<dependencies>
@@ -95,12 +104,14 @@
95104
</dependency>
96105

97106

107+
<!-- https://mvnrepository.com/artifact/de.gesellix/docker-client -->
98108
<dependency>
99109
<groupId>de.gesellix</groupId>
100110
<artifactId>docker-client</artifactId>
101-
<version>2023-05-07T23-22-00</version>
111+
<version>2023-08-16T08-25-00</version>
102112
</dependency>
103113

114+
104115
<!--dependency>
105116
<groupId>org.junit.jupiter</groupId>
106117
<artifactId>junit-jupiter-api</artifactId>
@@ -154,7 +165,7 @@
154165
<!-- Configured in pluginManagement instead of plugins, because we do not want a shaded parent POM -->
155166
<groupId>org.apache.maven.plugins</groupId>
156167
<artifactId>maven-shade-plugin</artifactId>
157-
<version>3.4.1</version>
168+
<version>3.5.0</version>
158169
<executions>
159170
<execution>
160171
<phase>package</phase>
@@ -175,6 +186,7 @@
175186
<exclude>META-INF/*.SF</exclude>
176187
<exclude>META-INF/*.DSA</exclude>
177188
<exclude>META-INF/*.RSA</exclude>
189+
<exclude>META-INF/*.MF</exclude>
178190
</excludes>
179191
</filter>
180192
</filters>
@@ -184,14 +196,10 @@
184196
<exclude>org.codehaus.groovy:*</exclude>
185197
<!--exclude>org.codehaus.groovy:groovy</exclude>
186198
<exclude>org.codehaus.groovy:groovy-astbuilder</exclude-->
187-
188199
<exclude>com.google.code.gson:gson</exclude>
189200
<exclude>org.apache.httpcomponents</exclude>
190-
191201
<!--exclude>commons-*</exclude--> <!--Seems to break JenkinsAndHarborDeployment.groovy:103, needs org/apache/commons/io/FileUtils-->
192-
193202
<exclude>com.kohlschutter.junixsocket:junixsocket-core</exclude>
194-
195203
</excludes>
196204

197205
</artifactSet>
@@ -200,12 +208,20 @@
200208
<pattern>com.eficode.atlassian</pattern>
201209
<shadedPattern>com.eficode.shaded.atlassian</shadedPattern>
202210
</relocation>
211+
<relocation>
212+
<pattern>okio</pattern>
213+
<shadedPattern>com.eficode.shaded.okio</shadedPattern>
214+
</relocation>
215+
<relocation>
216+
<pattern>okhttp3</pattern>
217+
<shadedPattern>com.eficode.shaded.okhttp3</shadedPattern>
218+
</relocation>
203219
</relocations>
204220

205221
<!-- NOTE: Any dependencies of the project will not show up in the standalone pom.
206222
This means that if those dependencies are not properly relocated and there is a class-loading conflict,
207223
user would not be able to figure out where the conflicting dependency is. -->
208-
<createDependencyReducedPom>true</createDependencyReducedPom>
224+
<createDependencyReducedPom>false</createDependencyReducedPom>
209225

210226
<createSourcesJar>true</createSourcesJar>
211227
</configuration>
@@ -265,28 +281,4 @@
265281
</plugins>
266282
</build>
267283

268-
<profiles>
269-
270-
<profile>
271-
<id>groovy-3</id>
272-
<activation>
273-
<activeByDefault>true</activeByDefault>
274-
</activation>
275-
<properties>
276-
<maven.compiler.source>11</maven.compiler.source>
277-
<maven.compiler.target>11</maven.compiler.target>
278-
<groovy.major.version>3.0</groovy.major.version>
279-
<groovy.version>[3.0,4.0)</groovy.version>
280-
<spock-core.version>2.4-M1-groovy-${groovy.major.version}</spock-core.version>
281-
<jiraShortcuts.version>2.0.3-SNAPSHOT-groovy-3.0</jiraShortcuts.version>
282-
<bitbucketinstancemanager.version>0.0.3-SNAPSHOT-groovy-3.0</bitbucketinstancemanager.version>
283-
</properties>
284-
</profile>
285-
286-
287-
288-
</profiles>
289-
290-
291-
292284
</project>

src/main/groovy/com/eficode/devstack/container/Container.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ trait Container {
5555
ArrayList<Mount> mounts = []
5656

5757

58+
/**
59+
* Prior to create a container, prepare mount-points
60+
* @param sourceAbs The source directory in the docker engine
61+
* @param target The target directory inside the container
62+
* @param readOnly
63+
*/
5864
void prepareBindMount(String sourceAbs, String target, boolean readOnly = true) {
5965

6066
Mount newMount = new Mount().tap { m ->
@@ -137,6 +143,15 @@ trait Container {
137143
}
138144

139145

146+
/**
147+
* Will create a Container that will sleep indefinitely, ie wont shut of once entrypoint has finished executing
148+
* @return container id
149+
*/
150+
String createSleepyContainer() {
151+
return createContainer([], ["tail", "-f", "/dev/null"])
152+
}
153+
154+
140155
boolean runOnFirstStartup() {
141156
return true
142157
}

src/main/groovy/com/eficode/devstack/container/impl/AlpineContainer.groovy

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ class AlpineContainer implements Container {
2525
}
2626
}
2727

28-
/**
29-
* Will create an Alpine Container that will sleep indefinitely
30-
* @return
31-
*/
32-
String createSleepyContainer() {
33-
return createContainer([], ["tail", "-f", "/dev/null"])
34-
}
28+
3529

3630

3731

src/main/groovy/com/eficode/devstack/container/impl/DoodContainer.groovy

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DoodContainer implements Container {
2323
if (dockerHost && dockerCertPath) {
2424
assert setupSecureRemoteConnection(dockerHost, dockerCertPath): "Error setting up secure remote docker connection"
2525
}
26-
prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock")
26+
prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock", false)
2727
}
2828

2929
@Override
@@ -57,6 +57,26 @@ class DoodContainer implements Container {
5757
return runAfterDockerSetup()
5858
}
5959

60+
/**
61+
* Changes the owner of /var/run/docker.sock, intended to give low privileged container users access to the docker engine
62+
* @param user defaults to the container images default user
63+
* @return true on success
64+
*/
65+
boolean changeDockerSockOwner(String user = "") {
66+
if (user == "") {
67+
user = runBashCommandInContainer("whoami").find {true}
68+
}
69+
70+
ArrayList<String> cmdOutput = runBashCommandInContainer("chown $user:$user /var/run/docker.sock && echo Status:\$?", 30, "root")
71+
assert cmdOutput.last() == "Status:0" : "Error changing docker socket owner to $user"
72+
73+
cmdOutput = runBashCommandInContainer("docker info | grep ID:", 10, user)
74+
assert cmdOutput.any {it.contains(dockerClient.info().content.getID() )} : "Error accessing docker socket as $user"
75+
76+
return true
77+
78+
}
79+
6080
/**
6181
* This is run once after the docker client has been installed and verified that it can talk with parent docker node
6282
* @return true on success
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.eficode.devstack.container.impl
2+
3+
class GroovyDoodContainer extends DoodContainer{
4+
5+
String containerName = "GroovyDood"
6+
String containerMainPort = ""
7+
String containerImage = "groovy"
8+
String containerImageTag = "jdk11-jammy"
9+
10+
11+
12+
@Override
13+
boolean runAfterDockerSetup() {
14+
return changeDockerSockOwner()
15+
}
16+
17+
}
18+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.eficode.devstack.container.impl
2+
3+
class UbuntuContainer extends AlpineContainer{
4+
5+
String containerName = "Ubuntu"
6+
String containerImage = "ubuntu"
7+
String defaultShell = "/bin/bash"
8+
9+
10+
UbuntuContainer(String dockerHost = "", String dockerCertPath = "") {
11+
if (dockerHost && dockerCertPath) {
12+
assert setupSecureRemoteConnection(dockerHost, dockerCertPath): "Error setting up secure remote docker connection"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)