From 7e14da18b0320c96859dc3d831cb4c9cd3ee96df Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Sep 2015 14:31:31 +0200 Subject: [server] Also inform lecture owners about expising images, if the image expires before the lecture ends --- .../org/openslx/bwlp/sat/mail/MailGenerator.java | 34 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'dozentenmodulserver/src/main/java') diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java index fd7b5a2b..aae515b4 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java @@ -13,6 +13,7 @@ import org.apache.log4j.Logger; import org.openslx.bwlp.sat.database.mappers.DbConfiguration; import org.openslx.bwlp.sat.database.mappers.DbImage; import org.openslx.bwlp.sat.database.mappers.DbImagePermissions; +import org.openslx.bwlp.sat.database.mappers.DbLecture; import org.openslx.bwlp.sat.database.mappers.DbLecturePermissions; import org.openslx.bwlp.sat.database.mappers.DbUser; import org.openslx.bwlp.sat.database.mappers.DbUser.User; @@ -151,8 +152,10 @@ public class MailGenerator { LOGGER.warn("Could not get image details for image version " + version.imageVersionId); return; } + boolean isCurrentlyLatest = image.latestVersionId == null + || image.latestVersionId.equals(version.imageVersionId); String message; - if (image.latestVersionId == null || image.latestVersionId.equals(version.imageVersionId)) { + if (isCurrentlyLatest) { message = "Die aktuellste Version der VM '" + image.imageName + "' läuft in " + days + " Tag(en) ab. Bitte aktualisieren Sie die VM, da verknüpfte" + " Veranstaltungen sonst deaktiviert werden."; @@ -163,9 +166,34 @@ public class MailGenerator { } else { return; } - List relevantUsers = getUserToMail(image); + List relevantUsers; + // Mail users responsible for this image + message = wordWrap(message); + relevantUsers = getUserToMail(image); for (UserInfo user : relevantUsers) { - MailQueue.queue(new Mail(user, wordWrap(message))); + MailQueue.queue(new Mail(user, message)); + } + // Mail users using this image for a lecture, but only if the image expires before the lecture ends + // And the image to delete is currently the newest image + if (!isCurrentlyLatest) + return; + List lectures; + try { + lectures = DbLecture.getLecturesUsingImageVersion(version.imageVersionId); + } catch (SQLException e) { + lectures = new ArrayList<>(0); + } + for (LectureSummary lecture : lectures) { + if (lecture.endTime < version.expireTime) + continue; + message = "Hinweis zur Veranstaltung '" + lecture.lectureName + "': Die verwendete VM '" + + image.imageName + "' läuft in " + days + " Tag(en) ab. Bitte aktualisieren" + + " oder wechseln Sie die VM."; + message = wordWrap(message); + relevantUsers = getUserToMail(lecture); + for (UserInfo user : relevantUsers) { + MailQueue.queue(new Mail(user, message)); + } } } -- cgit v1.2.3-55-g7522 From 864297f4ae1530da3a4d7a903782b6e34071b424 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Sep 2015 15:14:43 +0200 Subject: [server] Fix virtualizer store query --- .../bwlp/sat/database/mappers/DbLecture.java | 18 ++++++++++++++++ .../bwlp/sat/database/mappers/DbOrganization.java | 25 ++++++++++++++++++++++ .../bwlp/sat/database/mappers/DbOsVirt.java | 2 +- .../bwlp/sat/thrift/cache/OrganizationList.java | 18 +++++++++++++++- 4 files changed, 61 insertions(+), 2 deletions(-) (limited to 'dozentenmodulserver/src/main/java') 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 df34b97a..f82efe24 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 @@ -487,4 +487,22 @@ public class DbLecture { } } + public static List getLecturesUsingImageVersion(String imageVersionId) throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement(summaryBaseSql + + " WHERE l.imageversionid = :imageversionid"); + stmt.setString("userid", "-"); + stmt.setString("imageversionid", imageVersionId); + ResultSet rs = stmt.executeQuery(); + List list = new ArrayList<>(); + while (rs.next()) { + list.add(fillSummary(null, rs)); + } + return list; + } catch (SQLException e) { + LOGGER.error("Query failed in DbLecture.getExpiringLectures()", e); + throw e; + } + } + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOrganization.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOrganization.java index dbc75944..f208f2c2 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOrganization.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOrganization.java @@ -111,4 +111,29 @@ public class DbOrganization { } } + /** + * Return list of known organizations. This is a backup solution for + * fetching the list form them aster server, as this one doesn't fill all + * fields. + * + * @return list of all known organizations + * @throws SQLException + */ + public static List getAll() throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("SELECT" + " o.organizationid, o.displayname" + + " FROM organization o"); + ResultSet rsOrg = stmt.executeQuery(); + List list = new ArrayList<>(); + while (rsOrg.next()) { + list.add(new Organization(rsOrg.getString("organizationid"), rsOrg.getString("displayname"), + null, null)); + } + return list; + } catch (SQLException e) { + LOGGER.error("Query failed in DbOrganization.getAll()", e); + throw e; + } + } + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java index a8e24894..c34e5d72 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java @@ -84,7 +84,7 @@ public class DbOsVirt { + " ON DUPLICATE KEY UPDATE virtname = VALUES(virtname)"); for (Virtualizer virt : list) { stmt.setString("virtid", virt.virtId); - stmt.setString("displayname", virt.virtName); + stmt.setString("virtname", virt.virtName); stmt.executeUpdate(); } connection.commit(); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java index 4325b152..6d2ebda2 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java @@ -3,7 +3,9 @@ package org.openslx.bwlp.sat.thrift.cache; import java.sql.SQLException; import java.util.List; +import org.apache.log4j.Logger; import org.apache.thrift.TException; +import org.apache.thrift.transport.TTransportException; import org.openslx.bwlp.sat.database.mappers.DbOrganization; import org.openslx.bwlp.thrift.iface.Organization; import org.openslx.thrifthelper.ThriftManager; @@ -15,6 +17,8 @@ import org.openslx.util.QuickTimer.Task; * the master server. */ public class OrganizationList extends CacheBase> { + + private static final Logger LOGGER = Logger.getLogger(OrganizationList.class); private static final OrganizationList instance = new OrganizationList(); @@ -24,7 +28,19 @@ public class OrganizationList extends CacheBase> { @Override protected List getCallback() throws TException { - final List organizations = ThriftManager.getMasterClient().getOrganizations(); + final List organizations; + try { + organizations = ThriftManager.getMasterClient().getOrganizations(); + } catch (TException e1) { + LOGGER.warn("Could not fetch Organization list from master, using local data...", + e1 instanceof TTransportException ? null : e1); + try { + return DbOrganization.getAll(); + } catch (SQLException e) { + LOGGER.warn("Using local Organization list from database also failed.", e); + } + return null; + } // Also store the list in the local data base (asynchronous, in the timer thread) QuickTimer.scheduleOnce(new Task() { @Override -- cgit v1.2.3-55-g7522 From 561cafe5fbad785945239e3922f3a8c6035ea5ca Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Sep 2015 15:28:47 +0200 Subject: [server] Manually detect if latest version changed before triggering mail --- .../openslx/bwlp/sat/database/mappers/DbImage.java | 43 ++++++++++++---------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'dozentenmodulserver/src/main/java') 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 bbbb8c8d..bf79e88f 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 @@ -760,35 +760,40 @@ public class DbImage { * * @param connection mysql connection to use * @param imageBaseId base id of image in question - * @param latest image version that is to become the latest version, or + * @param newLatest image version that is to become the latest version, or * null if there is no valid version * @return true if changed to a different, non-null image * @throws SQLException */ private static boolean setLatestVersion(MysqlConnection connection, String imageBaseId, - LocalImageVersion latest) throws SQLException { - // Debug - MysqlStatement ds = connection.prepareStatement("SELECT latestversionid FROM imagebase WHERE imagebaseid = :imagebaseid"); - ds.setString("imagebaseid", imageBaseId); - ResultSet drs = ds.executeQuery(); - if (drs.next()) { - LOGGER.debug("set latest on " + imageBaseId + ": from " + drs.getString("latestversionid") - + " to " + (latest == null ? null : latest.imageVersionId)); - } else { - LOGGER.debug("set latest: could not determine old latest for " + imageBaseId); - } + LocalImageVersion newLatest) throws SQLException { + // Determine manually if anything changed, as executeQuery() always returns 1 for some reason + boolean latestVersionChanged = true; + do { + MysqlStatement ds = connection.prepareStatement("SELECT latestversionid FROM imagebase WHERE imagebaseid = :imagebaseid"); + ds.setString("imagebaseid", imageBaseId); + ResultSet drs = ds.executeQuery(); + if (drs.next()) { + String currentLatest = drs.getString("latestversionid"); + if (currentLatest == null && (newLatest == null || newLatest.imageVersionId == null)) { + latestVersionChanged = false; + } else if (currentLatest != null && newLatest != null + && currentLatest.equals(newLatest.imageVersionId)) { + latestVersionChanged = false; + } + } + } while (false); // Update latestversionid reference in imagebase table MysqlStatement latestStmt = connection.prepareStatement("UPDATE imagebase SET latestversionid = :newversionid" + " WHERE imagebaseid = :imagebaseid"); - latestStmt.setString("newversionid", latest == null ? null : latest.imageVersionId); + latestStmt.setString("newversionid", newLatest == null ? null : newLatest.imageVersionId); latestStmt.setString("imagebaseid", imageBaseId); // If nothing changed (because the deleted version was not the latest), bail out - int updateCount = latestStmt.executeUpdate(); - LOGGER.debug(updateCount + " rows affected"); - if (updateCount == 0) + latestStmt.executeUpdate(); + if (!latestVersionChanged) return false; // It there is no valid version, bail out as a shortcut - queries below wouldn't do anything - if (latest == null) + if (newLatest == null) return true; // Latest version changed - update expire dates of related versions // Set short expire date for versions that are NOT the latest version but are still marked valid @@ -796,7 +801,7 @@ public class DbImage { MysqlStatement oldStmt = connection.prepareStatement("UPDATE imageversion SET" + " expiretime = If(expiretime < :shortexpire, expiretime, :shortexpire)" + " WHERE imagebaseid = :imagebaseid AND imageversionid <> :imageversionid AND isvalid = 1"); - oldStmt.setString("imageversionid", latest.imageVersionId); + oldStmt.setString("imageversionid", newLatest.imageVersionId); oldStmt.setString("imagebaseid", imageBaseId); oldStmt.setLong("shortexpire", shortExpire); oldStmt.executeUpdate(); @@ -804,7 +809,7 @@ public class DbImage { MysqlStatement newStmt = connection.prepareStatement("UPDATE imageversion SET" + " expiretime = If(createtime + :maxvalid > expiretime, createtime + :maxvalid, expiretime)" + " WHERE imageversionid = :imageversionid"); - newStmt.setString("imageversionid", latest.imageVersionId); + newStmt.setString("imageversionid", newLatest.imageVersionId); newStmt.setLong("maxvalid", RuntimeConfig.getMaxImageValiditySeconds()); newStmt.executeUpdate(); return true; -- cgit v1.2.3-55-g7522 From 05dd12af33c1d233232793eddfc0e137280b0572 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Sep 2015 16:58:46 +0200 Subject: Renamed method in MSS --- dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java | 2 +- .../main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'dozentenmodulserver/src/main/java') diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java index acf6fd3a..36c64fdf 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java @@ -38,7 +38,7 @@ public class VmWrapper { machineDescription.length); } // Append disk & NAT - if (!vmwareConfig.addEthernetNat() || !vmwareConfig.addHddTemplate(diskFile.getName())) { + if (!vmwareConfig.addDefaultNat() || !vmwareConfig.addHddTemplate(diskFile.getName())) { throw new MetaDataMissingException(); } // The guestOS should be in the vmx, but the user could have changed it by editing 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 f82efe24..7f980b7f 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 @@ -29,6 +29,7 @@ import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.util.Json; import org.openslx.util.vm.VmMetaData; import org.openslx.util.vm.VmwareMetaData; +import org.openslx.util.vm.VmwareMetaData.EthernetType; import com.google.gson.JsonParseException; @@ -461,6 +462,7 @@ public class DbLecture { meta.setOs(rs.getString("virtoskeyword")); } meta.addHddTemplate("%VM_DISK_PATH%"); + meta.addEthernet(EthernetType.NAT); // TODO: Use config // Everything worked so far, update statistics counters MysqlStatement upStmt = connection.prepareStatement("UPDATE" + " lecture SET lastused = UNIX_TIMESTAMP(), usecount = usecount + 1" @@ -487,7 +489,8 @@ public class DbLecture { } } - public static List getLecturesUsingImageVersion(String imageVersionId) throws SQLException { + public static List getLecturesUsingImageVersion(String imageVersionId) + throws SQLException { try (MysqlConnection connection = Database.getConnection()) { MysqlStatement stmt = connection.prepareStatement(summaryBaseSql + " WHERE l.imageversionid = :imageversionid"); -- cgit v1.2.3-55-g7522