slamon-agent

Python implementation of a SLAMon Agent


License
Apache-2.0
Install
pip install slamon-agent==1.1.0

Documentation

SLAMon Python Agent

License

Latest PyPI Version Supported Python Versions Requirements Status

Build Status Coverage Status Code Health

Python implementation of SLAMon agent.

Prerequisites

Required libraries for running the agent:

$ pip install requests
$ pip install python-dateutil

Required libraries for running the tests:

$ pip install responses

Installing the agent

To easily install the agent with all required dependencies and the slamon-agent command line utility, use e.g. pip.

$ pip install slamon-agent

Running the agent

There are two ways for running the agent: a command line script and embedding agent in your own script.

Running the agent using command line script

slamon-agent command line utility is the simplest way running the agent. Agent Fleet Manager (AFM) Url, concurrent executor count and modules to load handlers from are defined as command line arguments as follows:

$ slamon-agent --help
usage: slamon-agent-script.py [-h] -u URL [-l LOAD] [-w DEFAULT_WAIT]
                              [-x NUM_EXECUTORS]

optional arguments:
  -h, --help            show this help message and exit
  -u URL, --url URL     Coordinator URL
  -l LOAD, --load LOAD  Load handlers from specified module or package e.g. slamon_agent.handlers
  -w DEFAULT_WAIT, --default-wait DEFAULT_WAIT
                        Seconds to wait before reconnection after connection
                        failure.
  -x NUM_EXECUTORS, --num-executors NUM_EXECUTORS
                        Number of concurrent task executors.

Starting an agent with only the wait task handler.

$ slamon-agent -u http://my_afm.url.com -l slamon_agent.handlers.wait_task_handler

Running the agent from python script

from slamon_agent.handlers import *
from slamon_agent import Agent
agent = Agent('http://localhost:8080')
agent.run()

Note that when running agent in a script, you need to explicitly import the modules you want to load task handlers from:

from my_custom_handler_package import *
import my_custom_handler_module

from slamon_agent import Agent
agent = Agent('http://localhost:8080')
agent.run()

Developing task handlers

Developing task handler is attempted to make as easy as possible. The agent core application will take care of the communication between an agent and the AFM, so that the task handler can be AFM agnostic and purely focus on just executing the task.

Task handlers are simple functions that take input data as dictionary parameter, do their work synchronously and return response data as a dictionary.

Task handlers are registered using @TaskHandler decorator.

from slamon_agent.handlers import TaskHandler

@TaskHandler("wait", 1)
def wait_task_handler(input_params):
   import time, random
   timeout = float(input_params['time']) - 0.5 + random.random()
   return {'time': timeout}

Docker images

Pre-existing images are built from master and dev branches: slamon/agent:stable and slamon/agent:latest respectively, and also from GitHub tags as slamon/agent:<tag>.

Using the images

The image offers an entrypoint that requires two environment values to be defined: AFM and HANDLERS (Note: The handlers are not generally required but since the agent is useless without them, it is required here). There also exists an EXTRA_FLAGS environment variable that can be used to add f.ex. verbosity flags.

If you want to override how slamon-agent is started, you need to redefine the CMD verb.

With Docker Compose

The following snippet can be used:

agent:
  image: slamon/agent:stable
  environment:
    AFM: "http://slamon-afm"
    HANDLERS: "slamon_agent.handlers"
    EXTRA_FLAGS: "-v"