From 5e59e0e697b08882e157c9cecd35107da6d22ebc Mon Sep 17 00:00:00 2001 From: farthinder Date: Wed, 20 Sep 2023 12:02:56 +0200 Subject: [PATCH] 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" --- pom.xml | 56 ++++++++----------- .../devstack/container/Container.groovy | 15 +++++ .../container/impl/AlpineContainer.groovy | 8 +-- .../container/impl/DoodContainer.groovy | 22 +++++++- .../container/impl/GroovyDoodContainer.groovy | 18 ++++++ .../container/impl/UbuntuContainer.groovy | 15 +++++ 6 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 src/main/groovy/com/eficode/devstack/container/impl/GroovyDoodContainer.groovy create mode 100644 src/main/groovy/com/eficode/devstack/container/impl/UbuntuContainer.groovy diff --git a/pom.xml b/pom.xml index 24677d2..3ed7b1a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,22 @@ com.eficode devstack - 2.3.7-SNAPSHOT + 2.3.8-SNAPSHOT jar DevStack A series of scripts for setting up common developer application suites + + 11 + 11 + 3.0 + [3.0,4.0) + 2.4-M1-groovy-${groovy.major.version} + 2.0.3-SNAPSHOT-groovy-3.0 + 0.0.3-SNAPSHOT-groovy-3.0 + @@ -95,12 +104,14 @@ + de.gesellix docker-client - 2023-05-07T23-22-00 + 2023-08-16T08-25-00 + org.apache.maven.plugins maven-shade-plugin - 3.4.1 + 3.5.0 package @@ -175,6 +186,7 @@ META-INF/*.SF META-INF/*.DSA META-INF/*.RSA + META-INF/*.MF @@ -184,14 +196,10 @@ org.codehaus.groovy:* - com.google.code.gson:gson org.apache.httpcomponents - - com.kohlschutter.junixsocket:junixsocket-core - @@ -200,12 +208,20 @@ com.eficode.atlassian com.eficode.shaded.atlassian + + okio + com.eficode.shaded.okio + + + okhttp3 + com.eficode.shaded.okhttp3 + - true + false true @@ -265,28 +281,4 @@ - - - - groovy-3 - - true - - - 11 - 11 - 3.0 - [3.0,4.0) - 2.4-M1-groovy-${groovy.major.version} - 2.0.3-SNAPSHOT-groovy-3.0 - 0.0.3-SNAPSHOT-groovy-3.0 - - - - - - - - - \ No newline at end of file diff --git a/src/main/groovy/com/eficode/devstack/container/Container.groovy b/src/main/groovy/com/eficode/devstack/container/Container.groovy index 51ac96f..f684c33 100644 --- a/src/main/groovy/com/eficode/devstack/container/Container.groovy +++ b/src/main/groovy/com/eficode/devstack/container/Container.groovy @@ -55,6 +55,12 @@ trait Container { ArrayList mounts = [] + /** + * Prior to create a container, prepare mount-points + * @param sourceAbs The source directory in the docker engine + * @param target The target directory inside the container + * @param readOnly + */ void prepareBindMount(String sourceAbs, String target, boolean readOnly = true) { Mount newMount = new Mount().tap { m -> @@ -137,6 +143,15 @@ trait Container { } + /** + * Will create a Container that will sleep indefinitely, ie wont shut of once entrypoint has finished executing + * @return container id + */ + String createSleepyContainer() { + return createContainer([], ["tail", "-f", "/dev/null"]) + } + + boolean runOnFirstStartup() { return true } diff --git a/src/main/groovy/com/eficode/devstack/container/impl/AlpineContainer.groovy b/src/main/groovy/com/eficode/devstack/container/impl/AlpineContainer.groovy index 72db03a..3d73e11 100644 --- a/src/main/groovy/com/eficode/devstack/container/impl/AlpineContainer.groovy +++ b/src/main/groovy/com/eficode/devstack/container/impl/AlpineContainer.groovy @@ -25,13 +25,7 @@ class AlpineContainer implements Container { } } - /** - * Will create an Alpine Container that will sleep indefinitely - * @return - */ - String createSleepyContainer() { - return createContainer([], ["tail", "-f", "/dev/null"]) - } + diff --git a/src/main/groovy/com/eficode/devstack/container/impl/DoodContainer.groovy b/src/main/groovy/com/eficode/devstack/container/impl/DoodContainer.groovy index de26c19..482e642 100644 --- a/src/main/groovy/com/eficode/devstack/container/impl/DoodContainer.groovy +++ b/src/main/groovy/com/eficode/devstack/container/impl/DoodContainer.groovy @@ -23,7 +23,7 @@ class DoodContainer implements Container { if (dockerHost && dockerCertPath) { assert setupSecureRemoteConnection(dockerHost, dockerCertPath): "Error setting up secure remote docker connection" } - prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock") + prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock", false) } @Override @@ -57,6 +57,26 @@ class DoodContainer implements Container { return runAfterDockerSetup() } + /** + * Changes the owner of /var/run/docker.sock, intended to give low privileged container users access to the docker engine + * @param user defaults to the container images default user + * @return true on success + */ + boolean changeDockerSockOwner(String user = "") { + if (user == "") { + user = runBashCommandInContainer("whoami").find {true} + } + + ArrayList cmdOutput = runBashCommandInContainer("chown $user:$user /var/run/docker.sock && echo Status:\$?", 30, "root") + assert cmdOutput.last() == "Status:0" : "Error changing docker socket owner to $user" + + cmdOutput = runBashCommandInContainer("docker info | grep ID:", 10, user) + assert cmdOutput.any {it.contains(dockerClient.info().content.getID() )} : "Error accessing docker socket as $user" + + return true + + } + /** * This is run once after the docker client has been installed and verified that it can talk with parent docker node * @return true on success diff --git a/src/main/groovy/com/eficode/devstack/container/impl/GroovyDoodContainer.groovy b/src/main/groovy/com/eficode/devstack/container/impl/GroovyDoodContainer.groovy new file mode 100644 index 0000000..d9fda04 --- /dev/null +++ b/src/main/groovy/com/eficode/devstack/container/impl/GroovyDoodContainer.groovy @@ -0,0 +1,18 @@ +package com.eficode.devstack.container.impl + +class GroovyDoodContainer extends DoodContainer{ + + String containerName = "GroovyDood" + String containerMainPort = "" + String containerImage = "groovy" + String containerImageTag = "jdk11-jammy" + + + + @Override + boolean runAfterDockerSetup() { + return changeDockerSockOwner() + } + +} + diff --git a/src/main/groovy/com/eficode/devstack/container/impl/UbuntuContainer.groovy b/src/main/groovy/com/eficode/devstack/container/impl/UbuntuContainer.groovy new file mode 100644 index 0000000..e25f000 --- /dev/null +++ b/src/main/groovy/com/eficode/devstack/container/impl/UbuntuContainer.groovy @@ -0,0 +1,15 @@ +package com.eficode.devstack.container.impl + +class UbuntuContainer extends AlpineContainer{ + + String containerName = "Ubuntu" + String containerImage = "ubuntu" + String defaultShell = "/bin/bash" + + + UbuntuContainer(String dockerHost = "", String dockerCertPath = "") { + if (dockerHost && dockerCertPath) { + assert setupSecureRemoteConnection(dockerHost, dockerCertPath): "Error setting up secure remote docker connection" + } + } +}