summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java108
1 files changed, 65 insertions, 43 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java
index e29d8d9c..f41b4b47 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java
@@ -26,12 +26,12 @@ public class ContainerDefinition {
private static final String CONTAINER_META_FILE = "container_meta.json";
/**
- * The file to construct a real container, could be an dockerfile or a singularity recipe.
+ * The file to construct a real container image, could be an dockerfile or a singularity recipe.
*/
- public String containerDescription;
+ public String containerRecipe = "";
/**
- * Further container information.
+ * Further container information, see {@link ContainerMeta}.
*/
public ContainerMeta containerMeta;
@@ -39,32 +39,30 @@ public class ContainerDefinition {
containerMeta = new ContainerMeta();
}
- public ContainerDefinition(ContainerMeta containerMeta) {
- this.containerMeta = containerMeta;
- }
-
/**
- * Constructor to create a temporal ContainerDefinition object, to construct DockerMetaDataDummy
- * TODO i think this is a temp. solution and could be changed in a later cleanup.
+ * Copy Constructor
*
- * @param file container recipe file
- * @param containerMeta container meta object
+ * @param containerDef {@link ContainerDefinition} from which to make a deep copy.
*/
- public ContainerDefinition(File file, ContainerMeta containerMeta) {
- this.containerDescription = readContainerRecipe(file);
- this.containerMeta = containerMeta;
+ public ContainerDefinition(ContainerDefinition containerDef) {
+ containerRecipe = String.valueOf(containerDef.getContainerRecipe());
+ containerMeta = new ContainerMeta(containerDef.getContainerMeta());
+ }
+
+ public String getContainerRecipe() {
+ return containerRecipe;
}
- public String getDescription() {
- return containerDescription;
+ public void setContainerRecipe(String containerRecipe) {
+ this.containerRecipe = containerRecipe;
}
- public void setDescription(String description) {
- containerDescription = description;
+ public void setContainerRecipe(File containerRecipeFile) {
+ this.containerRecipe = readContainerRecipe(containerRecipeFile);
}
- public void setContainerDescription(byte[] containerDescription) {
- this.containerDescription = new String(containerDescription, StandardCharsets.UTF_8);
+ public void setContainerRecipe(byte[] rawContainerRecipe) {
+ this.containerRecipe = new String(rawContainerRecipe, StandardCharsets.UTF_8);
}
public void setContainerMeta(byte[] containerMeta) {
@@ -80,11 +78,17 @@ public class ContainerDefinition {
public DockerMetaDataDummy createVmMeta() {
- byte[] rawContainerDefinition = toByteBuffer().array();
- return new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(), rawContainerDefinition,
- rawContainerDefinition.length);
+ byte[] rawContainerRecipe = toByteBuffer().array();
+ return new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(), rawContainerRecipe,
+ rawContainerRecipe.length);
}
+ /**
+ * Utility function to create a {@link ContainerDefinition} object for a byte array downloaded from the server.
+ *
+ * @param rawTarData Downloaded tar.gz file from the server as a byte array.
+ * @return New object of ContainerDefinition.
+ */
public static ContainerDefinition fromByteArray(byte[] rawTarData) {
ContainerDefinition containerDef = new ContainerDefinition();
@@ -96,18 +100,23 @@ public class ContainerDefinition {
TarEntry entry;
while ((entry = tis.getNextEntry()) != null) {
- int size = (int) entry.getSize();
- byte[] rawData = new byte[size];
- tis.read(rawData, 0, size);
+ byte[] rawData = new byte[1024];
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ int count;
+
+ // read everything from the TarInputStream for the current Entry
+ while ((count = tis.read(rawData)) != -1) {
+ output.write(rawData, 0, count);
+ }
if (entry.getName().equals(CONTAINER_FILE))
- containerDef.setContainerDescription(rawData);
+ containerDef.setContainerRecipe(output.toByteArray());
if (entry.getName().equals(CONTAINER_META_FILE))
- containerDef.setContainerMeta(rawData);
+ containerDef.setContainerMeta(output.toByteArray());
}
} catch (IOException e) {
- e.printStackTrace();
+ LOGGER.error("Could not create a ContainerDefinition Object for rawTarData", e);
}
return containerDef;
@@ -128,7 +137,7 @@ public class ContainerDefinition {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
TarArchiveUtil.tarPutFile(output, CONTAINER_META_FILE, gson.toJson(containerMeta));
- TarArchiveUtil.tarPutFile(output, CONTAINER_FILE, containerDescription);
+ TarArchiveUtil.tarPutFile(output, CONTAINER_FILE, containerRecipe);
output.close();
containerDef = ByteBuffer.wrap(baos.toByteArray());
@@ -141,26 +150,36 @@ public class ContainerDefinition {
}
private String readContainerRecipe(File file) {
- byte[] rawFile = null;
+ String recipe = null;
try {
+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- rawFile = new byte[(int) file.length()];
- bis.read(rawFile);
+ ByteArrayOutputStream rawFile = new ByteArrayOutputStream();
+ int count;
+ byte[] data = new byte[1024];
+ while ((count = bis.read(data)) != -1) {
+ rawFile.write(data,0,count);
+ }
+
+ String rawRecipe = new String(rawFile.toByteArray(), StandardCharsets.UTF_8);
+
+ // replace windows by unix EOL
+ recipe = rawRecipe.replaceAll("\\r\\n", "\n");
} catch (IOException e) {
LOGGER.error("Could not read Container Recipe", e);
}
- return byteArrayToString(rawFile);
- }
-
- private String byteArrayToString(byte[] containerRecipe) {
- return new String(containerRecipe, StandardCharsets.UTF_8);
+ return recipe;
}
+ /**
+ * Saves containerRecipe and containerMeta at the provided location.
+ *
+ * @param destDir destination directory for containerRecipe and containerMeta.
+ */
public void saveLocal(File destDir) {
- writeFile(destDir, containerDescription, CONTAINER_FILE);
-
+ writeFile(destDir, containerRecipe, CONTAINER_FILE);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
writeFile(destDir, gson.toJson(containerMeta), CONTAINER_META_FILE);
}
@@ -178,17 +197,20 @@ public class ContainerDefinition {
}
}
+ public ContainerBuildContextMethod getBuildContextMethod() {
+ return ContainerBuildContextMethod.fromInt(containerMeta.getBuildContextMethod());
+ }
+
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ContainerDefinition that = (ContainerDefinition) o;
- return containerDescription.equals(that.containerDescription) && containerMeta.equals(
- that.containerMeta);
+ return containerRecipe.equals(that.containerRecipe) && containerMeta.equals(that.containerMeta);
}
@Override public int hashCode() {
- return Objects.hash(containerDescription, containerMeta);
+ return Objects.hash(containerRecipe, containerMeta);
}
}