purpleidea / mgmt

Next generation distributed, event-driven, parallel config management!
https://purpleidea.com/tags/mgmtconfig/
GNU General Public License v3.0
3.58k stars 311 forks source link

Add trigger watch to exec command #772

Open Hiexy opened 6 days ago

Hiexy commented 6 days ago

In the exec resource, we sometimes resort to creating a trigger to know to run some commands if a file has been modified. A trigger file is created to compare the modified time of the file and the trigger file that has been created, and then the command is run based on that check.

For example, this exec resource is complex

$watch_file = "/path/to/watch_file"
$trigger_file = "/path/to/trigger_file"

file "${trigger_file} {
        state => $const.res.file.state.exists,
}

exec "foo" {
        watchcmd => "/usr/bin/inotifywait -e modify -m ${watch_file}",
        ifcmd => "test '${virtual_file}' -nt '${trigger_file}'",
        ifshell => "/bin/bash",
        cmd => "/path/to/bin ${watch_file}",
        donecmd => "/usr/bin/touch '${trigger_file}'",
    }

Can be written like this instead

exec "foo" {
        cmd => "/path/to/some/command',
        trigger_watch => "/etc/postfix/virtual",
}

The trigger_watch option would take in a file that it should watch. Mgmt would create the trigger file, manage it, and mgmt would do the checks on that file and run the command if a change has been made.

purpleidea commented 6 days ago

Thanks for posting the idea!

As we write more modules, we'll see if we see this pattern over and over again, and if so, it might be worth implementing something like this!

Some technical underpinnings: We may wish to implement this as a combination of watchfiles (to watch the input file) and donecmd (to touch the mtime file which we could hide in the res $vardir) and ifcmd which we have an mtime check done automatically with golang stat.

purpleidea commented 6 days ago

Note watchfiles is described in https://github.com/purpleidea/mgmt/issues/771

If we could come up with an elegant API, we may similarly want to implement an mtimeif so that we can compare mtimes without using bash.

On bikeshedding: I don't love the name trigger_watch, if we get there, we should consider what to do instead. Maybe trigger_file ? Or mtime_file or ???

purpleidea commented 6 days ago

(And lastly, all the autoedges magic could be done here too!)