core-extra/daemon/examples/myservices/sample.py

58 lines
1.7 KiB
Python

"""
Sample user-defined service.
"""
from core.misc.ipaddress import Ipv4Prefix
from core.service import CoreService
class MyService(CoreService):
"""
This is a sample user-defined service.
"""
# a unique name is required, without spaces
name = "MyService"
# you can create your own group here
group = "Utility"
# list executables that this service requires
executables = ()
# list of other services this service depends on
dependencies = ()
# per-node directories
dirs = ()
# generated files (without a full path this file goes in the node's dir,
# e.g. /tmp/pycore.12345/n1.conf/)
configs = ("myservice.sh",)
# list of startup commands, also may be generated during startup
startup = ("sh myservice.sh",)
# list of shutdown commands
shutdown = ()
@classmethod
def generate_config(cls, node, filename):
"""
Return a string that will be written to filename, or sent to the
GUI for user customization.
"""
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by MyService (sample.py)\n"
for ifc in node.netifs():
cfg += 'echo "Node %s has interface %s"\n' % (node.name, ifc.name)
# here we do something interesting
cfg += "\n".join(map(cls.subnetentry, ifc.addrlist))
break
return cfg
@staticmethod
def subnetentry(x):
"""
Generate a subnet declaration block given an IPv4 prefix string
for inclusion in the config file.
"""
if x.find(":") >= 0:
# this is an IPv6 address
return ""
else:
net = Ipv4Prefix(x)
return 'echo " network %s"' % net