Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit 0531d71

Browse files
committed
FIX Create current user in container
1 parent 4972482 commit 0531d71

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/config/UserConfigItem.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.gpuopenanalytics.jenkins.remotedocker.AbstractDockerLauncher;
2828
import com.gpuopenanalytics.jenkins.remotedocker.Utils;
2929
import hudson.Extension;
30-
import hudson.Launcher;
3130
import hudson.model.Descriptor;
3231
import hudson.util.ArgumentListBuilder;
3332
import net.sf.json.JSONObject;
@@ -131,29 +130,44 @@ public void postCreate(AbstractDockerLauncher launcher) throws IOException, Inte
131130
String uid = Utils.resolveVariables(launcher, this.uid);
132131
String username = Utils.resolveVariables(launcher, this.username);
133132

134-
ArgumentListBuilder groupAddArgs = new ArgumentListBuilder();
135-
groupAddArgs.add("groupadd", "-g", gid, username);
136-
int status = launcher.dockerExec(groupAddArgs, false).join();
137-
if (status != 0) {
138-
throw new IOException("Failed to create group");
139-
}
133+
createUserAndGroup(launcher, username, uid, gid);
134+
} else if (isCurrentUser()) {
135+
String uid = executeWithOutput(launcher, "id", "-u");
136+
String gid = executeWithOutput(launcher, "id", "-g");
137+
String username = executeWithOutput(launcher, "id", "-un");
140138

141-
ArgumentListBuilder userAddArgs = new ArgumentListBuilder();
142-
userAddArgs.add("useradd", "-g", gid, "-u", uid, username);
143-
status = launcher.dockerExec(userAddArgs, false).join();
144-
if (status != 0) {
145-
throw new IOException("Failed to create user");
146-
}
139+
createUserAndGroup(launcher, username, uid, gid);
140+
}
141+
}
142+
143+
private void createUserAndGroup(AbstractDockerLauncher launcher,
144+
String username,
145+
String uid,
146+
String gid) throws IOException, InterruptedException {
147+
ArgumentListBuilder groupAddArgs = new ArgumentListBuilder();
148+
groupAddArgs.add("groupadd", "-g", gid, username);
149+
int status = launcher.dockerExec(groupAddArgs, false).join();
150+
if (status != 0) {
151+
throw new IOException("Failed to create group");
152+
}
153+
154+
ArgumentListBuilder userAddArgs = new ArgumentListBuilder();
155+
userAddArgs.add("useradd", "-g", gid, "-u", uid, username);
156+
status = launcher.dockerExec(userAddArgs, false).join();
157+
if (status != 0) {
158+
throw new IOException("Failed to create user");
147159
}
148160
}
149161

150-
private String executeWithOutput(Launcher launcher, String... args) {
162+
private String executeWithOutput(AbstractDockerLauncher launcher,
163+
String... args) {
151164
try {
152165
ByteArrayOutputStream baos = new ByteArrayOutputStream();
153-
int status = launcher.launch()
166+
int status = launcher.getInner().launch()
154167
.cmds(args)
155168
.stdout(baos)
156169
.stderr(launcher.getListener().getLogger())
170+
.quiet(!launcher.isDebug())
157171
.join();
158172
if (status != 0) {
159173
throw new RuntimeException(
@@ -172,8 +186,8 @@ public void addRunArgs(AbstractDockerLauncher launcher,
172186
if (!isCurrentUser()) {
173187
args.add("--user", Utils.resolveVariables(launcher, username));
174188
} else {
175-
String uid = executeWithOutput(launcher.getInner(), "id", "-u");
176-
String gid = executeWithOutput(launcher.getInner(), "id", "-g");
189+
String uid = executeWithOutput(launcher, "id", "-u");
190+
String gid = executeWithOutput(launcher, "id", "-g");
177191
args.add("--user", uid + ":" + gid);
178192
}
179193
}

0 commit comments

Comments
 (0)