thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
221 stars 54 forks source link

Configuration management plugin to support user specified commands to apply downloaded config files #1121

Open albinsuresh opened 2 years ago

albinsuresh commented 2 years ago

Is your feature request related to a problem? Please describe.

The current c8y_configuration_management plugin sends a notification on tedge/configuration_change topic after a downloaded configuration file is placed in the config location, so that, some other user process can consume this MQTT notification and do the needful to apply that updated configuration.

Typically, the updated configuration is applied with a service reload(systemctl reload <service-name>) or restart(systemctl restart <service-name>). It would be too much work for the end-user to write dedicated scripts/programs that listens to these MQTT notifications just to call systemctl restart <service-name> in response to that.

It would have been nice if the c8y_configuration_management plugin supported a way to run such user defined commands after a configuration file is updated by it (after a download).

Describe the solution you'd like

Provide a way to define an optional command field in the the config management plugin configuration file as follows:

files = [
    { path = '/etc/mosquitto/mosquitto.conf', command = ''systemctl reload mosquitto" },
    { path = '/etc/tedge/tedge.toml', command="systemctl restart tedge-agent" },
    { path = '/some/other/config', command="some/script/with/multiple/command" },
    { path = '/some/k8s/deployment/config.yaml', command="kubectl apply /some/k8s/deployment/config.yaml" },
  ]

The plugin just needs to execute the provided command after the configuration file is updated, in addition to the MQTT notification that it's already sending.

This approach can be further extended by adding support for a target-path field as well, which is where the configuration plugin will keep the downloaded configuration file as follows:

files = [
    { path = '/etc/tedge/tedge.toml', target-path = "/tmp/mosquitto.conf", command="systemctl restart tedge-agent" },
  ]

When the provided command is executed, that command can validate the downloaded configuration file contents for sanity, before it is "applied" to the actual config location defined as path. If the target-path is not provided, the dowloaded file will directly be placed at path.

albinsuresh commented 2 years ago

Simple command executions like the ones shown above would be fine. But, if users start over using it by providing chained commands with && or piped commands with | etc, the handling of such complex commands would be really tricky and even error prone. We might even take a stance that the usage of such special characters are not supported and writing a custom script is recommended to wrap such complex logic.

hansdaniels commented 2 years ago

I like the idea of customizable "reload" commands 👍