diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java | 81 |
1 files changed, 57 insertions, 24 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java index 3d65ea5d..87f87131 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java @@ -14,10 +14,13 @@ import org.openslx.dozmod.gui.helper.GridManager; import org.openslx.dozmod.gui.helper.I18n; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.model.ContainerDefinition; +import org.openslx.dozmod.thrift.Session; +import org.openslx.dozmod.thrift.cache.ImageCache; import org.openslx.dozmod.thrift.cache.LectureCache; import org.openslx.thrifthelper.TConst; import org.openslx.thrifthelper.ThriftManager; import org.openslx.util.ThriftUtil; +import org.openslx.virtualization.configuration.container.ContainerMeta; import javax.swing.*; import java.awt.*; @@ -25,6 +28,7 @@ import java.io.*; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.ArrayList; import java.util.List; /** @@ -33,8 +37,10 @@ import java.util.List; public class ContainerUtils { private static final Logger LOGGER = Logger.getLogger(ContainerUtils.class); + /** - * Checks ImageBaseId of image if already linked with existing Lectures in LectureCache. + * Checks ImageBaseId of image if already linked with existing Lectures in + * LectureCache. * * @param image The image to check. * @return true if the images imageBaseId is already linked with a lecture. @@ -43,7 +49,8 @@ public class ContainerUtils { if (image != null && image.getVirtId().equals(TConst.VIRT_DOCKER)) { List<LectureSummary> lectureSummaries = LectureCache.get(true); for (LectureSummary lecture : lectureSummaries) { - // lecture.imageBaseId is null when no image linked to it -> NullPointer on equals + // lecture.imageBaseId is null when no image linked to it -> NullPointer on + // equals if (image.imageBaseId.equals(lecture.imageBaseId)) { return true; } @@ -52,6 +59,30 @@ public class ContainerUtils { return false; } + public static List<ImageSummaryRead> getDataContainerImages() { + String satelliteToken = Session.getSatelliteToken(); + List<ImageSummaryRead> images = ImageCache.get(true); + List<ImageSummaryRead> dataContainerImages = new ArrayList<>(); + for (ImageSummaryRead image : images) { + if (image.getVirtId().equals(TConst.VIRT_DOCKER)) { + try { + byte[] rawVirtConfig; + ByteBuffer byteBuffer = ThriftManager.getSatClient().getImageVersionVirtConfig(satelliteToken, + image.getLatestVersionId()); + rawVirtConfig = ThriftUtil.unwrapByteBuffer(byteBuffer); + ContainerDefinition containerDefinition = ContainerDefinition.fromByteArray(rawVirtConfig); + if (containerDefinition.getContainerMeta().getImageType() == ContainerMeta.ContainerImageType.DATA) + dataContainerImages.add(image); + + } catch (TException e) { + // LOGGER.error("Failed to retrieve virtualizer config for image version " + "'" + // + image.getLatestVersionId() + ", see trace: ", e); + } + } + } + return dataContainerImages; + } + public static void showWarning(Component c, Logger logger) { Gui.showMessageBox(c, I18n.WINDOW.getString("LectureDetails.Message.error.containerLinkedWithLecture"), MessageType.WARNING, logger, null); @@ -60,21 +91,21 @@ public class ContainerUtils { public static ContainerDefinition getContainerDefinition(String satelliteToken, String latestVersionId) { byte[] rawVirtConfig = null; try { - ByteBuffer byteBuffer = ThriftManager.getSatClient() - .getImageVersionVirtConfig(satelliteToken, latestVersionId); + ByteBuffer byteBuffer = ThriftManager.getSatClient().getImageVersionVirtConfig(satelliteToken, + latestVersionId); rawVirtConfig = ThriftUtil.unwrapByteBuffer(byteBuffer); } catch (TException e) { - LOGGER.error("Failed to retrieve virtualizer config for image version " + "'" - + latestVersionId + ", see trace: ", e); + LOGGER.error("Failed to retrieve virtualizer config for image version " + "'" + latestVersionId + + ", see trace: ", e); return null; } return ContainerDefinition.fromByteArray(rawVirtConfig); } /** - * Check if a provided tar file contains information about a single docker image. - * To check the validity of the file, the existence of two JSON files is checked - * and one of the files must only contain information for one image. + * Check if a provided tar file contains information about a single docker + * image. To check the validity of the file, the existence of two JSON files is + * checked and one of the files must only contain information for one image. * * The tar file have to be created by the 'docker save ...' command. * @@ -98,7 +129,7 @@ public class ContainerUtils { // because we want only to check manifest.json and repositories file tis.setDefaultSkip(true); while ((entry = tis.getNextEntry()) != null) { - if(!TarHeader.USTAR_MAGIC.equals(entry.getHeader().magic.toString())) + if (!TarHeader.USTAR_MAGIC.equals(entry.getHeader().magic.toString())) break; Arrays.fill(rawData, (byte) 0); output.reset(); @@ -109,8 +140,8 @@ public class ContainerUtils { while ((count = tis.read(rawData)) != -1) { output.write(rawData, 0, count); } - manifestJson = new JsonParser().parse( - new String(output.toByteArray(), StandardCharsets.UTF_8)).getAsJsonArray(); + manifestJson = new JsonParser().parse(new String(output.toByteArray(), StandardCharsets.UTF_8)) + .getAsJsonArray(); } if (entry.getName().equals("repositories")) { @@ -123,20 +154,15 @@ public class ContainerUtils { } tis.close(); // check the json files inside the tar file - if (containsManifest && containsRepositories && manifestJson.isJsonArray() - && manifestJson.size() == 1) { + if (containsManifest && containsRepositories && manifestJson.isJsonArray() && manifestJson.size() == 1) { isValid = true; String repoTag = manifestJson.get(0).getAsJsonObject().get("RepoTags").getAsString(); LOGGER.info(String.format("Tar File contains Docker Image with repoTag=%s", repoTag)); } else if (containsManifest && containsRepositories && manifestJson.isJsonArray() && manifestJson.size() > 1) { - Gui.showMessageBox("Tar File container more then one Images!", MessageType.ERROR, - LOGGER, - null); + Gui.showMessageBox("Tar File container more then one Images!", MessageType.ERROR, LOGGER, null); } else { - Gui.showMessageBox("No valid Tar File with Images provided!", MessageType.ERROR, - LOGGER, - null); + Gui.showMessageBox("No valid Tar File with Images provided!", MessageType.ERROR, LOGGER, null); } } catch (IOException e) { @@ -150,23 +176,30 @@ public class ContainerUtils { GridManager grid = new GridManager(pnlUserInfo, 1, true); JTextArea txtInfoText = new JTextArea(); - txtInfoText.setBorder(BorderFactory.createTitledBorder( - I18n.PAGE_LAYOUT.getString("Info"))); + txtInfoText.setBorder(BorderFactory.createTitledBorder(I18n.PAGE_LAYOUT.getString("Info"))); txtInfoText.setLineWrap(true); txtInfoText.setWrapStyleWord(true); txtInfoText.setEditable(false); txtInfoText.setFocusable(false); txtInfoText.setOpaque(false); txtInfoText.setText(infoText); - grid.add(txtInfoText).fill(true,true); + grid.add(txtInfoText).fill(true, true); grid.nextRow(); JTextField txtUserInfo = new JTextField(); txtUserInfo.setText(imageRepo); - grid.add(txtUserInfo).fill(true,true); + grid.add(txtUserInfo).fill(true, true); grid.finish(true); return pnlUserInfo; } + public static String getImageNameByBaseId(String imageBaseId) { + List<ImageSummaryRead> images = ImageCache.get(true); + for (ImageSummaryRead image : images) { + if (image.imageBaseId.equals(imageBaseId)) + return image.getImageName(); + } + return imageBaseId; + } } |