diff options
Diffstat (limited to 'src/main/java/io/github/jshipit/ContainerManager.java')
-rw-r--r-- | src/main/java/io/github/jshipit/ContainerManager.java | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/src/main/java/io/github/jshipit/ContainerManager.java b/src/main/java/io/github/jshipit/ContainerManager.java index 92482df..07dae9c 100644 --- a/src/main/java/io/github/jshipit/ContainerManager.java +++ b/src/main/java/io/github/jshipit/ContainerManager.java @@ -39,6 +39,9 @@ public class ContainerManager { this.dataStore = dataStore; } + /* + * Create a container directory + */ public void createContainer() { System.out.println("Creating container"); @@ -54,11 +57,18 @@ public class ContainerManager { String containerDirectory = dataStore.createContainerDirectory(this.containerImage, this.containerTag, this.containerName, this.containerApiRepo, this.containerRepo); - new File(containerDirectory + "/containerOverlay").mkdirs(); - new File(containerDirectory + "/root").mkdirs(); - new File(containerDirectory+"/work").mkdirs(); + new File(containerDirectory + "/containerOverlay").mkdirs(); // The upper directory of the overlay mount, user data and any changes to root will be stored here + new File(containerDirectory + "/root").mkdirs(); // The root directory of the overlay mount + new File(containerDirectory+"/work").mkdirs(); // The work directory of the overlay mount } + /* + * Start a container + * + * @param getCommand: If true, return the command to run the container + * + * @return: The command to run the container. If getCommand is false, return null + */ public String startContainer(boolean getCommand) { String containerDirectory = dataStore.getContainerPath(this.containerName); @@ -68,7 +78,9 @@ public class ContainerManager { } catch (IOException e) { throw new RuntimeException(e); } - assert content != null; + assert content != null; // assert my beloved + + // We parse the layers file to get the layers of the image List<String> layers = new ArrayList<>(); for (String line : content) { layers.add(this.dataStore.getPath() + "/blobs/" + line.replace("sha256:", "")); @@ -77,15 +89,19 @@ public class ContainerManager { SysUtils sysUtils = new SysUtils(); String[] diffs = layers.toArray(new String[0]); + // Depending on getCommand we either directly mount the layers or return the command to mount the layers return sysUtils.overlayMount(diffs, containerDirectory + "/containerOverlay", containerDirectory + "/root", containerDirectory + "/work", !getCommand); } + /* + * Run a command in a container + */ public void runCommand() { String containerDirectory = dataStore.getContainerPath(this.containerName); String dataStorePath = dataStore.getPath(); - File configPath = new File(dataStorePath + "/" + this.containerImage + "/" + this.containerTag + "/config"); + File configPath = new File(dataStorePath + "/" + this.containerImage + "/" + this.containerTag + "/config"); // Path to the config file of the image String content = null; try { content = Files.readAllLines(Paths.get(configPath.getAbsolutePath())).toString(); @@ -99,11 +115,11 @@ public class ContainerManager { String hostname = null; try { JsonNode config = mapper.readTree(content.toString()).get(0).get("config"); - config.get("Env").forEach((JsonNode envVar) -> { + config.get("Env").forEach((JsonNode envVar) -> { // Get the environment variables specified in the config file env.add(envVar.asText()); }); - cmd = config.get("Cmd").get(0).asText(); - hostname = config.get("Hostname").asText(); + cmd = config.get("Cmd").get(0).asText(); // Get the command specified in the config file + hostname = config.get("Hostname").asText(); // Get the hostname specified in the config file } catch (JsonProcessingException e) { throw new RuntimeException(e); } @@ -115,13 +131,13 @@ public class ContainerManager { List<String> bwrapCommand = new ArrayList<>(); for(String envVar : env) { - bwrapCommand.add("--setenv "+envVar.split("=")[0]+" "+envVar.split("=")[1]); + bwrapCommand.add("--setenv "+envVar.split("=")[0]+" "+envVar.split("=")[1]); // Create the flags for bwrap to set the environment variables } - bwrapCommand.add("--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 '"+(this.containerCommand != null ? this.containerCommand : cmd)+"'"); + bwrapCommand.add("--bind "+containerDirectory+"/root / --chdir /"); // Bind the root to / inside the bwrap namespace + bwrapCommand.add("--share-net"); // Share the network namespace + bwrapCommand.add("--unshare-uts --hostname "+ (!hostname.isBlank() ? hostname : this.containerName+"-"+this.containerImage)); // Unshare the UTS namespace and set the hostname + bwrapCommand.add("/bin/sh -c '"+(this.containerCommand != null ? this.containerCommand : cmd)+"'"); // Run the command specified in the config file or the command specified in the constructor SysUtils sysUtils = new SysUtils(); String bwrapCMD = sysUtils.execInBwrap(bwrapCommand.toArray(new String[0]), false); String mountCMD = startContainer(true); @@ -137,6 +153,9 @@ public class ContainerManager { } } + /* + * Delete a container + */ public void deleteContainer() { String containerDirectory = dataStore.getContainerPath(this.containerName); try { |