Beiran Daemon

Beiran daemon execution script to create server and schedule tasks by observing nodes and communication each other.

class beiran.daemon.main.BeiranDaemon(loop: asyncio.events.AbstractEventLoop = None)[source]

Bases: pyee.EventEmitter

Beiran Daemon

Initialize self. See help(type(self)) for accurate signature.

Check or wait uniil plugin status to be ‘ready’

Set ‘offline’ to all node status

Load and initiate plugin :param plugin_type: plugin type :type plugin_type: str :param plugin_name: plugin name :type plugin_name: str :param config: config parameters :type config: dict

Returns:

Return plugin instance

Initialize database

Initialize configured plugins

Main function

Discovered new node on beiran network :param ip_address: ip_address of new node :type ip_address: str :param service_port: service port of new node :type service_port: int

Placeholder for event on node removed

Placeholder for event on node removed

Track updates on (syncable) plugin states

This will be used for checking if nodes are in sync or not

Bootstrapping peer without discovery

Node on beiran network is down :param ip_address: ip_address of new node :type ip_address: str :param service_port: service port of new node :type service_port: int

Main function wrapper, creates the main loop and schedules the main function in there

Signal Handler

Temporary function for testing python plugin distribution methods

Set and emit node’s new status :param new_status: status :type new_status: str

Graceful shutdown

version = '0.0.8'
Wait until plugin status to be ‘ready’

Support library for beiran daemon

beiran.daemon.lib.collect_node_info() → dict[source]

Collect and return Node info

Returns:node information
Return type:dict
beiran.daemon.lib.get_advertise_address() → str[source]

First try environment variable BEIRAN_LISTEN_ADDRESS. If it is not set, return the listen address unless it is 0.0.0.0.

Lastly return default gateway’s ip address

Returns
string: listen address.
Returns:ip address of advertise address
Return type:string
beiran.daemon.lib.get_default_gateway_interface() → tuple[source]

Get default gateway’s ip and interface info.

Returns:ip address, interface name. (10.0.2.2, eth0)
Return type:tuple
beiran.daemon.lib.get_hostname() → str[source]

Gets hostname for discovery

beiran.daemon.lib.get_listen_address() → str[source]
Returns:daemon listen IP address
Return type:string
beiran.daemon.lib.get_listen_interface() → str[source]

Seek for listen interface in order described below and return it.

First try BEIRAN_LISTEN_INTERFACE env var. Second try to find the interface of ip address specified by config.listen_address. Third, if config.listen_address is not set return default gateway’s interface

Returns
string: listen address.
beiran.daemon.lib.get_sync_version() → int[source]

Gets last sync_version from local file.

Returns
int: sync version.
beiran.daemon.lib.local_node_uuid() → uuid.UUID[source]

Get UUID from config file if it exists, else return a new one and write it to config file

Returns:uuid in hex
Return type:(UUID)

Todo

  • Group this function and similar functionality in a class that will allow eliminate the global usage
beiran.daemon.lib.sync_version_file_path() → str[source]

Return sync_version file path

beiran.daemon.lib.update_sync_version_file(version: int)[source]

Write new sync_version to the sync_version file

Module for in memory node tracking object Nodes

class beiran.daemon.nodes.Nodes[source]

Bases: object

Nodes is in memory data model, composed of members of Beiran Cluster

Initialize self. See help(type(self)) for accurate signature.

Appends the new node into nodes dict or updates if exists

param node:node object
type node:Node
returns:node object
rtype:node (Node)

Get all nodes with docker information from database and dumps them into a dict.

returns:key value list of nodes as in form {‘uuid’: {‘ip’: ‘10.0.0.2’, ‘hostname’:’web1’}}
rtype:dict

Returns the node specified by ip address.

param ip_address:
 ip address
type ip_address:
 str
param service_port:
 port of node
type service_port:
 int
param from_db:indicate search scope
type from_db:bool
returns:(Node) found node object

