diff options
| author | ralph isenmann | 2020-09-17 16:18:05 +0200 |
|---|---|---|
| committer | ralph isenmann | 2020-09-17 16:18:05 +0200 |
| commit | e4b034267e115bc963ffa04825f754a70312779a (patch) | |
| tree | e8b1895651abea5403bebc14ec97279a11c4158a /dozentenmodul/src/main/java/org/openslx | |
| parent | [client] Refactoring (diff) | |
| download | tutor-module-e4b034267e115bc963ffa04825f754a70312779a.tar.gz tutor-module-e4b034267e115bc963ffa04825f754a70312779a.tar.xz tutor-module-e4b034267e115bc963ffa04825f754a70312779a.zip | |
[client] more refactoring
- tar.gz archive creation belongs in ContainerDefinition
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx')
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java | 46 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java | 64 |
2 files changed, 61 insertions, 49 deletions
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 4cf21439..4ae9e7c4 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 @@ -1,28 +1,23 @@ package org.openslx.dozmod.gui.wizard.page; -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.TarOutputStream; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; import org.openslx.dozmod.Config; 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.ContainerDefinition; 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.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.util.zip.GZIPOutputStream; public class DockerfileUploadPage extends ContainerUploadPageLayout { @@ -37,8 +32,6 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { private final ImageDetailsRead existingImage; private final ContainerMeta containerMeta; - private File dockerfile; - public DockerfileUploadPage(Wizard wizard, final UploadWizardState state_) { super(wizard); @@ -58,8 +51,7 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { state = uploadWizardState; existingImage = imageDetailsRead; - // TODO Restore ContainerMeta to show Configuration (and Dockerfile) in ImageDetailsWindow, - // Cannot be done, because vmx not downloaded! + containerMeta = new ContainerMeta(); lblImageName.setEnabled(existingImage == null); @@ -127,20 +119,19 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { else txtImageName.setText(existingImage.getImageName()); - dockerfile = file; state.descriptionFile = file; // TESTING: Upload also a prematurely created image (tar) String imageName = file.getParentFile().getName(); - File imageTarFile = new File(file.getParentFile(),imageName.concat(".tar")); - if(imageTarFile.exists()) { + File imageTarFile = new File(file.getParentFile(), imageName.concat(".tar")); + if (imageTarFile.exists()) { LOGGER.info("Upload also an created Image"); state.diskFile = imageTarFile; } else { try { // create a temp file with dummy content. - File tmpFile = File.createTempFile("tmp","img"); - FileUtils.writeStringToFile(tmpFile,"ZERO\n","UTF-8"); - state.diskFile = tmpFile; // + File tmpFile = File.createTempFile("tmp", "img"); + FileUtils.writeStringToFile(tmpFile, "ZERO\n", "UTF-8"); + state.diskFile = tmpFile; } catch (IOException e) { e.printStackTrace(); } @@ -193,36 +184,19 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { * This ensures that dockerfile and ContainerMeta are kept in the server and made available to clients when a lecture * starts with attached containers. */ + // TODO redundant already implemented in ContainerDefinition private void createContainerInfo() { - try { - File tarFile = File.createTempFile("bwlp-container-info_",".tar.gz"); - TarOutputStream output = new TarOutputStream(new GZIPOutputStream(new FileOutputStream(tarFile))); - - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(dockerfile)); - byte[] data = new byte[(int) dockerfile.length()]; - bis.read(data); - - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - TarArchiveUtil.tarPutFile(output,"container_meta.json",gson.toJson(containerMeta)); - TarArchiveUtil.tarPutFile(output,"dockerfile", data); - - output.close(); - - state.meta = new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(),tarFile); - } catch (IOException e) { - LOGGER.warn("Error writing to tar file", e); - } } - - @Override protected boolean wantNextOrFinish() { // are we creating a new image? then either: // get the image name either auto filled by VmwareMetaData or by user // get the image name from the image we are uploading a new version of state.name = existingImage != null ? existingImage.getImageName() : txtImageName.getText(); createContainerInfo(); + state.meta = new ContainerDefinition(state.descriptionFile, containerMeta).createVmMeta(); + // -- create image to get uuid -- // if (existingImage == null) { if (state.uuid == null) { 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 565b8833..7784ce86 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java @@ -7,8 +7,9 @@ import org.apache.log4j.Logger; import org.kamranzafar.jtar.TarEntry; import org.kamranzafar.jtar.TarInputStream; import org.kamranzafar.jtar.TarOutputStream; +import org.openslx.dozmod.thrift.cache.MetaDataCache; import org.openslx.dozmod.util.TarArchiveUtil; - +import org.openslx.util.vm.DockerMetaDataDummy; import java.io.*; import java.nio.ByteBuffer; @@ -19,10 +20,10 @@ import java.util.zip.GZIPOutputStream; public class ContainerDefinition { - private static final Logger LOGGER = Logger.getLogger(ContainerDefinition.class) ; + 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; /** @@ -34,42 +35,62 @@ public class ContainerDefinition { containerMeta = new 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. + * + * @param file container recipe file + * @param containerMeta container meta object + */ + public ContainerDefinition(File file, ContainerMeta containerMeta) { + this.containerDescription = readContainerRecipe(file); + this.containerMeta = 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); + this.containerMeta = gson.fromJson(new JsonReader( + new InputStreamReader(new ByteArrayInputStream(containerMeta), StandardCharsets.UTF_8)), + ContainerMeta.class); } public ContainerMeta getContainerMeta() { return containerMeta; } + public DockerMetaDataDummy createVmMeta() { + + byte[] rawContainerDefinition = toByteBuffer().array(); + return new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(), rawContainerDefinition, + rawContainerDefinition.length); + } public static ContainerDefinition fromByteArray(byte[] rawTarData) { ContainerDefinition containerDef = new ContainerDefinition(); try { - TarInputStream tis = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(rawTarData)))); + TarInputStream tis = new TarInputStream( + new GZIPInputStream(new BufferedInputStream(new ByteArrayInputStream(rawTarData)))); TarEntry entry; - while((entry = tis.getNextEntry()) != null) { - int size = (int)entry.getSize(); + while ((entry = tis.getNextEntry()) != null) { + int size = (int) entry.getSize(); byte[] rawData = new byte[size]; - tis.read(rawData,0,size); + tis.read(rawData, 0, size); if (entry.getName().equals("dockerfile")) containerDef.setContainerDescription(rawData); @@ -99,20 +120,37 @@ public class ContainerDefinition { Gson gson = new GsonBuilder().setPrettyPrinting().create(); - TarArchiveUtil.tarPutFile(output,"container_meta.json", gson.toJson(containerMeta)); - TarArchiveUtil.tarPutFile(output,"dockerfile", containerDescription); + 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); + LOGGER.warn("Could not create a tar file", e); } return containerDef; } + private String readContainerRecipe(File file) { + byte[] rawFile = null; + try { + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + rawFile = new byte[(int) file.length()]; + bis.read(rawFile); + + } 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); + } + @Override public boolean equals(Object o) { if (this == o) return true; |
