|
25 | 25 | package com.gpuopenanalytics.jenkins.remotedocker.config; |
26 | 26 |
|
27 | 27 | import com.gpuopenanalytics.jenkins.remotedocker.AbstractDockerLauncher; |
28 | | -import com.gpuopenanalytics.jenkins.remotedocker.DockerLauncher; |
29 | 28 | import com.gpuopenanalytics.jenkins.remotedocker.Utils; |
30 | 29 | import hudson.Extension; |
31 | | -import hudson.model.AbstractBuild; |
| 30 | +import hudson.Launcher; |
32 | 31 | import hudson.model.Descriptor; |
33 | 32 | import hudson.util.ArgumentListBuilder; |
34 | 33 | import net.sf.json.JSONObject; |
|
39 | 38 |
|
40 | 39 | import javax.annotation.Nonnull; |
41 | 40 | import javax.annotation.Nullable; |
| 41 | +import java.io.ByteArrayOutputStream; |
42 | 42 | import java.io.IOException; |
| 43 | +import java.nio.charset.StandardCharsets; |
| 44 | +import java.util.Arrays; |
43 | 45 |
|
44 | 46 | public class UserConfigItem extends ConfigItem { |
45 | 47 |
|
@@ -145,15 +147,33 @@ public void postCreate(AbstractDockerLauncher launcher) throws IOException, Inte |
145 | 147 | } |
146 | 148 | } |
147 | 149 |
|
| 150 | + private String executeWithOutput(Launcher launcher, String... args) { |
| 151 | + try { |
| 152 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 153 | + int status = launcher.launch() |
| 154 | + .cmds(args) |
| 155 | + .stdout(baos) |
| 156 | + .stderr(launcher.getListener().getLogger()) |
| 157 | + .join(); |
| 158 | + if (status != 0) { |
| 159 | + throw new RuntimeException( |
| 160 | + "Non-zero status " + status + ": " + Arrays |
| 161 | + .toString(args)); |
| 162 | + } |
| 163 | + return baos.toString(StandardCharsets.UTF_8.name()).trim(); |
| 164 | + } catch (InterruptedException | IOException e) { |
| 165 | + throw new RuntimeException(e); |
| 166 | + } |
| 167 | + } |
| 168 | + |
148 | 169 | @Override |
149 | 170 | public void addRunArgs(AbstractDockerLauncher launcher, |
150 | 171 | ArgumentListBuilder args) { |
151 | 172 | if (!isCurrentUser()) { |
152 | 173 | args.add("--user", Utils.resolveVariables(launcher, username)); |
153 | 174 | } else { |
154 | | - com.sun.security.auth.module.UnixSystem unix = new com.sun.security.auth.module.UnixSystem(); |
155 | | - long uid = unix.getUid(); |
156 | | - long gid = unix.getGid(); |
| 175 | + String uid = executeWithOutput(launcher.getInner(), "id", "-u"); |
| 176 | + String gid = executeWithOutput(launcher.getInner(), "id", "-g"); |
157 | 177 | args.add("--user", uid + ":" + gid); |
158 | 178 | } |
159 | 179 | } |
|
0 commit comments