def list_containers(self): if self.runtime == 'cluster': containers = [] hosts = self.get_hosts() for host in hosts: print('[ get containers ]', host.name) host_containers = host.get_docker_client().containers.list( filters={'name': prefix(self.name)}) containers.extend(host_containers) # TODO: fix this goddamit, sql is the last thing I want to mess with right now, but this is ugly return list(set(containers)) else: return docker.from_env().containers.list( filters={'name': prefix(self.name)})
async def stop_lab_api(): data = await request.json if data is None or 'name' not in data: return abort(400) name = data.get('name') try: lab = Lab.get(Lab.name == name) except peewee.DoesNotExist: abort(404) try: args = [] if lab.runtime == 'cluster': args.extend((lab.get_host_map(), Host.get_all_configs())) stop_lab(lab_path(name), prefix(name), *args, runtime=lab.runtime, remove=True) lab.delete_instance() msg_cache.delete(name) return 'OK', 200 except FileNotFoundError: return abort(404)
def __init__(self, lab_name): self.lab_name = lab_name self.prefix = prefix(lab_name) msg_cache.set(lab_name, { 'all': {}, 'status_count': {}, 'total': 0, 'avg_traffic': 0, 'start_time': int(time.time()) }) self.sock = ctx.socket(zmq.PULL) url = self.socket_url() self.sock.bind(url) asyncio.ensure_future(self.listen())
async def get_lab_containers(): name = request.args.get('name') if name is None: return abort(400) if not os.path.exists(lab_path(name)): abort(404) try: lab = Lab.get(Lab.name == name) containers = lab.list_containers() except peewee.DoesNotExist: containers = [] prefix_len = len(prefix(name)) return jsonify({c.name[prefix_len:]: c.short_id for c in containers})
async def get_messages(): lab = request.args.get('lab', 'DEMO') messages = Messenger.get_messages(lab) lab_prefix = prefix(lab) with open(os.path.join(lab_path(lab), 'lab.json')) as config_file: lab_config = json.load(config_file) for name, config in lab_config['nodes'].items(): fullname = lab_prefix + name # print(routers_cache, fullname, config) if not config['isRouter']: continue if routers_cache.has(fullname): messages[fullname] = {'status': 1} print('[ msg ]', messages) return jsonify(messages)
def lattice2xml(): if os.path.exists(config.builddir() + '/lattice2xml'): return config.builddir() + '/lattice2xml' else: return config.prefix() + '/bin/lattice2xml'
def systemLibraryPath(): if os.path.exists(config.prefix() + '/lib/xml/lattices.xml'): return config.prefix() + '/lib/xml/lattices.xml' else: return config.builddir() + '/../lib/xml/lattices.xml'
async def show_graph(): lab = request.args.get('lab', 'DEMO') return await render_template('graph.html', lab=lab, prefix=prefix(lab))
async def restart_lab_api(): ''' try: hosts_ids = request.json.get('hosts', list) except (ValueError, AttributeError): return abort(400) hosts = Host.select().where(Host.id in hosts_ids) if len(hosts) == 0: return abort(400) elif len(hosts) == 1: runtime = 'docker' else: runtime = 'cluster' ''' data = await request.json if data is None or 'name' not in data: return abort(400) name = data.get('name') lab, created = Lab.get_or_create(name=name, defaults={'runtime': runtime}) tc_options = data.get('tc_options', {}) hosts = Host.get_all_configs() if created: ret, killed_routers = start_lab(lab_path(name), prefix(name), *(() if runtime!='cluster' else \ (hosts,)), default_tc_options=tc_options, runtime=runtime) else: ret, killed_routers = restart_lab(lab_path(name), prefix(name), *(() if runtime!='cluster' else \ (lab.get_host_map(), hosts)), default_tc_options=tc_options, runtime=runtime) print('\n[ deleting old nodes & network ]\n') Node.delete().where(Node.lab_id == lab.id).execute() Network.delete().where(Network.lab_id == lab.id).execute() print('\n[ mid-state ]', list(Node.select())) if runtime == 'cluster': hosts_map = ret for host_name, (containers, networks) in ret.items(): Node.bulk_create([ Node(name=c.name, short_id=c.short_id, lab=lab, host_id=hosts[host_name]['id']) for c in containers ]) Network.bulk_create([ Network(name=n, lab=lab, host=hosts[host_name]['id']) for n in networks ]) msgr = Messenger(name) # with open(os.path.join('/sockets', name, 'lab.sock'), 'w') as f: # f.write('no') for container_name in killed_routers: routers_cache.set(container_name, True) return 'OK', 200