From 1defd46c9790d461b6dca84c6082001ee97c4ab3 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 21 Jan 2022 16:05:09 +0100 Subject: [PATCH] refactor domain & infra for k3s --- .../provs/server/domain/ApplicationService.kt | 5 ++- .../server/domain/ServerApplicationService.kt | 13 ------ .../provs/server/domain/k3s/K3sService.kt | 37 ++------------- .../{domain/k3s => infrastructure}/K3d.kt | 0 .../provs/server/infrastructure/K3s.kt | 45 +++++++++++++++++++ .../{domain => infrastructure}/K3dKtTest.kt | 4 +- 6 files changed, 56 insertions(+), 48 deletions(-) delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt rename src/main/kotlin/org/domaindrivenarchitecture/provs/server/{domain/k3s => infrastructure}/K3d.kt (100%) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt rename src/test/kotlin/org/domaindrivenarchitecture/provs/server/{domain => infrastructure}/K3dKtTest.kt (91%) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt index fdec3ef..0bd753d 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt @@ -1,7 +1,10 @@ package org.domaindrivenarchitecture.provs.server.domain import org.domaindrivenarchitecture.provs.framework.core.Prov +import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s fun provisionServer(prov: Prov, cmd: ServerCliCommand) { - + when(cmd.serverType) { + ServerType.K3S -> prov.provisionK3s() + } } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt deleted file mode 100644 index 8414db2..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.domaindrivenarchitecture.provs.server.application - -import org.domaindrivenarchitecture.provs.framework.core.Prov -import org.domaindrivenarchitecture.provs.server.domain.installK3sServer - - -/** - * Performs use case of provisioning a k3s server - */ -fun Prov.provisionK3s() = task { - installK3sServer() -} - diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt index c60fa1d..3120e0d 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt @@ -1,9 +1,10 @@ -package org.domaindrivenarchitecture.provs.server.domain +package org.domaindrivenarchitecture.provs.server.domain.k3s import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult import org.domaindrivenarchitecture.provs.framework.core.echoCommandForText import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall +import org.domaindrivenarchitecture.provs.server.infrastructure.provisionK3sInfra /** @@ -11,36 +12,6 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInsta * If docker is true, then docker will be installed (may conflict if docker is already existing) and k3s will be installed with docker option. * If tlsHost is specified, then tls (if configured) also applies to the specified host. */ -fun Prov.installK3sServer(docker: Boolean = false, tlsHost: String? = null, options: String? = null) = task { - val tlsSanOption = tlsHost?.let { "--tls-san ${it}" } ?: "" - - val k3sOptions = if (tlsHost == null && options == null) - "" - else - "INSTALL_K3S_EXEC=\"$options $tlsSanOption\"" - - 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 | $k3sOptions sh -s - --docker - """.trimIndent()) - } else { - cmd("curl -sfL https://get.k3s.io | $k3sOptions sh -") - } - } else { - ProvResult(true) - } -} - - -fun Prov.uninstallK3sServer() = task { - cmd("sudo /usr/local/bin/k3s-uninstall.sh") -} - - -fun Prov.applyK3sConfig(configAsYaml: String) = task { - cmd(echoCommandForText(configAsYaml) + " | sudo k3s kubectl apply -f -") +fun Prov.provisionK3s() = task { + provisionK3sInfra() } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3d.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3d.kt similarity index 100% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3d.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3d.kt diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt new file mode 100644 index 0000000..75374b8 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3s.kt @@ -0,0 +1,45 @@ +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.core.echoCommandForText +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall + +/** + * Installs a k3s server. + * If docker is true, then docker will be installed (may conflict if docker is already existing) and k3s will be installed with docker option. + * If tlsHost is specified, then tls (if configured) also applies to the specified host. + */ +fun Prov.provisionK3sInfra(docker: Boolean = false, tlsHost: String? = null, options: String? = null) = task { + val tlsSanOption = tlsHost?.let { "--tls-san ${it}" } ?: "" + + val k3sOptions = if (tlsHost == null && options == null) + "" + else + "INSTALL_K3S_EXEC=\"$options $tlsSanOption\"" + + 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 | $k3sOptions sh -s - --docker + """.trimIndent()) + } else { + cmd("curl -sfL https://get.k3s.io | $k3sOptions sh -") + } + } else { + ProvResult(true) + } +} + + +fun Prov.uninstallK3sServer() = task { + cmd("sudo /usr/local/bin/k3s-uninstall.sh") +} + + +fun Prov.applyK3sConfig(configAsYaml: String) = task { + cmd(echoCommandForText(configAsYaml) + " | sudo k3s kubectl apply -f -") +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/domain/K3dKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3dKtTest.kt similarity index 91% rename from src/test/kotlin/org/domaindrivenarchitecture/provs/server/domain/K3dKtTest.kt rename to src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3dKtTest.kt index 29de71a..6974650 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/domain/K3dKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3dKtTest.kt @@ -1,4 +1,4 @@ -package org.domaindrivenarchitecture.provs.server.domain +package org.domaindrivenarchitecture.provs.server.infrastructure import org.domaindrivenarchitecture.provs.framework.core.docker import org.domaindrivenarchitecture.provs.framework.core.docker.containerExec @@ -7,6 +7,8 @@ import org.domaindrivenarchitecture.provs.framework.core.local import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode import org.domaindrivenarchitecture.provs.server.apple.appleConfig import org.domaindrivenarchitecture.provs.server.apple.checkAppleService +import org.domaindrivenarchitecture.provs.server.domain.applyK8sConfig +import org.domaindrivenarchitecture.provs.server.domain.installK3sAsContainers import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.NonCi import org.junit.jupiter.api.Assertions.assertTrue