aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2023-05-25 23:16:38 +0200
committeraxtloss <axtlos@getcryst.al>2023-05-25 23:16:38 +0200
commit3fdb1012609b71a41e551ea0aad3b8a27b134e21 (patch)
tree93956331d1e3ca1ceb493fd0a0d8c1bcebdb6994 /src/main/java/io
parentcd0df48723ca7c603cececc3c65003bf43b09d59 (diff)
downloadjshipit-3fdb1012609b71a41e551ea0aad3b8a27b134e21.tar.gz
jshipit-3fdb1012609b71a41e551ea0aad3b8a27b134e21.tar.bz2
remove command, allow registry/repo/image:tag layout
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/jshipit/Commands.java34
-rw-r--r--src/main/java/io/github/jshipit/ContainerManager.java21
-rwxr-xr-xsrc/main/java/io/github/jshipit/JshipIT.java71
-rwxr-xr-xsrc/main/java/io/github/jshipit/OCIDataStore.java24
-rw-r--r--src/main/java/io/github/jshipit/SysUtils.java11
5 files changed, 114 insertions, 47 deletions
diff --git a/src/main/java/io/github/jshipit/Commands.java b/src/main/java/io/github/jshipit/Commands.java
index 89eaad4..ffdbe38 100644
--- a/src/main/java/io/github/jshipit/Commands.java
+++ b/src/main/java/io/github/jshipit/Commands.java
@@ -7,6 +7,9 @@ import com.beust.jcommander.Parameters;
class CommandShell {
@Parameter(names = {"--name", "-n"}, description = "Name of the container", required = true)
public String containerName;
+
+ @Parameter(names = "--help", help = true)
+ private boolean help;
}
@Parameters(commandDescription = "Start a container")
@@ -16,6 +19,9 @@ class CommandStart {
@Parameter(names = {"--command", "-c"}, description = "Command to run in the container", required = false)
public String containerCommand = null;
+
+ @Parameter(names = "--help", help = true)
+ private boolean help;
}
@Parameters(commandDescription = "Create a container")
@@ -26,15 +32,8 @@ class CommandCreate {
@Parameter(names = {"--image", "-i"}, description = "Image of the container")
public String containerImage;
- @Parameter(names = {"--tag", "-t"}, description = "Tag of the container")
- public String containerTag;
-
- @Parameter(names = {"--api-repo", "-a"}, description = "API repository of the container")
- public String containerApiRepo;
-
- @Parameter(names = {"--repo", "-r"}, description = "Repository of the container")
- public String containerRepo;
-
+ @Parameter(names = "--help", help = true)
+ private boolean help;
}
@Parameters(commandDescription = "Pull a container image")
@@ -42,13 +41,16 @@ class CommandPull {
@Parameter(names = {"--image", "-i"}, description = "Image of the container")
public String containerImage;
- @Parameter(names = {"--tag", "-t"}, description = "Tag of the container")
- public String containerTag;
+ @Parameter(names = "--help", help = true)
+ private boolean help;
- @Parameter(names = {"--api-repo", "-a"}, description = "API repository of the container")
- public String containerApiRepo;
+}
- @Parameter(names = {"--repo", "-r"}, description = "Repository of the container")
- public String containerRepo;
+@Parameters(commandDescription = "Delete a container")
+class CommandDelete {
+ @Parameter(names = {"--name", "-n"}, description = "Name of the container")
+ public String containerName;
-}
+ @Parameter(names = "--help", help = true)
+ private boolean help;
+} \ No newline at end of file
diff --git a/src/main/java/io/github/jshipit/ContainerManager.java b/src/main/java/io/github/jshipit/ContainerManager.java
index c1d9b38..92482df 100644
--- a/src/main/java/io/github/jshipit/ContainerManager.java
+++ b/src/main/java/io/github/jshipit/ContainerManager.java
@@ -9,7 +9,6 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
-import java.util.UUID;
public class ContainerManager {
private String containerName;
@@ -43,6 +42,11 @@ public class ContainerManager {
public void createContainer() {
System.out.println("Creating container");
+ if (dataStore.containerExists(this.containerName)) {
+ System.out.println("Container already exists");
+ return;
+ }
+
if (!Files.isDirectory(Paths.get(dataStore.getPath() + "/" + this.containerImage + "/" + this.containerTag))) {
System.out.println("Image does not exist");
return;
@@ -114,7 +118,7 @@ public class ContainerManager {
bwrapCommand.add("--setenv "+envVar.split("=")[0]+" "+envVar.split("=")[1]);
}
- bwrapCommand.add("--ro-bind "+containerDirectory+"/root / --chdir /");
+ 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)+"'");
@@ -133,7 +137,16 @@ public class ContainerManager {
}
}
- public String genContainerID() {
- return UUID.randomUUID().toString();
+ public void deleteContainer() {
+ String containerDirectory = dataStore.getContainerPath(this.containerName);
+ try {
+ Files.delete(Paths.get(containerDirectory + "/containerOverlay"));
+ Files.delete(Paths.get(containerDirectory + "/root"));
+ Files.delete(Paths.get(containerDirectory + "/work"));
+ Files.delete(Paths.get(containerDirectory));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ dataStore.deleteContainerFromDatabase(this.containerName);
}
} \ No newline at end of file
diff --git a/src/main/java/io/github/jshipit/JshipIT.java b/src/main/java/io/github/jshipit/JshipIT.java
index 1383d48..9ba74d0 100755
--- a/src/main/java/io/github/jshipit/JshipIT.java
+++ b/src/main/java/io/github/jshipit/JshipIT.java
@@ -1,12 +1,11 @@
package io.github.jshipit;
import com.beust.jcommander.JCommander;
-import com.fasterxml.jackson.databind.JsonNode;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import com.beust.jcommander.Parameter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
public class JshipIT {
@@ -15,34 +14,84 @@ public class JshipIT {
CommandStart commandStart = new CommandStart();
CommandCreate commandCreate = new CommandCreate();
CommandPull commandPull = new CommandPull();
+ CommandDelete commandDelete = new CommandDelete();
JCommander commands = JCommander.newBuilder()
.addCommand("create", commandCreate)
.addCommand("pull", commandPull)
.addCommand("start", commandStart)
.addCommand("shell", commandShell)
+ .addCommand("delete", commandDelete)
.build();
commands.parse(args);
- OCIDataStore dataStore = new OCIDataStore("./tmp");
+ OCIDataStore dataStore = new OCIDataStore(System.getenv("HOME") + "/.local/share/dataStore");
if (commands.getParsedCommand() == null) {
commands.usage();
System.exit(1);
} else if (commands.getParsedCommand().equals("create")) {
- ContainerManager containerManager = new ContainerManager(commandCreate.containerName, commandCreate.containerImage, commandCreate.containerTag, commandCreate.containerApiRepo, commandCreate.containerRepo, dataStore);
+ if (commandCreate.containerName == null) {
+ System.out.println("Container name is required");
+ System.exit(1);
+ }
+ List<String> image = new ArrayList<String>(Arrays.asList(commandCreate.containerImage.split("/")));
+ String containerImage = image.get(image.size() - 1).split(":")[0];
+ String apiRepo = image.get(0);
+ image.remove(0);
+ image.remove(image.size() - 1);
+ String containerRepo = String.join("/", image);
+
+ switch (apiRepo) {
+ case "docker.io":
+ apiRepo = "registry.docker.io";
+ break;
+ case "ghcr.io":
+ apiRepo = "ghcr.io";
+ break;
+ case "quay.io":
+ apiRepo = "quay.io";
+ break;
+ default:
+ break;
+ }
+
+
+ ContainerManager containerManager = new ContainerManager(commandCreate.containerName, containerImage, commandCreate.containerImage.split(":")[1], apiRepo, containerRepo, dataStore);
containerManager.createContainer();
} else if (commands.getParsedCommand().equals("pull")) {
- dataStore.createImage(commandPull.containerApiRepo, commandPull.containerRepo, commandPull.containerImage, commandPull.containerTag);
+
+ List<String> image = new ArrayList<String>(Arrays.asList(commandPull.containerImage.split("/")));
+ String containerImage = image.get(image.size() - 1).split(":")[0];
+ String apiRepo = image.get(0);
+ image.remove(0);
+ image.remove(image.size() - 1);
+ String containerRepo = String.join("/", image);
+
+ switch (apiRepo) {
+ case "docker.io":
+ apiRepo = "registry.docker.io";
+ break;
+ case "ghcr.io":
+ apiRepo = "ghcr.io";
+ break;
+ case "quay.io":
+ apiRepo = "quay.io";
+ break;
+ default:
+ break;
+ }
+ System.out.println("Pulling image " + containerImage + " from " + apiRepo + "/" + containerRepo);
+ dataStore.createImage(apiRepo, containerRepo, containerImage, commandPull.containerImage.split(":")[1]);
} else if (commands.getParsedCommand().equals("start")) {
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();
+ } else if (commands.getParsedCommand().equals("delete")) {
+ ContainerManager containerManager = new ContainerManager(commandDelete.containerName, null, dataStore);
+ containerManager.deleteContainer();
}
-
-
-
}
} \ No newline at end of file
diff --git a/src/main/java/io/github/jshipit/OCIDataStore.java b/src/main/java/io/github/jshipit/OCIDataStore.java
index 0a80b3f..e399e75 100755
--- a/src/main/java/io/github/jshipit/OCIDataStore.java
+++ b/src/main/java/io/github/jshipit/OCIDataStore.java
@@ -2,8 +2,6 @@ package io.github.jshipit;
import com.fasterxml.jackson.databind.JsonNode;
-import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
@@ -114,7 +112,23 @@ public class OCIDataStore {
}
}
- public String getContainerID(String name) {
+ public void deleteContainerFromDatabase(String name) {
+ String url = "jdbc:sqlite:" + this.databasePath;
+
+ try (Connection conn = DriverManager.getConnection(url)) {
+ if (conn != null) {
+ Statement statement = conn.createStatement();
+ statement.setQueryTimeout(30); // set timeout to 30 sec.
+
+ statement.executeUpdate("DELETE FROM containers WHERE name = '" + name + "'");
+ }
+
+ } catch (SQLException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+ public boolean containerExists(String name) {
String url = "jdbc:sqlite:" + this.databasePath;
try (Connection conn = DriverManager.getConnection(url)) {
@@ -123,13 +137,13 @@ public class OCIDataStore {
statement.setQueryTimeout(30); // set timeout to 30 sec.
ResultSet rs = statement.executeQuery("SELECT * FROM containers WHERE name = '" + name + "'");
- return rs.getString("containerID");
+ return rs.next();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
- return null;
+ return false;
}
public String getContainerPath(String name) {
diff --git a/src/main/java/io/github/jshipit/SysUtils.java b/src/main/java/io/github/jshipit/SysUtils.java
index 144c401..59450ad 100644
--- a/src/main/java/io/github/jshipit/SysUtils.java
+++ b/src/main/java/io/github/jshipit/SysUtils.java
@@ -6,16 +6,6 @@ import java.io.File;
public class SysUtils {
- public void chmod(String path, int mode) {
- ProcessBuilder pb = new ProcessBuilder("chmod", Integer.toString(mode), path);
- pb.inheritIO();
- try {
- Process p = pb.start();
- p.waitFor();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
public void untar(String in, String out) {
new File(out).mkdirs();
@@ -30,7 +20,6 @@ public class SysUtils {
}
public String execInBwrap(String[] args, boolean execute) {
- //System.out.println("bwrap "+String.join(" ", args));
if (!execute) {
return "bwrap "+String.join(" ", args);
}