def PollRequest(device_id): db = session() tree = cut_namespace_request() # create or update parameters for device device = db.query(Device).get(device_id) if device == None: device = Device() device.id = device_id db.add(device) device.management_system = tree.lookup('ManagementSystem/Name') device.management_version = tree.lookup('ManagementSystem/Version') device.name = tree.lookup('Identification/DeviceName') device.username = tree.lookup('Identification/UserName') device.platform = tree.lookup('Identification/Platform') device.processor = tree.lookup('Identification/Processor') device.ip_address = tree.lookup('Identification/NetworkAdapter/IPAddress') device.ip_subnet = tree.lookup('Identification/NetworkAdapter/IPSubnet') device.codepage = tree.lookup('Identification/CodePage') device.default_locale_id = tree.lookup('Identification/SystemDefaultLCID') device.last_poll_update() db.commit() # send current parameters back to device return render_template('poll_response.xml', device = device, server = Server())
def create_tables(self, device_id): engine = create_engine('sqlite:///%s' % self.db_file) # 创建表结构 model.Base.metadata.create_all(engine) session = self.get_session() device = session.query(Device).first() if device is None: device = Device(id=device_id, status='idle') else: device.id = device_id device.status = 'idle' session.add(device) session.commit() session.close()
def collectLldpAndMatchDevice(self): if (self.configurationInProgressCache.checkAndAddDevice(self.deviceIp)): # for real device the password is coming from pod tmpDevice = Device(self.deviceIp, None, 'root', self.pod.getCleartextPassword(), 'leaf', None, self.deviceIp, None) tmpDevice.id = self.deviceIp self.updateSelfDeviceContext(tmpDevice) logger.debug('Started two stage configuration for %s' % (self.deviceIp)) for i in range(1, self.attempt+1): # wait first: this will replace the original delay design logger.debug('Wait for %d seconds...' % (self.interval)) # don't do unnecessary context switch if self.interval > 0: self.stopEvent.wait(self.interval) if self.stopEvent.is_set(): return logger.debug('Connecting to %s: attempt %d' % (self.deviceIp, i)) try: self.connectToDevice() logger.debug('Connected to %s' % (self.deviceIp)) break except Exception as exc: pass if self.deviceConnectionHandle is None: logger.error('All %d attempts failed for %s' % (self.attempt, self.deviceIp)) self.configurationInProgressCache.doneDevice(self.deviceIp) raise DeviceConnectFailed('All %d attempts failed for %s' % (self.attempt, self.deviceIp)) try: self.device.family = self.deviceConnectionHandle.facts['model'].lower() self.device.serialNumber = self.deviceConnectionHandle.facts['serialnumber'] self.runPreLldpCommands() lldpData = self.collectLldpFromDevice() except Exception as exc: logger.error('Failed to execute deleteVcpPorts for %s, %s' % (self.deviceIp, exc)) raise DeviceRpcFailed('Failed to execute deleteVcpPorts %s' % (self.deviceIp), exc) uplinksWithIfds = self.filterUplinkAppendRemotePortIfd(lldpData, self.device.family) self.updateSpineStatusFromLldpData([x['ifd2'] for x in uplinksWithIfds]) device = self.findMatchedDevice(uplinksWithIfds) if device is None: logger.info('Did not find good enough match for %s' % (self.deviceIp)) self.configurationInProgressCache.doneDevice(self.deviceIp) return self.fixInterfaces(device, self.device.family, uplinksWithIfds) # persist serialNumber device.serialNumber = self.device.serialNumber self._dao.updateObjectsAndCommitNow(self._session, [device]) self.updateSelfDeviceContext(device) try: try: self.runPostLldpCommands() except SkipCommit: self.updateDeviceConfigStatus('good') return self.updateDeviceConfigStatus('processing') self.updateDeviceConfiguration() self.updateDeviceConfigStatus('good') except DeviceRpcFailed as exc: logger.error('Two stage configuration failed for %s, %s' % (self.deviceLogStr, exc)) self.updateDeviceConfigStatus(None, error = exc) raise except Exception as exc: logger.error('Two stage configuration failed for %s, %s' % (self.deviceLogStr, exc)) self.updateDeviceConfigStatus('error', str(exc)) raise finally: self.releaseConfigurationInProgressLock(self.deviceIp) logger.debug('Ended two stage configuration for %s' % (self.deviceLogStr)) else: logger.debug('Two stage configuration is already in progress for %s', (self.deviceLogStr))
def collectLldpAndMatchDevice(self): if (self.configurationInProgressCache.checkAndAddDevice( self.deviceIp)): # for real device the password is coming from pod tmpDevice = Device(self.deviceIp, None, 'root', self.pod.getCleartextPassword(), 'leaf', None, self.deviceIp, None) tmpDevice.id = self.deviceIp self.updateSelfDeviceContext(tmpDevice) logger.debug('Started two stage configuration for %s' % (self.deviceIp)) for i in range(1, self.attempt + 1): # wait first: this will replace the original delay design logger.debug('Wait for %d seconds...' % (self.interval)) # don't do unnecessary context switch if self.interval > 0: self.stopEvent.wait(self.interval) if self.stopEvent.is_set(): return logger.debug('Connecting to %s: attempt %d' % (self.deviceIp, i)) try: self.connectToDevice() logger.debug('Connected to %s' % (self.deviceIp)) break except Exception as exc: pass if self.deviceConnectionHandle is None: logger.error('All %d attempts failed for %s' % (self.attempt, self.deviceIp)) self.configurationInProgressCache.doneDevice(self.deviceIp) raise DeviceConnectFailed('All %d attempts failed for %s' % (self.attempt, self.deviceIp)) try: self.device.family = self.deviceConnectionHandle.facts[ 'model'].lower() self.device.serialNumber = self.deviceConnectionHandle.facts[ 'serialnumber'] self.runPreLldpCommands() lldpData = self.collectLldpFromDevice() except Exception as exc: logger.error('Failed to execute deleteVcpPorts for %s, %s' % (self.deviceIp, exc)) raise DeviceRpcFailed( 'Failed to execute deleteVcpPorts %s' % (self.deviceIp), exc) uplinksWithIfds = self.filterUplinkAppendRemotePortIfd( lldpData, self.device.family) self.updateSpineStatusFromLldpData( [x['ifd2'] for x in uplinksWithIfds]) device = self.findMatchedDevice(uplinksWithIfds) if device is None: logger.info('Did not find good enough match for %s' % (self.deviceIp)) self.configurationInProgressCache.doneDevice(self.deviceIp) return self.fixInterfaces(device, self.device.family, uplinksWithIfds) # persist serialNumber device.serialNumber = self.device.serialNumber self._dao.updateObjectsAndCommitNow(self._session, [device]) self.updateSelfDeviceContext(device) try: try: self.runPostLldpCommands() except SkipCommit: self.updateDeviceConfigStatus('good') return self.updateDeviceConfigStatus('processing') self.updateDeviceConfiguration() self.updateDeviceConfigStatus('good') except DeviceRpcFailed as exc: logger.error('Two stage configuration failed for %s, %s' % (self.deviceLogStr, exc)) self.updateDeviceConfigStatus(None, error=exc) raise except Exception as exc: logger.error('Two stage configuration failed for %s, %s' % (self.deviceLogStr, exc)) self.updateDeviceConfigStatus('error', str(exc)) raise finally: self.releaseConfigurationInProgressLock(self.deviceIp) logger.debug('Ended two stage configuration for %s' % (self.deviceLogStr)) else: logger.debug( 'Two stage configuration is already in progress for %s', (self.deviceLogStr))