From 469f86433940abf5ab3ea72c82e608930654a2b1 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 16 Aug 2024 11:53:46 +0200 Subject: [PATCH] refactorings, reformats, minor fixes, update docs --- README.md | 5 ++- .../provs/desktop/infrastructure/DevOps.kt | 41 +++---------------- .../provs/desktop/infrastructure/GraalVM.kt | 40 ++++++++++++++++++ .../provs/framework/core/Prov.kt | 28 +++++++------ .../provs/server/infrastructure/K9s.kt | 11 ++--- .../desktop/infrastructure/DevOpsKtTest.kt | 13 ------ .../desktop/infrastructure/GraalVMKtTest.kt | 24 +++++++++++ 7 files changed, 93 insertions(+), 69 deletions(-) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVM.kt create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt diff --git a/README.md b/README.md index b6bdd0e..582aa89 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,10 @@ ## Purpose provs provides cli-based tools for -* provisioning a desktop (various kinds) +* provisioning desktop software for different desktop types: + * basic + * office + * IDE * provisioning a k3s server * performing system checks 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 aaf5f1b..2f8f35d 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt @@ -18,39 +18,10 @@ fun Prov.installDevOps() = task { installGraalVM() } -fun Prov.installGraalVM():ProvResult = task{ - val version = "21.0.2" - val tmpDir = "~/tmp" - val filename = "graalvm-community-jdk-" - val additionalPartFilename = "_linux-x64_bin" - val packedFilename = "$filename$version$additionalPartFilename.tar.gz" - val extractedFilenameHunch = "graalvm-community-openjdk-" - val installationPath = "/usr/lib/jvm/" - - if ( !chk("/usr/local/bin/native-image --version") || version != cmd("/usr/local/bin/native-image --version|awk 'NR==1 {print $2}").out?.trim() || !chk("ls -d $installationPath$extractedFilenameHunch$version*")) { - downloadFromURL( - "https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-$version/$packedFilename", - path = tmpDir, - sha256sum = "b048069aaa3a99b84f5b957b162cc181a32a4330cbc35402766363c5be76ae48" - ) - if (!chk("ls -d $installationPath")) - cmd("sudo mkdir $installationPath") - else { - ProvResult(true, out = "$installationPath just exists, mkdir not necessary.") - } - cmd("sudo tar -C $installationPath -xzf $packedFilename", tmpDir) - val graalInstPath = installationPath + (cmd("ls /usr/lib/jvm/|grep -e graalvm-community-openjdk-$version").out?.replace("\n", "")) - cmd("sudo ln -s $graalInstPath/lib/svm/bin/native-image /usr/local/bin/native-image") - cmd("/usr/local/bin/native-image --version") - } else { - ProvResult(true, out = "GraalVM $version already installed") - } -} - fun Prov.installYq( version: String = "4.13.2", sha256sum: String = "d7c89543d1437bf80fee6237eadc608d1b121c21a7cbbe79057d5086d74f8d79" -): ProvResult = task { +) = task { val path = "/usr/bin/" val filename = "yq" if (!checkFile(path + filename)) { @@ -67,7 +38,7 @@ fun Prov.installYq( } } -fun Prov.installKubectlAndTools(): ProvResult = task { +fun Prov.installKubectlAndTools() = task { task("installKubectl") { if (!checkFile(KUBE_CONFIG_CONTEXT_SCRIPT)) { @@ -105,7 +76,7 @@ fun Prov.installKubeconform() = task { } } -fun Prov.installKubectl(): ProvResult = task { +fun Prov.installKubectl() = task { // see https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ val kubectlVersion = "1.27.4" @@ -126,13 +97,13 @@ fun Prov.installKubectl(): ProvResult = task { cmd("sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl", dir = tmpDir) } -fun Prov.configureKubectlBashCompletion(): ProvResult = task { +fun Prov.configureKubectlBashCompletion() = task { cmd("kubectl completion bash >> /etc/bash_completion.d/kubernetes", sudo = true) createDir(".bashrc.d") createFileFromResource(KUBE_CONFIG_CONTEXT_SCRIPT, "kubectl.sh", RESOURCE_PATH) } -fun Prov.installDevopsScripts() { +fun Prov.installDevopsScripts() = task { task("install ssh helper") { createFileFromResource( @@ -174,7 +145,7 @@ fun Prov.installDevopsScripts() { } } -fun Prov.installTerraform(): ProvResult = task { +fun Prov.installTerraform() = task { val dir = "/usr/lib/tfenv/" if (!checkDir(dir)) { diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVM.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVM.kt new file mode 100644 index 0000000..ec13402 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVM.kt @@ -0,0 +1,40 @@ +package org.domaindrivenarchitecture.provs.desktop.infrastructure + +import org.domaindrivenarchitecture.provs.framework.core.Prov +import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL + +const val GRAAL_VM_VERSION = "21.0.2" + + +fun Prov.installGraalVM() = taskWithResult { + val tmpDir = "~/tmp" + val filename = "graalvm-community-jdk-" + val additionalPartFilename = "_linux-x64_bin" + val packedFilename = "$filename$GRAAL_VM_VERSION$additionalPartFilename.tar.gz" + val extractedFilenameHunch = "graalvm-community-openjdk-" + val installationPath = "/usr/lib/jvm/" + + if ( GRAAL_VM_VERSION != graalVMVersion() || !chk("ls -d $installationPath$extractedFilenameHunch$GRAAL_VM_VERSION*")) { + downloadFromURL( + "https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-$GRAAL_VM_VERSION/$packedFilename", + path = tmpDir, + sha256sum = "b048069aaa3a99b84f5b957b162cc181a32a4330cbc35402766363c5be76ae48" + ) + if (!chk("ls -d $installationPath")) + cmd("sudo mkdir $installationPath") + else { + ProvResult(true, out = "$installationPath just exists, mkdir not necessary.") + } + cmd("sudo tar -C $installationPath -xzf $packedFilename", tmpDir) + val graalInstPath = installationPath + (cmd("ls /usr/lib/jvm/|grep -e graalvm-community-openjdk-$GRAAL_VM_VERSION").out?.replace("\n", "")) + cmd("sudo ln -sf $graalInstPath/lib/svm/bin/native-image /usr/local/bin/native-image") + cmd("/usr/local/bin/native-image --version") + } else { + ProvResult(true, out = "GraalVM $GRAAL_VM_VERSION already installed") + } +} + +fun Prov.graalVMVersion(): String { + return cmdNoEval("/usr/local/bin/native-image --version|awk 'NR==1 {print $2}'").out?.trim() ?: "" +} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt index f8c24fd..a335f9e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/Prov.kt @@ -80,8 +80,8 @@ open class Prov protected constructor( } /** - * A task is the base execution unit in provs. In the results overview it is represented by one line resp. result (of either success or failure). - * Returns success if no sub-tasks are called or if all subtasks finish with success. + * A task is the fundamental execution unit. In the results overview it is represented by one line with a success or failure result. + * Returns success if all sub-tasks finished with success or if no sub-tasks are called at all. */ fun task(name: String? = null, taskLambda: Prov.() -> Unit): ProvResult { printDeprecationWarningIfLevel0("task") @@ -89,8 +89,10 @@ open class Prov protected constructor( } /** - * Same as task but the provided lambda is explicitly required to provide a ProvResult to be returned. - * The returned result is included in the evaluation. + * Same as task above but the lambda parameter must have a ProvResult as return type. + * The returned ProvResult is included in the success resp. failure evaluation, + * i.e. if the returned ProvResult from the lambda fails, the returned ProvResult from + * taskWithResult also fails, else success depends on potentially called sub-tasks. */ fun taskWithResult(name: String? = null, taskLambda: Prov.() -> ProvResult): ProvResult { printDeprecationWarningIfLevel0("taskWithResult") @@ -98,27 +100,27 @@ open class Prov protected constructor( } /** - * defines a task, which returns the returned result, the results of sub-tasks are not considered + * defines a task, which returns the returned result from the lambda, the results of sub-tasks are not considered */ - fun requireLast(name: String? = null, a: Prov.() -> ProvResult): ProvResult { + fun requireLast(name: String? = null, taskLambda: Prov.() -> ProvResult): ProvResult { printDeprecationWarningIfLevel0("requireLast") - return evaluate(ResultMode.LAST, name) { a() } + return evaluate(ResultMode.LAST, name) { taskLambda() } } /** - * defines a task, which always returns success + * Defines a task, which always returns success. */ - fun optional(name: String? = null, a: Prov.() -> ProvResult): ProvResult { + fun optional(name: String? = null, taskLambda: Prov.() -> ProvResult): ProvResult { printDeprecationWarningIfLevel0("optional") - return evaluate(ResultMode.OPTIONAL, name) { a() } + return evaluate(ResultMode.OPTIONAL, name) { taskLambda() } } /** - * defines a task, which exits the overall execution on failure + * Defines a task, which exits the overall execution on failure result of the taskLambda. */ - fun exitOnFailure(a: Prov.() -> ProvResult): ProvResult { + fun exitOnFailure(taskLambda: Prov.() -> ProvResult): ProvResult { printDeprecationWarningIfLevel0("exitOnFailure") - return evaluate(ResultMode.FAILEXIT) { a() } + return evaluate(ResultMode.FAILEXIT) { taskLambda() } } /** diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K9s.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K9s.kt index 4288327..d1801da 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K9s.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K9s.kt @@ -1,7 +1,6 @@ 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.* import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL @@ -12,10 +11,8 @@ const val K9S_VERSION = "v0.32.5" // ----------------------------------- public functions -------------------------------- -fun Prov.installK9s(): ProvResult { - return taskWithResult { - createDir("/tmp", sudo = true) - downloadFromURL( "https://github.com/derailed/k9s/releases/download/" + K9S_VERSION + "/k9s_linux_amd64.deb", "k9s_linux_amd64.deb", "/tmp") - cmd("sudo dpkg -i k9s_linux_amd64.deb", "/tmp") - } +fun Prov.installK9s() = task { + createDir("/tmp", sudo = true) + downloadFromURL("https://github.com/derailed/k9s/releases/download/" + K9S_VERSION + "/k9s_linux_amd64.deb", "k9s_linux_amd64.deb", "/tmp") + cmd("sudo dpkg -i k9s_linux_amd64.deb", "/tmp") } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt index 8248ea5..d02c088 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOpsKtTest.kt @@ -59,17 +59,4 @@ internal class DevOpsKtTest { assertTrue(res.success) assertTrue(prov.checkFile("/usr/local/bin/kubeconform")) } - - @ContainerTest - fun installGraalVM() { - // given - val prov = defaultTestContainer() - - // when - val res = prov.installGraalVM() - - // then - assertTrue(res.success) - assertTrue(prov.checkFile("/usr/local/bin/native-image")) - } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt new file mode 100644 index 0000000..04957aa --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GraalVMKtTest.kt @@ -0,0 +1,24 @@ +package org.domaindrivenarchitecture.provs.desktop.infrastructure + +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.* + +class GraalVMKtTest { + + @ContainerTest + fun installGraalVM() { + // given + val prov = defaultTestContainer() + + // when + val res = prov.installGraalVM() + + // then + assertTrue(res.success) + assertTrue(GRAAL_VM_VERSION == prov.graalVMVersion()) + assertTrue(prov.checkFile("/usr/local/bin/native-image")) + } +} \ No newline at end of file