feat(ansible): support cron special_time schedules
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
fd6b8b0d2f
commit
2927fb4c78
3 changed files with 25 additions and 0 deletions
|
|
@ -1564,6 +1564,7 @@ func moduleCronWithClient(_ *Executor, client sshRunner, args map[string]any) (*
|
|||
state := getStringArg(args, "state", "present")
|
||||
user := getStringArg(args, "user", "root")
|
||||
disabled := getBoolArg(args, "disabled", false)
|
||||
specialTime := getStringArg(args, "special_time", "")
|
||||
|
||||
minute := getStringArg(args, "minute", "*")
|
||||
hour := getStringArg(args, "hour", "*")
|
||||
|
|
@ -1583,6 +1584,9 @@ func moduleCronWithClient(_ *Executor, client sshRunner, args map[string]any) (*
|
|||
|
||||
// Build cron entry
|
||||
schedule := sprintf("%s %s %s %s %s", minute, hour, day, month, weekday)
|
||||
if specialTime != "" {
|
||||
schedule = "@" + specialTime
|
||||
}
|
||||
entry := sprintf("%s %s # %s", schedule, job, name)
|
||||
if disabled {
|
||||
entry = "# " + entry
|
||||
|
|
|
|||
|
|
@ -2768,6 +2768,7 @@ func (e *Executor) moduleCron(ctx context.Context, client sshExecutorClient, arg
|
|||
state := getStringArg(args, "state", "present")
|
||||
user := getStringArg(args, "user", "root")
|
||||
disabled := getBoolArg(args, "disabled", false)
|
||||
specialTime := getStringArg(args, "special_time", "")
|
||||
|
||||
minute := getStringArg(args, "minute", "*")
|
||||
hour := getStringArg(args, "hour", "*")
|
||||
|
|
@ -2787,6 +2788,9 @@ func (e *Executor) moduleCron(ctx context.Context, client sshExecutorClient, arg
|
|||
|
||||
// Build cron entry
|
||||
schedule := sprintf("%s %s %s %s %s", minute, hour, day, month, weekday)
|
||||
if specialTime != "" {
|
||||
schedule = "@" + specialTime
|
||||
}
|
||||
entry := sprintf("%s %s # %s", schedule, job, name)
|
||||
if disabled {
|
||||
entry = "# " + entry
|
||||
|
|
|
|||
|
|
@ -487,6 +487,23 @@ func TestModulesAdv_ModuleCron_Good_CustomUser(t *testing.T) {
|
|||
assert.True(t, mock.containsSubstring("0 */4 * * *"))
|
||||
}
|
||||
|
||||
func TestModulesAdv_ModuleCron_Good_SpecialTime(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
mock := NewMockSSHClient()
|
||||
mock.expectCommand(`crontab -u root`, "", "", 0)
|
||||
|
||||
result, err := e.moduleCron(context.Background(), mock, map[string]any{
|
||||
"name": "daily-backup",
|
||||
"job": "/usr/local/bin/backup.sh",
|
||||
"special_time": "daily",
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result.Changed)
|
||||
assert.False(t, result.Failed)
|
||||
assert.True(t, mock.containsSubstring(`@daily /usr/local/bin/backup.sh # daily-backup`))
|
||||
}
|
||||
|
||||
func TestModulesAdv_ModuleCron_Good_DisabledJobCommentsEntry(t *testing.T) {
|
||||
e, mock := newTestExecutorWithMock("host1")
|
||||
mock.expectCommand(`crontab -u root`, "", "", 0)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue