def setup_calvinlib(): class MockJson(object): def fromstring(self, string): from json import loads return loads(string) def tostring(self, data): from json import dumps return dumps(data) class MockBase64(object): def encode(self, data): from base64 import b64encode return b64encode(data) def decode(self, string): from base64 import b64decode return b64decode(string) class MockCopy(object): def copy(self, data): from copy import copy return copy(data) import calvin.runtime.north.calvinlib as calvinlib calvinlib.TESTING = True from calvin.runtime.north.calvinlib import get_calvinlib lib = get_calvinlib() lib.set("json", MockJson()) lib.set("base64", MockBase64()) lib.set("copy", MockCopy())
def handle_get_node_capabilities(self, handle, connection, match, data, hdr): """ GET /capabilities Get capabilities of this calvin node Response status code: OK Response: list of capabilities """ self.send_response( handle, connection, json.dumps(self.node._calvinsys.list_capabilities() + get_calvinsys().list_capabilities() + get_calvinlib().list_capabilities()))
def check_requirements_and_sec_policy(self, requirements, security=None, actor_id=None, signer=None, decision_from_migration=None, callback=None): for req in requirements: if not get_calvinsys().has_capability(req) and not get_calvinlib().has_capability(req): raise Exception("Actor requires %s" % req) if security_enabled(): # Check if access is permitted for the actor by the security policy. # Will continue directly with callback if authorization is not enabled. security.check_security_policy(callback, actor_id=actor_id, requires=['runtime'] + requirements, element_type="actor", element_value=signer, decision_from_migration=decision_from_migration) else: callback()
def setup_calvinlib(): import calvin.runtime.north.calvinlib as calvinlib calvinlib.TESTING = True from calvin.runtime.north.calvinlib import get_calvinlib lib = get_calvinlib() lib.init( capabilities={ "math.arithmetic.compare": { "module": "mathlib.Arithmetic" }, "math.arithmetic.operator": { "module": "mathlib.Arithmetic" }, "math.arithmetic.eval": { "module": "mathlib.Arithmetic" }, "math.random": { "module": "mathlib.Random", "attributes": { "seed": 10 } }, "json": { "module": "jsonlib.Json" }, "base64": { "module": "base64lib.Base64" }, "copy": { "module": "datalib.Copy" }, "mustache": { "module": "textformatlib.Pystache" }, "time": { "module": "timelib.Time" }, "regexp": { "module": "regexp.PyRegexp", } })
def setup_calvinlib(): import calvin.runtime.north.calvinlib as calvinlib calvinlib.TESTING=True from calvin.runtime.north.calvinlib import get_calvinlib lib = get_calvinlib() lib.init(capabilities={ "math.arithmetic.compare": { "module": "mathlib.Arithmetic" }, "math.arithmetic.operator": { "module": "mathlib.Arithmetic" }, "math.arithmetic.eval": { "module": "mathlib.Arithmetic" }, "math.random": { "module": "mathlib.Random", "attributes": {"seed": 10} }, "json": { "module": "jsonlib.Json" }, "base64": { "module": "base64lib.Base64" }, "copy": { "module": "datalib.Copy" }, "mustache": { "module": "textformatlib.Pystache" }, "time": { "module": "timelib.Time" }, "regexp": { "module": "regexp.PyRegexp", } })
def check_requirements_and_sec_policy(self, requirements, security=None, actor_id=None, signer=None, decision_from_migration=None, callback=None): for req in requirements: if not get_calvinsys().has_capability( req) and not get_calvinlib().has_capability(req): raise Exception("Actor requires %s" % req) if security_enabled(): # Check if access is permitted for the actor by the security policy. # Will continue directly with callback if authorization is not enabled. security.check_security_policy( callback, actor_id=actor_id, requires=['runtime'] + requirements, element_type="actor", element_value=signer, decision_from_migration=decision_from_migration) else: callback()
def dispose(obj): get_calvinlib().remove(obj)
def use(name, **kwargs): return get_calvinlib().use(name, **kwargs)
def __init__(self, uris, control_uri, attributes=None): super(Node, self).__init__() self.quitting = False # Warn if its not a uri if not isinstance(uris, list): _log.error("Calvin uris must be a list %s" % uris) raise TypeError("Calvin uris must be a list!") # Uris self.uris = uris if attributes: ext_uris = attributes.pop('external_uri', None) if ext_uris is not None: self.uris += ext_uris # Control uri self.control_uri = control_uri self.external_control_uri = attributes.pop('external_control_uri', self.control_uri) \ if attributes else self.control_uri try: self.attributes = AttributeResolver(attributes) except: _log.exception("Attributes not correct, uses empty attribute!") self.attributes = AttributeResolver(None) self.node_name = self.attributes.get_node_name_as_str() # Obtain node id, when using security also handle runtime certificate try: security_dir = _conf.get("security", "security_dir") self.runtime_credentials = RuntimeCredentials(self.node_name, node=self, security_dir=security_dir) self.id = self.runtime_credentials.get_node_id() except Exception as err: _log.debug("No runtime credentials, err={}".format(err)) self.runtime_credentials = None self.id = calvinuuid.uuid("Node") self.certificate_authority = certificate_authority.CertificateAuthority(self) self.authentication = authentication.Authentication(self) self.authorization = authorization.Authorization(self) self.metering = metering.set_metering(metering.Metering(self)) self.monitor = Event_Monitor() self.am = actormanager.ActorManager(self) self.rm = replicationmanager.ReplicationManager(self) self.control = calvincontrol.get_calvincontrol() _scheduler = scheduler.DebugScheduler if _log.getEffectiveLevel() <= logging.DEBUG else scheduler.Scheduler self.sched = _scheduler(self, self.am, self.monitor) self.async_msg_ids = {} self._calvinsys = CalvinSys(self) calvinsys = get_calvinsys() calvinsys.init(self) calvinlib = get_calvinlib() calvinlib.init(self) # Default will multicast and listen on all interfaces # TODO: be able to specify the interfaces # @TODO: Store capabilities self.storage = storage.Storage(self) self.network = CalvinNetwork(self) self.proto = CalvinProto(self, self.network) self.pm = PortManager(self, self.proto) self.app_manager = appmanager.AppManager(self) # The initialization that requires the main loop operating is deferred to start function async.DelayedCall(0, self.start)
def handle_get_node_capabilities(self, handle, connection, match, data, hdr): """ GET /capabilities Get capabilities of this calvin node Response status code: OK Response: list of capabilities """ self.send_response(handle, connection, json.dumps(get_calvinsys().list_capabilities() + get_calvinlib().list_capabilities()))
def use(name, **kwargs): return get_calvinlib().use(name, **kwargs)
def __init__(self, uris, control_uri, attributes=None): super(Node, self).__init__() self.quitting = False self.super_node_class = None # Warn if its not a uri if not isinstance(uris, list): _log.error("Calvin uris must be a list %s" % uris) raise TypeError("Calvin uris must be a list!") # Uris self.uris = uris if attributes: ext_uris = attributes.pop('external_uri', None) if ext_uris is not None: self.uris += ext_uris # Control uri self.control_uri = control_uri self.external_control_uri = attributes.pop('external_control_uri', self.control_uri) \ if attributes else self.control_uri try: self.attributes = AttributeResolver(attributes) except: _log.exception("Attributes not correct, uses empty attribute!") self.attributes = AttributeResolver(None) self.node_name = self.attributes.get_node_name_as_str() # Obtain node id, when using security also handle runtime certificate try: security_dir = _conf.get("security", "security_dir") self.runtime_credentials = RuntimeCredentials(self.node_name, node=self, security_dir=security_dir) self.id = self.runtime_credentials.get_node_id() except Exception as err: _log.debug("No runtime credentials, err={}".format(err)) self.runtime_credentials = None self.id = calvinuuid.uuid("Node") self.certificate_authority = certificate_authority.CertificateAuthority(self) self.authentication = authentication.Authentication(self) self.authorization = authorization.Authorization(self) self.am = actormanager.ActorManager(self) self.rm = replicationmanager.ReplicationManager(self) self.control = calvincontrol.get_calvincontrol() # _scheduler = scheduler.DebugScheduler if _log.getEffectiveLevel() <= logging.DEBUG else scheduler.Scheduler # _scheduler = scheduler.NonPreemptiveScheduler # _scheduler = scheduler.RoundRobinScheduler _scheduler = scheduler.SimpleScheduler # _scheduler = scheduler.BaselineScheduler self.sched = _scheduler(self, self.am) self.async_msg_ids = {} calvinsys = get_calvinsys() calvinsys.init(self) calvinlib = get_calvinlib() calvinlib.init() # Default will multicast and listen on all interfaces # TODO: be able to specify the interfaces # @TODO: Store capabilities self.storage = storage.Storage(self) self.network = CalvinNetwork(self) self.proto = CalvinProto(self, self.network) self.pm = PortManager(self, self.proto) self.app_manager = appmanager.AppManager(self) self.cpu_monitor = CpuMonitor(self.id, self.storage) self.mem_monitor = MemMonitor(self.id, self.storage) self.proxy_handler = ProxyHandler(self) # The initialization that requires the main loop operating is deferred to start function async.DelayedCall(0, self.start)