Source

pyohio_talk / cf_example

Full commit
from cfbuild import *

class Service(BasicService):
    "test service"
    names = ("testservice", )
    required_services = ( "ip", )

    # These params come in from services.xml
    # There are different types of Parms, outlined in CfServiceParms
    parms = RecordParm(make_map(
            test = StringParm(),
        ))

    # These params come in from the host template (whatever is calling it).
    hostparms = RecordParm(make_map(
            test1 = StringParm(required=1),
            test2 = StringParm(required=1),
    ))

    def assemble(self):
        hostvars = {}
        # We're going to use pretty printer 
        # so we can see whats going on.
        import pprint
        pp = pprint.PrettyPrinter(indent=4)
        # This creates the testservice/site.ini
        # sets some basic vars needed at runtime.
        sitevars = self.auto_sitevars()
        for host, hargs in self.host_args.items():
            # Define host variable
            # will end up in testservice/site.ini
            # and will be available in runtime script.
            # It should be noted that these vars get evaluated in by Perl at run time,
            # so be careful what you put in here (quotes an backticks will be bad)
            hostvars["testvar1"] = hargs.test1
            hostvars["testvar2"] = hargs.test1
            print pp.pprint(host)
            print pp.pprint(hargs.test1)
            print pp.pprint(hargs.test2)
            # Set the vars we defined above
            self.set_hostvars(host, hostvars)
        # Create script to be executed on the host when cf is executed.
        self.add_file("configure.pl", configure_pl)

# This is the code that gets executed on the host at runtime
configure_pl = r"""#
# $Id: $
use strict;

# This will be run on the host when CF is executed
sub configure {
    my ($o) = get_options({}, @_);
    # Do fun stuff   
 
    # This will print out the $testvar defined in the python code above
    warning($testvar1);
    warning($testvar2);
}
"""
Now, add this to services.xml with the arguments defined above in the parms Record.


<testservice test="test"/>

And finally, in a host template (anyweb, for example) call the service:


<testservice test1="test_one" test2="test_two"/>