Unless from_db is True, get node dict from self.all_nodes memory object, if available.

If from_db is True or node is not in memory, try to get from db.

param uuid:node uuid
type uuid:str
param from_db:ask for db if True, else get from memory if available
type from_db:bool
returns:node object
rtype:node (Node)

Get node from database :param uuid: node uuid :type uuid: Optional[str]

returns:node object
rtype:node (Node)

List all nodes from database or nodes dict

param from_db:db lookup for nodes or not
type from_db:bool
returns:list of node objects
rtype:list

Remove node from nodes dict

param node:node model object
type node:Node

Returns:

Remove node from online nodes collection

Append node to online nodes collection

Peer class for handling;
  • beirand-beirand communications
  • status changes
  • information updates
class beiran.daemon.peer.Peer(node=None, nodes=None, loop=None, local=False)[source]

Bases: pyee.EventEmitter

Peer class

param node:node object
type node:Node
param nodes:
type nodes:Nodes
param loop:asyncio loop
type loop:object
param local:local peer or not
type local:bool

Add this peer to node’s peer registry

Fetches node information using url :param peer_address: peer_address object :type peer_address: PeerAddress

returns:(Node) node object with fetched info

Find node in peer registty or create new Peer

lifecycle of a beiran-node connection

param peer_address:
 address of peer which will be probed
type peer_address:
 PeerAddress
param extra_addr:
 additional addresses we should check
type extra_addr:
 list
param retries:retry number, -1 means forever until node found
type retries:int
returns:probed node
rtype:(Node)

Probe remote node at peer_address and ask probe back local node

param peer_address:
 peer address to be probed
type peer_address:
 peer_address
param extra_addr:
 additional addresses we should check
type extra_addr:
 list
returns:node object
rtype:(Node)

Request probe from a remote node :param peer_address: peer address to be asked to probe back :type peer_address: PeerAddress :param probe_back: whether to be asked to probe back :type probe_back: bool

returns:node object
rtype:(Node)

schedule handling of peer in the asyncio loop

Sync plugin states with other peers

Delete nodes’s peer registry

HTTP and WS API implementation of beiran daemon

class beiran.daemon.http_ws.ApiRootHandler(application, request, **kwargs)[source]

Bases: tornado.web.RequestHandler

API Root endpoint / handling

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.

class beiran.daemon.http_ws.EchoWebSocket(application, request, **kwargs)[source]

Bases: tornado.websocket.WebSocketHandler

Websocket implementation for test

Web socket data received in chunk :param chunk: Current received data

Monitor if websocket is closed

Received message from websocket

Monitor if websocket is opened

class beiran.daemon.http_ws.NodeInfo(application, request, **kwargs)[source]

Bases: tornado.web.RequestHandler

Endpoint which reports node information

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.

Retrieve info of the node by uuid or the local node

class beiran.daemon.http_ws.NodesHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs)[source]

Bases: beiran.cmd_req_handler.RPCEndpoint

List nodes by arguments specified in uri all, online, offline, etc.

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.

Return list of nodes, if specified all from database or discovered ones from memory.

returns:list of nodes, it is a dict, since tornado does not write list for security reasons; see: http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.write
rtype:(dict)

Probe the node on address specified in request body.

returns:http response
public_methods = ['probe']
class beiran.daemon.http_ws.Ping(application, request, **kwargs)[source]

Bases: tornado.web.RequestHandler

Ping / Pong endpoint

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.

Just return a PONG response

class beiran.daemon.http_ws.PluginStatusHandler(application, request, **kwargs)[source]

Bases: tornado.web.RequestHandler

Status endpoint for plugins

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.

class beiran.daemon.http_ws.StatusHandler(application, request, **kwargs)[source]

Bases: tornado.web.RequestHandler

Status endpoint

Implement this method to handle streamed request data.

Requires the .stream_request_body decorator.