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. * Adds a schedule for a monthly reboot of the (Linux) system.
* ATTENTION: Use with care!! * ATTENTION: Use with care!! System will be shut down, restart might not succeed in all cases.
*/ */
fun Prov.scheduleMonthlyReboot() = task { fun Prov.scheduleMonthlyReboot() = task {
// use controlled "shutdown" instead of direct "reboot" // 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.framework.core.Prov
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionHetznerCSIForK8s import org.domaindrivenarchitecture.provs.server.infrastructure.provisionHetznerCSIForK8s
fun Prov.provisionHetznerCSI(configResolved: HetznerCSIConfigResolved) = fun Prov.provisionHetznerCSI(configResolved: HetznerCSIConfigResolved) = task {
provisionHetznerCSIForK8s(configResolved.hcloudApiToken, configResolved.encryptionPassphrase) 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() } val applicationFile = cli.applicationFileName?.let { DefaultApplicationFileRepository(cli.applicationFileName).getFile() }
provisionK3s(k3sConfigReprovision, grafanaConfigResolved, hcloudConfigResolved, applicationFile) provisionK3s(k3sConfigReprovision, grafanaConfigResolved, hcloudConfigResolved, applicationFile)
} else { } else {
provisionGrafana(cli.onlyModules, grafanaConfigResolved) cli.onlyModules.forEach { module ->
provisionHetznerCSI(cli.onlyModules, hcloudConfigResolved) when (module.uppercase()) {
scheduleMonthlyRebootOnly(cli.onlyModules) ServerOnlyModule.MONTHLY_REBOOT.name -> scheduleMonthlyReboot()
ServerOnlyModule.HETZNER_CSI.name -> provisionHetznerCSI(hcloudConfigResolved)
ServerOnlyModule.GRAFANA.name -> provisionGrafana(grafanaConfigResolved)
}
}
} }
} }
@ -91,25 +95,18 @@ fun Prov.provisionK3s(
} }
private fun Prov.provisionGrafana( private fun Prov.provisionGrafana(
onlyModules: List<String>?,
grafanaConfigResolved: GrafanaAgentConfigResolved? grafanaConfigResolved: GrafanaAgentConfigResolved?
) = task { ) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.GRAFANA.name.lowercase())) {
if (grafanaConfigResolved == null) { if (grafanaConfigResolved == null) {
println("ERROR: Could not find grafana config.") println("ERROR: Could not find grafana config.")
exitProcess(7) exitProcess(7)
} }
provisionGrafanaAgent(grafanaConfigResolved) provisionGrafanaAgent(grafanaConfigResolved)
} }
}
private fun Prov.provisionHetznerCSI( private fun Prov.provisionHetznerCSI(
onlyModules: List<String>?,
hetznerCSIConfigResolved: HetznerCSIConfigResolved? hetznerCSIConfigResolved: HetznerCSIConfigResolved?
) = task { ) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.HETZNER_CSI.name.lowercase())) {
if (hetznerCSIConfigResolved == null) { if (hetznerCSIConfigResolved == null) {
println("ERROR: Could not find hetznerCSI config.") println("ERROR: Could not find hetznerCSI config.")
exitProcess(7) exitProcess(7)
@ -117,12 +114,3 @@ private fun Prov.provisionHetznerCSI(
provisionHetznerCSI(hetznerCSIConfigResolved) 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.processors.DummyProcessor
import org.domaindrivenarchitecture.provs.framework.core.remote import org.domaindrivenarchitecture.provs.framework.core.remote
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain.scheduleMonthlyReboot 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.domain.k3s.provisionK3s
import org.domaindrivenarchitecture.provs.server.infrastructure.findHetznerCSIConfig
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
val dummyResult = ProvResult(true, cmd = "mocked command")
class ApplicationKtTest { class ApplicationKtTest {
@Test @Test
fun test_main_with_only_monthly_reboot() { fun test_main_with_only_monthly_reboot() {
// given // given
@ -26,27 +35,65 @@ class ApplicationKtTest {
mockkStatic(::remote) mockkStatic(::remote)
every { remote(any(), any(), any(), any()) } returns dummyProv every { remote(any(), any(), any(), any()) } returns dummyProv
mockkStatic(Prov::provisionHetznerCSI)
every { any<Prov>().provisionHetznerCSI(any()) } returns dummyResult
mockkStatic(Prov::scheduleMonthlyReboot) mockkStatic(Prov::scheduleMonthlyReboot)
every { any<Prov>().scheduleMonthlyReboot() } returns ProvResult( every { any<Prov>().scheduleMonthlyReboot() } returns dummyResult
true,
cmd = "mocked command"
)
mockkStatic(Prov::provisionK3s) mockkStatic(Prov::provisionK3s)
every { any<Prov>().provisionK3s(any(), any(), any(), any()) } returns ProvResult( every { any<Prov>().provisionK3s(any(), any(), any(), any()) } returns dummyResult
true,
cmd = "mocked command"
)
// when // when
main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "monthly_reboot")) main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "monthly_reboot"))
//then //then
verify(exactly = 1) { any<Prov>().scheduleMonthlyReboot() } verify(exactly = 1) { any<Prov>().scheduleMonthlyReboot() }
verify(exactly = 0) { any<Prov>().provisionHetznerCSI(any()) }
verify(exactly = 0) { any<Prov>().provisionK3s(any(), any(), any(), any()) } verify(exactly = 0) { any<Prov>().provisionK3s(any(), any(), any(), any()) }
// cleanup // cleanup
unmockkAll() 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()
}
} }