diff options
author | axtloss <axtlos@getcryst.al> | 2023-05-25 22:06:02 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2023-05-25 22:06:02 +0200 |
commit | cd0df48723ca7c603cececc3c65003bf43b09d59 (patch) | |
tree | 6f39ce65f7897dce1dc7a8ab6f4179a5aa2f3617 /src/main/java/io/github/jshipit | |
parent | 75bbdb15e6f90d4f690b839f6db0cedfb61045be (diff) | |
download | jshipit-cd0df48723ca7c603cececc3c65003bf43b09d59.tar.gz jshipit-cd0df48723ca7c603cececc3c65003bf43b09d59.tar.bz2 |
Add shell subcommand
Diffstat (limited to 'src/main/java/io/github/jshipit')
-rw-r--r-- | src/main/java/io/github/jshipit/Commands.java | 11 | ||||
-rw-r--r-- | src/main/java/io/github/jshipit/ContainerManager.java | 11 | ||||
-rwxr-xr-x | src/main/java/io/github/jshipit/JshipIT.java | 9 |
3 files changed, 24 insertions, 7 deletions
diff --git a/src/main/java/io/github/jshipit/Commands.java b/src/main/java/io/github/jshipit/Commands.java index 98283e3..89eaad4 100644 --- a/src/main/java/io/github/jshipit/Commands.java +++ b/src/main/java/io/github/jshipit/Commands.java @@ -3,10 +3,19 @@ package io.github.jshipit; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; +@Parameters(commandDescription = "Open a shell in a container") +class CommandShell { + @Parameter(names = {"--name", "-n"}, description = "Name of the container", required = true) + public String containerName; +} + @Parameters(commandDescription = "Start a container") class CommandStart { - @Parameter(names = {"--name", "-n"}, description = "Name of the container") + @Parameter(names = {"--name", "-n"}, description = "Name of the container", required = true) public String containerName; + + @Parameter(names = {"--command", "-c"}, description = "Command to run in the container", required = false) + public String containerCommand = null; } @Parameters(commandDescription = "Create a container") diff --git a/src/main/java/io/github/jshipit/ContainerManager.java b/src/main/java/io/github/jshipit/ContainerManager.java index 8a3ecc7..c1d9b38 100644 --- a/src/main/java/io/github/jshipit/ContainerManager.java +++ b/src/main/java/io/github/jshipit/ContainerManager.java @@ -17,6 +17,8 @@ public class ContainerManager { private String containerImage; private String containerTag; private String containerApiRepo; + + private String containerCommand; private OCIDataStore dataStore; public ContainerManager(String containerName, String containerImage, String containerTag, String containerApiRepo, String containerRepo, OCIDataStore dataStore) { @@ -28,12 +30,13 @@ public class ContainerManager { this.dataStore = dataStore; } - public ContainerManager(String containerName, OCIDataStore dataStore) { + public ContainerManager(String containerName, String containerCommand, OCIDataStore dataStore) { this.containerName = containerName; this.containerImage = dataStore.getContainerImage(containerName); this.containerTag = dataStore.getContainerTag(containerName); this.containerApiRepo = dataStore.getContainerApiRepo(containerName); this.containerRepo = dataStore.getContainerRepo(containerName); + this.containerCommand = containerCommand; this.dataStore = dataStore; } @@ -74,7 +77,7 @@ public class ContainerManager { } - public void runCommand(String command) { + public void runCommand() { String containerDirectory = dataStore.getContainerPath(this.containerName); String dataStorePath = dataStore.getPath(); @@ -114,12 +117,12 @@ public class ContainerManager { bwrapCommand.add("--ro-bind "+containerDirectory+"/root / --chdir /"); bwrapCommand.add("--share-net"); bwrapCommand.add("--unshare-uts --hostname "+ (!hostname.isBlank() ? hostname : this.containerName+"-"+this.containerImage)); - bwrapCommand.add("/bin/sh -c "+cmd); + bwrapCommand.add("/bin/sh -c '"+(this.containerCommand != null ? this.containerCommand : cmd)+"'"); SysUtils sysUtils = new SysUtils(); String bwrapCMD = sysUtils.execInBwrap(bwrapCommand.toArray(new String[0]), false); String mountCMD = startContainer(true); - String CMD = "unshare --user --map-root-user --mount bash -c \""+mountCMD+";"+bwrapCMD+";"+command+"\""; // I am starting to realize that this project was not a good idea + String CMD = "unshare --user --map-root-user --mount bash -c \""+mountCMD+";"+bwrapCMD+"\""; // I am starting to realize that this project was not a good idea ProcessBuilder pb = new ProcessBuilder("bash", "-c", CMD); pb.inheritIO(); try { diff --git a/src/main/java/io/github/jshipit/JshipIT.java b/src/main/java/io/github/jshipit/JshipIT.java index becd91b..1383d48 100755 --- a/src/main/java/io/github/jshipit/JshipIT.java +++ b/src/main/java/io/github/jshipit/JshipIT.java @@ -11,6 +11,7 @@ import com.beust.jcommander.Parameter; public class JshipIT { public JshipIT(String[] args) { + CommandShell commandShell = new CommandShell(); CommandStart commandStart = new CommandStart(); CommandCreate commandCreate = new CommandCreate(); CommandPull commandPull = new CommandPull(); @@ -18,6 +19,7 @@ public class JshipIT { .addCommand("create", commandCreate) .addCommand("pull", commandPull) .addCommand("start", commandStart) + .addCommand("shell", commandShell) .build(); commands.parse(args); @@ -33,8 +35,11 @@ public class JshipIT { } else if (commands.getParsedCommand().equals("pull")) { dataStore.createImage(commandPull.containerApiRepo, commandPull.containerRepo, commandPull.containerImage, commandPull.containerTag); } else if (commands.getParsedCommand().equals("start")) { - ContainerManager containerManager = new ContainerManager(commandStart.containerName, dataStore); - containerManager.runCommand(null); + ContainerManager containerManager = new ContainerManager(commandStart.containerName, commandStart.containerCommand, dataStore); + containerManager.runCommand(); + } else if (commands.getParsedCommand().equals("shell")) { + ContainerManager containerManager = new ContainerManager(commandShell.containerName, "/bin/sh", dataStore); // A proper linux system should always have /bin/sh, skill issue if it doesn't + containerManager.runCommand(); } |