add function checkFile, make fileExists deprecated

This commit is contained in:
ansgarz 2022-03-18 22:24:50 +01:00
parent a3a81722b2
commit d778c75937
17 changed files with 53 additions and 44 deletions

View file

@ -21,7 +21,7 @@ fun Prov.installYq(
): ProvResult = task {
val path = "/usr/bin/"
val filename = "yq"
if (!fileExists(path + filename)) {
if (!checkFile(path + filename)) {
downloadFromURL(
"https://github.com/mikefarah/yq/releases/download/v$version/yq_linux_amd64",
filename,
@ -40,7 +40,7 @@ fun Prov.installKubectlAndTools(): ProvResult = task {
task("installKubectl") {
val kubeConfigFile = "~/.bashrc.d/kubectl.sh"
if (!fileExists(kubeConfigFile)) {
if (!checkFile(kubeConfigFile)) {
// prerequisites -- see https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/
cmd("sudo apt-get update")
aptInstall("apt-transport-https ca-certificates curl")

View file

@ -46,7 +46,7 @@ fun Prov.configureGopass(gopassRootFolder: String? = null) = task {
val defaultRootFolder = userHome() + ".password-store"
val rootFolder = gopassRootFolder ?: defaultRootFolder
if (fileExists(configFile)) {
if (checkFile(configFile)) {
return@task ProvResult(true, out = "Gopass already configured in file $configFile")
}

View file

@ -2,7 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.extensions.server_software.
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
@ -17,7 +17,7 @@ fun Prov.provisionCertbot(serverName: String, email: String?, additionalOptions:
sudo snap install --classic certbot
""".trimIndent())
if (!fileExists("/usr/bin/certbot")) {
if (!checkFile("/usr/bin/certbot")) {
cmd("sudo ln -s /snap/bin/certbot /usr/bin/certbot")
val emailOption = email?.let { " -m $it" } ?: "--register-unsafely-without-email"
cmd("sudo certbot $additionalOptions -n --agree-tos $emailOption -d $serverName")

View file

@ -4,7 +4,7 @@ import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.core.docker.containerRuns
import org.domaindrivenarchitecture.provs.framework.core.remote
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.createUser
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.certbot.provisionCertbot
@ -31,7 +31,7 @@ fun Prov.provisionNexusWithDocker(portAccessibleFromNetwork: Boolean = false) =
cmd("sudo docker run -d --restart unless-stopped -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3")
for (n in 0..3) {
if (fileExists("/var/lib/docker/volumes/$volume/_data/admin.password", sudo = true)) {
if (checkFile("/var/lib/docker/volumes/$volume/_data/admin.password", sudo = true)) {
val res = cmd("sudo cat /var/lib/docker/volumes/$volume/_data/admin.password")
println("Admin Password:" + res.out)
break

View file

@ -5,7 +5,7 @@ import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.NginxConf
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.createNginxLocationFolders
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
@ -19,7 +19,7 @@ fun Prov.provisionNginxStandAlone(config: NginxConf? = null) = task {
createNginxLocationFolders()
if (config != null) {
if (fileExists(NGINX_CONFIG_FILE)) {
if (checkFile(NGINX_CONFIG_FILE)) {
cmd("sudo mv $NGINX_CONFIG_FILE $NGINX_CONFIG_FILE-orig")
}
createFile(NGINX_CONFIG_FILE, config.conf, sudo = true)

View file

@ -9,10 +9,18 @@ import java.io.File
/**
* Returns true if the given file exists.
*/
@Deprecated("Use checkFile", replaceWith = ReplaceWith("checkFile(file)"))
fun Prov.fileExists(file: String, sudo: Boolean = false): Boolean {
return cmdNoEval(prefixWithSudo("test -e " + file, sudo)).success
}
/**
* Returns true if the given file exists.
*/
fun Prov.checkFile(file: String, sudo: Boolean = false): Boolean {
return cmdNoEval(prefixWithSudo("test -e " + file, sudo)).success
}
/**
* Creates a file with its content retrieved from a local resource file
@ -88,7 +96,7 @@ fun Prov.createFile(
posixFilePermission?.let {
ensureValidPosixFilePermission(posixFilePermission)
}
if (!overwriteIfExisting && fileExists(fullyQualifiedFilename, sudo)) {
if (!overwriteIfExisting && checkFile(fullyQualifiedFilename, sudo)) {
return@task ProvResult(true, "File $fullyQualifiedFilename already existing.")
}
@ -128,7 +136,7 @@ fun Prov.createSecretFile(
fun Prov.deleteFile(file: String, path: String? = null, sudo: Boolean = false): ProvResult = task {
val fullyQualifiedFilename = (path?.normalizePath() ?: "") + file
if (fileExists(fullyQualifiedFilename, sudo = sudo)) {
if (checkFile(fullyQualifiedFilename, sudo = sudo)) {
cmd(prefixWithSudo("rm $fullyQualifiedFilename", sudo))
} else {
ProvResult(true, "File to be deleted did not exist.")
@ -142,9 +150,10 @@ fun Prov.fileContainsText(file: String, content: String, sudo: Boolean = false):
val fileContent = fileContent(file, sudo = sudo)
return if (fileContent == null) {
false
} else
} else {
fileContent.contains(content)
}
}
fun Prov.fileContent(file: String, sudo: Boolean = false): String? {

View file

@ -63,7 +63,7 @@ private fun Prov.trustHost(host: String, fingerprintsOfKeysToBeAdded: Set<String
if (isHostKnown(host)) {
return@task ProvResult(true, out = "Host already known")
}
if (!fileExists(knownHostsFile)) {
if (!checkFile(knownHostsFile)) {
createDir(".ssh")
createFile(knownHostsFile, null)
}

View file

@ -5,7 +5,7 @@ import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.core.Secret
import org.domaindrivenarchitecture.provs.framework.core.processors.RemoteProcessor
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.userHome
import org.domaindrivenarchitecture.provs.framework.ubuntu.git.provisionGit
import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base.gpgFingerprint
@ -36,7 +36,7 @@ fun Prov.createUser(
makeUserSudoerWithNoSudoPasswordRequired(userName)
}
val authorizedKeysFile = userHome() + ".ssh/authorized_keys"
if (copyAuthorizedSshKeysFromCurrentUser && fileExists(authorizedKeysFile)) {
if (copyAuthorizedSshKeysFromCurrentUser && checkFile(authorizedKeysFile)) {
val sshPathForNewUser = "/home/$userName/.ssh"
createDirs(sshPathForNewUser, sudo = true)
cmd("chown $userName $sshPathForNewUser", sudo = true)
@ -91,7 +91,7 @@ fun Prov.makeUserSudoerWithNoSudoPasswordRequired(
overwriteFile: Boolean = false
): ProvResult = task {
val userSudoFile = "/etc/sudoers.d/$userName"
if (!fileExists(userSudoFile) || overwriteFile) {
if (!checkFile(userSudoFile) || overwriteFile) {
val sudoPrefix = if (password == null) "sudo" else "echo ${password.plain()} | sudo -S"
// see https://stackoverflow.com/questions/323957/how-do-i-edit-etc-sudoers-from-a-script
val result = cmdNoLog(sudoPrefix + " sh -c \"echo '$userName ALL=(ALL) NOPASSWD:ALL' | (sudo su -c 'EDITOR=\"tee\" visudo -f " + userSudoFile + "')\"")

View file

@ -4,7 +4,7 @@ import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.normalizePath
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
@ -27,7 +27,7 @@ fun Prov.downloadFromURL(
val finalFilename: String = filename ?: url.substringAfterLast("/")
val fqFilename: String = (path?.normalizePath() ?: "") + finalFilename
if (!overwrite && fileExists(fqFilename, sudo = sudo)) {
if (!overwrite && checkFile(fqFilename, sudo = sudo)) {
return@task ProvResult(true, out = "File $fqFilename already exists.")
}

View file

@ -38,7 +38,7 @@ private val localPathProvisionerConfig = File(k3sManualManifestsDir, "local-path
// ----------------------------------- public functions --------------------------------
fun Prov.testConfigExists(): Boolean {
return fileExists(k3sConfigFile.path)
return checkFile(k3sConfigFile.path)
}

View file

@ -3,14 +3,14 @@ package org.domaindrivenarchitecture.provs.server.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFileFromResourceTemplate
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
val loopbackFile = "/etc/netplan/99-loopback.yaml"
val resourcePath = "org/domaindrivenarchitecture/provs/server/infrastructure/network/"
fun Prov.testNetworkExists(): Boolean {
return fileExists(loopbackFile)
return checkFile(loopbackFile)
}
fun Prov.provisionNetwork(k3sConfig: K3sConfig) = task {

View file

@ -1,6 +1,6 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertTrue
@ -13,11 +13,11 @@ internal class BinariesC4kKtTest {
val res = defaultTestContainer().installBinariesC4k()
// then
assertTrue(defaultTestContainer().fileExists("/usr/local/bin/c4k-nextcloud-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists("/usr/local/bin/c4k-jira-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists("/usr/local/bin/c4k-keycloak-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists("/usr/local/bin/c4k-mastodon-bot-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists("/usr/local/bin/c4k-shynet-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile("/usr/local/bin/c4k-nextcloud-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile("/usr/local/bin/c4k-jira-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile("/usr/local/bin/c4k-keycloak-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile("/usr/local/bin/c4k-mastodon-bot-standalone.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile("/usr/local/bin/c4k-shynet-standalone.jar", sudo = true))
assertTrue(res.success)
}
}

View file

@ -1,6 +1,6 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertTrue
@ -17,7 +17,7 @@ internal class BinariesProvsKtTest {
// then
assertTrue(res.success)
assertTrue(defaultTestContainer().fileExists(" /usr/local/bin/provs-server.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists(" /usr/local/bin/provs-desktop.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile(" /usr/local/bin/provs-server.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile(" /usr/local/bin/provs-desktop.jar", sudo = true))
}
}

View file

@ -1,7 +1,7 @@
package org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx
import org.domaindrivenarchitecture.provs.framework.extensions.server_software.standalone_server.nginx.base.*
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.replaceTextInFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
@ -67,7 +67,7 @@ internal class ProvisionNginxKtTest {
val a = defaultTestContainer()
a.task {
val file = "/etc/ssl/openssl.cnf"
if (fileExists(file)) {
if (checkFile(file)) {
replaceTextInFile(file, "RANDFILE", "#RANDFILE")
}
aptInstall("openssl")

View file

@ -4,7 +4,7 @@ import org.domaindrivenarchitecture.provs.desktop.domain.DesktopType
import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace
import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplaceSubmodules
import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertTrue
@ -62,8 +62,8 @@ internal class ProvisionWorkplaceKtTest {
// then
assertTrue(res.success)
assertTrue(defaultTestContainer().fileExists(" /usr/local/bin/provs-server.jar", sudo = true))
assertTrue(defaultTestContainer().fileExists(" /usr/local/bin/provs-desktop.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile(" /usr/local/bin/provs-server.jar", sudo = true))
assertTrue(defaultTestContainer().checkFile(" /usr/local/bin/provs-desktop.jar", sudo = true))
}
}

View file

@ -75,13 +75,13 @@ internal class FilesystemKtTest {
val prov = defaultTestContainer()
// when
val res1 = prov.fileExists("testfile")
val res1 = prov.checkFile("testfile")
val res2 = prov.createFile("testfile", "some content")
val res3 = prov.fileExists("testfile")
val res3 = prov.checkFile("testfile")
val res4a = prov.fileContainsText("testfile", "some content")
val res4b = prov.fileContainsText("testfile", "some non-existing content")
val res5 = prov.deleteFile("testfile")
val res6 = prov.fileExists("testfile")
val res6 = prov.checkFile("testfile")
val res7 = prov.deleteFile("testfile") // idem-potent
// then
@ -104,15 +104,15 @@ internal class FilesystemKtTest {
// when
val file = "/testfile"
val res1 = prov.fileExists(file)
val res1 = prov.checkFile(file)
val res2 = prov.createFile(file, "some content", sudo = true)
val res3 = prov.fileExists(file)
val res3 = prov.checkFile(file)
val res4a = prov.fileContainsText(file, "some content")
val res4b = prov.fileContainsText(file, "some non-existing content")
val res5 = prov.deleteFile(file)
val res6 = prov.fileExists(file)
val res6 = prov.checkFile(file)
val res7 = prov.deleteFile(file, sudo = true)
val res8 = prov.fileExists(file)
val res8 = prov.checkFile(file)
val res9 = prov.deleteFile(file, sudo = true) // check idem-potence
// then

View file

@ -2,7 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.web.base
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContent
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.*
@ -65,7 +65,7 @@ internal class WebKtTest {
val res = a.downloadFromURL("file:///tmp/" + srcFile, targetFile, "tmp", sha256sum = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824WRONG", overwrite = true)
// then
val res2 = a.fileExists("tmp/$targetFile")
val res2 = a.checkFile("tmp/$targetFile")
assertFalse(res.success)
assertFalse(res2)