custom promises: use data from python list in mustache template

11 views
Skip to first unread message

Xander Cage

unread,
Nov 23, 2022, 7:08:22 AM11/23/22
to help-cfengine
Hi,

i need to create a "custom promise" which read some string base data from system files. i want to use this data inside a cfengine mustache template to write out to another file.
i skimmed through the "custom" api docs but could not find a hint how to transpose data from the underlying module code (python list) in an cfengine variable. or is it as easy as just  returning the list as a json string?
 is this even possible?

wbr

chris



Xander Cage

unread,
Nov 23, 2022, 8:38:57 AM11/23/22
to help-cfengine
toyed around a bit...returning just json seems not to work very well

import os
import json
from cfengine import PromiseModule, ValidationError, Result


class MkSudoPermsModule(PromiseModule):
    def __init__(self):
        super().__init__("itsv_mk_sudo_perms_promise_module", "0.0.1")

    def validate_promise(self, promiser, attributes):
        pass

    def evaluate_promise(self, promiser, attributes):
        lst = ['jboss1', 'jboss2', 'jboss3']
        return json.dumps(lst)

        return Result.KEPT


if __name__ == "__main__":
    MkSudoPermsModule().start()
~

result:

verbose:  Begin policy/promise evaluation
 verbose: ----------------------------------------------------------------
 verbose: Using bundlesequence =>  {"main"}
 verbose: B: *****************************************************************
 verbose: B: BEGIN bundle main
 verbose: B: *****************************************************************
   debug: DeRefCopyPromise(): promiser:'/usr/security/user'
   debug: DeRefCopyPromise():     copying constraint: 'name'
 verbose: Starting custom promise module '/var/cfengine/modules/promises/itsv_mk_sudo_perms.py' with command '/opt/freeware/bin/python3 /var/cfengine/modules/promises/itsv_mk_sudo_perms.py'
   debug: Received header from promise module: 'itsv_mk_sudo_perms_promise_module 0.0.1 v1 json_based'
   debug: Received line from module: '{"operation": "validate_promise", "promiser": "/usr/security/user", "attributes": {"name": "jboss"}, "result": "valid"}'
   debug: Received line from module: '{"operation": "evaluate_promise", "promiser": "/usr/security/user", "attributes": {"name": "jboss"}, "result": "[\"jboss1\", \"jboss2\", \"jboss3\"]"}'
   error: Promise module returned unacceptable result: '["jboss1", "jboss2", "jboss3"]' (mk_sudo_perms promise, promiser: '/usr/security/user' module: '/var/cfengine/modules/promises/itsv_mk_sudo_perms.py')
 verbose: A: Promise NOT KEPT!
 verbose: P: END mk_sudo_perms promise (/usr/security/user)
   debug: DeRefCopyPromise(): promiser:'/usr/security/user'
   debug: DeRefCopyPromise():     copying constraint: 'name'
   debug: DeRefCopyPromise(): promiser:'/usr/security/user'
   debug: DeRefCopyPromise():     copying constraint: 'name'
 verbose: A: ...................................................
 verbose: A: Bundle Accounting Summary for 'main' in namespace default


not very promising *gg*

Vratislav Podzimek

unread,
Nov 23, 2022, 11:07:30 AM11/23/22
to help-c...@googlegroups.com
Hi Chris! 
Yes, that's expected. The custom promise types are just like the other, built in, promise types -- they implement support for promises, i.e. state definitions, and the communication protocol only passes the definitions to the custom module and then promise results and log messages back. What your use case requires would be custom policy functions support of which is part of our plans [1], but currently not with high priority.


--
Vratislav
--
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/22988450-5566-4e45-bbe8-79c0c54a85b4n%40googlegroups.com.

signature.asc

Xander Cage

unread,
Nov 23, 2022, 11:12:45 AM11/23/22
to help-cfengine
hi,

ah ok...thanx for the explanation, will implement to whole file write stuff in the module than.


chris
Reply all
Reply to author
Forward
0 new messages