in api/plugins/registry.py [0:0]
def __init__(self, session, nodeid = None):
""" Loads a node from the registry or inits a new one """
self._data = {}
self.session = session
self.conn = session.DB.sqlite.open('nodes.db')
# node variables
self.hostname = ""
self.pem = ""
self.id = None
self.description = None
self.location = None
self.ipv6 = False
self.verified = False
self.enabled = False
self.ip = "0.0.0.0"
self.lastping = int(time.time())
if nodeid:
doc = None
nc = self.conn.cursor()
# Load by API Key?
if isinstance(nodeid, str) and re.match(r"^[a-f0-9]+-[a-f0-9-]+$", nodeid):
self.apikey = nodeid
nc.execute("SELECT * FROM `registry` WHERE `apikey` = ? LIMIT 1", (self.apikey,))
doc = nc.fetchone()
# Load by Node ID?
elif re.match(r"^[0-9]+$", str(nodeid)):
self.id = int(nodeid)
nc.execute("SELECT * FROM `registry` WHERE `id` = ? LIMIT 1", (self.id,))
doc = nc.fetchone()
if doc:
self.apikey = doc['apikey']
self.hostname = doc['hostname']
self.pem = doc['pubkey']
self.id = doc['id']
self.description = doc['description']
self.location = doc['location']
self.ipv6 = False # TODO!
self.verified = (doc['verified'] == 1)
self.enabled = (doc['enabled'] == 1)
self.ip = doc['ip']
self.lastping = doc['lastping']
self.version = doc['version']
self.key = plugins.crypto.loads(self.pem)
self.fingerprint = plugins.crypto.fingerprint(self.pem)
else:
raise Exception("No such node found in registry")
# new node from scratch?
else:
self.apikey = str(uuid.uuid4())