-
Notifications
You must be signed in to change notification settings - Fork 14
Added policy-dispatcher experimental module #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b89a5a8
Added policy-dispatcher experimental module
olehermanse bcecc2a
cfengine format with the newest version
olehermanse fa92001
policy-dispatcher: Changed to use folder in statedir
olehermanse 754da51
policy-dispatcher: Improved README
olehermanse cd99668
policy-dispatcher: Small cleanups and improvements
olehermanse 80d8e71
policy-dispatcher: Clean up leftover files
olehermanse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| This module enables the running of CFEngine policy snippets based on input from Build in Mission Portal or `cfbs input`. | ||
|
|
||
| Each policy snippet is saved to a file in `/var/cfengine/state/policy-dispatcher/<number>.cf` and then run with `cf-agent --no-lock --file <file>`, based on the `condition`-variable. | ||
|
|
||
| **Note:** The policy snippets are run with root-privilege (uid=0). | ||
| Each snippet must be a complete, standalone policy file, containing a `main` bundle (this is what `cf-agent` runs by default). | ||
|
|
||
| **Usage:** | ||
|
|
||
| - `policy` - The policy snippet to save and run. Must contain a `main` bundle. | ||
| - `condition` - Condition for running. Use a class expression (e.g., `linux|bsd`). Defaults to `"any"` | ||
| - `ifelapsed` - Number of minutes between assessments. Defaults to 5 minutes. | ||
|
|
||
| E.g. | ||
|
|
||
| ```json | ||
| { | ||
| "policy": "bundle agent main { reports: \"Hello World\"; } ", | ||
| "condition": "linux", | ||
| "ifelapsed": "5" | ||
| } | ||
| ``` | ||
|
|
||
| Would report "Hello World" on every linux device with 5 minute intervals. | ||
|
|
||
| ## Contribute | ||
|
|
||
| Feel free to open pull requests to expand this documentation, add features or fix problems. | ||
| You can also pick up an existing task or file an issue in [our bug tracker](https://tracker.mender.io/issues/). | ||
|
|
||
| ## License | ||
|
|
||
| This software is licensed under the MIT License. See LICENSE in the root of the repository for the full license text. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "variables": { | ||
| "policy_dispatcher:main.policy_files_to_run": { | ||
| "value": [ | ||
| { | ||
| "policy": "bundle agent main\n{\n reports:\n \"Hello World\";\n}\n", | ||
| "condition": "any", | ||
| "ifelapsed": "5" | ||
| }, | ||
| { | ||
| "policy": "bundle agent main\n{\n files:\n \"/tmp/policy-dispatcher-example.txt\"\n create => \"true\",\n content => \"This file is managed by the policy-dispatcher module\n\";\n}\n", | ||
| "condition": "linux", | ||
| "ifelapsed": "60" | ||
| } | ||
| ], | ||
| "comment": "Example data generated by 'cfbs input' / Mission Portal build page" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| body file control | ||
| { | ||
| namespace => "policy_dispatcher"; | ||
| } | ||
|
|
||
| bundle agent main | ||
| { | ||
| classes: | ||
| "enabled" expression => isvariable("policy_files_to_run"); | ||
|
|
||
| vars: | ||
| enabled:: | ||
| "i" slist => getindices(policy_files_to_run); | ||
|
|
||
| "_policy_file[$(i)]" | ||
| string => "$(policy_files_to_run[$(i)][policy])", | ||
| if => isvariable("policy_files_to_run[$(i)][policy]"); | ||
|
|
||
| "_condition[$(i)]" | ||
| string => ifelse( | ||
| not(strcmp("$(policy_files_to_run[$(i)][condition])", "")), | ||
| "$(policy_files_to_run[$(i)][condition])", | ||
| "any" | ||
| ); | ||
|
|
||
| "_ifelapsed[$(i)]" | ||
| string => ifelse( | ||
| not(strcmp("$(policy_files_to_run[$(i)][ifelapsed])", "")), | ||
| "$(policy_files_to_run[$(i)][ifelapsed])", | ||
| "5" | ||
| ); | ||
|
|
||
| "_file[$(i)]" string => "$(sys.statedir)/policy-dispatcher/$(i).cf"; | ||
|
|
||
| "_expected_files" | ||
| slist => maplist("$(sys.statedir)/policy-dispatcher/$(this).cf", @(i)); | ||
|
|
||
| "_actual_files" | ||
| slist => findfiles("$(sys.statedir)/policy-dispatcher/*.cf"); | ||
|
|
||
| "_unexpected_files" slist => difference(_actual_files, _expected_files); | ||
|
|
||
| files: | ||
| enabled:: | ||
| "$(_file[$(i)])" | ||
| create => "true", | ||
| content => "$(_policy_file[$(i)])", | ||
| perms => mo("600", "root"), | ||
| if => "$(_condition[$(i)])"; | ||
|
|
||
| "$(_unexpected_files)" delete => tidy; | ||
|
|
||
| reports: | ||
| enabled.DEBUG:: | ||
| "Policy file [$(i)]: $(_file[$(i)]), condition: $(_condition[$(i)]), ifelapsed: $(_ifelapsed[$(i)])"; | ||
|
|
||
| !enabled.DEBUG:: | ||
| "policy-dispatcher: policy_files_to_run variable not found"; | ||
|
|
||
| commands: | ||
| enabled:: | ||
| "$(sys.cf_agent) --no-lock --file $(_file[$(i)])" | ||
| if => "$(_condition[$(i)])", | ||
| action => ifelapsed("$(_ifelapsed[$(i)])"); | ||
| } | ||
|
|
||
| body delete tidy | ||
| # @brief Delete the file and remove empty directories | ||
| # and links to directories | ||
| { | ||
| dirlinks => "delete"; | ||
| rmdirs => "true"; | ||
| } | ||
|
|
||
| body perms mo(mode, owner) | ||
| { | ||
| mode => "$(mode)"; | ||
| owners => { "$(owner)" }; | ||
| rxdirs => "false"; | ||
| } | ||
|
|
||
| body action ifelapsed(x) | ||
| { | ||
| ifelapsed => "$(x)"; | ||
| } | ||
|
|
||
| body file control | ||
| { | ||
| namespace => "default"; | ||
| } | ||
|
|
||
| bundle agent __main__ | ||
| { | ||
| methods: | ||
| "policy_dispatcher:main"; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.