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 new file mode 100644 index 0000000..edebd50 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobs.kt @@ -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.")) + } +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobs.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt similarity index 58% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobs.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt index f96ff0f..40ce7b7 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobs.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt @@ -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.")) - } -} 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 85e7dcb..745d05c 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 @@ -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())) { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobsKtTest.kt new file mode 100644 index 0000000..06c454a --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/domain/ScheduledJobsKtTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt similarity index 72% rename from src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobsKtTest.kt rename to src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt index 8944d09..c2b4535 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/cron/infrastructure/CronJobsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt @@ -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) - } } \ No newline at end of file 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 3fc9dc7..800a7d1 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/ApplicationKtTest.kt @@ -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