2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Definition of CoreService class that is subclassed to define
|
2013-08-29 15:21:13 +01:00
|
|
|
startup services and routing for nodes. A service is typically a daemon
|
2017-06-20 02:09:28 +01:00
|
|
|
program launched when a node starts that provides some sort of service.
|
|
|
|
The CoreServices class handles configuration messages for sending
|
2013-08-29 15:21:13 +01:00
|
|
|
a list of available services to the GUI and for configuring individual
|
|
|
|
services.
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
2017-08-04 00:33:54 +01:00
|
|
|
|
2017-08-03 22:27:39 +01:00
|
|
|
import importlib
|
|
|
|
import inspect
|
2017-06-20 02:09:28 +01:00
|
|
|
import os
|
|
|
|
import shlex
|
|
|
|
import sys
|
|
|
|
import time
|
2013-08-29 15:21:13 +01:00
|
|
|
from itertools import repeat
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-08-07 19:58:51 +01:00
|
|
|
from core import logger
|
2017-06-20 02:09:28 +01:00
|
|
|
from core.conf import Configurable
|
|
|
|
from core.conf import ConfigurableManager
|
2017-08-03 22:27:39 +01:00
|
|
|
from core.data import ConfigData
|
|
|
|
from core.data import EventData
|
|
|
|
from core.data import FileData
|
2017-06-20 02:09:28 +01:00
|
|
|
from core.enumerations import ConfigDataTypes
|
|
|
|
from core.enumerations import ConfigFlags
|
|
|
|
from core.enumerations import EventTypes
|
|
|
|
from core.enumerations import MessageFlags
|
|
|
|
from core.enumerations import RegisterTlvs
|
|
|
|
from core.misc import utils
|
|
|
|
|
|
|
|
|
2017-08-04 00:33:54 +01:00
|
|
|
def _valid_module(path, file_name):
|
2017-08-03 22:27:39 +01:00
|
|
|
"""
|
|
|
|
Check if file is a valid python module.
|
|
|
|
|
|
|
|
:param str path: path to file
|
|
|
|
:param str file_name: file name to check
|
|
|
|
:return: True if a valid python module file, False otherwise
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
file_path = os.path.join(path, file_name)
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if file_name.startswith("_"):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not file_name.endswith(".py"):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-08-04 00:33:54 +01:00
|
|
|
def _is_service(module, member):
|
2017-08-03 22:27:39 +01:00
|
|
|
"""
|
|
|
|
Validates if a module member is a class and an instance of a CoreService.
|
|
|
|
|
|
|
|
:param module: module to validate for service
|
|
|
|
:param member: member to validate for service
|
|
|
|
:return: True if a valid service, False otherwise
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
if not inspect.isclass(member):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not issubclass(member, CoreService):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if member.__module__ != module.__name__:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
class ServiceManager(object):
|
|
|
|
"""
|
|
|
|
Manages services available for CORE nodes to use.
|
|
|
|
"""
|
|
|
|
services = []
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
@classmethod
|
|
|
|
def add(cls, service):
|
|
|
|
"""
|
|
|
|
Add a service to manager.
|
|
|
|
|
|
|
|
:param CoreService service: service to add
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
insert = 0
|
|
|
|
for index, known_service in enumerate(cls.services):
|
|
|
|
if known_service._group == service._group:
|
|
|
|
insert = index + 1
|
|
|
|
break
|
|
|
|
|
|
|
|
logger.info("loading service: %s - %s: %s", insert, service, service._name)
|
|
|
|
cls.services.insert(insert, service)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
"""
|
|
|
|
Retrieve a service from the manager.
|
|
|
|
|
|
|
|
:param str name: name of the service to retrieve
|
|
|
|
:return: service if it exists, None otherwise
|
|
|
|
:rtype: CoreService
|
|
|
|
"""
|
|
|
|
for service in cls.services:
|
|
|
|
if service._name == name:
|
|
|
|
return service
|
|
|
|
return None
|
2014-04-30 22:27:41 +01:00
|
|
|
|
2017-08-04 00:33:54 +01:00
|
|
|
@classmethod
|
|
|
|
def add_services(cls, path):
|
|
|
|
"""
|
|
|
|
Method for retrieving all CoreServices from a given path.
|
|
|
|
|
|
|
|
:param str path: path to retrieve services from
|
|
|
|
:return: list of core services
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2018-01-09 23:52:29 +00:00
|
|
|
# validate path exists
|
|
|
|
logger.info("attempting to add services from path: %s", path)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
logger.warn("invalid custom service directory specified" ": %s" % path)
|
|
|
|
# check if path is in sys.path
|
2017-08-04 00:33:54 +01:00
|
|
|
logger.info("getting custom services from: %s", path)
|
|
|
|
parent_path = os.path.dirname(path)
|
|
|
|
if parent_path not in sys.path:
|
|
|
|
logger.info("adding parent path to allow imports: %s", parent_path)
|
|
|
|
sys.path.append(parent_path)
|
2017-08-07 19:58:51 +01:00
|
|
|
|
|
|
|
# retrieve potential service modules, and filter out invalid modules
|
2017-08-04 00:33:54 +01:00
|
|
|
base_module = os.path.basename(path)
|
|
|
|
module_names = os.listdir(path)
|
|
|
|
module_names = filter(lambda x: _valid_module(path, x), module_names)
|
|
|
|
module_names = map(lambda x: x[:-3], module_names)
|
|
|
|
|
2017-08-07 19:58:51 +01:00
|
|
|
# import and add all service modules in the path
|
2017-08-04 00:33:54 +01:00
|
|
|
for module_name in module_names:
|
|
|
|
import_statement = "%s.%s" % (base_module, module_name)
|
|
|
|
logger.info("importing custom service module: %s", import_statement)
|
2017-08-08 17:17:09 +01:00
|
|
|
try:
|
|
|
|
module = importlib.import_module(import_statement)
|
|
|
|
members = inspect.getmembers(module, lambda x: _is_service(module, x))
|
|
|
|
for member in members:
|
|
|
|
clazz = member[1]
|
|
|
|
clazz.on_load()
|
|
|
|
cls.add(clazz)
|
|
|
|
except:
|
|
|
|
logger.exception("unexpected error during import, skipping: %s", import_statement)
|
2017-08-04 00:33:54 +01:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
class CoreServices(ConfigurableManager):
|
|
|
|
"""
|
|
|
|
Class for interacting with a list of available startup services for
|
|
|
|
nodes. Mostly used to convert a CoreService into a Config API
|
|
|
|
message. This class lives in the Session object and remembers
|
|
|
|
the default services configured for each node type, and any
|
|
|
|
custom service configuration. A CoreService is not a Configurable.
|
|
|
|
"""
|
|
|
|
name = "services"
|
|
|
|
config_type = RegisterTlvs.UTILITY.value
|
|
|
|
|
|
|
|
_invalid_custom_names = (
|
2018-01-12 17:50:56 +00:00
|
|
|
'core', 'api', 'bsd', 'emane', 'misc', 'netns', 'phys', 'services', 'xen'
|
2017-06-20 02:09:28 +01:00
|
|
|
)
|
2017-02-24 00:20:57 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def __init__(self, session):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Creates a CoreServices instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: session this manager is tied to
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
ConfigurableManager.__init__(self)
|
|
|
|
self.session = session
|
2013-08-29 15:21:13 +01:00
|
|
|
# dict of default services tuples, key is node type
|
|
|
|
self.defaultservices = {}
|
|
|
|
# dict of tuple of service objects, key is node number
|
|
|
|
self.customservices = {}
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
# TODO: remove need for cyclic import
|
|
|
|
from core.services import startup
|
|
|
|
self.is_startup_service = startup.Startup.is_startup_service
|
2015-09-11 22:27:08 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def reset(self):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Called when config message with reset flag is received
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.defaultservices.clear()
|
|
|
|
self.customservices.clear()
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
def getdefaultservices(self, service_type):
|
|
|
|
"""
|
|
|
|
Get the list of default services that should be enabled for a
|
|
|
|
node for the given node type.
|
|
|
|
|
|
|
|
:param service_type: service type to get default services for
|
|
|
|
:return: default services
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
logger.debug("getting default services for type: %s", service_type)
|
|
|
|
results = []
|
|
|
|
if service_type in self.defaultservices:
|
|
|
|
defaults = self.defaultservices[service_type]
|
2013-08-29 15:21:13 +01:00
|
|
|
for name in defaults:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.debug("checking for service with service manager: %s", name)
|
|
|
|
service = ServiceManager.get(name)
|
|
|
|
if not service:
|
|
|
|
logger.warn("default service %s is unknown", name)
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
results.append(service)
|
|
|
|
return results
|
|
|
|
|
|
|
|
def getcustomservice(self, object_id, service):
|
|
|
|
"""
|
|
|
|
Get any custom service configured for the given node that matches the specified service name.
|
|
|
|
If no custom service is found, return the specified service.
|
|
|
|
|
|
|
|
:param int object_id: object id to get service from
|
|
|
|
:param CoreService service: custom service to retrieve
|
|
|
|
:return: custom service from the node
|
|
|
|
:rtype: CoreService
|
|
|
|
"""
|
|
|
|
if object_id in self.customservices:
|
|
|
|
for s in self.customservices[object_id]:
|
2013-08-29 15:21:13 +01:00
|
|
|
if s._name == service._name:
|
|
|
|
return s
|
|
|
|
return service
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
def setcustomservice(self, object_id, service, values):
|
|
|
|
"""
|
|
|
|
Store service customizations in an instantiated service object
|
|
|
|
using a list of values that came from a config message.
|
|
|
|
|
|
|
|
:param int object_id: object id to set custom service for
|
|
|
|
:param class service: service to set
|
|
|
|
:param list values: values to
|
|
|
|
:return:
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if service._custom:
|
|
|
|
s = service
|
|
|
|
else:
|
|
|
|
# instantiate the class, for storing config customization
|
|
|
|
s = service()
|
|
|
|
# values are new key=value format; not all keys need to be present
|
|
|
|
# a missing key means go with the default
|
|
|
|
if Configurable.haskeyvalues(values):
|
|
|
|
for v in values:
|
|
|
|
key, value = v.split('=', 1)
|
|
|
|
s.setvalue(key, value)
|
|
|
|
# old-style config, list of values
|
|
|
|
else:
|
|
|
|
s.fromvaluelist(values)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# assume custom service already in dict
|
|
|
|
if service._custom:
|
|
|
|
return
|
|
|
|
# add the custom service to dict
|
2017-06-20 02:09:28 +01:00
|
|
|
if object_id in self.customservices:
|
|
|
|
self.customservices[object_id] += (s,)
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
self.customservices[object_id] = (s,)
|
|
|
|
|
|
|
|
def addservicestonode(self, node, nodetype, services_str):
|
|
|
|
"""
|
|
|
|
Populate the node.service list using (1) the list of services
|
|
|
|
requested from the services TLV, (2) using any custom service
|
|
|
|
configuration, or (3) using the default services for this node type.
|
|
|
|
|
|
|
|
:param core.coreobj.PyCoreNode node: node to add services to
|
|
|
|
:param str nodetype: node type to add services to
|
|
|
|
:param str services_str: string formatted service list
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if services_str is not None:
|
2017-07-11 18:35:08 +01:00
|
|
|
logger.info("setting node specific services: %s", services_str)
|
2017-06-20 02:09:28 +01:00
|
|
|
services = services_str.split("|")
|
2013-08-29 15:21:13 +01:00
|
|
|
for name in services:
|
2017-06-20 02:09:28 +01:00
|
|
|
s = ServiceManager.get(name)
|
2013-08-29 15:21:13 +01:00
|
|
|
if s is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("configured service %s for node %s is unknown", name, node.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.info("adding configured service %s to node %s", s._name, node.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
s = self.getcustomservice(node.objid, s)
|
|
|
|
node.addservice(s)
|
|
|
|
else:
|
2017-07-11 18:35:08 +01:00
|
|
|
logger.info("setting default services for node (%s) type (%s)", node.objid, nodetype)
|
2013-08-29 15:21:13 +01:00
|
|
|
services = self.getdefaultservices(nodetype)
|
|
|
|
for s in services:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.info("adding default service %s to node %s", s._name, node.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
s = self.getcustomservice(node.objid, s)
|
|
|
|
node.addservice(s)
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
def getallconfigs(self, use_clsmap=True):
|
|
|
|
"""
|
|
|
|
Return (nodenum, service) tuples for all stored configs. Used when reconnecting to a
|
|
|
|
session or opening XML.
|
|
|
|
|
|
|
|
:param bool use_clsmap: should a class map be used, default to True
|
|
|
|
:return: list of tuples of node ids and services
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
configs = []
|
2013-08-29 15:21:13 +01:00
|
|
|
for nodenum in self.customservices:
|
2017-06-20 02:09:28 +01:00
|
|
|
for service in self.customservices[nodenum]:
|
|
|
|
configs.append((nodenum, service))
|
|
|
|
return configs
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getallfiles(self, service):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Return all customized files stored with a service.
|
2013-08-29 15:21:13 +01:00
|
|
|
Used when reconnecting to a session or opening XML.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:param CoreService service: service to get files for
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
files = []
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if not service._custom:
|
2017-06-20 02:09:28 +01:00
|
|
|
return files
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
for filename in service._configs:
|
|
|
|
data = self.getservicefiledata(service, filename)
|
|
|
|
if data is None:
|
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
files.append((filename, data))
|
|
|
|
|
|
|
|
return files
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def bootnodeservices(self, node):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Start all services on a node.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to start services on
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
services = sorted(node.services, key=lambda service: service._startindex)
|
|
|
|
use_startup_service = any(map(self.is_startup_service, services))
|
2013-08-29 15:21:13 +01:00
|
|
|
for s in services:
|
2014-06-02 20:38:02 +01:00
|
|
|
if len(str(s._starttime)) > 0:
|
2014-03-05 16:28:32 +00:00
|
|
|
try:
|
|
|
|
t = float(s._starttime)
|
|
|
|
if t > 0.0:
|
2017-06-20 02:09:28 +01:00
|
|
|
fn = self.bootnodeservice
|
|
|
|
self.session.event_loop.add_event(t, fn, node, s, services, False)
|
2014-03-05 16:28:32 +00:00
|
|
|
continue
|
|
|
|
except ValueError:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("error converting start time to float")
|
|
|
|
self.bootnodeservice(node, s, services, use_startup_service)
|
|
|
|
|
|
|
|
def bootnodeservice(self, node, service, services, use_startup_service):
|
|
|
|
"""
|
|
|
|
Start a service on a node. Create private dirs, generate config
|
|
|
|
files, and execute startup commands.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to boot services on
|
|
|
|
:param CoreService service: service to start
|
|
|
|
:param list services: service list
|
|
|
|
:param bool use_startup_service: flag to use startup services or not
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
if service._custom:
|
|
|
|
self.bootnodecustomservice(node, service, services, use_startup_service)
|
2015-09-11 22:23:15 +01:00
|
|
|
return
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
logger.info("starting service %s (%s)" % (service._name, service._startindex))
|
|
|
|
for directory in service._dirs:
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
node.privatedir(directory)
|
|
|
|
except:
|
|
|
|
logger.exception("Error making node %s dir %s", node.name, directory)
|
|
|
|
|
|
|
|
for filename in service.getconfigfilenames(node.objid, services):
|
|
|
|
cfg = service.generateconfig(node, filename, services)
|
|
|
|
node.nodefile(filename, cfg)
|
|
|
|
|
|
|
|
if use_startup_service and not self.is_startup_service(service):
|
2015-09-11 22:27:08 +01:00
|
|
|
return
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
for cmd in service.getstartup(node, services):
|
2015-09-11 22:23:15 +01:00
|
|
|
try:
|
|
|
|
# NOTE: this wait=False can be problematic!
|
2017-06-20 02:09:28 +01:00
|
|
|
node.cmd(shlex.split(cmd), wait=False)
|
|
|
|
except:
|
|
|
|
logger.exception("error starting command %s", cmd)
|
|
|
|
|
|
|
|
def bootnodecustomservice(self, node, service, services, use_startup_service):
|
|
|
|
"""
|
|
|
|
Start a custom service on a node. Create private dirs, use supplied
|
|
|
|
config files, and execute supplied startup commands.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to boot services on
|
|
|
|
:param CoreService service: service to start
|
|
|
|
:param list services: service list
|
|
|
|
:param bool use_startup_service: flag to use startup services or not
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2017-08-02 22:07:56 +01:00
|
|
|
logger.info("starting service(%s) %s (%s)(custom)",
|
|
|
|
service, service._name, service._startindex)
|
2017-06-20 02:09:28 +01:00
|
|
|
for directory in service._dirs:
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
node.privatedir(directory)
|
|
|
|
except:
|
2017-08-02 22:07:56 +01:00
|
|
|
logger.exception("error making node %s dir %s", node.name, directory)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-08-02 22:07:56 +01:00
|
|
|
logger.info("service configurations: %s", service._configs)
|
2017-06-20 02:09:28 +01:00
|
|
|
for i, filename in enumerate(service._configs):
|
2017-08-02 22:07:56 +01:00
|
|
|
logger.info("generating service config: %s", filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
if len(filename) == 0:
|
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
cfg = self.getservicefiledata(service, filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
if cfg is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
cfg = service.generateconfig(node, filename, services)
|
2013-08-29 15:21:13 +01:00
|
|
|
# cfg may have a file:/// url for copying from a file
|
|
|
|
try:
|
|
|
|
if self.copyservicefile(node, filename, cfg):
|
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
except IOError:
|
|
|
|
logger.exception("error copying service file '%s'", filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-06-20 02:09:28 +01:00
|
|
|
node.nodefile(filename, cfg)
|
|
|
|
|
|
|
|
if use_startup_service and not self.is_startup_service(service):
|
2015-09-11 22:27:08 +01:00
|
|
|
return
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
for cmd in service._startup:
|
2015-09-11 22:23:15 +01:00
|
|
|
try:
|
|
|
|
# NOTE: this wait=False can be problematic!
|
2017-06-20 02:09:28 +01:00
|
|
|
node.cmd(shlex.split(cmd), wait=False)
|
|
|
|
except:
|
|
|
|
logger.exception("error starting command %s", cmd)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def copyservicefile(self, node, filename, cfg):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Given a configured service filename and config, determine if the
|
2013-08-29 15:21:13 +01:00
|
|
|
config references an existing file that should be copied.
|
|
|
|
Returns True for local files, False for generated.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to copy service for
|
|
|
|
:param str filename: file name for a configured service
|
|
|
|
:param str cfg: configuration string
|
|
|
|
:return: True if successful, False otherwise
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if cfg[:7] == 'file://':
|
|
|
|
src = cfg[7:]
|
|
|
|
src = src.split('\n')[0]
|
2017-06-20 02:09:28 +01:00
|
|
|
src = utils.expandcorepath(src, node.session, node)
|
2013-08-29 15:21:13 +01:00
|
|
|
# TODO: glob here
|
2017-06-20 02:09:28 +01:00
|
|
|
node.nodefilecopy(filename, src, mode=0644)
|
2013-08-29 15:21:13 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def validatenodeservices(self, node):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Run validation commands for all services on a node.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to validate services for
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
services = sorted(node.services, key=lambda service: service._startindex)
|
2013-08-29 15:21:13 +01:00
|
|
|
for s in services:
|
|
|
|
self.validatenodeservice(node, s, services)
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
def validatenodeservice(self, node, service, services):
|
|
|
|
"""
|
|
|
|
Run the validation command(s) for a service.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to validate service for
|
|
|
|
:param CoreService service: service to validate
|
|
|
|
:param list services: services for node
|
|
|
|
:return: service validation status
|
|
|
|
:rtype: int
|
|
|
|
"""
|
|
|
|
logger.info("validating service for node (%s - %s): %s (%s)",
|
|
|
|
node.objid, node.name, service._name, service._startindex)
|
|
|
|
if service._custom:
|
|
|
|
validate_cmds = service._validate
|
2014-03-05 16:28:32 +00:00
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
validate_cmds = service.getvalidate(node, services)
|
|
|
|
|
|
|
|
status = 0
|
|
|
|
# has validate commands
|
|
|
|
if len(validate_cmds) > 0:
|
2014-03-05 16:28:32 +00:00
|
|
|
for cmd in validate_cmds:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.info("validating service %s using: %s", service._name, cmd)
|
2014-03-05 16:28:32 +00:00
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
status, result = node.cmdresult(shlex.split(cmd))
|
2014-03-05 16:28:32 +00:00
|
|
|
if status != 0:
|
2017-06-20 02:09:28 +01:00
|
|
|
raise ValueError("non-zero exit status")
|
2014-03-05 16:28:32 +00:00
|
|
|
except:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("validate command failed: %s", cmd)
|
2014-03-05 16:28:32 +00:00
|
|
|
status = -1
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2014-03-05 16:28:32 +00:00
|
|
|
return status
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def stopnodeservices(self, node):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Stop all services on a node.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to stop services on
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
services = sorted(node.services, key=lambda service: service._startindex)
|
2013-08-29 15:21:13 +01:00
|
|
|
for s in services:
|
|
|
|
self.stopnodeservice(node, s)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
def stopnodeservice(self, node, service):
|
|
|
|
"""
|
|
|
|
Stop a service on a node.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to stop a service on
|
|
|
|
:param CoreService service: service to stop
|
|
|
|
:return: status for stopping the services
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2014-03-05 16:28:32 +00:00
|
|
|
status = ""
|
2017-06-20 02:09:28 +01:00
|
|
|
if len(service._shutdown) == 0:
|
2014-03-05 16:28:32 +00:00
|
|
|
# doesn't have a shutdown command
|
|
|
|
status += "0"
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
for cmd in service._shutdown:
|
2014-03-05 16:28:32 +00:00
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
tmp = node.cmd(shlex.split(cmd), wait=True)
|
|
|
|
status += "%s" % tmp
|
2014-03-05 16:28:32 +00:00
|
|
|
except:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("error running stop command %s", cmd)
|
2014-03-05 16:28:32 +00:00
|
|
|
status += "-1"
|
|
|
|
return status
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
def configure_request(self, config_data):
|
|
|
|
"""
|
|
|
|
Receive configuration message for configuring services.
|
|
|
|
With a request flag set, a list of services has been requested.
|
|
|
|
When the opaque field is present, a specific service is being
|
|
|
|
configured or requested.
|
|
|
|
|
|
|
|
:param core.conf.ConfigData config_data: configuration data for carrying out a configuration
|
|
|
|
:return: response messages
|
|
|
|
:rtype: ConfigData
|
|
|
|
"""
|
|
|
|
node_id = config_data.node
|
|
|
|
session_id = config_data.session
|
|
|
|
opaque = config_data.opaque
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-07-12 18:35:53 +01:00
|
|
|
logger.info("configuration request: node(%s) session(%s) opaque(%s)", node_id, session_id, opaque)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# send back a list of available services
|
|
|
|
if opaque is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
type_flag = ConfigFlags.NONE.value
|
|
|
|
data_types = tuple(repeat(ConfigDataTypes.BOOL.value, len(ServiceManager.services)))
|
|
|
|
values = "|".join(repeat('0', len(ServiceManager.services)))
|
|
|
|
names = map(lambda x: x._name, ServiceManager.services)
|
2013-08-29 15:21:13 +01:00
|
|
|
captions = "|".join(names)
|
2017-06-20 02:09:28 +01:00
|
|
|
possible_values = ""
|
|
|
|
for s in ServiceManager.services:
|
2013-08-29 15:21:13 +01:00
|
|
|
if s._custom_needed:
|
2017-06-20 02:09:28 +01:00
|
|
|
possible_values += '1'
|
|
|
|
possible_values += '|'
|
|
|
|
groups = self.buildgroups(ServiceManager.services)
|
2013-08-29 15:21:13 +01:00
|
|
|
# send back the properties for this service
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
if node_id is None:
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
2017-07-12 18:35:53 +01:00
|
|
|
node = self.session.get_object(node_id)
|
|
|
|
if node is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Request to configure service for unknown node %s", node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
|
|
|
servicesstring = opaque.split(':')
|
2017-07-12 18:35:53 +01:00
|
|
|
services, unknown = self.servicesfromopaque(opaque, node.objid)
|
2014-03-05 16:28:32 +00:00
|
|
|
for u in unknown:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Request for unknown service '%s'" % u)
|
2014-03-05 16:28:32 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if len(services) < 1:
|
|
|
|
return None
|
2017-07-12 18:35:53 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if len(servicesstring) == 3:
|
|
|
|
# a file request: e.g. "service:zebra:quagga.conf"
|
2017-07-12 18:35:53 +01:00
|
|
|
file_data = self.getservicefile(services, node, servicesstring[2])
|
2017-07-11 23:51:56 +01:00
|
|
|
self.session.broadcast_file(file_data)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-07-12 18:35:53 +01:00
|
|
|
# short circuit this request early to avoid returning response below
|
|
|
|
return None
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# the first service in the list is the one being configured
|
|
|
|
svc = services[0]
|
|
|
|
# send back:
|
|
|
|
# dirs, configs, startindex, startup, shutdown, metadata, config
|
2017-06-20 02:09:28 +01:00
|
|
|
type_flag = ConfigFlags.UPDATE.value
|
|
|
|
data_types = tuple(repeat(ConfigDataTypes.STRING.value, len(svc.keys)))
|
2017-07-12 18:35:53 +01:00
|
|
|
values = svc.tovaluelist(node, services)
|
2013-08-29 15:21:13 +01:00
|
|
|
captions = None
|
2017-06-20 02:09:28 +01:00
|
|
|
possible_values = None
|
2013-08-29 15:21:13 +01:00
|
|
|
groups = None
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
return ConfigData(
|
|
|
|
message_type=0,
|
|
|
|
node=node_id,
|
|
|
|
object=self.name,
|
|
|
|
type=type_flag,
|
|
|
|
data_types=data_types,
|
|
|
|
data_values=values,
|
|
|
|
captions=captions,
|
|
|
|
possible_values=possible_values,
|
|
|
|
groups=groups,
|
|
|
|
session=session_id,
|
|
|
|
opaque=opaque
|
|
|
|
)
|
|
|
|
|
|
|
|
def configure_values(self, config_data):
|
|
|
|
"""
|
|
|
|
Receive configuration message for configuring services.
|
|
|
|
With a request flag set, a list of services has been requested.
|
|
|
|
When the opaque field is present, a specific service is being
|
|
|
|
configured or requested.
|
|
|
|
|
|
|
|
:param core.conf.ConfigData config_data: configuration data for carrying out a configuration
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
data_types = config_data.data_types
|
|
|
|
values = config_data.data_values
|
|
|
|
node_id = config_data.node
|
|
|
|
opaque = config_data.opaque
|
|
|
|
|
|
|
|
error_message = "services config message that I don't know how to handle"
|
2013-08-29 15:21:13 +01:00
|
|
|
if values is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.error(error_message)
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
values = values.split('|')
|
|
|
|
|
|
|
|
if opaque is None:
|
|
|
|
# store default services for a node type in self.defaultservices[]
|
2017-06-20 02:09:28 +01:00
|
|
|
if data_types is None or data_types[0] != ConfigDataTypes.STRING.value:
|
|
|
|
logger.info(error_message)
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
|
|
|
key = values.pop(0)
|
|
|
|
self.defaultservices[key] = values
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.info("default services for type %s set to %s" % (key, values))
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
|
|
|
# store service customized config in self.customservices[]
|
2017-06-20 02:09:28 +01:00
|
|
|
if node_id is None:
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
2017-06-20 02:09:28 +01:00
|
|
|
services, unknown = self.servicesfromopaque(opaque, node_id)
|
2014-03-05 16:28:32 +00:00
|
|
|
for u in unknown:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Request for unknown service '%s'" % u)
|
2014-03-05 16:28:32 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if len(services) < 1:
|
|
|
|
return None
|
|
|
|
svc = services[0]
|
2017-06-20 02:09:28 +01:00
|
|
|
self.setcustomservice(node_id, svc, values)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
def servicesfromopaque(self, opaque, object_id):
|
|
|
|
"""
|
|
|
|
Build a list of services from an opaque data string.
|
|
|
|
|
|
|
|
:param str opaque: opaque data string
|
|
|
|
:param int object_id: object id
|
|
|
|
:return: services and unknown services lists tuple
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
services = []
|
2014-03-05 16:28:32 +00:00
|
|
|
unknown = []
|
2013-08-29 15:21:13 +01:00
|
|
|
servicesstring = opaque.split(':')
|
|
|
|
if servicesstring[0] != "service":
|
|
|
|
return []
|
|
|
|
servicenames = servicesstring[1].split(',')
|
|
|
|
for name in servicenames:
|
2017-06-20 02:09:28 +01:00
|
|
|
s = ServiceManager.get(name)
|
|
|
|
s = self.getcustomservice(object_id, s)
|
2013-08-29 15:21:13 +01:00
|
|
|
if s is None:
|
2014-03-05 16:28:32 +00:00
|
|
|
unknown.append(name)
|
|
|
|
else:
|
|
|
|
services.append(s)
|
2017-06-20 02:09:28 +01:00
|
|
|
return services, unknown
|
|
|
|
|
|
|
|
def buildgroups(self, servicelist):
|
|
|
|
"""
|
|
|
|
Build a string of groups for use in a configuration message given
|
|
|
|
a list of services. The group list string has the format
|
|
|
|
"title1:1-5|title2:6-9|10-12", where title is an optional group title
|
|
|
|
and i-j is a numeric range of value indices; groups are
|
|
|
|
separated by commas.
|
|
|
|
|
|
|
|
:param list servicelist: service list to build group string from
|
|
|
|
:return: groups string
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
i = 0
|
|
|
|
r = ""
|
|
|
|
lastgroup = "<undefined>"
|
|
|
|
for service in servicelist:
|
|
|
|
i += 1
|
|
|
|
group = service._group
|
|
|
|
if group != lastgroup:
|
|
|
|
lastgroup = group
|
|
|
|
# finish previous group
|
|
|
|
if i > 1:
|
2017-06-20 02:09:28 +01:00
|
|
|
r += "-%d|" % (i - 1)
|
2013-08-29 15:21:13 +01:00
|
|
|
# optionally include group title
|
|
|
|
if group == "":
|
|
|
|
r += "%d" % i
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
r += "%s:%d" % (group, i)
|
2013-08-29 15:21:13 +01:00
|
|
|
# finish the last group list
|
|
|
|
if i > 0:
|
|
|
|
r += "-%d" % i
|
|
|
|
return r
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def getservicefile(self, services, node, filename):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Send a File Message when the GUI has requested a service file.
|
2013-08-29 15:21:13 +01:00
|
|
|
The file data is either auto-generated or comes from an existing config.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:param list services: service list
|
|
|
|
:param core.netns.nodes.CoreNode node: node to get service file from
|
|
|
|
:param str filename: file name to retrieve
|
|
|
|
:return: file message for node
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
svc = services[0]
|
|
|
|
# get the filename and determine the config file index
|
|
|
|
if svc._custom:
|
|
|
|
cfgfiles = svc._configs
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
cfgfiles = svc.getconfigfilenames(node.objid, services)
|
2013-08-29 15:21:13 +01:00
|
|
|
if filename not in cfgfiles:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Request for unknown file '%s' for service '%s'" % (filename, services[0]))
|
2013-08-29 15:21:13 +01:00
|
|
|
return None
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# get the file data
|
|
|
|
data = self.getservicefiledata(svc, filename)
|
|
|
|
if data is None:
|
|
|
|
data = "%s" % (svc.generateconfig(node, filename, services))
|
|
|
|
else:
|
|
|
|
data = "%s" % data
|
|
|
|
filetypestr = "service:%s" % svc._name
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-07-11 23:51:56 +01:00
|
|
|
return FileData(
|
|
|
|
message_type=MessageFlags.ADD.value,
|
|
|
|
node=node.objid,
|
|
|
|
name=filename,
|
|
|
|
type=filetypestr,
|
|
|
|
data=data
|
|
|
|
)
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def getservicefiledata(self, service, filename):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Get the customized file data associated with a service. Return None
|
2013-08-29 15:21:13 +01:00
|
|
|
for invalid filenames or missing file data.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:param CoreService service: service to get file data from
|
|
|
|
:param str filename: file name to get data from
|
|
|
|
:return: file data
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
|
|
|
i = service._configs.index(filename)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
if i >= len(service._configtxt) or service._configtxt[i] is None:
|
|
|
|
return None
|
|
|
|
return service._configtxt[i]
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def setservicefile(self, nodenum, type, filename, srcname, data):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Receive a File Message from the GUI and store the customized file
|
2013-08-29 15:21:13 +01:00
|
|
|
in the service config. The filename must match one from the list of
|
|
|
|
config files in the service.
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
:param int nodenum: node id to set service file
|
|
|
|
:param str type: file type to set
|
|
|
|
:param str filename: file name to set
|
|
|
|
:param str srcname: source name of file to set
|
|
|
|
:param data: data for file to set
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if len(type.split(':')) < 2:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Received file type did not contain service info.")
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
if srcname is not None:
|
|
|
|
raise NotImplementedError
|
2017-06-20 02:09:28 +01:00
|
|
|
svcid, svcname = type.split(':')[:2]
|
|
|
|
svc = ServiceManager.get(svcname)
|
2013-08-29 15:21:13 +01:00
|
|
|
svc = self.getcustomservice(nodenum, svc)
|
|
|
|
if svc is None:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Received filename for unknown service '%s'" % svcname)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
cfgfiles = svc._configs
|
|
|
|
if filename not in cfgfiles:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Received unknown file '%s' for service '%s'" % (filename, svcname))
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
i = cfgfiles.index(filename)
|
|
|
|
configtxtlist = list(svc._configtxt)
|
|
|
|
numitems = len(configtxtlist)
|
2017-06-20 02:09:28 +01:00
|
|
|
if numitems < i + 1:
|
2013-08-29 15:21:13 +01:00
|
|
|
# add empty elements to list to support index assignment
|
|
|
|
for j in range(1, (i + 2) - numitems):
|
|
|
|
configtxtlist += None,
|
|
|
|
configtxtlist[i] = data
|
|
|
|
svc._configtxt = configtxtlist
|
2017-06-20 02:09:28 +01:00
|
|
|
|
|
|
|
def handleevent(self, event_data):
|
|
|
|
"""
|
|
|
|
Handle an Event Message used to start, stop, restart, or validate
|
|
|
|
a service on a given node.
|
|
|
|
|
|
|
|
:param EventData event_data: event data to handle
|
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
event_type = event_data.event_type
|
|
|
|
node_id = event_data.node
|
|
|
|
name = event_data.name
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
node = self.session.get_object(node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Ignoring event for service '%s', unknown node '%s'", name, node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2014-03-05 16:28:32 +00:00
|
|
|
fail = ""
|
2017-06-20 02:09:28 +01:00
|
|
|
services, unknown = self.servicesfromopaque(name, node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
for s in services:
|
2017-06-20 02:09:28 +01:00
|
|
|
if event_type == EventTypes.STOP.value or event_type == EventTypes.RESTART.value:
|
2014-03-05 16:28:32 +00:00
|
|
|
status = self.stopnodeservice(node, s)
|
|
|
|
if status != "0":
|
2017-06-20 02:09:28 +01:00
|
|
|
fail += "Stop %s," % s._name
|
|
|
|
if event_type == EventTypes.START.value or event_type == EventTypes.RESTART.value:
|
2013-08-29 15:21:13 +01:00
|
|
|
if s._custom:
|
|
|
|
cmds = s._startup
|
|
|
|
else:
|
|
|
|
cmds = s.getstartup(node, services)
|
2014-03-05 16:28:32 +00:00
|
|
|
if len(cmds) > 0:
|
|
|
|
for cmd in cmds:
|
|
|
|
try:
|
2017-06-20 02:09:28 +01:00
|
|
|
# node.cmd(shlex.split(cmd), wait = False)
|
|
|
|
status = node.cmd(shlex.split(cmd), wait=True)
|
2014-03-05 16:28:32 +00:00
|
|
|
if status != 0:
|
|
|
|
fail += "Start %s(%s)," % (s._name, cmd)
|
|
|
|
except:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("error starting command %s", cmd)
|
|
|
|
fail += "Start %s," % s._name
|
|
|
|
if event_type == EventTypes.PAUSE.value:
|
2014-03-05 16:28:32 +00:00
|
|
|
status = self.validatenodeservice(node, s, services)
|
|
|
|
if status != 0:
|
2017-06-20 02:09:28 +01:00
|
|
|
fail += "%s," % s._name
|
|
|
|
if event_type == EventTypes.RECONFIGURE.value:
|
2013-12-05 23:44:25 +00:00
|
|
|
if s._custom:
|
|
|
|
cfgfiles = s._configs
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
cfgfiles = s.getconfigfilenames(node.objid, services)
|
2014-03-05 16:28:32 +00:00
|
|
|
if len(cfgfiles) > 0:
|
|
|
|
for filename in cfgfiles:
|
|
|
|
if filename[:7] == "file:///":
|
2017-08-07 23:37:41 +01:00
|
|
|
# TODO: implement this
|
|
|
|
raise NotImplementedError
|
2014-03-05 16:28:32 +00:00
|
|
|
cfg = self.getservicefiledata(s, filename)
|
|
|
|
if cfg is None:
|
|
|
|
cfg = s.generateconfig(node, filename, services)
|
|
|
|
try:
|
|
|
|
node.nodefile(filename, cfg)
|
|
|
|
except:
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("error in configure file: %s", filename)
|
|
|
|
fail += "%s," % s._name
|
2014-03-05 16:28:32 +00:00
|
|
|
|
2017-06-20 02:09:28 +01:00
|
|
|
fail_data = ""
|
2014-03-05 16:28:32 +00:00
|
|
|
if len(fail) > 0:
|
2017-06-20 02:09:28 +01:00
|
|
|
fail_data += "Fail:" + fail
|
|
|
|
unknown_data = ""
|
|
|
|
num = len(unknown)
|
2014-03-05 16:28:32 +00:00
|
|
|
if num > 0:
|
|
|
|
for u in unknown:
|
2017-06-20 02:09:28 +01:00
|
|
|
unknown_data += u
|
2014-03-05 16:28:32 +00:00
|
|
|
if num > 1:
|
2017-06-20 02:09:28 +01:00
|
|
|
unknown_data += ", "
|
2014-03-05 16:28:32 +00:00
|
|
|
num -= 1
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.warn("Event requested for unknown service(s): %s", unknown_data)
|
|
|
|
unknown_data = "Unknown:" + unknown_data
|
|
|
|
|
|
|
|
event_data = EventData(
|
|
|
|
node=node_id,
|
|
|
|
event_type=event_type,
|
|
|
|
name=name,
|
|
|
|
data=fail_data + ";" + unknown_data,
|
|
|
|
time="%s" % time.time()
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session.broadcast_event(event_data)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
class CoreService(object):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Parent class used for defining services.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# service name should not include spaces
|
|
|
|
_name = ""
|
|
|
|
# group string allows grouping services together
|
|
|
|
_group = ""
|
|
|
|
# list name(s) of services that this service depends upon
|
|
|
|
_depends = ()
|
2017-06-20 02:09:28 +01:00
|
|
|
keys = ["dirs", "files", "startidx", "cmdup", "cmddown", "cmdval", "meta", "starttime"]
|
2013-08-29 15:21:13 +01:00
|
|
|
# private, per-node directories required by this service
|
|
|
|
_dirs = ()
|
|
|
|
# config files written by this service
|
|
|
|
_configs = ()
|
|
|
|
# index used to determine start order with other services
|
|
|
|
_startindex = 0
|
|
|
|
# time in seconds after runtime to run startup commands
|
|
|
|
_starttime = ""
|
|
|
|
# list of startup commands
|
|
|
|
_startup = ()
|
|
|
|
# list of shutdown commands
|
|
|
|
_shutdown = ()
|
|
|
|
# list of validate commands
|
|
|
|
_validate = ()
|
|
|
|
# metadata associated with this service
|
|
|
|
_meta = ""
|
|
|
|
# custom configuration text
|
|
|
|
_configtxt = ()
|
|
|
|
_custom = False
|
|
|
|
_custom_needed = False
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Services are not necessarily instantiated. Classmethods may be used
|
|
|
|
against their config. Services are instantiated when a custom
|
|
|
|
configuration is used to override their default parameters.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self._custom = True
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2017-08-08 00:40:39 +01:00
|
|
|
@classmethod
|
|
|
|
def on_load(cls):
|
|
|
|
pass
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2017-06-20 02:09:28 +01:00
|
|
|
def getconfigfilenames(cls, nodenum, services):
|
|
|
|
"""
|
|
|
|
Return the tuple of configuration file filenames. This default method
|
|
|
|
returns the cls._configs tuple, but this method may be overriden to
|
|
|
|
provide node-specific filenames that may be based on other services.
|
|
|
|
|
|
|
|
:param int nodenum: node id to get config file names for
|
|
|
|
:param list services: node services
|
|
|
|
:return: class configuration files
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return cls._configs
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2017-06-20 02:09:28 +01:00
|
|
|
def generateconfig(cls, node, filename, services):
|
|
|
|
"""
|
|
|
|
Generate configuration file given a node object. The filename is
|
|
|
|
provided to allow for multiple config files. The other services are
|
|
|
|
provided to allow interdependencies (e.g. zebra and OSPF).
|
|
|
|
Return the configuration string to be written to a file or sent
|
|
|
|
to the GUI for customization.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to generate config for
|
|
|
|
:param str filename: file name to generate config for
|
|
|
|
:param list services: services for node
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
raise NotImplementedError
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
2017-06-20 02:09:28 +01:00
|
|
|
def getstartup(cls, node, services):
|
|
|
|
"""
|
|
|
|
Return the tuple of startup commands. This default method
|
|
|
|
returns the cls._startup tuple, but this method may be
|
|
|
|
overridden to provide node-specific commands that may be
|
|
|
|
based on other services.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to get startup for
|
|
|
|
:param list services: services for node
|
|
|
|
:return: startup commands
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return cls._startup
|
|
|
|
|
|
|
|
@classmethod
|
2017-06-20 02:09:28 +01:00
|
|
|
def getvalidate(cls, node, services):
|
|
|
|
"""
|
|
|
|
Return the tuple of validate commands. This default method
|
|
|
|
returns the cls._validate tuple, but this method may be
|
|
|
|
overriden to provide node-specific commands that may be
|
|
|
|
based on other services.
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to validate
|
|
|
|
:param list services: services for node
|
|
|
|
:return: validation commands
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return cls._validate
|
2017-06-20 02:09:28 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
@classmethod
|
|
|
|
def tovaluelist(cls, node, services):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Convert service properties into a string list of key=value pairs,
|
|
|
|
separated by "|".
|
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to get value list for
|
|
|
|
:param list services: services for node
|
|
|
|
:return: value list string
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
valmap = [cls._dirs, cls._configs, cls._startindex, cls._startup,
|
2013-08-29 15:21:13 +01:00
|
|
|
cls._shutdown, cls._validate, cls._meta, cls._starttime]
|
|
|
|
if not cls._custom:
|
|
|
|
# this is always reached due to classmethod
|
|
|
|
valmap[valmap.index(cls._configs)] = \
|
|
|
|
cls.getconfigfilenames(node.objid, services)
|
|
|
|
valmap[valmap.index(cls._startup)] = \
|
2017-06-20 02:09:28 +01:00
|
|
|
cls.getstartup(node, services)
|
|
|
|
vals = map(lambda a, b: "%s=%s" % (a, str(b)), cls.keys, valmap)
|
2013-08-29 15:21:13 +01:00
|
|
|
return "|".join(vals)
|
|
|
|
|
|
|
|
def fromvaluelist(self, values):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Convert list of values into properties for this instantiated
|
|
|
|
(customized) service.
|
|
|
|
|
|
|
|
:param list values: value list to set properties from
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# TODO: support empty value? e.g. override default meta with ''
|
|
|
|
for key in self.keys:
|
|
|
|
try:
|
|
|
|
self.setvalue(key, values[self.keys.index(key)])
|
|
|
|
except IndexError:
|
|
|
|
# old config does not need to have new keys
|
2017-06-20 02:09:28 +01:00
|
|
|
logger.exception("error indexing into key")
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def setvalue(self, key, value):
|
2017-06-20 02:09:28 +01:00
|
|
|
"""
|
|
|
|
Set values for this service.
|
|
|
|
|
|
|
|
:param str key: key to set value for
|
|
|
|
:param value: value of key to set
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if key not in self.keys:
|
2017-05-18 23:39:19 +01:00
|
|
|
raise ValueError('key `%s` not in `%s`' % (key, self.keys))
|
2013-08-29 15:21:13 +01:00
|
|
|
# this handles data conversion to int, string, and tuples
|
|
|
|
if value:
|
|
|
|
if key == "startidx":
|
|
|
|
value = int(value)
|
|
|
|
elif key == "meta":
|
|
|
|
value = str(value)
|
|
|
|
else:
|
2017-06-20 02:09:28 +01:00
|
|
|
value = utils.maketuplefromstr(value, str)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if key == "dirs":
|
|
|
|
self._dirs = value
|
|
|
|
elif key == "files":
|
|
|
|
self._configs = value
|
|
|
|
elif key == "startidx":
|
|
|
|
self._startindex = value
|
|
|
|
elif key == "cmdup":
|
|
|
|
self._startup = value
|
|
|
|
elif key == "cmddown":
|
|
|
|
self._shutdown = value
|
|
|
|
elif key == "cmdval":
|
|
|
|
self._validate = value
|
|
|
|
elif key == "meta":
|
|
|
|
self._meta = value
|
|
|
|
elif key == "starttime":
|
|
|
|
self._starttime = value
|