pass at cleaning up custom service documentation and removing the need for pycco for this case

This commit is contained in:
Blake Harnden 2019-06-25 16:03:37 -07:00
parent f8960cc519
commit f6af078e7e
4 changed files with 82 additions and 407 deletions

View file

@ -1,26 +0,0 @@
This directory contains a sample custom service that you can use as a template
for creating your own services.
Follow these steps to add your own services:
1. Modify the sample service MyService to do what you want. It could generate
config/script files, mount per-node directories, start processes/scripts,
etc. sample.py is a Python file that defines one or more classes to be
imported. You can create multiple Python files that will be imported.
Add any new filenames to the __init__.py file.
2. Put these files in a directory such as /home/username/.core/myservices
Note that the last component of this directory name 'myservices' should not
be named something like 'services' which conflicts with an existing Python
name (the syntax 'from myservices import *' is used).
3. Add a 'custom_services_dir = /home/username/.core/myservices' entry to the
/etc/core/core.conf file.
4. Restart the CORE daemon (core-daemon). Any import errors (Python syntax)
should be displayed in the /var/log/core-daemon.log log file (or on screen).
5. Start using your custom service on your nodes. You can create a new node
type that uses your service, or change the default services for an existing
node type, or change individual nodes.

View file

@ -1,64 +1,81 @@
"""
Sample user-defined service.
Simple example for a user-defined service.
"""
from core.services.coreservices import CoreService
from core.services.coreservices import ServiceMode
## Custom CORE Service
class MyService(CoreService):
### Service Attributes
"""
Custom CORE Service
# Name used as a unique ID for this service and is required, no spaces.
:var str name: name used as a unique ID for this service and is required, no spaces
:var str group: allows you to group services within the GUI under a common name
:var tuple executables: executables this service depends on to function, if executable is
not on the path, service will not be loaded
:var tuple dependencies: services that this service depends on for startup, tuple of service names
:var tuple dirs: directories that this service will create within a node
:var tuple configs: files that this service will generate, without a full path this file goes in
the node's directory e.g. /tmp/pycore.12345/n1.conf/myfile
:var tuple startup: commands used to start this service, any non-zero exit code will cause a failure
:var tuple validate: commands used to validate that a service was started, any non-zero exit code
will cause a failure
:var ServiceMode validation_mode: validation mode, used to determine startup success.
NON_BLOCKING - runs startup commands, and validates success with validation commands
BLOCKING - runs startup commands, and validates success with the startup commands themselves
TIMER - runs startup commands, and validates success by waiting for "validation_timer" alone
:var int validation_timer: time in seconds for a service to wait for validation, before determining
success in TIMER/NON_BLOCKING modes.
:var float validation_validation_period: period in seconds to wait before retrying validation,
only used in NON_BLOCKING mode
:var tuple shutdown: shutdown commands to stop this service
"""
name = "MyService"
# Allows you to group services within the GUI under a common name.
group = "Utility"
# Executables this service depends on to function, if executable is not on the path, service will not be loaded.
executables = ()
# Services that this service depends on for startup, tuple of service names.
dependencies = ()
# Directories that this service will create within a node.
dirs = ()
# Files that this service will generate, without a full path this file goes in the node's directory.
# e.g. /tmp/pycore.12345/n1.conf/myfile
configs = ("myservice1.sh", "myservice2.sh")
# Commands used to start this service, any non-zero exit code will cause a failure.
startup = ("sh %s" % configs[0], "sh %s" % configs[1])
# Commands used to validate that a service was started, any non-zero exit code will cause a failure.
validate = ()
# Validation mode, used to determine startup success.
#
# * NON_BLOCKING - runs startup commands, and validates success with validation commands
# * BLOCKING - runs startup commands, and validates success with the startup commands themselves
# * TIMER - runs startup commands, and validates success by waiting for "validation_timer" alone
validation_mode = ServiceMode.NON_BLOCKING
# Time in seconds for a service to wait for validation, before determining success in TIMER/NON_BLOCKING modes.
validation_timer = 5
# Period in seconds to wait before retrying validation, only used in NON_BLOCKING mode.
validation_period = 0.5
# Shutdown commands to stop this service.
shutdown = ()
### On Load
@classmethod
def on_load(cls):
# Provides a way to run some arbitrary logic when the service is loaded, possibly to help facilitate
# dynamic settings for the environment.
"""
Provides a way to run some arbitrary logic when the service is loaded, possibly to help facilitate
dynamic settings for the environment.
:return: nothing
"""
pass
### Get Configs
@classmethod
def get_configs(cls, node):
# Provides a way to dynamically generate the config files from the node a service will run.
# Defaults to the class definition and can be left out entirely if not needed.
"""
Provides a way to dynamically generate the config files from the node a service will run.
Defaults to the class definition and can be left out entirely if not needed.
:param node: core node that the service is being ran on
:return: tuple of config files to create
"""
return cls.configs
### Generate Config
@classmethod
def generate_config(cls, node, filename):
# Returns a string representation for a file, given the node the service is starting on the config filename
# that this information will be used for. This must be defined, if "configs" are defined.
"""
Returns a string representation for a file, given the node the service is starting on the config filename
that this information will be used for. This must be defined, if "configs" are defined.
:param node: core node that the service is being ran on
:param str filename: configuration file to generate
:return: configuration file content
:rtype: str
"""
cfg = "#!/bin/sh\n"
if filename == cls.configs[0]:
@ -70,16 +87,24 @@ class MyService(CoreService):
return cfg
### Get Startup
@classmethod
def get_startup(cls, node):
# Provides a way to dynamically generate the startup commands from the node a service will run.
# Defaults to the class definition and can be left out entirely if not needed.
"""
Provides a way to dynamically generate the startup commands from the node a service will run.
Defaults to the class definition and can be left out entirely if not needed.
:param node: core node that the service is being ran on
:return: tuple of startup commands to run
"""
return cls.startup
### Get Validate
@classmethod
def get_validate(cls, node):
# Provides a way to dynamically generate the validate commands from the node a service will run.
# Defaults to the class definition and can be left out entirely if not needed.
"""
Provides a way to dynamically generate the validate commands from the node a service will run.
Defaults to the class definition and can be left out entirely if not needed.
:param node: core node that the service is being ran on
:return: tuple of commands to validate service startup with
"""
return cls.validate