From d50f61a4d6b8da61cab96f83d89136246a233351 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 20 Dec 2024 13:02:26 +0100 Subject: [PATCH] ddd refactoring ScheduledJobs.kt --- .../scheduledjobs/domain/ScheduledJobs.kt | 14 ++----------- .../scheduledjobs/infrastructure/CronJobs.kt | 15 ++++++++++---- .../infrastructure/CronJobsKtTest.kt | 20 ++++++++++++++++--- 3 files changed, 30 insertions(+), 19 deletions(-) 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 1705e51..cd67cef 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 @@ -1,9 +1,7 @@ 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 /** @@ -11,14 +9,6 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.check * 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" - val shutdown = "/sbin/shutdown" - - // ensure shutdown command exists - if (checkFile(shutdown, sudo = true)) { - // reboot each first Tuesday in a month at 3:00 - createCronJob("50_monthly_reboot", "0 2 1-7 * 2", "$shutdown -r now", "root") - } else { - addResultToEval(ProvResult(false, err = "$shutdown not found.")) - } + // reboot each first Tuesday in a month at 3:00 + createCronJob("50_monthly_reboot", "0 2 1-7 * 2", "/sbin/shutdown", "-r now", "root") } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt index 40ce7b7..8f67a6f 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobs.kt @@ -1,6 +1,8 @@ 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 @@ -13,10 +15,15 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami * @param command the executed command * @param user the user with whom the command will be executed, if null the current user is used */ -fun Prov.createCronJob(cronFilename: String, schedule: String, command: String, user: String? = null) = task { +fun Prov.createCronJob(cronFilename: String, schedule: String, command: String, parameter: String, user: String? = null) = task { val cronUser = user ?: whoami() - val cronLine = "$schedule $cronUser $command\n" - createDirs("/etc/cron.d/", sudo = true) - createFile("/etc/cron.d/$cronFilename", cronLine, "644", sudo = true, overwriteIfExisting = true) + // ensure command exists + if (checkFile(command, sudo = true)) { + val cronLine = "$schedule $cronUser $command $parameter\n" + createDirs("/etc/cron.d/", sudo = true) + createFile("/etc/cron.d/$cronFilename", cronLine, "644", sudo = true, overwriteIfExisting = true) + } else { + addResultToEval(ProvResult(false, err = "$command not found.")) + } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt index 4396e67..cd3338a 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/scheduledjobs/infrastructure/CronJobsKtTest.kt @@ -19,7 +19,7 @@ class CronJobsKtTest { val cronFilename = "50_test_cron" // when - val result = prov.createCronJob(cronFilename, "0 * * * *", "echo hello > /dev/null 2>&1") + val result = prov.createCronJob(cronFilename, "0 * * * *", "/usr/bin/echo", "hello > /dev/null 2>&1") // then assertTrue(result.success) @@ -27,7 +27,21 @@ class CronJobsKtTest { assertTrue(prov.checkFile(fqFilename), "") val actualFileContent = prov.fileContent(fqFilename, sudo = true) val expectedUser = prov.whoami() - assertEquals("0 * * * * $expectedUser echo hello > /dev/null 2>&1\n", actualFileContent) + assertEquals("0 * * * * $expectedUser /usr/bin/echo hello > /dev/null 2>&1\n", actualFileContent) + } + + @ContainerTest + fun createCronJob_is_not_created_due_to_command_not_found() { + // given + val prov = defaultTestContainer() + val cronFilename = "88_test_cron" + + // when + val result = prov.createCronJob(cronFilename, "0 * * * *", "/usr/bin/dontexist", "") + + // then + assertTrue(!result.success) + assertTrue(!prov.checkFile(cronFilename), "File should not exist: $cronFilename") } @@ -56,7 +70,7 @@ class CronJobsKtTest { val result = prov.createCronJob( cronFilename, "*/1 * * * *", - "echo \"xxx\" > /home/$user/tmp/\$(/usr/bin/date +\\%Y_\\%m_\\%d-\\%H_\\%M)" + "/usr/bin/echo", "\"xxx\" > /home/$user/tmp/\$(/usr/bin/date +\\%Y_\\%m_\\%d-\\%H_\\%M)" ) // then