diff options
| author | ralph isenmann | 2020-10-02 14:33:31 +0200 |
|---|---|---|
| committer | ralph isenmann | 2020-10-02 14:33:31 +0200 |
| commit | 959bdb9b90e5fc9cb00ea75e90ec2338d4aa0009 (patch) | |
| tree | 013b764808d414184c0f245b59d8292f8a8b2560 /dozentenmodul/src/main/java/org | |
| parent | [client] update download behavior for container definitions (diff) | |
| download | tutor-module-959bdb9b90e5fc9cb00ea75e90ec2338d4aa0009.tar.gz tutor-module-959bdb9b90e5fc9cb00ea75e90ec2338d4aa0009.tar.xz tutor-module-959bdb9b90e5fc9cb00ea75e90ec2338d4aa0009.zip | |
[client] Allow User to use Git Repos as Input for a ContainerDefinition
- This allows user to create complex images with ressource files (COPY in dockerfile)
Diffstat (limited to 'dozentenmodul/src/main/java/org')
5 files changed, 174 insertions, 67 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java index 9b86089a..86034171 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageDetailsWindowLayout.java @@ -284,6 +284,7 @@ public abstract class ImageDetailsWindowLayout extends JDialog { ********************************************************************************/ pnlTabContainer = new JPanel(); txtContainerDescription = new JTextArea(); + JScrollPane scrollableTextArea = new JScrollPane(txtContainerDescription); JPanel pnlContainerMeta = new JPanel(); GridManager grdContainerMeta = new GridManager(pnlContainerMeta, 3, false, new Insets(8, 2, 8, 2)); @@ -306,7 +307,7 @@ public abstract class ImageDetailsWindowLayout extends JDialog { grdContainerMeta.add(txtContainerMountUserDir,1).fill(true,false); grdContainerMeta.finish(true); - JScrollPane scrollableTextArea = new JScrollPane(txtContainerDescription); + GridManager grdContainer = new GridManager(pnlTabContainer, 1, false, new Insets(8, 2, 8, 2)); grdContainer.add(scrollableTextArea,1).fill(true, true).expand(true,true); grdContainer.add(pnlContainerMeta,1).fill(true, false) diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/ContainerUploadPageLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/ContainerUploadPageLayout.java index 26425813..bbb85850 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/ContainerUploadPageLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/layout/ContainerUploadPageLayout.java @@ -6,7 +6,8 @@ import org.openslx.dozmod.gui.wizard.Wizard; import org.openslx.dozmod.gui.wizard.WizardPage; import javax.swing.*; -import java.awt.event.KeyEvent; +import java.awt.*; +import java.awt.event.*; public class ContainerUploadPageLayout extends WizardPage { @@ -19,29 +20,66 @@ public class ContainerUploadPageLayout extends WizardPage { protected final JTextField txtContainerRun; protected final QLabel lblContainerRun; - protected final QLabel lblMountUserDir; - protected final JCheckBox chkMountUserDir; + + protected final JTabbedPane tpInput; + protected final JTextField txtGitRepo; + + public enum BuildContextMethod { + FILE, GIT_REPOSITORY; + } // TODO set proper Text labels + /** * Page for uploading an imagefile */ public ContainerUploadPageLayout(Wizard wizard) { super(wizard, "Neue VM anlegen"); - setDescription("Bitte wählen Sie eine Virtuelle Maschine zum Hochladen aus."); + setDescription("Please provide an Input for a Docker Image"); GridManager grid = new GridManager(this, 3, false); // -- Browse for VM -- - QLabel imageFileCaption = new QLabel("Virtuelle Maschine"); + + JPanel p1 = new JPanel(); + p1.setVisible(true); + GridManager g1 = new GridManager(p1, 3, true, new Insets(5, 0, 5, 0)); + QLabel imageFileCaption = new QLabel("Docker File"); txtImageFile = new JTextField(); txtImageFile.setEditable(false); btnBrowseForImage = new JButton("Durchsuchen"); btnBrowseForImage.setMnemonic(KeyEvent.VK_B); - grid.add(imageFileCaption); - grid.add(txtImageFile).fill(true, false).expand(true, false); - grid.add(btnBrowseForImage); - grid.nextRow(); + g1.add(imageFileCaption); + g1.add(txtImageFile).fill(true, false).expand(true, false); + g1.add(btnBrowseForImage); + g1.finish(false); + + JPanel p2 = new JPanel(); + p2.setVisible(false); + GridManager g2 = new GridManager(p2, 2, true, new Insets(5, 0, 5, 0)); + QLabel lblGitRepo = new QLabel("Git Repository"); + txtGitRepo = new JTextField(); + g2.add(lblGitRepo); + g2.add(txtGitRepo).fill(true, false).expand(true, false); + g2.finish(false); + + JPanel p3 = new JPanel(); + p3.setVisible(false); + GridManager g3 = new GridManager(p3, 3, true, new Insets(5, 0, 5, 0)); + QLabel tmp = new QLabel("DUMMY"); + g3.add(tmp).fill(true, false).expand(true, false); + g3.finish(false); + + tpInput = new JTabbedPane(); + tpInput.addTab("Dockerfile", p1); + tpInput.addTab("Git Repository", p2); +// tpInput.addTab("Simple Input", p3); + tpInput.setSelectedIndex(BuildContextMethod.FILE.ordinal()); + + grid.add(tpInput, 3).fill(true, false); + + // Start as with Dockerfile as input! + tpInput.setSelectedIndex(0); lblImageName = new QLabel("Name"); txtImageName = new JTextField(); @@ -56,22 +94,16 @@ public class ContainerUploadPageLayout extends WizardPage { grid.add(chkLicenseRestricted, 2, 1).fill(false, false).expand(true, false); grid.nextRow(); - grid.add(new JSeparator(),3); + grid.add(new JSeparator(), 3); grid.nextRow(); lblContainerRun = new QLabel("Container Start Optionen"); lblContainerRun.setToolTipText("Geben Sie die Container Run Optionen an (Port, Name, Env,...)"); txtContainerRun = new JTextField(); - lblMountUserDir = new QLabel("Mount User Directory"); - chkMountUserDir = new JCheckBox(); grid.add(lblContainerRun); grid.add(txtContainerRun, 2, 1).fill(true, false).expand(true, false); - grid.add(lblMountUserDir); - grid.add(chkMountUserDir,2,1).fill(false, false).expand(true, false); - grid.nextRow(); - - + grid.nextRow(); grid.add(Box.createVerticalGlue(), 3).expand(true, true); @@ -86,7 +118,10 @@ public class ContainerUploadPageLayout extends WizardPage { grid.add(txtInfoText, 3).fill(true, false).expand(true, false); grid.nextRow(); - grid.finish(false); + grid.finish(true); } + protected BuildContextMethod getBuildContextMethod() { + return BuildContextMethod.values()[tpInput.getSelectedIndex()]; + } } 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 bc7a22e2..af025783 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 @@ -13,8 +13,11 @@ 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.vm.DockerMetaDataDummy; import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileFilter; import java.awt.event.*; import java.io.*; @@ -22,7 +25,6 @@ import java.io.*; public class DockerfileUploadPage extends ContainerUploadPageLayout { private final Logger LOGGER = Logger.getLogger(DockerfileUploadPage.class); - private final String ZERO_FILE = new String("ZERO-FILE"); /** * Page for uploading an imagefile @@ -31,7 +33,7 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { */ private final UploadWizardState state; private final ImageDetailsRead existingImage; - private final ContainerMeta containerMeta; + public DockerfileUploadPage(Wizard wizard, final UploadWizardState state_) { super(wizard); @@ -42,7 +44,7 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { // HACK set dummy os state.selectedOs = MetaDataCache.getOsById(18); - containerMeta = new ContainerMeta(); + init(); } @@ -53,8 +55,6 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { state = uploadWizardState; existingImage = imageDetailsRead; - containerMeta = new ContainerMeta(); - lblImageName.setEnabled(existingImage == null); txtImageName.setEnabled(existingImage == null); txtInfoText.setVisible(existingImage == null); @@ -94,9 +94,20 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { } }); + tpInput.addChangeListener(new ChangeListener() { + @Override public void stateChanged(ChangeEvent e) { + reactOnUserInput(); + } + }); + + txtGitRepo.getDocument().addDocumentListener(new TextChangeListener() { + @Override public void changed() { + reactOnUserInput(); + } + }); + btnBrowseForImage.requestFocus(); txtInfoText.setText("Many Text"); - } private void browseFile() { @@ -128,62 +139,90 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { LOGGER.info("Upload also an created Image"); state.diskFile = imageTarFile; } else { - // create a temp file with dummy content, - // because vmchooser relies on an existing image after choosing a lecture. - // TODO change behavior in vmchooser to allow start lectures without images. - try { - String configDir = Config.getPath(); - File zeroFile = new File(configDir, ZERO_FILE); - if (!zeroFile.exists()) { - zeroFile.createNewFile(); - FileUtils.writeStringToFile(zeroFile, "ZERO\n", "UTF-8"); - } - state.diskFile = zeroFile; - LOGGER.info("Use dummy image file"); - } catch (IOException e) { - e.printStackTrace(); - LOGGER.error("Could not create a dummy image file.",e); - } + state.diskFile = getDummyFile(); } Config.setUploadPath(file.getParent()); reactOnUserInput(); } - private class DockerfileFilter extends FileFilter { + private File getDummyFile() { + String configDir = Config.getPath(); + File zeroFile = new File(configDir, "ZERO-FILE"); + // create a temp file with dummy content, + // because vmchooser relies on an existing image after choosing a lecture. + // TODO change behavior in vmchooser to allow start lectures without images. + try { + if (!zeroFile.exists()) { + zeroFile.createNewFile(); + FileUtils.writeStringToFile(zeroFile, "ZERO\n", "UTF-8"); + } + } catch (IOException e) { + e.printStackTrace(); + LOGGER.error("Could not create a dummy file.", e); + } + LOGGER.info("Use dummy file"); + return zeroFile; + } - @Override public boolean accept(File f) { - boolean accept = true; - if (f.isFile() && !f.getName().equals("dockerfile")) - accept = false; - return accept; - } - @Override public String getDescription() { - return "Dockerfile"; - } + private void reactOnUserInput() { + boolean completed = checkUserInput(); + if (completed) + setDescription("Container definition finished"); + setPageComplete(completed); } - private void reactOnUserInput() { - boolean completed = true; + private boolean checkUserInput() { + BuildContextMethod method = getBuildContextMethod(); + + switch (method) { + case FILE: + if (txtImageFile.getText() == null || txtImageFile.getText().isEmpty()) { + setWarningMessage("No Container Recipe provided!"); + return false; + } + break; + case GIT_REPOSITORY: + if (txtGitRepo.getText() == null || txtGitRepo.getText().isEmpty()) { + setWarningMessage("No Git Repository provided!"); + return false; + } + break; + } - state.name = txtImageName.getText(); - containerMeta.setImageName(state.name); - if (state.name == null || state.name.isEmpty()) { - completed = false; + if (txtImageName.getText() == null || txtImageName.getText().isEmpty()) { setWarningMessage("Set proper Image Name"); + return false; } - containerMeta.setRunOptions(txtContainerRun.getText()); - if (containerMeta.getRunOptions() == null || containerMeta.getRunOptions().isEmpty()) { - completed = false; + if (txtContainerRun.getText() == null || txtContainerRun.getText().isEmpty()) { setWarningMessage("set container run options"); + return false; } + return true; + } - if (completed) - setDescription("Container definition finished"); - setPageComplete(completed); + private DockerMetaDataDummy createVmMeta() { + + ContainerDefinition cd = null; + ContainerMeta containerMeta = new ContainerMeta(); + containerMeta.setBuildContextMethod(getBuildContextMethod().ordinal()); + containerMeta.setImageName(txtImageName.getText()); + containerMeta.setRunOptions(txtContainerRun.getText()); + switch (getBuildContextMethod()) { + case FILE: + cd = new ContainerDefinition(state.descriptionFile, containerMeta); + break; + case GIT_REPOSITORY: + containerMeta.setBuildContextUrl(txtGitRepo.getText()); + cd = new ContainerDefinition(containerMeta); + state.diskFile = getDummyFile(); + state.descriptionFile = getDummyFile(); + break; + } + return cd.createVmMeta(); } @Override protected boolean wantNextOrFinish() { @@ -191,7 +230,7 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { // 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(); - state.meta = new ContainerDefinition(state.descriptionFile, containerMeta).createVmMeta(); + state.meta = createVmMeta(); // -- create image to get uuid -- if (existingImage == null) { @@ -225,4 +264,20 @@ public class DockerfileUploadPage extends ContainerUploadPageLayout { state.upload.startHashing(); return true; } + + + private static class DockerfileFilter extends FileFilter { + + @Override public boolean accept(File f) { + + boolean accept = true; + if (f.isFile() && !f.getName().equals("dockerfile")) + accept = false; + return accept; + } + + @Override public String getDescription() { + return "Dockerfile"; + } + } } 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 0b378ea6..e29d8d9c 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerDefinition.java @@ -39,6 +39,10 @@ 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. diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java index a5b84ad0..4522aab8 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/model/ContainerMeta.java @@ -1,6 +1,6 @@ package org.openslx.dozmod.model; -import org.openslx.dozmod.gui.wizard.page.DockerfileUploadPage; +import org.openslx.dozmod.gui.wizard.layout.ContainerUploadPageLayout; import java.util.Objects; @@ -10,7 +10,7 @@ import java.util.Objects; */ public class ContainerMeta { - + private int build_context_method; private String build_context_url; private String image_name; private String run_options; @@ -19,6 +19,8 @@ public class ContainerMeta { private String user_mount_path; public ContainerMeta() { + + build_context_method = ContainerUploadPageLayout.BuildContextMethod.FILE.ordinal(); build_context_url = ""; image_name = ""; run_options = ""; @@ -26,12 +28,21 @@ public class ContainerMeta { user_mount_path = ""; } + + public int getBuildContextMethod() { + return build_context_method; + } + + public void setBuildContextMethod(int buildContextMethod) { + this.build_context_method = buildContextMethod; + } + public String getBuildContextUrl() { return build_context_url; } public void setBuildContextUrl(String buildContextUrl) { - this.build_context_url = build_context_url; + this.build_context_url = buildContextUrl; } public String getRunOptions() { @@ -84,4 +95,5 @@ public class ContainerMeta { public int hashCode() { return Objects.hash(build_context_url, image_name, run_options, mount_user_dir, user_mount_path); } + } |
