refactor scheduleMonthlyReboot to domain

This commit is contained in:
ansgarz 2024-11-29 11:00:29 +01:00
parent cd99efad5b
commit 4f77c2f909
6 changed files with 76 additions and 51 deletions
src
main/kotlin/org/domaindrivenarchitecture/provs
framework/ubuntu/scheduledjobs
domain
infrastructure
server/domain/k3s
test/kotlin/org/domaindrivenarchitecture/provs
framework/ubuntu/scheduledjobs
server/application

View file

@ -0,0 +1,22 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure.createCronJob
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
/**
* Adds a cronJob for a monthly reboot of the (Linux) system.
* ATTENTION: Use with care!!
*/
fun Prov.scheduleMonthlyReboot() = task {
val shutdown = "/sbin/shutdown"
if (checkFile(shutdown, sudo = true)) {
// reboot each first Tuesday in a month at 3:00
// use controlled "shutdown" instead of direct "reboot"
createCronJob("50_monthly_reboot", "0 3 1-7 * 2", "shutdown -r now", "root")
} else {
addResultToEval(ProvResult(false, err = "$shutdown not found."))
}
}

View file

@ -1,8 +1,6 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.cron.infrastructure
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami
@ -22,19 +20,3 @@ fun Prov.createCronJob(cronFilename: String, schedule: String, command: String,
createDirs("/etc/cron.d/", sudo = true)
createFile("/etc/cron.d/$cronFilename", cronLine, "644", sudo = true, overwriteIfExisting = true)
}
/**
* Adds a cronJob for a monthly reboot of the (Linux) system.
* ATTENTION: Use with care!!
*/
fun Prov.scheduleMonthlyReboot() = task {
val shutdown = "/sbin/shutdown"
if (checkFile(shutdown, sudo = true)) {
// reboot each first Tuesday in a month at 3:00
// use controlled "shutdown" instead of direct "reboot"
createCronJob("50_monthly_reboot", "0 3 1-7 * 2", "shutdown -r now", "root")
} else {
addResultToEval(ProvResult(false, err = "$shutdown not found."))
}
}

View file

@ -2,12 +2,23 @@ package org.domaindrivenarchitecture.provs.server.domain.k3s
import org.domaindrivenarchitecture.provs.configuration.infrastructure.DefaultConfigFileRepository
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.ubuntu.cron.infrastructure.scheduleMonthlyReboot
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain.scheduleMonthlyReboot
import org.domaindrivenarchitecture.provs.server.domain.hetzner_csi.HetznerCSIConfigResolved
import org.domaindrivenarchitecture.provs.server.domain.hetzner_csi.provisionHetznerCSI
import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.GrafanaAgentConfigResolved
import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.provisionGrafanaAgent
import org.domaindrivenarchitecture.provs.server.infrastructure.*
import org.domaindrivenarchitecture.provs.server.infrastructure.DefaultApplicationFileRepository
import org.domaindrivenarchitecture.provs.server.infrastructure.deprovisionK3sInfra
import org.domaindrivenarchitecture.provs.server.infrastructure.findHetznerCSIConfig
import org.domaindrivenarchitecture.provs.server.infrastructure.findK8sGrafanaConfig
import org.domaindrivenarchitecture.provs.server.infrastructure.getK3sConfig
import org.domaindrivenarchitecture.provs.server.infrastructure.installK3s
import org.domaindrivenarchitecture.provs.server.infrastructure.installK9s
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionK3sApplication
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionK3sCertManager
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionK3sEcho
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionNetwork
import org.domaindrivenarchitecture.provs.server.infrastructure.provisionServerCliConvenience
import kotlin.system.exitProcess
@ -26,7 +37,7 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task {
} else {
provisionGrafana(cli.onlyModules, grafanaConfigResolved)
provisionHetznerCSI(cli.onlyModules, hcloudConfigResolved)
scheduleMonthlyReboot(cli.onlyModules)
scheduleMonthlyRebootOnly(cli.onlyModules)
}
}
@ -108,7 +119,7 @@ private fun Prov.provisionHetznerCSI(
}
fun Prov.scheduleMonthlyReboot(
fun Prov.scheduleMonthlyRebootOnly(
onlyModules: List<String>?,
) = task {
if (onlyModules != null && onlyModules.contains(ServerOnlyModule.MONTHLY_REBOOT.name.lowercase())) {

View file

@ -0,0 +1,34 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContent
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
class ScheduledJobsKtTest {
@Test
fun tests_scheduleMonthlyReboot() {
// given
val prov = defaultTestContainer()
// create dummy shutdown in test container if missing (containers do usually not have shutdown installed)
prov.createFile(
"/sbin/shutdown",
"dummy file for test of scheduleMonthlyReboot",
sudo = true,
overwriteIfExisting = false
)
// when
val result = prov.scheduleMonthlyReboot()
// then
assertTrue(result.success)
val fqFilename = "/etc/cron.d/50_monthly_reboot"
assertTrue(prov.checkFile(fqFilename), "")
val actualFileContent = prov.fileContent(fqFilename, sudo = true)
assertEquals("0 3 1-7 * 2 root shutdown -r now\n", actualFileContent)
}
}

View file

@ -1,13 +1,13 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.cron.infrastructure
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContent
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
@ -68,28 +68,4 @@ class CronJobsKtTest {
// after a minute check manually if files exist, e.g. with: sudo docker exec provs_test /bin/bash -c "ls -l tmp"
// each minute a new file should be created with the timestamp
}
@Test
fun scheduleMonthlyReboot() {
// given
val prov = defaultTestContainer()
// create dummy shutdown in test container if missing (containers do usually not have shutdown installed)
prov.createFile(
"/sbin/shutdown",
"dummy file for test of scheduleMonthlyReboot",
sudo = true,
overwriteIfExisting = false
)
// when
val result = prov.scheduleMonthlyReboot()
// then
assertTrue(result.success)
val fqFilename = "/etc/cron.d/50_monthly_reboot"
assertTrue(prov.checkFile(fqFilename), "")
val actualFileContent = prov.fileContent(fqFilename, sudo = true)
assertEquals("0 3 1-7 * 2 root shutdown -r now\n", actualFileContent)
}
}

View file

@ -6,7 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.core.ProvResult
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.cron.infrastructure.scheduleMonthlyReboot
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain.scheduleMonthlyReboot
import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s
import org.junit.jupiter.api.Test