diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobs.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobs.kt index 404f53f..1705e51 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobs.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobs.kt @@ -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" diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/hetzner_csi/HetznerCSI.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/hetzner_csi/HetznerCSI.kt index 2d5df8b..946b70e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/hetzner_csi/HetznerCSI.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/hetzner_csi/HetznerCSI.kt @@ -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) \ No newline at end of file +fun Prov.provisionHetznerCSI(configResolved: HetznerCSIConfigResolved) = task { + provisionHetznerCSIForK8s(configResolved.hcloudApiToken, configResolved.encryptionPassphrase) +} \ No newline at end of file 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 745d05c..3d308bc 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 @@ -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?, 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?, 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?, -) = task { - if (onlyModules != null && onlyModules.contains(ServerOnlyModule.MONTHLY_REBOOT.name.lowercase())) { - scheduleMonthlyReboot() - } -} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt index 800a7d1..d2d67e7 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt @@ -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().provisionHetznerCSI(any()) } returns dummyResult + mockkStatic(Prov::scheduleMonthlyReboot) - every { any().scheduleMonthlyReboot() } returns ProvResult( - true, - cmd = "mocked command" - ) + every { any().scheduleMonthlyReboot() } returns dummyResult mockkStatic(Prov::provisionK3s) - every { any().provisionK3s(any(), any(), any(), any()) } returns ProvResult( - true, - cmd = "mocked command" - ) + every { any().provisionK3s(any(), any(), any(), any()) } returns dummyResult // when main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "monthly_reboot")) //then verify(exactly = 1) { any().scheduleMonthlyReboot() } + verify(exactly = 0) { any().provisionHetznerCSI(any()) } verify(exactly = 0) { any().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().provisionHetznerCSI(any()) } returns dummyResult + + mockkStatic(Prov::scheduleMonthlyReboot) + every { any().scheduleMonthlyReboot() } returns dummyResult + + mockkStatic(Prov::provisionK3s) + every { any().provisionK3s(any(), any(), any(), any()) } returns dummyResult + + // when + main(arrayOf("k3s", "user123:pw@host123.meissa", "-o", "hetzner_csi")) + + //then + verify(exactly = 1) { any().provisionHetznerCSI(any()) } + verify(exactly = 0) { any().scheduleMonthlyReboot() } + verify(exactly = 0) { any().provisionK3s(any(), any(), any(), any()) } + + // cleanup + unmockkAll() + } } \ No newline at end of file