[skip ci] refactoring

This commit is contained in:
zam 2025-02-13 18:14:26 +01:00
parent 353837f899
commit 7ad3dc8c24
13 changed files with 69 additions and 59 deletions
src
main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure
test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure

View file

@ -8,15 +8,15 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFrom
fun Prov.installBabashka(
version: String = "1.12.196",
enforceVersion: Boolean = false,
sha256sum: String = "13c197bf1151cac038abedfa2869a27303f62650474f334867264e13ee9f8cd6"
reInstall: Boolean = false,
sha256sum: String = "18dbf47c79cc136fe9903642a7b0c9ab75f52282984197855b489b80469b8d8f"
) = taskWithResult {
if (checkCommand("bb") && !enforceVersion) {
if (checkCommand("bb") && !reInstall) {
return@taskWithResult ProvResult(true)
}
if (checkBabashkaVersion(version)) {
return@taskWithResult ProvResult(true, out = "Version $version of babashka is already installed.")
return@taskWithResult ProvResult(true, info = "Babashka $version is already installed.")
}
val downloadUrl = "https://github.com/babashka/babashka/releases/download/v$version/babashka-$version-linux-amd64.tar.gz"
@ -30,7 +30,7 @@ fun Prov.installBabashka(
)
if (result.success) {
cmd("tar -C /usr/local/bin/ -xzf $target/go1.23.5.linux-amd64.tar.gz --no-same-owner", sudo = true)
cmd("tar -C /usr/local/bin/ -xzf $target/babashka-$version-linux-amd64.tar.gz --no-same-owner", sudo = true)
deleteFile("$target/$filename")
// check and assert installation
@ -46,6 +46,6 @@ fun Prov.checkBabashkaVersion(version: String): Boolean {
}
internal fun Prov.babashkaVersion(): String? {
val result = cmdNoEval("/usr/local/bb version")
val result = cmdNoEval("/usr/local/bin/bb version")
return if (!result.success) null else result.out
}

View file

@ -158,20 +158,19 @@ fun Prov.installTerraform() = task {
cmd("ln -s " + dir + "bin/* /usr/local/bin", sudo = true)
}
cmd("tfenv install", sudo = true)
cmd("tfenv install latest:^1.4.6", sudo = true)
cmd("tfenv use latest:^1.4.6", sudo = true)
cmd("tfenv install latest:^1.4.7", sudo = true)
cmd("tfenv use latest:^1.4.7", sudo = true)
}
fun Prov.installDirenv() = taskWithResult {
val bashConfigFile = "~/.bashrc.d/direnv.sh"
if (!checkFile(bashConfigFile) && !checkPackage("direnv")) {
aptInstall("direnv")
val content = """
eval "$(direnv hook bash)"
""" + "\n".trimIndent()
val content = """eval "$(direnv hook bash)"
""" + "\n".trimIndent()
createFile(bashConfigFile, content)
addResult(checkPackage("direnv"), info = "direnv has been installed.")
} else {
return@taskWithResult ProvResult(true, out = "direnv or ~/.bashrc.d/direnv.sh already installed")
return@taskWithResult ProvResult(true, info = "direnv or ~/.bashrc.d/direnv.sh already installed")
}
}

View file

@ -9,15 +9,15 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFrom
//from https://go.dev/dl/
fun Prov.installGo(
version: String = "1.23.5",
enforceVersion: Boolean = false,
reInstall: Boolean = false,
sha256sum: String = "cbcad4a6482107c7c7926df1608106c189417163428200ce357695cc7e01d091"
) = taskWithResult {
if (checkCommand("go") && !enforceVersion) {
if (checkCommand("go") && !reInstall) {
return@taskWithResult ProvResult(true)
}
if (checkGoVersion(version)) {
return@taskWithResult ProvResult(true, out = "Version $version of go is already installed.")
return@taskWithResult ProvResult(true, out = "Go $version is already installed.")
}
val downloadUrl = "https://go.dev/dl/go$version.linux-amd64.tar.gz"
@ -38,7 +38,7 @@ fun Prov.installGo(
val content = "export PATH=\$PATH:/usr/local/go/bin\n"
createFile(bashConfigFile, content)
// check and assert installation
addResult(checkGoVersion(version), info = "Go version $version has been installed.")
addResult(checkGoVersion(version), info = "Go $version has been installed.")
} else {
return@taskWithResult ProvResult(false, err = "Go $version could not be downloaded and installed. " + result.err)
}

View file

@ -12,16 +12,16 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFrom
fun Prov.installGopass(
version: String = "1.15.13", // NOTE: when adjusting, pls also adjust checksum below and version of gopass bridge json api
enforceVersion: Boolean = false,
reInstall: Boolean = false,
// from https://github.com/gopasspw/gopass/releases/tag/v1.15.13
sha256sum: String = "409ed5617e64fa2c781d5e2807ba7fcd65bc383a4e110f410f90b590e51aec55"
) = taskWithResult {
if (checkPackage("gopass") && !enforceVersion) {
if (checkPackage("gopass") && !reInstall) {
return@taskWithResult ProvResult(true)
}
if (checkGopassVersion(version)) {
return@taskWithResult ProvResult(true, out = "Version $version of gopass is already installed.")
return@taskWithResult ProvResult(true, out = "Gopass $version is already installed.")
}
val path = "tmp"

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.*
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalled
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.checkPackage
import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL
@ -53,7 +53,7 @@ fun Prov.installGopassJsonApi() = taskWithResult {
}
} else {
if (installedJsonApiVersion.startsWith("gopass-jsonapi version $gopassJsonApiVersion")) {
ProvResult(true, out = "Version $gopassJsonApiVersion of gopass-jsonapi is already installed")
ProvResult(true, info = "Gopass-jsonapi $gopassJsonApiVersion is already installed")
} else {
ProvResult(
false,
@ -85,7 +85,7 @@ fun Prov.configureApparmorForGopassWrapperShForFirefox() = task {
}
fun Prov.configureGopassJsonApi() = taskWithResult {
if (isPackageInstalled("gopass-jsonapi")) {
if (checkPackage("gopass-jsonapi")) {
// configures gopass-jsonapi for firefox and chooses default for each:
// * "Install for all users? [y/N/q]",
// * "In which path should gopass_wrapper.sh be installed? [/home/<user>/.config/gopass]"

View file

@ -23,10 +23,9 @@ fun Prov.installNpmByNvm(version: String = "0.40.1"): ProvResult = task {
// configure bash - create file ".bashrc.d/npmbynvm.sh" with settings
configureBashForUser()
val content = """
export NVM_DIR="${userHome()}.nvm"
[ -s "${"\$NVM_DIR/nvm.sh"}" ] && \. "${"\$NVM_DIR/nvm.sh"}"
""" + "\n".trimIndent()
val content = """export NVM_DIR="${userHome()}.nvm"
[ -s "${"\$NVM_DIR/nvm.sh"}" ] && \. "${"\$NVM_DIR/nvm.sh"}"
""" + "\n".trimIndent()
createFile(bashConfigFile, content)
// install Node.js and NPM

View file

@ -8,32 +8,31 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInsta
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.checkPackage
fun Prov.installOpentofu(
enforceVersion: Boolean = false
reInstall: Boolean = false
) = taskWithResult {
if (checkCommand("tofu -version") && !enforceVersion) {
val versionInst = cmd("tofu -version").toString()
return@taskWithResult ProvResult(true, out = "Opentofu v$versionInst is already installed.")
if (checkCommand("tofu -version") && !reInstall) {
val versionInst = cmd("tofu -version").out
return@taskWithResult ProvResult(true, info = "Opentofu v$versionInst is already installed.")
}
val pathKeyrings = "/etc/apt/keyrings"
val result = checkDir(pathKeyrings)
val result = checkDir("/etc/apt/keyrings")
if (result) {
cmd("curl -fsSL https://get.opentofu.org/opentofu.gpg | sudo tee /etc/apt/keyrings/opentofu.gpg > /dev/null")
cmd("curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpgkey/ | sudo gpg --no-tty --batch --dearmor -o /etc/apt/keyrings/opentofu-repo.gpg > /dev/null")
cmd("chmod a+r /etc/apt/keyrings/opentofu.gpg /etc/apt/keyrings/opentofu-repo.gpg", sudo = true)
val TofuListFile = "/etc/apt/sources.list.d/opentofu.list"
val content = """deb [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main""" + "\n" +
"""deb-src [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main""" + "\n" +
"".trimIndent()
createFile(TofuListFile, content, sudo = true)
val tofuListFile = "/etc/apt/sources.list.d/opentofu.list"
val content =
"""deb [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main""" + "\n" +
"""deb-src [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main""" + "\n" +
"".trimIndent()
createFile(tofuListFile, content, sudo = true)
cmd("sudo apt-get update -q=2")
aptInstall("tofu")
addResult(checkPackage("tofu"), info = "Opentofu has been installed.")
}else {
} else {
return@taskWithResult ProvResult(false, err = "Opentofu could not be downloaded and installed. ")
}
}

View file

@ -8,15 +8,15 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFrom
fun Prov.installTerragrunt(
version: String = "0.72.6",
enforceVersion: Boolean = false,
reInstall: Boolean = false,
sha256sum: String = "df63a41576b8b4129b498da5b698b5792a5a228ea5012bbecdcbe49d4d662be3"
) = taskWithResult {
if (checkCommand("terragrunt") && !enforceVersion) {
if (checkCommand("terragrunt") && !reInstall) {
return@taskWithResult ProvResult(true)
}
if (checkTerragruntVersion(version)) {
return@taskWithResult ProvResult(true, out = "Terragrunt is already installed.")
return@taskWithResult ProvResult(true, info = "Terragrunt is already installed.")
}
val downloadUrl = "https://github.com/gruntwork-io/terragrunt/releases/download/v$version/terragrunt_linux_amd64"
@ -30,12 +30,8 @@ fun Prov.installTerragrunt(
)
if (result.success) {
cmd("sudo cp $target/$filename /usr/local/bin/terragrunt", sudo = true)
cmd("sudo mv $target/$filename /usr/local/bin/terragrunt", sudo = true)
cmd("chmod 755 /usr/local/bin/terragrunt", sudo = true)
cmd("chown root /usr/local/bin/terragrunt", sudo = true)
cmd("chgrp root /usr/local/bin/terragrunt", sudo = true)
deleteFile("$target/$filename")
configureBashForUser()
// check and assert installation
addResult(checkTerragruntVersion(version), info = "Terragrunt version $version has been installed.")
} else {

View file

@ -4,17 +4,17 @@ import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertTrue
class BabashkaKtTest {
class BabashkaKtTest {
@ExtensiveContainerTest
@ExtensiveContainerTest
fun installBabashka() {
// given
val prov = defaultTestContainer()
// when
val res = prov.task {
installGo()
installGo() // check repeatability
installBabashka()
installBabashka() // check repeatability
}
// then

View file

@ -1,6 +1,7 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.*
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
@ -59,7 +60,23 @@ internal class DevOpsKtTest {
assertTrue(prov.checkFile("/usr/local/bin/kubeconform"))
}
@ExtensiveContainerTest
@ExtensiveContainerTest
fun installTerraform() {
// given
val prov = defaultTestContainer()
// when
val res = prov.task {
aptInstall("git curl unzip")
installTerraform()
installTerraform() // check repeatability
}
// then
assertTrue(res.success)
}
@ExtensiveContainerTest
fun installDirenv() {
// given
val prov = defaultTestContainer()
@ -72,6 +89,5 @@ internal class DevOpsKtTest {
// then
assertTrue(res.success)
//assertTrue(prov.checkFile("/usr/local/bin/kubeconform"))
}
}

View file

@ -72,7 +72,7 @@ internal class GopassBridgeKtTest {
trust = true,
skipIfExistin = false
)
installGopass("1.11.0", enforceVersion = true, "1ec9e0dfcfd9bcc241943e1a7d92f31bf3e66bb16f61ae5d079981325c31baa6")
installGopass("1.11.0", reInstall = true, "1ec9e0dfcfd9bcc241943e1a7d92f31bf3e66bb16f61ae5d079981325c31baa6")
configureGopass(publicGpgKey = Secret(publicGPGSnakeoilKey()))
}
assertTrue(preparationResult.success)
@ -100,7 +100,7 @@ internal class GopassBridgeKtTest {
trust = true,
skipIfExistin = false
)
installGopass("1.9.0", enforceVersion = true, "fe13ef810d7fe200495107161e99eac081368aa0ce5e53971b1bd47a64eba4db")
installGopass("1.9.0", reInstall = true, "fe13ef810d7fe200495107161e99eac081368aa0ce5e53971b1bd47a64eba4db")
configureGopass(publicGpgKey = Secret(publicGPGSnakeoilKey()))
}
assertTrue(preparationResult.success)

View file

@ -1,5 +1,6 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
@ -10,7 +11,7 @@ internal class NpmByNvmKtTest {
@ContainerTest
fun installNpmByNvm() {
// given
val prov = defaultTestContainer()
val prov = defaultTestContainer(ContainerStartMode.CREATE_NEW_KILL_EXISTING)
prov.aptInstall("curl")
// when

View file

@ -6,15 +6,15 @@ import org.junit.jupiter.api.Assertions.assertTrue
class TerragruntKtTest {
@ExtensiveContainerTest
@ExtensiveContainerTest
fun installTerragrunt() {
// given
val prov = defaultTestContainer()
// when
val res = prov.task {
installOpentofu()
installOpentofu() // check repeatability
installTerragrunt()
installTerragrunt() // check repeatability
}
// then