diff options
| author | Jonathan Bauer | 2015-09-09 17:41:14 +0200 |
|---|---|---|
| committer | Jonathan Bauer | 2015-09-09 17:41:14 +0200 |
| commit | 13dcbae97eb2e7b2e64bf02d81e315c51311fb7c (patch) | |
| tree | c3e062a35bd49018c2d2ad3428436e24c5abe4ba /dozentenmodulserver/src/main/java | |
| parent | [client] reworked def/custom permissions handling in ImageDetailsWindow (diff) | |
| parent | [client] Use new authentication RPC to master (diff) | |
| download | tutor-module-13dcbae97eb2e7b2e64bf02d81e315c51311fb7c.tar.gz tutor-module-13dcbae97eb2e7b2e64bf02d81e315c51311fb7c.tar.xz tutor-module-13dcbae97eb2e7b2e64bf02d81e315c51311fb7c.zip | |
Merge branch 'v1.1' of stp:openslx-ng/tutor-module into v1.1
Diffstat (limited to 'dozentenmodulserver/src/main/java')
6 files changed, 119 insertions, 24 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 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 * <code>null</code> 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; 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..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,4 +489,23 @@ public class DbLecture { } } + public static List<LectureSummary> 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<LectureSummary> 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<Organization> 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<Organization> 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/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<UserInfo> relevantUsers = getUserToMail(image); + List<UserInfo> 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<LectureSummary> 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)); + } } } 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<List<Organization>> { + + 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<List<Organization>> { @Override protected List<Organization> getCallback() throws TException { - final List<Organization> organizations = ThriftManager.getMasterClient().getOrganizations(); + final List<Organization> 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 |
