From d778c75937d2ecc841b391ae6d7c451111e33631 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 18 Mar 2022 22:24:50 +0100 Subject: [PATCH] add function checkFile, make fileExists deprecated --- .../provs/desktop/infrastructure/DevOps.kt | 4 ++-- .../provs/desktop/infrastructure/Gopass.kt | 2 +- .../standalone_server/certbot/ProvisionCertbot.kt | 4 ++-- .../standalone_server/nexus/ProvisionNexus.kt | 4 ++-- .../standalone_server/nginx/ProvisionNginx.kt | 4 ++-- .../ubuntu/filesystem/base/Filesystem.kt | 15 ++++++++++++--- .../provs/framework/ubuntu/git/base/Git.kt | 2 +- .../provs/framework/ubuntu/user/base/User.kt | 6 +++--- .../provs/framework/ubuntu/web/base/Web.kt | 4 ++-- .../provs/server/infrastructure/K3s.kt | 2 +- .../provs/server/infrastructure/network.kt | 4 ++-- .../desktop/infrastructure/BinariesC4kKtTest.kt | 12 ++++++------ .../desktop/infrastructure/BinariesProvsKtTest.kt | 6 +++--- .../nginx/ProvisionNginxKtTest.kt | 4 ++-- .../workplace/ProvisionWorkplaceKtTest.kt | 6 +++--- .../ubuntu/filesystem/base/FilesystemKtTest.kt | 14 +++++++------- .../provs/framework/ubuntu/web/base/WebKtTest.kt | 4 ++-- 17 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt index a5019a4..13949cc 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt @@ -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") diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt index 5b31e4b..e210e21 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Gopass.kt @@ -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") } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/certbot/ProvisionCertbot.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/certbot/ProvisionCertbot.kt index e020c14..597119a 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/certbot/ProvisionCertbot.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/certbot/ProvisionCertbot.kt @@ -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") diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nexus/ProvisionNexus.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nexus/ProvisionNexus.kt index f6edc88..dc5d275 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nexus/ProvisionNexus.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nexus/ProvisionNexus.kt @@ -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 diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nginx/ProvisionNginx.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nginx/ProvisionNginx.kt index 643ecf4..9de1bf1 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nginx/ProvisionNginx.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/extensions/server_software/standalone_server/nginx/ProvisionNginx.kt @@ -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) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt index 4b43fad..34f9731 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt @@ -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,8 +150,9 @@ 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) + } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt index b96991e..89b3381 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt @@ -63,7 +63,7 @@ private fun Prov.trustHost(host: String, fingerprintsOfKeysToBeAdded: Set