Merge branch 'master' of gitlab.com:domaindrivenarchitecture/provs

This commit is contained in:
see 2022-04-08 11:26:07 +02:00
commit 9f4d40ae6a
7 changed files with 140 additions and 12 deletions

View file

@ -86,6 +86,39 @@ fun Prov.installKubectlAndTools(): ProvResult = task {
)
}
task("install k8sConnect") {
val k8sConnectFile = "/usr/local/bin/k8s-connect.sh"
createFileFromResource(
k8sConnectFile,
"k8s-connect.sh",
resourcePath,
"555",
sudo = true
)
}
task("install k3sCreateContext") {
val k3sContextFile = "/usr/local/bin/k3s-create-context.sh"
createFileFromResource(
k3sContextFile,
"k3s-create-context.sh",
resourcePath,
"555",
sudo = true
)
}
task("install k3sConnect") {
val k3sConnectFile = "/usr/local/bin/k3s-connect.sh"
createFileFromResource(
k3sConnectFile,
"k3s-connect.sh",
resourcePath,
"555",
sudo = true
)
}
}
fun Prov.installTerraform(): ProvResult = task {

View file

@ -10,20 +10,29 @@ private var aptInit = false
* Installs package(s) by using package manager "apt".
*
* @param packages the packages to be installed, packages must be separated by space if there are more than one
* @param ignoreAlreadyInstalled if true, then for an already installed package no action will be taken,
* if "ignoreAlreadyInstalled" is false, then installation is always attempted, which normally results in an upgrade if package wa already installed
*/
fun Prov.aptInstall(packages: String): ProvResult = task {
fun Prov.aptInstall(packages: String, ignoreAlreadyInstalled: Boolean = true): ProvResult = task {
val packageList = packages.split(" ")
val allInstalled: Boolean = packageList.map { isPackageInstalled(it) }.fold(true, { a, b -> a && b })
if (!allInstalled) {
if (!isPackageInstalled(packages)) {
if (!aptInit) {
cmd("sudo apt-get update")
cmd("sudo apt-get install -qy apt-utils")
aptInit = true
}
}
val packageList = packages.split(" ")
for (packg in packageList) {
// see https://superuser.com/questions/164553/automatically-answer-yes-when-using-apt-get-install
cmd("sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy $packg")
}
ProvResult(true) // dummy
} else {
ProvResult(true, out = "All packages are already installed. [$packages]")
}
}

View file

@ -0,0 +1,19 @@
#!/bin/bash
set -e
set -o noglob
function main() {
local cluster_name="${1}"; shift
/usr/local/bin/k3s-create-context.sh ${cluster_name}
kubectl config use-context ${cluster_name}
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@${cluster_name}.meissa-gmbh.de -L 8002:localhost:8002 -L 6443:192.168.5.1:6443
}
if [ $# -eq 1 ]
then
main $1
else
echo "Requires argument cluster_name in server fqdn {cluster_name}.meissa-gmbh.de"
exit -1
fi

View file

@ -0,0 +1,27 @@
#!/bin/bash
set -e
set -o noglob
function main() {
local cluster_name="${1}"; shift
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@${cluster_name}.meissa-gmbh.de \
"cat /etc/rancher/k3s/k3s.yaml" | \
yq e ".clusters[0].name=\"${cluster_name}\" \
| .clusters[0].cluster.server=\"https://kubernetes:6443\" \
| .contexts[0].context.cluster=\"${cluster_name}\" \
| .contexts[0].context.user=\"${cluster_name}\" \
| .contexts[0].name=\"${cluster_name}\" \
| del(.current-context) \
| del(.preferences) \
| .users[0].name=\"${cluster_name}\"" - \
> ~/.kube/custom-contexts/${cluster_name}.yml
}
if [ $# -eq 1 ]
then
main $1
else
echo "Requires argument cluster_name in server fqdn {cluster_name}.meissa-gmbh.de"
exit -1
fi

View file

@ -0,0 +1,19 @@
#!/bin/bash
set -e
set -o noglob
function main() {
local cluster_name="${1}"; shift
/usr/local/bin/k8s-create-context.sh ${cluster_name}
kubectl config use-context ${cluster_name}
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@${cluster_name}.meissa-gmbh.de -L 8002:localhost:8002 -L 6443:192.168.5.1:6443
}
if [ $# -eq 1 ]
then
main $1
else
echo "Requires argument cluster_name in server fqdn {cluster_name}.meissa-gmbh.de"
exit -1
fi

View file

@ -18,4 +18,10 @@ function main() {
> ~/.kube/custom-contexts/${cluster_name}.yml
}
if [ $# -eq 1 ]
then
main $1
else
echo "Requires argument cluster_name in server fqdn {cluster_name}.meissa-gmbh.de"
exit -1
fi

View file

@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.install.base
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
@ -22,6 +23,20 @@ internal class InstallKtTest {
assertTrue(res.success)
}
@ContainerTest
@Test
fun aptInstall_ignores_packages_already_installed() {
// given
val a = defaultTestContainer()
// when
val res = a.aptInstall("sed grep")
// then
assertTrue(res.success)
assertEquals("All packages are already installed. [sed grep]", res.out)
}
@ContainerTest
@Test
@Disabled // run manually if needed;