Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(RepositoryUpdater): make sure temp directories are deleted (#2010)
* fix(RepositoryUpdater): delete temp directories

* review changes

* remove unused code
  • Loading branch information
mpro7 committed Mar 4, 2022
1 parent eed2767 commit 9c9a1bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
6 changes: 0 additions & 6 deletions webapi/src/main/resources/application.conf
Expand Up @@ -335,12 +335,6 @@ app {
allow-reload-over-http = false
allow-reload-over-http = ${?KNORA_WEBAPI_ALLOW_RELOAD_OVER_HTTP}

// Configuration for updating the repository when Knora is upgraded
upgrade {
// The directory to which the repository should be downloaded. If not configured, a temporary directory is used.
// download-dir = "/tmp/knora-upgrade"
}

knora-api {
// relevant for direct communication inside the knora stack
internal-host = "0.0.0.0"
Expand Down
Expand Up @@ -281,12 +281,6 @@ class KnoraSettingsImpl(config: Config, log: LoggingAdapter) extends Extension {

val prometheusEndpoint: Boolean = config.getBoolean("app.monitoring.prometheus-endpoint")

val upgradeDownloadDir: Option[String] = if (config.hasPath("app.upgrade.download-dir")) {
Some(config.getString("app.upgrade.download-dir"))
} else {
None
}

val shaclShapesDir: Path = Paths.get(config.getString("app.shacl.shapes-dir"))

val featureToggles: Set[FeatureToggleBaseConfig] = if (config.hasPath(featureTogglesPath)) {
Expand Down
@@ -1,6 +1,8 @@
package org.knora.webapi.store.triplestore.upgrade

import java.nio.file.{Files, Path, Paths}
import java.io.File
import scala.reflect.io.Directory

import akka.actor.{ActorRef, ActorSystem}
import akka.http.scaladsl.util.FastFuture
Expand Down Expand Up @@ -72,6 +74,24 @@ class RepositoryUpdater(
log = log
)

private val tempDirNamePrefix: String = "knora"

/**
* Deletes directories inside temp directory starting with `tempDirNamePrefix`.
*/
def deleteTempDirectories(): Unit = {
val rootDir = new File("/tmp/")
val getTempToDelete = rootDir.listFiles.filter(_.getName.startsWith(tempDirNamePrefix))

if (getTempToDelete.length != 0) {
getTempToDelete.foreach(dir => {
val dirToDelete = new Directory(dir)
dirToDelete.deleteRecursively()
})
log.info(s"Deleted temp directories: ${getTempToDelete.map(_.getName()).mkString(", ")}")
}
}

/**
* Updates the repository, if necessary, to work with the current version of Knora.
*
Expand All @@ -83,18 +103,20 @@ class RepositoryUpdater(
requiredRepositoryVersion = org.knora.webapi.KnoraBaseVersion

// Is the repository up to date?
repositoryUpToData = foundRepositoryVersion.contains(requiredRepositoryVersion)
repositoryUpToDate: Boolean = foundRepositoryVersion.contains(requiredRepositoryVersion)

repositoryUpdatedResponse: RepositoryUpdatedResponse <-
if (repositoryUpToData) {
if (repositoryUpToDate) {
// Yes. Nothing more to do.
FastFuture.successful(RepositoryUpdatedResponse(s"Repository is up to date at $requiredRepositoryVersion"))
} else {
// No. Construct the list of updates that it needs.

log.info(
s"Repository not up-to-date. Found: ${foundRepositoryVersion.getOrElse("None")}, Required: $requiredRepositoryVersion"
)

deleteTempDirectories()

val selectedPlugins: Seq[PluginForKnoraBaseVersion] = selectPluginsForNeededUpdates(foundRepositoryVersion)
log.info(s"Updating repository with transformations: ${selectedPlugins.map(_.versionString).mkString(", ")}")

Expand Down Expand Up @@ -164,21 +186,8 @@ class RepositoryUpdater(
private def updateRepositoryWithSelectedPlugins(
pluginsForNeededUpdates: Seq[PluginForKnoraBaseVersion]
): Future[RepositoryUpdatedResponse] = {
// Was a download directory specified in the application settings?
val downloadDir: Path = settings.upgradeDownloadDir match {
case Some(configuredDir) =>
// Yes. Use that directory.
log.info(s"Repository update using configured download directory $configuredDir")
val dirFile = Paths.get(configuredDir)
Files.createDirectories(dirFile)
dirFile

case None =>
// No. Create a temporary directory.
val dirFile = Files.createTempDirectory("knora")
log.info(s"Repository update using download directory $dirFile")
dirFile
}
val downloadDir: Path = Files.createTempDirectory(tempDirNamePrefix)
log.info(s"Repository update using download directory $downloadDir")

// The file to save the repository in.
val downloadedRepositoryFile = downloadDir.resolve("downloaded-repository.nq")
Expand Down

0 comments on commit 9c9a1bd

Please sign in to comment.