|
| 1 | +"""The ultimate CFEngine custom promise module""" |
| 2 | + |
| 3 | +import glob |
| 4 | +import json |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | + |
| 11 | +from cfengine import PromiseModule, ValidationError, Result |
| 12 | + |
| 13 | + |
| 14 | +class AnsiballINIModule(PromiseModule): |
| 15 | + def __init__(self): |
| 16 | + super().__init__("ansible_ini_promise_module", "0.0.1") |
| 17 | + |
| 18 | + # AnsiballINIModule specific (the path from the policy) |
| 19 | + self.add_attribute("module_path", str, default="/tmp/cfe/ini_file.py") |
| 20 | + |
| 21 | + self.add_attribute("path", str, default_to_promiser=True) |
| 22 | + self.add_attribute("allow_no_value", bool, default=False) |
| 23 | + self.add_attribute("attributes", str) |
| 24 | + self.add_attribute("backup", bool, default=False) |
| 25 | + self.add_attribute("create", str, default="yes") |
| 26 | + self.add_attribute("group", str) |
| 27 | + self.add_attribute("mode", str) |
| 28 | + self.add_attribute("no_extra_spaces", str, default="yes") |
| 29 | + self.add_attribute("option", str) |
| 30 | + self.add_attribute("owner", str) |
| 31 | + self.add_attribute("section", str, required=True) |
| 32 | + # TODO - Add seLinux attrs |
| 33 | + # self.add_attribute("selevel", str, default="sO") |
| 34 | + # self.add_attribute("serole") |
| 35 | + |
| 36 | + self.add_attribute("state", str, default="present") |
| 37 | + self.add_attribute("unsafe_writes", bool, default=False) |
| 38 | + self.add_attribute("value", str) |
| 39 | + |
| 40 | + def validate_promise(self, promiser: str, attributes: dict, meta: dict) -> None: |
| 41 | + self.log_debug( |
| 42 | + "Validating the ansible ini promise: %s %s %s" |
| 43 | + % (promiser, attributes, meta) |
| 44 | + ) |
| 45 | + |
| 46 | + if not meta.get("promise_type"): |
| 47 | + raise ValidationError("Promise type not specified") |
| 48 | + |
| 49 | + # Not an Ansible attribute |
| 50 | + if not attributes.get("module_path"): |
| 51 | + self.log_error("'module_path' is a required promise attribute") |
| 52 | + return (result.NOT_KEPT, []) |
| 53 | + |
| 54 | + def evaluate_promise(self, promiser: str, attributes: dict, meta: dict): |
| 55 | + self.log_debug( |
| 56 | + "Evaluating the ansible ini promise %s, %s, %s" |
| 57 | + % (promiser, attributes, meta) |
| 58 | + ) |
| 59 | + |
| 60 | + # INI module specific - should not be passed on to Ansible |
| 61 | + module_path = attributes["module_path"] |
| 62 | + del attributes["module_path"] |
| 63 | + |
| 64 | + # NOTE - needed because 'default_to_promiser' is not respected |
| 65 | + attributes.setdefault("path", promiser) |
| 66 | + |
| 67 | + proc = subprocess.run( |
| 68 | + [ |
| 69 | + "python", |
| 70 | + module_path, |
| 71 | + ], |
| 72 | + input=json.dumps({"ANSIBLE_MODULE_ARGS": attributes}).encode("utf-8"), |
| 73 | + stdout=subprocess.PIPE, |
| 74 | + stderr=subprocess.PIPE, |
| 75 | + ) |
| 76 | + |
| 77 | + if not proc: |
| 78 | + self.log_error("Failed to run the ansible module") |
| 79 | + return ( |
| 80 | + Result.NOT_KEPT, |
| 81 | + [], |
| 82 | + ) |
| 83 | + |
| 84 | + if proc.returncode != 0: |
| 85 | + self.log_error("Failed to run the ansible module") |
| 86 | + self.log_error("Ansible INI module returned: %s" % proc.stdout) |
| 87 | + self.log_error("Ansible INI module returned: %s" % proc.stderr) |
| 88 | + return ( |
| 89 | + Result.NOT_KEPT, |
| 90 | + [], |
| 91 | + ) |
| 92 | + |
| 93 | + self.log_debug("Received output: %s (stdout)" % proc.stdout) |
| 94 | + self.log_debug("Received output (stderr): %s" % proc.stderr) |
| 95 | + return ( |
| 96 | + Result.KEPT, |
| 97 | + [], |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + AnsiballINIModule().start() |
0 commit comments