def run(self): self.createTables(Network) Network(name="172.28.141.192/28", network="172.28.141.192/28", gateway="172.28.141.249").add() Network(name="172.28.138.128/25", network="172.28.138.128/25", gateway="172.28.138.249", disabled=1).add() Network(name="172.28.147.0/24", network="172.28.147.0/24", gateway="172.28.147.249", disabled=1).add() Network(name="10.16.197.64/26", network="10.16.197.64/26", gateway="10.16.197.249", disabled=1).add() Network(name="10.16.198.0/24", network="10.16.198.0/24", gateway="10.16.198.249", disabled=1).add() Db.commit() self.createTables(Address) self.createTables(Node) return self.results
def crtTbl(): Db.createTable("accountGroups", ( "`id` INTEGER PRIMARY KEY UNIQUE", "`ownerId` INTEGER NOT NULL", "`name` VARCHAR(48) NOT NULL", "`description` VARCHAR(255) DEFAULT ''", ))
def crtTbl(): Db.createTable(table, ( "`id` INTEGER PRIMARY KEY UNIQUE", "`firstName` VARCHAR(48) NOT NULL", "`lastName` VARCHAR(48) NOT NULL", "`tags` VARCHAR(1024) NOT NULL", "`description` VARCHAR(255) DEFAULT ''", ))
def crtTbl(): Db.createTable(table, ( "`network` CHAR(18) PRIMARY KEY UNIQUE", "`gateway` CHAR(15) NOT NULL", "`name` VARCHAR(48) NOT NULL", "`disabled` INTEGER DEFAULT 0", "`description` VARCHAR(255) DEFAULT ''", ))
def add(self): masterPassword = "******" kvargs = self.__dict__ if kvargs["publicKey"] is None: kvargs["publicKey"], kvargs["secretKey"] = RSA.generate( masterPassword) kvargs["lastChange"] = f"{{:{config.datetimeFormat}}}".format( datetime.now()) self.id = super().add(table, **kvargs) Db.commit() return self
def crtTbl(): Db.createTable( table, ("`id` INTEGER PRIMARY KEY UNIQUE", "`ownerId` INTEGER NOT NULL", "`login` VARCHAR(24) NOT NULL", "`password` VARCHAR(64) NOT NULL", "`tags` VARCHAR(1024) NOT NULL", "`lastChange` CHAR(19) NOT NULL", "`expired` CHAR(19)", "`publicKey` VARCHAR(1548) NOT NULL", "`secretKey` VARCHAR(3048) NOT NULL", "`description` VARCHAR(255) DEFAULT ''"))
def crtTbl(): Db.createTable(table, ( "`id` VARCHAR(48) NOT NULL UNIQUE", "`name` VARCHAR(48)", "`os` VARCHAR(12)", "`osName` VARCHAR(48)", "`auth` CHAR(6)", "`agent` VARCHAR(24)", "`addresses` VARCHAR(255)", "`description` VARCHAR(255) DEFAULT ''", ))
def getNodes(**kvargs): if not "fields" in kvargs: kvargs["fields"] = Node.fields return { item["id"]: Node(**item) for item in Db().getAll(table, **kvargs) }
def getAccounts(**kvargs): if not "fields" in kvargs: kvargs["fields"] = Account.fields return { item["id"]: Account(**item) for item in Db().getAll(table, **kvargs) }
def onStart(): Db.connect(config)
def onStop(): Db.close()
memcachedb = memcache.Client([settings['memcache_host']]) bcc = None if settings['debug'] == False: bcc = MemcachedBytecodeCache(memcachedb) jinja_environment = Environment( loader=FileSystemLoader(settings['template_path']), bytecode_cache=bcc,#缓存模板编译的结果到memcache auto_reload=settings['debug'], autoescape=False) db_old = Db({'db': 'carlife', 'host': '123.57.217.29', 'port': settings['db_port'], \ 'user': '******', 'passwd': '9%qVP*vz', 'charset': 'utf8', \ 'max_connections':settings['max_connections'], 'stale_timeout':settings['stale_timeout']}) db_move = Db({'db': 'czj', 'host': '123.57.217.29', 'port': settings['db_port'], \ 'user': '******', 'passwd': '9%qVP*vz', 'charset': 'utf8', \ 'max_connections':settings['max_connections'], 'stale_timeout':settings['stale_timeout']}) db = Db({'db': settings['db_name'], 'host': settings['db_host'], 'port': settings['db_port'], \ 'user': settings['db_user'], 'passwd': settings['db_passwd'], 'charset': 'utf8', \ 'max_connections':settings['max_connections'], 'stale_timeout':settings['stale_timeout']}) db_move3 = Db({'db': 'czjmove3', 'host': settings['db_host'], 'port': settings['db_port'], \ 'user': settings['db_user'], 'passwd': settings['db_passwd'], 'charset': 'utf8', \ 'max_connections':settings['max_connections'], 'stale_timeout':settings['stale_timeout']})
def add(self): kvargs = self.__dict__ kvargs["created"] = f"{{:{config.datetimeFormat}}}".format(datetime.now()) super().add(table, **kvargs) Db.commit() return self
def add(self): self.id = super().add(table, **self.__dict__) Db.commit() return self
def getNetworks(**kvargs): if not "fields" in kvargs: kvargs["fields"] = Network.fields return {item["network"]: Network(**item) for item in Db().getAll(table, **kvargs)}
def crtTbl(): Db.createTable("account_group", ( "`accountId` INTEGER NOT NULL", "`groupId` INTEGER NOT NULL", ))
def loop(self): sleep(10) while True: Events.onStart() for network in Network.getNetworks(where="disabled=0").values(): addresses = Address.getAddresses( where=f'network="{network.network}"') with Pool(processes=config.discovery.numProc) as pool: for netId, ip, availabled in pool.starmap_async( self.ping, zip(repeat(network.network), IPv4Network(network.network).hosts())).get(): if ip in addresses: address = addresses[ip] if address.disabled == 0: address.setAvailabled(availabled) if availabled == 1: if address.isChanged: print("log>", f'Address "{ip}" availabled') # if address.nodeId is None: self.getNode(address) else: if datetime.fromisoformat( address.lastChange) + timedelta( days=config.discovery. notAvailDeleteDelay ) > datetime.now(): node = Node().get( where='id="%{address.nodeId}%"') if node.id: if ip == node.addresses: node.delete() print( "log>", f'Node "{node.id} ({node.name})" not availabled more then {config.discovery.notAvailDeleteDelay} days. Node deleted' ) else: node.removeAddress(ip) node.update() address.delete() print( "log>", f'Address "{ip}" not availabled more then {config.discovery.notAvailDeleteDelay} days. Address deleted' ) elif address.isChanged: print( "log>", f'Address "{ip}" not availabled') address.update() if address.isChanged: print(address) elif availabled == 1: address = Address(network=netId, ip=ip, availabled=1) self.getNode(address) address.add() print("log>", f'Address "{ip}" availabled') print("add", address) Db.commit() Events.onStop sleep(config.discovery.period * 3600)
def update(self, where): kvargs = self.__dict__ super().update(table, where, **kvargs) Db.commit() return self
def crtTbl(): Db.createTable("user_group", ( "`userId` INTEGER NOT NULL", "`groupId` INTEGER NOT NULL", ))
def update(self, where): kvargs = self.__dict__ # kvargs["lastChange"] = f"{{:{config.datetimeFormat}}}".format(datetime.now()) super().update(table, where, **kvargs) Db.commit() return self
def getUsers(**kvargs): if not "fields" in kvargs: kvargs["fields"] = User.fields return {item["id"]: User(**item) for item in Db().getAll(table, **kvargs)}
'template_path': os.path.join(os.path.dirname(__file__), 'template2'), 'static_path': os.path.join(os.path.dirname(__file__), 'style2'), 'upload_path': os.path.join(os.path.dirname(__file__), 'upload'), 'cookie_secret': "F*x+poCnRSGIb/EsikPs5gI0BTwBBkN5k8U4kPxaV1o=", 'login_url': '/signin', "xsrf_cookies": True, 'autoescape': None }) memcachedb = memcache.Client([settings['memcache_host']]) bcc = None if settings['debug'] == False: bcc = MemcachedBytecodeCache(memcachedb) jinja_environment = Environment( loader=FileSystemLoader(settings['template_path']), bytecode_cache=bcc, #缓存模板编译的结果到memcache auto_reload=settings['debug'], autoescape=False) db = Db({'db': settings['db_name'], 'host': settings['db_host'], 'port': settings['db_port'], \ 'user': settings['db_user'], 'passwd': settings['db_passwd'], 'charset': 'utf8', \ 'max_connections':settings['max_connections'], 'stale_timeout':settings['stale_timeout']})
def update(self, where): super().update(table, where, **self.__dict__) Db.commit() return self
def crtTbl(): Db.createTable(table, ( "`id` CHAR(36) NOT NULL UNIQUE", "`created` CHAR(19) NOT NULL", "`userId` INTEGER" ))