diff options
| author | ralph isenmann | 2020-09-16 16:06:28 +0200 |
|---|---|---|
| committer | ralph isenmann | 2020-09-16 16:06:28 +0200 |
| commit | 50334a6117f8cc77a04e98468d963552e003e6ca (patch) | |
| tree | b73c242eb67e3b4bd7bf96ad4026d4bbc4822fdb | |
| parent | [client] Check for same ContainerDefinition (diff) | |
| download | tutor-module-50334a6117f8cc77a04e98468d963552e003e6ca.tar.gz tutor-module-50334a6117f8cc77a04e98468d963552e003e6ca.tar.xz tutor-module-50334a6117f8cc77a04e98468d963552e003e6ca.zip | |
[client] Refactoring
- extract ContainerMeta ContainerDefinition
- create TarArchiveUtil
5 files changed, 251 insertions, 240 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java index c0364d00..b1378852 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java @@ -2,13 +2,9 @@ package org.openslx.dozmod.gui.window; import java.awt.*; import java.awt.event.*; -import java.io.*; import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; import java.util.*; import java.util.List; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; import javax.swing.AbstractAction; import javax.swing.JComponent; @@ -19,15 +15,8 @@ import javax.swing.KeyStroke; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.stream.JsonReader; import org.apache.log4j.Logger; import org.apache.thrift.TException; -import org.kamranzafar.jtar.TarEntry; -import org.kamranzafar.jtar.TarHeader; -import org.kamranzafar.jtar.TarInputStream; -import org.kamranzafar.jtar.TarOutputStream; import org.openslx.bwlp.thrift.iface.ImageBaseWrite; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; import org.openslx.bwlp.thrift.iface.ImagePermissions; @@ -50,7 +39,7 @@ import org.openslx.dozmod.gui.window.UserListWindow.UserAddedCallback; import org.openslx.dozmod.gui.window.layout.ImageDetailsWindowLayout; import org.openslx.dozmod.gui.wizard.ImageUpdateWizard; import org.openslx.dozmod.gui.wizard.LectureWizard; -import org.openslx.dozmod.gui.wizard.page.DockerfileUploadPage; +import org.openslx.dozmod.model.ContainerDefinition; import org.openslx.dozmod.permissions.ImagePerms; import org.openslx.dozmod.thrift.ImageDetailsActions; import org.openslx.dozmod.thrift.Session; @@ -433,131 +422,7 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe }); } - public static class ContainerDefinition { - /** - * The file to construct a real container, could be an dockerfile or a singularity recipe. - */ - public String containerDescription; - - /** - * Further container information. - */ - public DockerfileUploadPage.ContainerMeta containerMeta; - - public ContainerDefinition() { - containerMeta = new DockerfileUploadPage.ContainerMeta(); - } - - public String getDescription() { - return containerDescription; - } - public void setDescription(String description) { - containerDescription = description; - } - - - public void setContainerDescription(byte[] containerDescription) { - this.containerDescription = new String(containerDescription, StandardCharsets.UTF_8); - } - - public void setContainerMeta(byte[] containerMeta) { - Gson gson = new GsonBuilder().create(); - this.containerMeta = gson.fromJson(new JsonReader(new InputStreamReader(new ByteArrayInputStream(containerMeta), - StandardCharsets.UTF_8)), DockerfileUploadPage.ContainerMeta.class); - } - - public DockerfileUploadPage.ContainerMeta getContainerMeta() { - return containerMeta; - } - - - public static ContainerDefinition fromByteArray(byte[] rawTarData) { - - ContainerDefinition containerDef = new ContainerDefinition(); - - try { - TarInputStream tis = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(rawTarData)))); - - TarEntry entry; - - while((entry = tis.getNextEntry()) != null) { - int size = (int)entry.getSize(); - byte[] rawData = new byte[size]; - tis.read(rawData,0,size); - - if (entry.getName().equals("dockerfile")) - containerDef.setContainerDescription(rawData); - if (entry.getName().equals("container_meta.json")) - containerDef.setContainerMeta(rawData); - } - - } catch (IOException e) { - e.printStackTrace(); - } - - return containerDef; - } - - /** - * Serializes the ContainerMeta and Container Description (e.g. dockerfile) into an tar.gz archive. - * - * @return A ByteBuffer object of the container definition. Can be uploaded so satellite server. - */ - public ByteBuffer toByteBuffer() { - - ByteBuffer containerDef = null; - - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - TarOutputStream output = new TarOutputStream(new GZIPOutputStream(baos)); - - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - tarPutFile(output,"container_meta.json", gson.toJson(containerMeta)); - tarPutFile(output,"dockerfile", containerDescription); - - output.close(); - - containerDef = ByteBuffer.wrap(baos.toByteArray()); - - } catch (IOException e) { - LOGGER.warn("Error writing to tar file", e); - } - - return containerDef; - } - - @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); - } - - @Override public int hashCode() { - return Objects.hash(containerDescription, containerMeta); - } - } - - private static void tarPutFile(TarOutputStream output, String fileName, String data) throws IOException - { - if (data == null) - return; - tarPutFile(output, fileName, data.getBytes(StandardCharsets.UTF_8)); - } - - private static void tarPutFile(TarOutputStream output, String fileName, byte[] data) throws IOException - { - if (data == null) - return; - output.putNextEntry(new TarEntry( - TarHeader.createHeader(fileName, data.length, Util.unixTime(), false, 0644))); - output.write(data); - } /** * Uploads ByteBuffer data to satellite server and stores it in ImageVersion.VirtConfig @@ -787,7 +652,6 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe conDev.getContainerMeta().setUserMountPath(txtContainerMountUserDir.getText()); if(!conDev.equals(containerDefinition)) { - // TODO do this only if the containerDefinition has changed. uploadContainerDef(image.versions.get(0).versionId,conDev.toByteBuffer()); LOGGER.info("Upload new DockerDefinition"); } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java index a922347e..4cf21439 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java @@ -4,8 +4,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; -import org.kamranzafar.jtar.TarEntry; -import org.kamranzafar.jtar.TarHeader; import org.kamranzafar.jtar.TarOutputStream; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; import org.openslx.dozmod.Config; @@ -13,18 +11,17 @@ import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.helper.*; import org.openslx.dozmod.gui.wizard.Wizard; import org.openslx.dozmod.gui.wizard.layout.ContainerUploadPageLayout; +import org.openslx.dozmod.model.ContainerMeta; import org.openslx.dozmod.state.UploadWizardState; import org.openslx.dozmod.thrift.*; import org.openslx.dozmod.thrift.cache.MetaDataCache; -import org.openslx.util.Util; +import org.openslx.dozmod.util.TarArchiveUtil; import org.openslx.util.vm.DockerMetaDataDummy; import javax.swing.*; import javax.swing.filechooser.FileFilter; import java.awt.event.*; import java.io.*; -import java.nio.charset.StandardCharsets; -import java.util.Objects; import java.util.zip.GZIPOutputStream; public class DockerfileUploadPage extends ContainerUploadPageLayout { @@ -168,88 +165,6 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { } } - /** - * ContainerMeta is used to store container specific information. - * An object of this class will be serialized with gson to a json file. - */ - public static class ContainerMeta { - - - private String build_context_url; - private String image_name; - private String run_options; - private Boolean mount_user_dir; - - private String user_mount_path; - - public ContainerMeta() { - build_context_url = ""; - image_name = ""; - run_options = ""; - mount_user_dir = false; - user_mount_path = ""; - } - - public String getBuildContextUrl() { - return build_context_url; - } - - public void setBuildContextUrl(String buildContextUrl) { - this.build_context_url = build_context_url; - } - - public String getRunOptions() { - return run_options; - } - - public void setRunOptions(String run_options) { - this.run_options = run_options; - } - - public String getImageName() { - return image_name; - } - - public void setImageName(String image_name) { - this.image_name = image_name; - } - - // TODO (Ralph) i dont like that naming - public Boolean mountUserDir() { - return mount_user_dir; - } - - public void mountUserDir(Boolean mountUserDir) { - this.mount_user_dir = mountUserDir; - } - - public String getUserMountPath() { - return user_mount_path; - } - - public void setUserMountPath(String userMountPath) { - user_mount_path = userMountPath; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - ContainerMeta that = (ContainerMeta) o; - return Objects.equals(build_context_url, that.build_context_url) && Objects.equals(image_name, - that.image_name) && Objects.equals(run_options, that.run_options) && Objects.equals( - mount_user_dir, that.mount_user_dir) && Objects.equals(user_mount_path, - that.user_mount_path); - } - - @Override - public int hashCode() { - return Objects.hash(build_context_url, image_name, run_options, mount_user_dir, user_mount_path); - } - } - private void reactOnUserInput() { boolean completed = true; @@ -289,8 +204,8 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { bis.read(data); Gson gson = new GsonBuilder().setPrettyPrinting().create(); - tarPutFile(output,"container_meta.json",gson.toJson(containerMeta)); - tarPutFile(output,"dockerfile", data); + TarArchiveUtil.tarPutFile(output,"container_meta.json",gson.toJson(containerMeta)); + TarArchiveUtil.tarPutFile(output,"dockerfile", data); output.close(); @@ -300,20 +215,6 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { } } - private static void tarPutFile(TarOutputStream output, String fileName, String data) throws IOException - { - if (data == null) - return; - tarPutFile(output, fileName, data.getBytes(StandardCharsets.UTF_8)); - } - - private static void tarPutFile(TarOutputStream output, String fileName, byte[] data) throws IOException - { - if (data == null) - return; - output.putNextEntry(new TarEntry(TarHeader.createHeader(fileName, data.length, Util.unixTime(), false, 0644))); - output.write(data); - } @Override protected boolean wantNextOrFinish() { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java new file mode 100644 index 00000000..565b8833 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java @@ -0,0 +1,129 @@ +package org.openslx.dozmod.model; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.stream.JsonReader; +import org.apache.log4j.Logger; +import org.kamranzafar.jtar.TarEntry; +import org.kamranzafar.jtar.TarInputStream; +import org.kamranzafar.jtar.TarOutputStream; +import org.openslx.dozmod.util.TarArchiveUtil; + + +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Objects; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +public class ContainerDefinition { + + private static final Logger LOGGER = Logger.getLogger(ContainerDefinition.class) ; + /** + * The file to construct a real container, could be an dockerfile or a singularity recipe. + */ + public String containerDescription; + + /** + * Further container information. + */ + public ContainerMeta containerMeta; + + public ContainerDefinition() { + containerMeta = new ContainerMeta(); + } + + public String getDescription() { + return containerDescription; + } + public void setDescription(String description) { + containerDescription = description; + } + + + public void setContainerDescription(byte[] containerDescription) { + this.containerDescription = new String(containerDescription, StandardCharsets.UTF_8); + } + + public void setContainerMeta(byte[] containerMeta) { + Gson gson = new GsonBuilder().create(); + this.containerMeta = gson.fromJson(new JsonReader(new InputStreamReader(new ByteArrayInputStream(containerMeta), + StandardCharsets.UTF_8)), ContainerMeta.class); + } + + public ContainerMeta getContainerMeta() { + return containerMeta; + } + + + public static ContainerDefinition fromByteArray(byte[] rawTarData) { + + ContainerDefinition containerDef = new ContainerDefinition(); + + try { + TarInputStream tis = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(rawTarData)))); + + TarEntry entry; + + while((entry = tis.getNextEntry()) != null) { + int size = (int)entry.getSize(); + byte[] rawData = new byte[size]; + tis.read(rawData,0,size); + + if (entry.getName().equals("dockerfile")) + containerDef.setContainerDescription(rawData); + if (entry.getName().equals("container_meta.json")) + containerDef.setContainerMeta(rawData); + } + + } catch (IOException e) { + e.printStackTrace(); + } + + return containerDef; + } + + /** + * Serializes the ContainerMeta and Container Description (e.g. dockerfile) into an tar.gz archive. + * + * @return A ByteBuffer object of the container definition. Can be uploaded so satellite server. + */ + public ByteBuffer toByteBuffer() { + + ByteBuffer containerDef = null; + + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + TarOutputStream output = new TarOutputStream(new GZIPOutputStream(baos)); + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + TarArchiveUtil.tarPutFile(output,"container_meta.json", gson.toJson(containerMeta)); + TarArchiveUtil.tarPutFile(output,"dockerfile", containerDescription); + + output.close(); + + containerDef = ByteBuffer.wrap(baos.toByteArray()); + + } catch (IOException e) { + LOGGER.warn("Error writing to tar file", e); + } + + return containerDef; + } + + @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); + } + + @Override public int hashCode() { + return Objects.hash(containerDescription, containerMeta); + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java new file mode 100644 index 00000000..a5b84ad0 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java @@ -0,0 +1,87 @@ +package org.openslx.dozmod.model; + +import org.openslx.dozmod.gui.wizard.page.DockerfileUploadPage; + +import java.util.Objects; + +/** + * ContainerMeta is used to store container specific information. + * An object of this class will be serialized with gson to a json file. + */ +public class ContainerMeta { + + + private String build_context_url; + private String image_name; + private String run_options; + private Boolean mount_user_dir; + + private String user_mount_path; + + public ContainerMeta() { + build_context_url = ""; + image_name = ""; + run_options = ""; + mount_user_dir = false; + user_mount_path = ""; + } + + public String getBuildContextUrl() { + return build_context_url; + } + + public void setBuildContextUrl(String buildContextUrl) { + this.build_context_url = build_context_url; + } + + public String getRunOptions() { + return run_options; + } + + public void setRunOptions(String run_options) { + this.run_options = run_options; + } + + public String getImageName() { + return image_name; + } + + public void setImageName(String image_name) { + this.image_name = image_name; + } + + // TODO (Ralph) i dont like that naming + public Boolean mountUserDir() { + return mount_user_dir; + } + + public void mountUserDir(Boolean mountUserDir) { + this.mount_user_dir = mountUserDir; + } + + public String getUserMountPath() { + return user_mount_path; + } + + public void setUserMountPath(String userMountPath) { + user_mount_path = userMountPath; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + ContainerMeta that = (ContainerMeta) o; + return Objects.equals(build_context_url, that.build_context_url) && Objects.equals(image_name, + that.image_name) && Objects.equals(run_options, that.run_options) && Objects.equals( + mount_user_dir, that.mount_user_dir) && Objects.equals(user_mount_path, + that.user_mount_path); + } + + @Override + public int hashCode() { + return Objects.hash(build_context_url, image_name, run_options, mount_user_dir, user_mount_path); + } +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/TarArchiveUtil.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/TarArchiveUtil.java new file mode 100644 index 00000000..c1a282b6 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/TarArchiveUtil.java @@ -0,0 +1,30 @@ +package org.openslx.dozmod.util; + +import org.kamranzafar.jtar.TarEntry; +import org.kamranzafar.jtar.TarHeader; +import org.kamranzafar.jtar.TarOutputStream; +import org.openslx.util.Util; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public class TarArchiveUtil { + + + + public static void tarPutFile(TarOutputStream output, String fileName, String data) throws IOException + { + if (data == null) + return; + tarPutFile(output, fileName, data.getBytes(StandardCharsets.UTF_8)); + } + + public static void tarPutFile(TarOutputStream output, String fileName, byte[] data) throws IOException + { + if (data == null) + return; + output.putNextEntry(new TarEntry( + TarHeader.createHeader(fileName, data.length, Util.unixTime(), false, 0644))); + output.write(data); + } +} |
