Skip to content

Commit

Permalink
Add wrapper method for Linux commands (#2711)
Browse files Browse the repository at this point in the history
Check if user or group exists with corresponding uid/gid instead of creating them each time

Change-Id: I016e25c2d90bc527865c98b76dc6d91597c3d925
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf authored Oct 6, 2016
1 parent 24a24fa commit 19591c8
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ protected void installTypeScriptCompiler() throws IOException, InterruptedExcept

// avoid root permissions in generated files
if (SystemInfo.isLinux()) {

command.add("groupadd -g " + getGid() + " user && useradd -u" + getUid() + " -g user user && (chown --silent -R user.user /usr/src/app || true) && cd /usr/src/app/ && npm install && (chown --silent -R user.user /usr/src/app || true)");
command.add(wrapLinuxCommand("npm install"));
} else {
command.add("npm install");
}

// setup typescript compiler
ProcessBuilder processBuilder = new ProcessBuilder().command(command).directory(rootPath.toFile()).redirectErrorStream(true).inheritIO();
Process process = processBuilder.start();
Expand All @@ -224,6 +224,21 @@ protected void installTypeScriptCompiler() throws IOException, InterruptedExcept

}


/**
* Wrap the given command into a command with chown. Also add group/user that match host environment if not exists
* @param command the command to wrap
* @return an updated command with chown applied on it
*/
protected String wrapLinuxCommand(String command) throws IOException, InterruptedException {

String setGroup = "export GROUP_NAME=`(getent group " + getGid() + " || (groupadd -g " + getGid() + " user && echo user:x:" + getGid() +")) | cut -d: -f1`";
String setUser = "export USER_NAME=`(getent passwd " + getUid() + " || (useradd -u " + getUid() + " -g ${GROUP_NAME} user && echo user:x:" + getGid() +")) | cut -d: -f1`";
String chownCommand= "chown --silent -R ${USER_NAME}.${GROUP_NAME} /usr/src/app || true";
return setGroup + " && " + setUser + " && " + chownCommand + " && " + command + " && " + chownCommand;
}


/**
* Starts tests by compiling first generated DTO from maven plugin
* @throws IOException if unable to start process
Expand Down Expand Up @@ -253,7 +268,7 @@ public void compileDTOAndLaunchTests() throws IOException, InterruptedException

// avoid root permissions in generated files
if (SystemInfo.isLinux()) {
command.add("groupadd -g " + getGid() + " user && useradd -u" + getUid() + " -g user user && (chown --silent -R user.user /usr/src/app || true) && npm test && (chown --silent -R user.user /usr/src/app || true)");
command.add(wrapLinuxCommand("npm test"));
} else {
command.add("npm test");
}
Expand Down

0 comments on commit 19591c8

Please sign in to comment.