From b0da27651d7a481f65806a0d0a28f5523a4061d4 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 2 Sep 2015 18:05:14 +0200 Subject: [server] Return machine description via http --- .../src/main/java/fi/iki/elonen/NanoHTTPD.java | 7 +--- .../bwlp/sat/database/mappers/DbLecture.java | 44 ++++++++++++++++++++-- .../openslx/bwlp/sat/web/VmChooserEntryXml.java | 5 +-- .../java/org/openslx/bwlp/sat/web/WebServer.java | 25 +++++++++++- 4 files changed, 66 insertions(+), 15 deletions(-) (limited to 'dozentenmodulserver/src/main/java') diff --git a/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java b/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java index 854f1686..f8e44fa8 100644 --- a/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/dozentenmodulserver/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -40,7 +40,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.PrintWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.UnsupportedEncodingException; @@ -52,9 +51,7 @@ import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -63,7 +60,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.StringTokenizer; -import java.util.TimeZone; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; @@ -72,7 +68,6 @@ import java.util.concurrent.TimeUnit; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.DateTimeFormatterBuilder; /** * A simple, tiny, nicely embeddable HTTP server in Java @@ -838,7 +833,7 @@ public abstract class NanoHTTPD implements Runnable { } catch (Exception e) { safeClose(inputStream); safeClose(outputStream); - throw new SocketException("NanoHttpd Shutdown"); + throw e; } if (read == -1) { // socket was been closed diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java index 1afcdb93..b49d3ae0 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java @@ -15,6 +15,7 @@ import org.openslx.bwlp.sat.database.MysqlStatement; import org.openslx.bwlp.sat.database.models.LocalImageVersion; import org.openslx.bwlp.sat.mail.MailGenerator; import org.openslx.bwlp.sat.permissions.User; +import org.openslx.bwlp.sat.thrift.cache.OperatingSystemList; import org.openslx.bwlp.sat.util.Json; import org.openslx.bwlp.sat.util.Util; import org.openslx.bwlp.sat.web.VmChooserEntryXml; @@ -25,6 +26,8 @@ import org.openslx.bwlp.thrift.iface.LectureWrite; import org.openslx.bwlp.thrift.iface.NetRule; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.bwlp.thrift.iface.UserInfo; +import org.openslx.util.vm.VmMetaData; +import org.openslx.util.vm.VmwareMetaData; import com.google.gson.JsonParseException; @@ -406,7 +409,7 @@ public class DbLecture { try (MysqlConnection connection = Database.getConnection()) { MysqlStatement stmt = connection.prepareStatement("SELECT" + " l.lectureid, l.displayname AS lecturename, l.description," - + " l.endtime, l.usecount, o.displayname AS osname, v.virtname, b.istemplate, i.filepath" + + " l.endtime, l.usecount, o.displayname AS osname, v.virtname, b.istemplate" + " FROM lecture l " + " INNER JOIN imageversion i USING (imageversionid)" + " INNER JOIN imagebase b USING (imagebaseid)" @@ -419,9 +422,9 @@ public class DbLecture { VmChooserListXml list = new VmChooserListXml(true); while (rs.next()) { String prio = rs.getBoolean("istemplate") ? "10" : "100"; - list.add(new VmChooserEntryXml(prio, rs.getString("filepath"), "-", - rs.getString("lecturename"), rs.getString("description"), rs.getString("lectureid"), - rs.getString("virtname"), rs.getString("osname"), "")); + list.add(new VmChooserEntryXml(prio, "-", rs.getString("lecturename"), + rs.getString("description"), rs.getString("lectureid"), rs.getString("virtname"), + rs.getString("osname"), "")); } return list; } catch (SQLException e) { @@ -430,4 +433,37 @@ public class DbLecture { } } + public static VmMetaData getClientLaunchData(String lectureId) throws SQLException, TNotFoundException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("SELECT" + + " l.displayname AS lecturename, l.starttime, l.endtime, l.isenabled," + + " o.virtoskeyword, i.filepath, i.virtualizerconfig" + + " FROM lecture l " + + " INNER JOIN imageversion i USING (imageversionid)" + + " INNER JOIN imagebase b USING (imagebaseid)" + + " INNER JOIN os_x_virt o USING (osid, virtid)" + " WHERE l.lectureid = :lectureid"); + stmt.setString("lectureid", lectureId); + ResultSet rs = stmt.executeQuery(); + long now = Util.unixTime(); + if (!rs.next() || !rs.getBoolean("isenabled") || rs.getLong("starttime") > now + || rs.getLong("endtime") < now) { + throw new TNotFoundException(); + } + byte[] config = rs.getBytes("virtualizerconfig"); + if (config == null) { + return null; + } + VmwareMetaData meta = new VmwareMetaData(OperatingSystemList.get(), config, config.length); + meta.addDisplayName(rs.getString("lecturename")); + if (meta.getOs() == null) { + meta.setOs(rs.getString("virtoskeyword")); + } + meta.addHddTemplate("%VM_DISK_PATH%"); + return meta; + } catch (SQLException e) { + LOGGER.error("Query failed in DbLecture.getClientLaunchData()", e); + throw e; + } + } + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java index 9f973bb9..d4b07606 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java @@ -10,8 +10,6 @@ public class VmChooserEntryXml { @Element private VmChooserParamXml priority; @Element - private VmChooserParamXml image_name; - @Element private VmChooserParamXml creator; @Element private VmChooserParamXml short_description; @@ -26,10 +24,9 @@ public class VmChooserEntryXml { @Element private VmChooserParamXml icon; - public VmChooserEntryXml(String priority, String image_name, String creator, String short_description, + public VmChooserEntryXml(String priority, String creator, String short_description, String long_description, String uuid, String virtualmachine, String os, String icon) { this.priority = new VmChooserParamXml(priority); - this.image_name = new VmChooserParamXml(image_name); this.creator = new VmChooserParamXml(creator); this.short_description = new VmChooserParamXml(short_description); this.long_description = new VmChooserParamXml(long_description); 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 a7a13305..c68e5a8f 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 @@ -1,10 +1,13 @@ package org.openslx.bwlp.sat.web; import java.io.ByteArrayInputStream; +import java.sql.SQLException; import org.apache.commons.io.output.ByteArrayOutputStream; import org.openslx.bwlp.sat.database.mappers.DbLecture; +import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.util.GenericDataCache; +import org.openslx.util.vm.VmMetaData; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; @@ -40,12 +43,32 @@ public class WebServer extends NanoHTTPD { if (uri.startsWith("/vmchooser/list")) { return serveVmChooserList(); } + if (uri.startsWith("/vmchooser/lecture/")) { + return serveLectureStart(uri.substring(19)); + } + + return notFound(); + } + private Response notFound() { return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "Nicht gefunden!"); } + private Response serveLectureStart(String lectureId) { + VmMetaData meta; + try { + meta = DbLecture.getClientLaunchData(lectureId); + } catch (TNotFoundException e) { + return notFound(); + } catch (SQLException e) { + return internalServerError(); + } + return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8", + new ByteArrayInputStream(meta.getFilteredDefinitionArray())); + } + private Response serveVmChooserList() { - return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/xml; charset=utf-8", + return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/xml; charset=utf-8", new ByteArrayInputStream(lectureListCache.get())); } -- cgit v1.2.3-55-g7522