small cleanup to sdn services, added loadservice line to daemon, and updated example service for how current services are coded

This commit is contained in:
Blake J. Harnden 2017-07-05 13:32:16 -07:00
parent e39682c5b4
commit 6bfa81f3a8
3 changed files with 48 additions and 41 deletions

View file

@ -3,19 +3,19 @@
# Copyright (c)2010-2012 the Boeing Company.
# See the LICENSE file included in this distribution.
#
''' Sample user-defined service.
'''
import os
"""
Sample user-defined service.
"""
from core.misc.ipaddress import Ipv4Prefix
from core.misc.ipaddress import Ipv6Prefix
from core.service import CoreService
from core.service import ServiceManager
class MyService(CoreService):
''' This is a sample user-defined service.
'''
"""
This is a sample user-defined service.
"""
# a unique name is required, without spaces
_name = "MyService"
# you can create your own group here
@ -26,7 +26,7 @@ class MyService(CoreService):
_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', )
_configs = ('myservice.sh',)
# this controls the starting order vs other enabled services
_startindex = 50
# list of startup commands, also may be generated during startup
@ -36,31 +36,34 @@ class MyService(CoreService):
@classmethod
def generateconfig(cls, node, filename, services):
''' Return a string that will be written to filename, or sent to the
GUI for user customization.
'''
"""
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
# 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.
'''
"""
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)
net = Ipv4Prefix(x)
return 'echo " network %s"' % net
# this line is required to add the above class to the list of available services
ServiceManager.add(MyService)
def load_services():
# this line is required to add the above class to the list of available services
ServiceManager.add(MyService)