From 862f40432c2f33cd3fbd38f34a538ba81af982a3 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 24 Sep 2021 17:09:17 +0200 Subject: [PATCH] add python, bashrc.d & devops install refactor gopass --- .../provs/workplace/infrastructure/Bash.kt | 35 ++++++++++ .../provs/workplace/infrastructure/DevOps.kt | 65 ++++++++++++++++++- .../infrastructure}/Gopass.kt | 0 .../provs/workplace/infrastructure/Python.kt | 41 ++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Bash.kt rename src/main/kotlin/org/domaindrivenarchitecture/provs/{extensions/workplace/base => workplace/infrastructure}/Gopass.kt (100%) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Python.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Bash.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Bash.kt new file mode 100644 index 0000000..d8dd4a7 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Bash.kt @@ -0,0 +1,35 @@ +package org.domaindrivenarchitecture.provs.workplace.infrastructure + +import org.domaindrivenarchitecture.provs.core.Prov +import org.domaindrivenarchitecture.provs.core.ProvResult +import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.* +import org.domaindrivenarchitecture.provs.ubuntu.install.base.aptInstall +import java.io.File + + +fun Prov.installBash() = def { + installBashForUser() +} + +fun Prov.installBashForUser(): ProvResult = def { + var dirname = "~/.bashrd.d" + if(!dirExists(dirname)) { + createDir(dirname) + cmd("chmod 755 " + dirname) + aptInstall("bash-completion screen") + + var enhance = """ + # source .bashrc.d files + if [ -d ~/.bashrc.d ]; then + for i in ~/.bashrc.d/*.sh; do + if [ -r \$\{i} ]; then + . \\\$\{i} + fi + done + unset i + fi """.trimIndent() + addTextToFile(text = enhance, file = File("~/.bashrc")) + } else { + ProvResult(true) + } +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/DevOps.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/DevOps.kt index 23d890c..60cb469 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/DevOps.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/DevOps.kt @@ -5,11 +5,74 @@ import org.domaindrivenarchitecture.provs.core.ProvResult import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.createDirs import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.createFile import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.dirExists +import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.fileExists +import org.domaindrivenarchitecture.provs.ubuntu.install.base.aptInstall +import org.domaindrivenarchitecture.provs.ubuntu.web.base.downloadFromURL fun Prov.installDevOps() = def { installTerraform() - installAwsCredentials("", "") // TODO: get credentials from gopass + //installAwsCredentials("", "") // TODO: get credentials from gopass + installKubectl() + installYq() +} + +fun Prov.installYq( + version: String = "4.13.2", + sha256sum: String = "d7c89543d1437bf80fee6237eadc608d1b121c21a7cbbe79057d5086d74f8d79" +): ProvResult = def { + var path = "/usr/bin/" + var filename = "yq" + if(!fileExists(path + filename)) { + val result = downloadFromURL( + "https://github.com/mikefarah/yq/releases/download/v$version/yq_linux_amd64", + filename, + path, + sha256sum = sha256sum, + sudo = true + ) + cmd("chmod +x " + path + filename, sudo = true) + } else { + ProvResult(true) + } +} + +fun Prov.installKubectl(): ProvResult = def { + var kubeConfigFile = "~/.bashrc.d/kubectl.sh" + if(!fileExists(kubeConfigFile)) { + var kubeConfig = """ + # Set the default kube context if present + DEFAULT_KUBE_CONTEXTS="\$\{HOME}/.kube/config" + if test -f "\$\{DEFAULT_KUBE_CONTEXTS}" + then + export KUBECONFIG="\$\{DEFAULT_KUBE_CONTEXTS}" + fi + + # Additional contexts should be in ~/.kube/custom-contexts/ + CUSTOM_KUBE_CONTEXTS="\$\{HOME}/.kube/custom-contexts" + mkdir -p "\$\{CUSTOM_KUBE_CONTEXTS}" + + OIFS="\$\{IFS}" + IFS=$'\n' + for contextFile in `find "\$\{CUSTOM_KUBE_CONTEXTS}" -type f -name "*.yml"` + do + export KUBECONFIG="\$\{contextFile}:\$\{KUBECONFIG}" + done + IFS="\$\{OIFS}" + """.trimIndent() + createFile(kubeConfigFile, kubeConfig, "640") + + var tunnelAlias = """ + alias sshu='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' + alias ssht='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -L 8002:localhost:8002 -L 6443:192.168.5.1:6443' + """.trimIndent() + createFile("~/.bashrc.d/ssh_alias.sh", tunnelAlias, "640") + + aptInstall("kubectl") + cmd("kubectl completion bash >> /etc/bash_completion.d/kubernetes", sudo = true) + } else { + ProvResult(true) + } } fun Prov.installTerraform(): ProvResult = def { diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/workplace/base/Gopass.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Gopass.kt similarity index 100% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/extensions/workplace/base/Gopass.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Gopass.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Python.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Python.kt new file mode 100644 index 0000000..0fbf751 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/workplace/infrastructure/Python.kt @@ -0,0 +1,41 @@ +package org.domaindrivenarchitecture.provs.workplace.infrastructure + +import org.domaindrivenarchitecture.provs.core.Prov +import org.domaindrivenarchitecture.provs.core.ProvResult +import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.createDirs +import org.domaindrivenarchitecture.provs.ubuntu.filesystem.base.dirExists +import org.domaindrivenarchitecture.provs.ubuntu.install.base.aptInstall + +fun Prov.installPython() = def { + installPython3() + installVenv() + installPybuilder() + installRestClient() + installJupyterlab() +} + + + +fun Prov.installPython3(): ProvResult = def { + aptInstall("python3.8-venv") +} + +fun Prov.installVenv(): ProvResult = def { + var venvHome = "~/.python/meissa" + cmd("python3 -m venv " + venvHome) + cmd("source " + venvHome + "/bin/activate") + cmd("pip install pip --upgrade") +} + +fun Prov.installPybuilder(): ProvResult = def { + cmd("pip install pybuilder ddadevops pypandoc mockito coverage unittest-xml-reporting deprecation python_terraform " + + "boto3") +} + +fun Prov.installRestClient(): ProvResult = def { + cmd("pip install requests") +} + +fun Prov.installJupyterlab(): ProvResult = def { + cmd("pip install jupyterlab pandas matplotlib") +}