summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java165
1 files changed, 165 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java
new file mode 100644
index 00000000..53b3e199
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/panel/ContainerPanel.java
@@ -0,0 +1,165 @@
+package org.openslx.dozmod.gui.panel;
+
+import org.apache.log4j.Logger;
+import org.apache.thrift.TException;
+import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
+import org.openslx.dozmod.gui.Gui;
+import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor;
+import org.openslx.dozmod.gui.configurator.ContainerBindMountConfigurator;
+import org.openslx.dozmod.gui.control.QLabel;
+import org.openslx.dozmod.gui.helper.GridManager;
+import org.openslx.dozmod.gui.helper.I18n;
+import org.openslx.dozmod.model.ContainerBuildContextMethod;
+import org.openslx.dozmod.model.ContainerDefinition;
+import org.openslx.thrifthelper.ThriftManager;
+import org.openslx.util.ThriftUtil;
+
+import javax.swing.*;
+import java.awt.*;
+import java.nio.ByteBuffer;
+
+public class ContainerPanel extends JPanel {
+
+ // TODO Set the height of txtContainerRecipe dependent of the Context. Full height for IMAGE_CONTEXT.
+
+ public static final String IMAGE_CONTEXT = "IMAGE";
+ public static final String CONTAINER_CONTEXT = "CONTAINER";
+
+ private final Logger LOGGER = Logger.getLogger(ContainerBindMountConfigurator.class);
+
+ private final QLabel lblContainerRunOpt;
+ private final QLabel lblContainerImageName;
+
+ private final JTextArea txtContainerRecipe;
+ private final JTextField txtContainerRun;
+ private final JTextField txtContainerImageName;
+ private final ContainerBindMountConfigurator bindMountConfigurator;
+
+ private ContainerDefinition containerDefinition = null;
+
+ public ContainerPanel() {
+
+ JPanel pnlContainerMeta = new JPanel();
+ GridManager grdContainerMeta = new GridManager(pnlContainerMeta, 3);
+
+ lblContainerRunOpt = new QLabel(
+ I18n.WINDOW_LAYOUT.getString("ImageDetails.Label.ContainerRunOptions.text"));
+ grdContainerMeta.add(lblContainerRunOpt);
+ txtContainerRun = new JTextField();
+ grdContainerMeta.add(txtContainerRun, 2).fill(true, false).expand(true, false);
+ grdContainerMeta.nextRow();
+
+ bindMountConfigurator = new ContainerBindMountConfigurator();
+ grdContainerMeta.add(bindMountConfigurator, 3).fill(true, true).expand(true, true);
+ grdContainerMeta.finish(true);
+
+ GridManager grdContainer = new GridManager(this, 2, false, new Insets(8, 2, 8, 2));
+
+ lblContainerImageName = new QLabel(I18n.WINDOW_LAYOUT.getString("ImageDetails.Label.ImageName.text"));
+ grdContainer.add(lblContainerImageName);
+ txtContainerImageName = new JTextField();
+ grdContainer.add(txtContainerImageName, 1).fill(true, false).expand(true, false);
+ grdContainer.nextRow();
+
+ txtContainerRecipe = new JTextArea();
+ JScrollPane scrollableTextArea = new JScrollPane(txtContainerRecipe);
+ scrollableTextArea.setMinimumSize(Gui.getScaledDimension(0, 200));
+ scrollableTextArea.setPreferredSize(Gui.getScaledDimension(0, 200));
+ grdContainer.add(scrollableTextArea, 2).fill(true, true).expand(true, true);
+ grdContainer.add(pnlContainerMeta, 2).fill(true, true).expand(true, true);
+ grdContainer.finish(true);
+
+ }
+
+ /**
+ * Retrieves Container specific details for the currently displayed lecture and disables gui elements for
+ * the specific context.
+ *
+ * @param satelliteToken The satelliteToken from which the information are retrieved.
+ * @param image The ImageDetailsRead which has general information about Image (name, type, version, etc.)
+ * @param context In which context this Panel ist used (Image or Lecture)
+ */
+ public void init(String satelliteToken, ImageDetailsRead image, String context) {
+
+ try {
+ byte[] rawVirtConfig;
+ ByteBuffer byteBuffer = ThriftManager.getSatClient()
+ .getImageVersionVirtConfig(satelliteToken, image.getLatestVersionId());
+ rawVirtConfig = ThriftUtil.unwrapByteBuffer(byteBuffer);
+ containerDefinition = ContainerDefinition.fromByteArray(rawVirtConfig);
+ } catch (TException e) {
+ LOGGER.error("Failed to retrieve virtualizer config for image version " + "'"
+ + image.getLatestVersionId() + ", see trace: ", e);
+ }
+
+ txtContainerImageName.setText(image.imageName);
+ txtContainerImageName.setEnabled(false);
+ // TODO simplify this mess. ContainerBuildContextMethod is to complex
+ if (containerDefinition.getBuildContextMethod() == ContainerBuildContextMethod.FILE) {
+ txtContainerRecipe.setText(containerDefinition.getContainerRecipe());
+ } else if (containerDefinition.getBuildContextMethod()
+ == ContainerBuildContextMethod.GIT_REPOSITORY) {
+ txtContainerRecipe.setText(containerDefinition.getContainerMeta().getBuildContextUrl());
+ }
+
+ if (context.equals(IMAGE_CONTEXT)) {
+ initImageDetails();
+ } else if (context.equals(CONTAINER_CONTEXT)) {
+ initContainerDetails();
+ } else {
+ LOGGER.error("Container Panel init failed: Please use proper context");
+ }
+
+ }
+
+ private void initImageDetails() {
+
+ // currently do not allow user to change the Dockerfile in the suite.
+ txtContainerRecipe.setEnabled(false);
+ lblContainerRunOpt.setVisible(false);
+ txtContainerRun.setVisible(false);
+ bindMountConfigurator.setVisible(false);
+ }
+
+ private void initContainerDetails() {
+
+ txtContainerRecipe.setEnabled(false);
+ txtContainerRun.setText(containerDefinition.getContainerMeta().getRunOptions());
+ bindMountConfigurator.setData(containerDefinition.getContainerMeta().getBindMountConfig());
+ }
+
+ public void addToChangeMonitor(DialogChangeMonitor changeMonitor) {
+ changeMonitor.add(txtContainerRecipe);
+ changeMonitor.add(txtContainerRun);
+ changeMonitor.add(bindMountConfigurator);
+ }
+
+ /**
+ * Stores changes in ContainerDefinition and Uploads changes to Sat.
+ *
+ * @param satelliteToken for current session to communicate with Sat.
+ * @param image of the image version, which will be updated.
+ * @return Returns true uf upload successfully finished, false if error occurred.
+ */
+ public boolean saveChanges(String satelliteToken, ImageDetailsRead image) {
+
+ ContainerDefinition newConDev = new ContainerDefinition(containerDefinition);
+
+ newConDev.getContainerMeta().setRunOptions(txtContainerRun.getText());
+ newConDev.getContainerMeta().setBindMountConfig(bindMountConfigurator.getData());
+
+ // TODO do I really need this check? Maybe just get every setting and upload it.
+ if (!newConDev.equals(containerDefinition)) {
+ try {
+ ThriftManager.getSatClient()
+ .setImageVersionVirtConfig(satelliteToken, image.getLatestVersionId(),
+ newConDev.toByteBuffer());
+ } catch (TException e) {
+ LOGGER.error("Upload new ContainerDefinition failed", e);
+ return false;
+ }
+ }
+ return true;
+ }
+}
+