summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java
index 0e5a1d15..62e5dd0d 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java
@@ -1,6 +1,11 @@
package org.openslx.dozmod.util;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonParser;
import org.apache.log4j.Logger;
+import org.kamranzafar.jtar.TarEntry;
+import org.kamranzafar.jtar.TarHeader;
+import org.kamranzafar.jtar.TarInputStream;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.dozmod.gui.Gui;
@@ -10,6 +15,9 @@ import org.openslx.dozmod.thrift.cache.LectureCache;
import org.openslx.thrifthelper.TConst;
import java.awt.*;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.List;
/**
@@ -17,6 +25,7 @@ import java.util.List;
*/
public class ContainerUtils {
+ private static final Logger LOGGER = Logger.getLogger(ContainerUtils.class);
/**
* Checks ImageBaseId of image if already linked with existing Lectures in LectureCache.
*
@@ -40,4 +49,78 @@ public class ContainerUtils {
Gui.showMessageBox(c, I18n.WINDOW.getString("LectureDetails.Message.error.containerLinkedWithLecture"),
MessageType.WARNING, logger, null);
}
+
+ /**
+ * Check if a provided tar file contains information about a single docker image.
+ * To check the validity of the file, the existence of two JSON files is checked
+ * and one of the files must only contain information for one image.
+ *
+ * The tar file have to be created by the 'docker save ...' command.
+ *
+ * @param tarFile the user selected tar file.
+ * @return true if the images imageBaseId is already linked with a lecture.
+ */
+ public static boolean isValidTar(File tarFile) {
+
+ boolean isValid = false;
+ boolean containsManifest = false;
+ boolean containsRepositories = false;
+ JsonArray manifestJson = null;
+
+ try {
+ TarInputStream tis = new TarInputStream(new FileInputStream(tarFile));
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] rawData = new byte[1024];
+ TarEntry entry;
+
+ // setDefaultSkip seems to fasten things up while processing the file,
+ // because we want only to check manifest.json and repositories file
+ tis.setDefaultSkip(true);
+ while ((entry = tis.getNextEntry()) != null) {
+ if(!TarHeader.USTAR_MAGIC.equals(entry.getHeader().magic.toString()))
+ break;
+ Arrays.fill(rawData, (byte) 0);
+ output.reset();
+ int count = 0;
+
+ if (entry.getName().equals("manifest.json")) {
+ containsManifest = true;
+ while ((count = tis.read(rawData)) != -1) {
+ output.write(rawData, 0, count);
+ }
+ manifestJson = new JsonParser().parse(
+ new String(output.toByteArray(), StandardCharsets.UTF_8)).getAsJsonArray();
+ }
+
+ if (entry.getName().equals("repositories")) {
+ containsRepositories = true;
+ // dont read the file, no checks for the Content
+ }
+
+ if (containsManifest && containsRepositories)
+ break;
+ }
+ tis.close();
+ // check the json files inside the tar file
+ if (containsManifest && containsRepositories && manifestJson.isJsonArray()
+ && manifestJson.size() == 1) {
+ isValid = true;
+ String repoTag = manifestJson.get(0).getAsJsonObject().get("RepoTags").getAsString();
+ LOGGER.info(String.format("Tar File contains Docker Image with repoTag=%s", repoTag));
+ } else if (containsManifest && containsRepositories && manifestJson.isJsonArray()
+ && manifestJson.size() > 1) {
+ Gui.showMessageBox("Tar File container more then one Images!", MessageType.ERROR,
+ LOGGER,
+ null);
+ } else {
+ Gui.showMessageBox("No valid Tar File with Images provided!", MessageType.ERROR,
+ LOGGER,
+ null);
+ }
+
+ } catch (IOException e) {
+ LOGGER.error("IOError while processing tar file", e);
+ }
+ return isValid;
+ }
}