From c31d115ea5a3ab47f5f8b76d394fce2e1e02d9b1 Mon Sep 17 00:00:00 2001 From: az Date: Thu, 25 Nov 2021 11:09:26 +0100 Subject: [PATCH] v0.8.22-SNAPSHOT add k3s --- build.gradle | 2 +- .../extensions/server_software/k3s/K3s.kt | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/K3s.kt diff --git a/build.gradle b/build.gradle index 68d6f6a..b45e326 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ apply plugin: 'kotlinx-serialization' group = 'org.domaindrivenarchitecture.provs' -version = '0.8.21' +version = '0.8.22-SNAPSHOT' repositories { mavenCentral() diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/K3s.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/K3s.kt new file mode 100644 index 0000000..bce183d --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/K3s.kt @@ -0,0 +1,55 @@ +package org.domaindrivenarchitecture.provs.extensions.server_software.k3s + +import org.domaindrivenarchitecture.provs.core.Prov +import org.domaindrivenarchitecture.provs.core.ProvResult +import org.domaindrivenarchitecture.provs.core.remote +import org.domaindrivenarchitecture.provs.ubuntu.install.base.aptInstall +import org.domaindrivenarchitecture.provs.ubuntu.secret.secretSources.PromptSecretSource + + +/** + * Installs a k3s server. + * If docker is true, then k3s will be installed with docker option and also docker will be installed (may conflict if docker is already existing). + * If host is specified, then tls (if configured) also applies to host. + */ +fun Prov.installK3sServer(docker: Boolean = false, host: String? = null) = def { + val tls = host?.let { "INSTALL_K3S_EXEC=\"--tls-san ${it}\"" } ?: "" + aptInstall("curl") + if (!chk("k3s -version")) { + if (docker) { + // might not work if docker already installed + sh(""" + curl https://releases.rancher.com/install-docker/19.03.sh | sh + curl -sfL https://get.k3s.io | sh -s - --docker + """.trimIndent()) + } else { + cmd("curl -sfL https://get.k3s.io | $tls sh -") + } + } else { + ProvResult(true) + } +} + + +fun Prov.uninstallK3sServer() = def { + cmd("sudo /usr/local/bin/k3s-uninstall.sh") +} + + +fun main() { + + val host = "192.168.56.123" + val remoteUser = "remoteUsername" + val passwordK3sUser = PromptSecretSource("Enter Password").secret() + + remote(host, remoteUser, passwordK3sUser).def { + + val result = installK3sServer() + + // print pods for information purpose + println(cmd("sudo k3s kubectl get pods --all-namespaces").out) + + // return result of installation + result + } +} \ No newline at end of file