refactor server only modules

This commit is contained in:
ansgarz 2024-12-11 22:08:42 +01:00
parent bb383ca274
commit 333c9bf50e
4 changed files with 71 additions and 35 deletions

View file

@ -7,8 +7,8 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.check
/**
* Adds a cronJob for a monthly reboot of the (Linux) system.
* ATTENTION: Use with care!!
* Adds a schedule for a monthly reboot of the (Linux) system.
* ATTENTION: Use with care!! System will be shut down, restart might not succeed in all cases.
*/
fun Prov.scheduleMonthlyReboot() = task {
// use controlled "shutdown" instead of direct "reboot"

View file

@ -3,5 +3,6 @@ package org.domaindrivenarchitecture.provs.server.domain.hetzner_csi
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionHetznerCSIForK8s
fun Prov.provisionHetznerCSI(configResolved: HetznerCSIConfigResolved) =
provisionHetznerCSIForK8s(configResolved.hcloudApiToken, configResolved.encryptionPassphrase)
fun Prov.provisionHetznerCSI(configResolved: HetznerCSIConfigResolved) = task {
provisionHetznerCSIForK8s(configResolved.hcloudApiToken, configResolved.encryptionPassphrase)
}

View file

@ -35,9 +35,13 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task {
val applicationFile = cli.applicationFileName?.let { DefaultApplicationFileRepository(cli.applicationFileName).getFile() }
provisionK3s(k3sConfigReprovision, grafanaConfigResolved, hcloudConfigResolved, applicationFile)
} else {
provisionGrafana(cli.onlyModules, grafanaConfigResolved)
provisionHetznerCSI(cli.onlyModules, hcloudConfigResolved)
scheduleMonthlyRebootOnly(cli.onlyModules)
cli.onlyModules.forEach { module ->
when (module.uppercase()) {
ServerOnlyModule.MONTHLY_REBOOT.name -> scheduleMonthlyReboot()
ServerOnlyModule.HETZNER_CSI.name -> provisionHetznerCSI(hcloudConfigResolved)
ServerOnlyModule.GRAFANA.name -> provisionGrafana(grafanaConfigResolved)
}
}
}
}
@ -91,38 +95,22 @@ fun Prov.provisionK3s(
}
private fun Prov.provisionGrafana(
onlyModules: List<String>?,
grafanaConfigResolved: GrafanaAgentConfigResolved?
) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.GRAFANA.name.lowercase())) {
if (grafanaConfigResolved == null) {
println("ERROR: Could not find grafana config.")
exitProcess(7)
}
provisionGrafanaAgent(grafanaConfigResolved)
if (grafanaConfigResolved == null) {
println("ERROR: Could not find grafana config.")
exitProcess(7)
}
provisionGrafanaAgent(grafanaConfigResolved)
}
private fun Prov.provisionHetznerCSI(
onlyModules: List<String>?,
hetznerCSIConfigResolved: HetznerCSIConfigResolved?
) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.HETZNER_CSI.name.lowercase())) {
if (hetznerCSIConfigResolved == null) {
println("ERROR: Could not find hetznerCSI config.")
exitProcess(7)
}
provisionHetznerCSI(hetznerCSIConfigResolved)
}
}
fun Prov.scheduleMonthlyRebootOnly(
onlyModules: List<String>?,
) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.MONTHLY_REBOOT.name.lowercase())) {
scheduleMonthlyReboot()
}
}

View file

@ -7,11 +7,20 @@ import org.domaindrivenarchitecture.provs.framework.core.local
import org.domaindrivenarchitecture.provs.framework.core.processors.DummyProcessor
import org.domaindrivenarchitecture.provs.framework.core.remote
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain.scheduleMonthlyReboot
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSourceType
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSupplier
import org.domaindrivenarchitecture.provs.server.domain.hetzner_csi.HetznerCSIConfig
import org.domaindrivenarchitecture.provs.server.domain.hetzner_csi.provisionHetznerCSI
import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s
import org.domaindrivenarchitecture.provs.server.infrastructure.findHetznerCSIConfig
import org.junit.jupiter.api.Test
val dummyResult = ProvResult(true, cmd = "mocked command")
class ApplicationKtTest {
@Test
fun test_main_with_only_monthly_reboot() {
// given
@ -26,27 +35,65 @@ class ApplicationKtTest {
mockkStatic(::remote)
every { remote(any(), any(), any(), any()) } returns dummyProv
mockkStatic(Prov::provisionHetznerCSI)
every { any<Prov>().provisionHetznerCSI(any()) } returns dummyResult
mockkStatic(Prov::scheduleMonthlyReboot)
every { any<Prov>().scheduleMonthlyReboot() } returns ProvResult(
true,
cmd = "mocked command"
)
every { any<Prov>().scheduleMonthlyReboot() } returns dummyResult
mockkStatic(Prov::provisionK3s)
every { any<Prov>().provisionK3s(any(), any(), any(), any()) } returns ProvResult(
true,
cmd = "mocked command"
)
every { any<Prov>().provisionK3s(any(), any(), any(), any()) } returns dummyResult
// when
main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "monthly_reboot"))
//then
verify(exactly = 1) { any<Prov>().scheduleMonthlyReboot() }
verify(exactly = 0) { any<Prov>().provisionHetznerCSI(any()) }
verify(exactly = 0) { any<Prov>().provisionK3s(any(), any(), any(), any()) }
// cleanup
unmockkAll()
}
@Test
fun test_main_with_only_hetzner_csi() {
// given
val dummyProv = Prov.newInstance(DummyProcessor())
mockkObject(Prov)
every { Prov.newInstance(any(), any(), any(), any()) } returns dummyProv
mockkStatic(::local)
every { local() } returns dummyProv
mockkStatic(::remote)
every { remote(any(), any(), any(), any()) } returns dummyProv
mockkStatic(::findHetznerCSIConfig)
every { findHetznerCSIConfig(any()) } returns HetznerCSIConfig(
hcloudApiToken = SecretSupplier(SecretSourceType.PLAIN,"dummy"),
encryptionPassphrase = SecretSupplier(SecretSourceType.PLAIN,"dummy"),
)
mockkStatic(Prov::provisionHetznerCSI)
every { any<Prov>().provisionHetznerCSI(any()) } returns dummyResult
mockkStatic(Prov::scheduleMonthlyReboot)
every { any<Prov>().scheduleMonthlyReboot() } returns dummyResult
mockkStatic(Prov::provisionK3s)
every { any<Prov>().provisionK3s(any(), any(), any(), any()) } returns dummyResult
// when
main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "hetzner_csi"))
//then
verify(exactly = 1) { any<Prov>().provisionHetznerCSI(any()) }
verify(exactly = 0) { any<Prov>().scheduleMonthlyReboot() }
verify(exactly = 0) { any<Prov>().provisionK3s(any(), any(), any(), any()) }
// cleanup
unmockkAll()
}
}