summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/DockerfileUploadPage.java161
1 files changed, 137 insertions, 24 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 324b3112..a90329a1 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,25 +1,32 @@
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.TarEntry;
+import org.kamranzafar.jtar.TarHeader;
+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.ImageUploadPageLayout;
+import org.openslx.dozmod.gui.wizard.layout.ContainerUploadPageLayout;
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.util.vm.DockerMetaDataDummy;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.*;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.util.zip.GZIPOutputStream;
-public class DockerfileUploadPage extends ImageUploadPageLayout {
+public class DockerfileUploadPage extends ContainerUploadPageLayout {
private final static Logger LOGGER = Logger.getLogger(DockerfileUploadPage.class);
@@ -30,22 +37,31 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
*/
private final UploadWizardState state;
private final ImageDetailsRead existingImage;
+ private final ContainerMeta containerMeta;
- public DockerfileUploadPage(Wizard wizard, final UploadWizardState state) {
+ private File dockerfile;
+
+ public DockerfileUploadPage(Wizard wizard, final UploadWizardState state_) {
super(wizard);
- this.canComeBack = false;
- this.state = state;
+ canComeBack = false;
+ state = state_;
existingImage = null;
+ // HACK set dummy os
+ state.selectedOs = MetaDataCache.getOsById(18);
+ containerMeta = new ContainerMeta();
init();
}
- public DockerfileUploadPage(Wizard wizard, UploadWizardState state, ImageDetailsRead image) {
+ public DockerfileUploadPage(Wizard wizard, UploadWizardState state_, ImageDetailsRead image) {
super(wizard);
- this.state = state;
- this.existingImage = image;
+ state = state_;
+ existingImage = image;
+ // TODO Restore ContainerMeta to show Configuration (and Dockerfile) in ImageDetailsWindow,
+ // Cannot be done, because vmx not downloaded!
+ containerMeta = new ContainerMeta();
lblImageName.setEnabled(existingImage == null);
txtImageName.setEnabled(existingImage == null);
@@ -66,7 +82,6 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
}
private void init() {
-
this.txtImageFile.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
browseFile();
@@ -80,17 +95,18 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
txtImageName.getDocument().addDocumentListener(new TextChangeListener() {
@Override public void changed() {
- String imageName = txtImageName.getText();
- if (imageName.equals(""))
- setPageComplete(false);
- else
- setPageComplete(true);
- state.name = imageName;
+ reactOnUserInput();
+ }
+ });
+ txtContainerRun.getDocument().addDocumentListener(new TextChangeListener() {
+ @Override public void changed() {
+ reactOnUserInput();
}
});
btnBrowseForImage.requestFocus();
txtInfoText.setText("Many Text");
+
}
private void browseFile() {
@@ -114,11 +130,11 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
else
txtImageName.setText(existingImage.getImageName());
+ dockerfile = file;
state.descriptionFile = file;
// TESTING: Upload also a prematurely created image (tar)
String imageName = file.getParentFile().getName();
- String tarExt = ".tar";
- File imageTarFile = new File(file.getParentFile(),imageName.concat(tarExt));
+ File imageTarFile = new File(file.getParentFile(),imageName.concat(".tar"));
if(imageTarFile.exists()) {
LOGGER.info("Upload also an created Image");
state.diskFile = imageTarFile;
@@ -133,10 +149,8 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
}
}
- state.meta = new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(),file);
-
Config.setUploadPath(file.getParent());
- setPageComplete(true);
+ reactOnUserInput();
}
private class DockerfileFilter extends FileFilter {
@@ -154,12 +168,112 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
}
}
+ /**
+ * ContainerMeta is used to store container specific information.
+ * An Object of this class will be converted with gson to json file.
+ */
+ private class ContainerMeta {
+
+ private String image_name;
+ private String run_options;
+ private Boolean mount_user_dir;
+
+ 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;
+ }
+ public Boolean getMountUserDir() {
+ return mount_user_dir;
+ }
+
+ public void setMountUserDir(Boolean mountUserDir) {
+ this.mount_user_dir = mountUserDir;
+ }
+ }
+
+ private void reactOnUserInput() {
+ boolean completed = true;
+
+ state.name = txtImageName.getText();
+ containerMeta.setImageName(state.name);
+ if (state.name == null || state.name.isEmpty()) {
+ completed = false;
+ setWarningMessage("Set proper Image Name");
+ }
+
+ containerMeta.setRunOptions(txtContainerRun.getText());
+ if (containerMeta.getRunOptions() == null || containerMeta.getRunOptions().isEmpty()) {
+ completed = false;
+ setWarningMessage("set container run options");
+ }
+
+ if (completed)
+ setDescription("Container definition finished");
+ setPageComplete(completed);
+ }
+
+ /**
+ * This Method takes the user provided dockerfile and a ContainerMeta object
+ * and packages those in temporally .tar.gz-file.
+ * This file is then attached to a DockerMetaDataDummy object as VMX.
+ * This ensures that dockerfile and ContainerMeta are kept in the server and made available to clients when a lecture
+ * starts with attached containers.
+ */
+ 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();
+ tarPutFile(output,"container_meta.json",gson.toJson(containerMeta));
+ tarPutFile(output,"dockerfile", data);
+
+ output.close();
+
+ state.meta = new DockerMetaDataDummy(MetaDataCache.getOperatingSystems(),tarFile);
+ } catch (IOException e) {
+ LOGGER.warn("Error writing to tar file", e);
+ }
+ }
+
+ 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() {
// 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();
// -- create image to get uuid --
// if (existingImage == null) {
if (state.uuid == null) {
@@ -191,6 +305,5 @@ public class DockerfileUploadPage extends ImageUploadPageLayout {
// Start the hash check now
state.upload.startHashing();
return true;
-
}
}