def deploy(): db.drop_all() db.create_all() users = [ { 'name': '*****@*****.**', 'password': '******' }, ] roles = [ { 'name': 'admin' }, ] with db.auto_commit(): for user in users: User.save(**user) for role in roles: Role.save(**role) for item in app.url_map.iter_rules(): if item.endpoint != 'static': Permission.save(endpoint=item.endpoint) with db.auto_commit(): admin_user = User.get_item_by_name(name='*****@*****.**') admin_role = Role.get_item_by_name(name='admin') admin_role.update( permissions=[permission for permission in Permission.list_items()]) admin_user.update(roles=[admin_role]) with db.auto_commit(): # Zone.save(name='test') zone = Zone.save(name='公网') Zone.save(name='私有云', parent_uid=zone.uid) Zone.save(name='公有云', parent_uid=zone.uid) Zone.save(name='内网') zone = Zone.query.filter(Zone.name == '公有云').first() import json with open('domain.txt') as f: domain_list = json.loads(f.read()) with db.auto_commit(): for domain in domain_list: Domain.save(zone_uid=zone.uid, **domain) with open('host.txt') as f: host_list = json.loads(f.read()) with db.auto_commit(): for host in host_list: Host.save(zone_uid=zone.uid, **host) with open('service.txt') as f: service_list = json.loads(f.read()) with db.auto_commit(): for service in service_list: Service.save(zone_uid=zone.uid, **service)
def get_target_list(self, target_option): db.session.close() target_list = [{ 'uid': service.uid, 'target': { 'ip': service.host_ip, 'port': service.port } } for service in Service.list_items_paginate_by_search( **target_option).items] return target_list
def run(self): for service in Service.list_items_paginate_by_search( page=1, per_page=10000).items: es.index(index='master-host-{}'.format( time.strftime('%Y-%m-%d', time.localtime())), doc_type='doc', body={ 'ip': service.host_ip, 'port': service.port, 'name': service.name, 'info': service.info })
def remove(self): hosts = Host.list_items_by_zone_uid(self.uid) for host in hosts: host.update(zone_uid=None) domains = Domain.list_items_by_zone_uid(self.uid) for domain in domains: domain.update(zone_uid=None) services = Service.list_items_by_zone_uid(self.uid) for service in services: service.update(zone_uid=None) https = HTTP.list_items_by_zone_uid(self.uid) for http in https: http.update(zone_uid=None) super().remove()
def get_service(): page_no = request.args.get('pageNo', 1, type=int) page_size = request.args.get('pageSize', 10, type=int) uid = request.args.get('uid', '', type=str) zone_uid = request.args.get('zone_uid', '', type=str) host_ip = request.args.get('host_ip', '', type=str) port = request.args.get('port', '', type=str) name = request.args.get('name', '', type=str) protocol = request.args.get('protocol', '', type=str) tunnel = request.args.get('tunnel', '', type=str) cpe = request.args.get('cpe', '', type=str) pagination = Service.list_items_paginate_by_search( page=page_no, per_page=page_size, uid=uid, host_ip=host_ip, port=port, status=1, cpe=cpe, name=name, zone_list=Zone.recursion_children_uid_list(zone_uid), protocol=protocol, tunnel=tunnel) return Success(msg=pagination)
def get_list(): page_no = request.args.get('pageNo', 1, type=int) page_size = request.args.get('pageSize', 10, type=int) zone_uid = request.args.get('zone_uid', '', type=str) uid = request.args.get('uid', '', type=str) host_ip = request.args.get('host_ip', '', type=str) port = request.args.get('port', '', type=str) name = request.args.get('name', '', type=str) protocol = request.args.get('protocol', '', type=str) tunnel = request.args.get('tunnel', '', type=str) cpe = request.args.get('cpe', '', type=str) status = request.args.get('status', 1, type=int) pagination = Service.list_items_paginate_by_search(page=page_no, per_page=page_size, uid=uid, host_ip=host_ip, port=port, status=status, cpe=cpe, name=name, zone_list=Zone.recursion_children_uid_list(zone_uid), protocol=protocol, tunnel=tunnel) data = { 'pageSize': pagination.per_page, 'pageNo': pagination.page, 'totalCount': pagination.total, 'data': [{ 'uid': s.uid, 'zone_uid': s.zone_uid, 'host_uid': s.host_uid, 'host_ip': s.host_ip, 'port': s.port, 'protocol': s.protocol, 'tunnel': s.tunnel, 'name': s.name, 'cpe': s.cpe, 'status': s.status, 'info': s.info } for s in pagination.items], 'zones': Zone.recursion_items() } return Success(msg=data)