Device lifecycle hooks
The Red Hat Edge Manager agent can run user-defined commands at specific points in the device lifecycle by using device lifecycle hooks.
For example, you can add a shell script to your operating system images that backs up your application data. You can then specify that the script must run and complete successfully before the agent can start updating the operating system.
As another example, certain applications or system services do not automatically reload their configuration file when the file changes on the disk. You can manually reload the configuration file by specifying a command as another hook, which is called after the agent completes the update process.
The Red Hat Edge Manager agent discovers hook rules only in subdirectories of /etc/flightctl/hooks.d/ and /usr/lib/flightctl/hooks.d/. Those subdirectory names are case-sensitive and must be all lowercase. For example, afterUpdating is not valid; use afterupdating.
The following subdirectories are supported:
hooks.d subdirectory |
Description |
|---|---|
|
|
The hook is called after the agent completes preparing for the update, but before changing the operating system. If an action in this hook returns with a failure, the agent cancels the update. |
|
|
The hook is called after the agent writes the update to disk. If an action in this hook returns with a failure, the agent cancels and rolls back the update. |
|
|
The hook is called before the system reboots. The agent blocks the reboot until the action completes or times out. If any action in this hook returns with a failure, the agent cancels and rolls back the update. |
|
|
The hook is called when the agent first starts after a reboot. If any action in this hook returns with a failure, the agent reports the failure but continues starting up. |
Rule files
You can define device lifecycle hooks by adding rule files under one of the following directory trees on the device:
/usr/lib/flightctl/hooks.d/<hook_directory>/: Read-only from the running system; add rules here by baking them into the operating system image./etc/flightctl/hooks.d/<hook_directory>/: Read-writable at runtime; you can update these rules by several methods.
In both paths, replace <hook_directory> with one of beforeupdating, afterupdating, beforerebooting, or afterrebooting. For example, a custom rule that runs after the update is written to disk belongs in a file such as /etc/flightctl/hooks.d/afterupdating/50-my-hook.yaml.
When creating and placing the files, you must consider the following practices:
- The name of the rule must be all lower case.
- The hook subdirectory name under
hooks.d/must be one of the lowercase names in the table; mixed-case or camel case names are ignored by the agent. - If you define rules in both locations, the rules are merged.
- If you add more than one rule files to a lifecycle hook directory, the files are processed in lexical order of the file names.
- If you define files with identical file names in both locations, the file in the
/etcfolder takes precedence over the file of the same name in the/usrfolder.
A rule file is written in YAML format and contains a list of one or more actions. An action can be an instruction to run an external command.
When you specify many actions for a hook, the actions are performed in sequence, finishing one action before starting the next.
If an action returns with a failure, the following actions are skipped.
A run action takes the following parameters:
| Parameter | Description |
|---|---|
|
|
The command to run, including any flags or arguments. Specify the full path to the executable (for example |
|
|
Optional. A list of key-value pairs to set as environment variables for the command. |
|
|
Optional. The directory the command is run from. |
|
|
Optional. The maximum duration that is allowed for the action to complete. Specify the duration as a single positive integer followed by a time unit. The following units are supported: |
|
|
Optional. A list of conditions that must be true for the action to be run. If not provided, actions run unconditionally. |
By default, actions are performed every time the hook is triggered. However, for the afterupdating hook, you can use the If parameter to add conditions that must be true for an action to be performed. Otherwise, the action is skipped.
For example, to run an action only if a given file or directory changes during the update, you can define a path condition that takes the following parameters:
| Parameter | Description |
|---|---|
|
|
An absolute path to a file or directory that must change during the update as condition for the action to be performed.
Specify paths by using forward slashes ( |
|
|
A list of file operations, such as |
If you specify a path condition for an action in the afterupdating hook, you can use the following variables in arguments to your command. These variables are replaced with the absolute paths to the changed files:
| Variable | Description |
|---|---|
|
|
The absolute path to the file or directory specified in the path condition. |
|
|
A space-separated list of absolute paths of the files that changed during the update and are covered by the path condition. |
|
|
A space-separated list of absolute paths of the files that were created during the update and are covered by the path condition. |
|
|
A space-separated list of absolute paths of the files that were updated during the update and are covered by the path condition. |
|
|
A space-separated list of absolute paths of the files that were removed during the update and are covered by the path condition. |
The Red Hat Edge Manager agent includes a built-in set of rules defined in /usr/lib/flightctl/hooks.d/afterupdating/00-default.yaml. The following commands are executed if certain files are changed:
| File | Command | Description |
|---|---|---|
|
|
|
Changes to |
|
|
|
Changes to |
|
|
|
Changes to the permanent configuration of |
For networking configuration details, see Configuring and managing networking in the Additional resources section.