def provision_self(update_manager): """ Provision the node. Returns a deferred. """ name = "node-{:x}".format(devices.get_hardware_serial()) conf = read_provisioning_conf() batch_id = conf.get("batch_id", None) batch_key = conf.get("batch_key", None) def cbresponse(response): router = response.data['router'] nexus.core.provision(router['_id']) nexus.core.saveKey(router['password'], 'apitoken') nexus.core.jwt_valid = True batch = response.data['batch'] hostconfig_patch = batch.get("hostconfig_patch", []) zerotier_networks = batch.get("zerotier_networks", []) update_manager.add_provision_update(hostconfig_patch, zerotier_networks) write_provisioning_result(response.data) data = { "name": name, "key": batch_key, "zerotier_address": zerotier.getAddress() } request = PDServerRequest('/api/batches/{}/provision'.format(batch_id)) d = request.post(**data) d.addCallback(cbresponse) return d
def send(self, report): request = PDServerRequest('/api/routers/{router_id}/' + self.model) d = request.post(**report) # Check for error code and retry. def cbresponse(response): if not response.success: out.warn('{} to {} returned code {}'.format( request.method, request.url, response.code)) if self.max_retries is None or self.retries < self.max_retries: reactor.callLater(self.retryDelay, self.send, report) self.retries += 1 self.increaseDelay() nexus.core.jwt_valid = False else: nexus.core.jwt_valid = True # Check for connection failures and retry. def cberror(ignored): out.warn('{} to {} failed'.format(request.method, request.url)) if self.max_retries is None or self.retries < self.max_retries: reactor.callLater(self.retryDelay, self.send, report) self.retries += 1 self.increaseDelay() nexus.core.jwt_valid = False d.addCallback(cbresponse) d.addErrback(cberror) return d
def send(self, report): request = PDServerRequest('/api/routers/{router_id}/' + self.model) d = request.post(**report) # Check for error code and retry. def cbresponse(response): if not response.success: out.warn('{} to {} returned code {}'.format(request.method, request.url, response.code)) if self.max_retries is None or self.retries < self.max_retries: reactor.callLater(self.retryDelay, self.send, report) self.retries += 1 self.increaseDelay() nexus.core.jwt_valid = False else: nexus.core.jwt_valid = True # Check for connection failures and retry. def cberror(ignored): out.warn('{} to {} failed'.format(request.method, request.url)) if self.max_retries is None or self.retries < self.max_retries: reactor.callLater(self.retryDelay, self.send, report) self.retries += 1 self.increaseDelay() nexus.core.jwt_valid = False d.addCallback(cbresponse) d.addErrback(cberror) return d
def progress(self, message): if self.pkg is not None: self.pkg.request.write(message + '\n') # TODO Look into this. # This might happen during router initialization. If nexus.core is # None, we do not know the router's identity, so we cannot publish any # messages. if nexus.core is None: return data = {'time': time.time(), 'message': message} def handleError(error): print("Error sending message: {}".format(error.getErrorMessage())) # The external field is set for updates from pdserver but not for # locally-initiated (sideloaded) updates. update_id = None if hasattr(self, 'external'): update_id = self.external['update_id'] request = PDServerRequest( '/api/routers/{}/updates/{}/messages'.format( nexus.core.info.pdid, update_id)) d = request.post(**data) d.addErrback(handleError) session = getattr(nexus.core, 'session', None) if session is not None: data['update_id'] = update_id # Catch the occasional Exception due to connectivity failure. We # don't want to fail a chute installation just because we had problems # sending the log messages. try: session.publish(session.uriPrefix + 'updateProgress', data) except Exception as error: out.warn("Publish failed: {} {}".format( error.__class__, error)) # Send messages to internal consumers (e.g. open websocket connections) self.messages.append(data) for observer in self.message_observers: observer.on_message(data)
def provision_self(update_manager): """ Provision the node. Returns a deferred. """ name = "node-{:x}".format(devices.get_hardware_serial()) conf = read_provisioning_conf() batch_id = conf.get("batch_id", None) batch_key = conf.get("batch_key", None) def cbresponse(response): router = response.data['router'] nexus.core.provision(router['_id']) nexus.core.saveKey(router['password'], 'apitoken') nexus.core.jwt_valid = True batch = response.data['batch'] hostconfig_patch = batch.get("hostconfig_patch", []) zerotier_networks = batch.get("zerotier_networks", []) update_manager.add_provision_update(hostconfig_patch, zerotier_networks) write_provisioning_result(response.data) data = { "name": name, "key": batch_key, "zerotier_address": zerotier.getAddress() } request = PDServerRequest('/api/batches/{}/provision'.format(batch_id)) d = request.post(**data) d.addCallback(cbresponse) return d