From 0dc7e9a7d4278283ef965f03ddcdab8d933ad1dc Mon Sep 17 00:00:00 2001 From: ralph isenmann Date: Thu, 24 Jun 2021 13:49:42 +0200 Subject: [server] Add URL path /bwlp/container/clusterimages to retrieve information about registered container images --- .../openslx/bwlp/sat/database/mappers/DbImage.java | 85 +++++++++++++++++----- .../java/org/openslx/bwlp/sat/web/WebServer.java | 18 +++++ 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index cbaae1ab..bac3ad6c 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -1,6 +1,6 @@ package org.openslx.bwlp.sat.database.mappers; -import java.io.File; +import java.io.*; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -37,6 +37,10 @@ import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; import org.openslx.util.Util; +// master-sync-shared +import org.openslx.virtualization.configuration.container.ContainerDefinition; +import org.openslx.virtualization.configuration.container.ContainerMeta; + public class DbImage { private static final Logger LOGGER = Logger.getLogger(DbImage.class); @@ -44,7 +48,7 @@ public class DbImage { /** * Get list of all images visible to the given user, optionally filtered by * the given list of tags. - * + * * @param user Instance of {@link UserInfo} representing the user in * question * @param tagSearch list of tags an image must have to be included in the @@ -202,7 +206,7 @@ public class DbImage { /** * Private helper to create an {@link ImageSummaryRead} instance from a * {@link ResultSet} - * + * * @param rs * @return * @throws SQLException @@ -229,7 +233,7 @@ public class DbImage { /** * Get summary about an image by its base id. - * + * * @param user * @param imageBaseId * @return @@ -272,7 +276,7 @@ public class DbImage { protected static List getImageVersions(MysqlConnection connection, String imageBaseId, UserInfo user) throws SQLException { List versionList = new ArrayList<>(); - MysqlStatement stmt = null; + MysqlStatement stmt = null; if (user != null && user.role == Role.STUDENT) { stmt = connection.prepareStatement("SELECT" + " imageversionid, createtime, expiretime, filesize," @@ -308,7 +312,7 @@ public class DbImage { /** * Create new row in the imagebase table. - * + * * @param user the user the image will belong to * @param imageName name of the image to be created * @return UUID of the newly created image @@ -340,7 +344,7 @@ public class DbImage { /** * Create or update a base image with the given publish data. * Used for replication from master server. - * + * * @param user The user who triggered the download, and will be considered * the creator; if null, the creator of the image will be used * @param image The image to create @@ -445,7 +449,7 @@ public class DbImage { /** * Get the UUID of the image base belonging to the given image version UUID. * Returns null if the UUID does not exist. - * + * * @param imageVersionId * @return * @throws SQLException @@ -462,7 +466,7 @@ public class DbImage { /** * Get the UUID of the image base belonging to the given image version UUID. - * + * * @param imageVersionId * @return * @throws SQLException @@ -485,7 +489,7 @@ public class DbImage { /** * Update meta data of a specific image version. - * + * * @param user user doing the edit * @param imageVersionId UUID of image version * @param image meta data to set @@ -525,7 +529,7 @@ public class DbImage { * setting the expire timestamp to the past, and by setting the * image disabled and invalid. Next time the cleanup task runs, the image * will be deleted. - * + * * @param imageVersionId UUID of image version to delete * @throws SQLException * @throws TNotFoundException @@ -657,7 +661,7 @@ public class DbImage { /** * Set validity of given image versions. Returns list of images where the * validity actually changed. - * + * * @param connection * @param valid * @param imageVersion @@ -767,7 +771,7 @@ public class DbImage { /** * Makes sure the latestVersionId-field of the given base image is * consistent, while also updating any affected lectures. - * + * * @param connection mysql connection to use * @param changingImageVersionId the version id of the image that changed * (REQUIRED) @@ -830,7 +834,7 @@ public class DbImage { * Set the latest version id of the given base image. Returns true if and * only if the latest version id of the base image did actually change * through this call. - * + * * @param connection mysql connection to use * @param imageBaseId base id of image in question * @param newLatest image version that is to become the latest version, or @@ -891,7 +895,7 @@ public class DbImage { /** * Get all images with mussing virtid or osid. - * + * * @return * @throws SQLException */ @@ -982,7 +986,7 @@ public class DbImage { throw e; } } - + public static void setVirtualizerConfig(String imageVersionId, byte[] machineDescription) throws SQLException, TNotFoundException { if (imageVersionId == null || machineDescription == null || machineDescription.length == 0) @@ -1066,9 +1070,9 @@ public class DbImage { /** * Reset all image versions where the server decided that they should be * deleted to the 'keep' state. - * + * * @return list of version ids that were reset - * + * * @throws SQLException */ public static Set resetDeleteState() throws SQLException { @@ -1128,4 +1132,49 @@ public class DbImage { } } + + public static List getContainerImageCluster () throws SQLException { + + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement( + "SELECT ib.imagebaseid, iv.filepath, iv.filesize, iv.virtualizerconfig, u.firstname, u.lastname" + + " FROM imagebase AS ib" + + " JOIN imageversion AS iv ON (ib.imagebaseid=iv.imagebaseid AND ib.virtid = 'docker' AND ib.latestversionid IS NOT NULL)" + + " JOIN user as u ON ib.ownerid = u.userid"); + ResultSet rs = stmt.executeQuery(); + List result = new ArrayList<>(); + while (rs.next()) { + ContainerDefinition condev = ContainerDefinition.fromByteArray(rs.getBytes("iv.virtualizerconfig")); + if (condev.getContainerMeta().getImageType() == ContainerMeta.ContainerImageType.LECTURE) + continue; + ContainerImages entry = new ContainerImages( + rs.getString("u.firstname"), + rs.getString("u.lastname"), + condev.getContainerMeta().getImageRepo(), + condev.getContainerMeta().getImageType().name() + ); + result.add(entry); + } + return result; + } catch (Exception e) { + LOGGER.error("Query failed in DbImage.getContainerImages()", e); + throw e; + } + } + + static class ContainerImages { + public final String owner_firstname; + public final String owner_lastname; + public final String image; + public final String image_type; + + public ContainerImages(String owner_firstname, String owner_lastname, String image, String image_type) { + this.owner_firstname = owner_firstname; + this.owner_lastname = owner_lastname; + this.image = image; + this.image_type = image_type; + } + } + + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java index e9394b5e..9840a4c2 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java @@ -20,6 +20,7 @@ import org.apache.log4j.Logger; import org.kamranzafar.jtar.TarEntry; import org.kamranzafar.jtar.TarHeader; import org.kamranzafar.jtar.TarOutputStream; +import org.openslx.bwlp.sat.database.mappers.DbImage; import org.openslx.bwlp.sat.database.mappers.DbLecture; import org.openslx.bwlp.sat.database.mappers.DbLecture.LaunchData; import org.openslx.bwlp.sat.database.mappers.DbLecture.RunScript; @@ -94,6 +95,10 @@ public class WebServer extends NanoHTTPD { } return notFound(); } + if (uri.startsWith("/bwlp/container/clusterimages")) { + return serverContainerImages(); + } + if (uri.startsWith("/status/fileserver")) { return serveStatus(); } @@ -291,4 +296,17 @@ public class WebServer extends NanoHTTPD { } return new NanoHTTPD.Response(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", message); } + + /** + * create a json response with information about existing container images in bwlehrpool + */ + private Response serverContainerImages () { + try { + return new Response(Response.Status.OK, "application/json; charset=utf-8", + Json.serialize(DbImage.getContainerImageCluster())); + } catch (SQLException e) { + LOGGER.error("error -- could not server container images", e); + return internalServerError(); + } + } } -- cgit v1.2.3-55-g7522