def generate_accounts(self) -> None: short_names: Set[str] = set() long_names: Set[str] = set() limitsets: Set[LimitSet] = set() allhosts: Set[str] = set() routers: Set[str] = set() letters = string.ascii_lowercase while len(short_names) < 8: short_names.add(''.join(random.choice(letters) for _ in range(2))) while len(long_names) < 8: long_names.add(''.join(random.choice(letters) for _ in range(random.randint(5, 9)))) while len(routers) < 3: routers.add(''.join(random.choice(letters) for _ in range(random.randint(5, 9)))) while len(limitsets) < 3: limitsets.add(self.generate_limitset()) accounts = [] for sn, name in zip(short_names, long_names): generated_hosts: Set[str] = set() while len(generated_hosts) < random.randint(2, 7): new_host = ''.join(random.choice(letters) for _ in range(random.randint(3, 8))) if new_host in allhosts: continue generated_hosts.add(new_host) allhosts.add(new_host) hosts: Tuple[Host, ...] = tuple(Host(x) for x in generated_hosts) mark = random.choice(list(Mark)) limit = random.choice(list(limitsets)) acc = Account(sn, name, hosts, limit, mark) accounts.append(acc) self.accounts = {o.short: o for o in accounts} self.hosts = tuple(allhosts) self.limits = tuple(limitsets) self.routers = tuple(routers) self.config['test_host'] = random.choice(self.hosts) self.config['test_router'] = random.choice(self.routers)
def addHost(self): host = Host(self, "New Host") newItem = self.tree.AppendItem(self.root, host.getName()) self.tree.SetPyData(newItem, host) self.tree.SetItemImage(newItem, self.fldridx, wx.TreeItemIcon_Normal) self.tree.SetItemImage(newItem, self.fldropenidx, wx.TreeItemIcon_Expanded) self.selectionPanel()
def test_list(self): type = Type.create_type(Type.MYSQL_TCP_IP) hostsNames = ["teste", "teste2"] host = Host(hostsNames[0], type) host2 = Host(hostsNames[1], type) listHost = List() listHost.add(host) listHost.add(host2) i = 0 for v in listHost.iteritems(): self.assertEquals(hostsNames[i], v.name) i += 1 try: listHost.add(host2) except hosts.ExceptHostNameExist: expr = True else: expr = False self.assertTrue(expr)
def main(): devices = inventory.get_device_data() vms = inventory.get_vm_data() ips = inventory.get_ip_data() services = inventory.get_service_data() ansible_inventory = {} ansible_inventory["_meta"] = {} hosts = {} groups = {} for d in devices+vms: host = Host(d, ips) hosts[host.name] = host role = host.hostrole if role not in groups.keys(): groups[role] = {} groups[role]["hosts"] = [] groups[role]["vars"] = {} groups[role]["children"] = [] groups[role]["hosts"].append(host.name) tags = host.tags for tag in tags: tagstr = "tags_" + str(tag) if tagstr not in groups.keys(): groups[tagstr] = {} groups[tagstr]["hosts"] = [] groups[tagstr]["vars"] = {} groups[tagstr]["children"] = [] groups[tagstr]["hosts"].append(host.name) for role in groups.keys(): ansible_inventory[role] = groups[role] for s in services: serv = Service(s) host = hosts[serv.host] if host.services is None: host._data["services"] = [] host._data["services"].append(serv) ansible_inventory["_meta"]["hostvars"] = hosts print(json.dumps(ansible_inventory, default=serialize))
def clear_hosts(): hosts_file = os.path.expanduser("~/.ssh/known_hosts") hosts = [] with open(hosts_file, 'r') as h: # let's read them all for line in h: hosts.append(Host(line)) h.close() # now write out only the non local hosts with open(hosts_file, 'w') as h: for host in hosts: if not host.is_local(): h.write(str(host)) h.close() print("New hosts written") return
def get_config(env, filename, debug): try: with open(filename) as f: test_data = json.load(f) except FileNotFoundError as error: print("File not found") test_data = None return hosts = [] routers = [] links = [] flows = [] nodes = [] # create link objects for link in test_data["links"]: link_info = test_data['links'][link] l = Link(env, \ link_info['link_id'],\ link_info['link_delay'], \ link_info['link_buffer'], \ link_info['link_rate'], \ link_info['link_source'], \ link_info['link_destination'], debug) links.append(l) # create hosts for host in test_data['hosts']: host_info = test_data['hosts'][host] link = next((l for l in links if l.id == test_data['hosts'][host]['link_id'] \ and l.source == host_info['host_id']), None) assert link != None h = Host(env, host_info['host_id'], link, debug) hosts.append(h) # create flow objects # TODO: Start of flows changes for flow in test_data['flows']: flow_info = test_data['flows'][flow] id = flow_info['flow_id'] source = next((h for h in hosts if h.id == flow_info['flow_src']), None) f = Tahoe(id, env, source, flow_info['flow_dest'], flow_info['data_amt'], flow_info['flow_start']) flows.append(f) # create routers for router in test_data['routers']: router_info = test_data['routers'][router] id = router_info['router_id'] links_list = router_info['links'] router_links = [l for l in links if l.id in links_list \ and l.source == router_info['router_id']] assert len(router_links) == len(router_info['links']) r = Router(env, id, router_links, debug) routers.append(r) # Add source/destination obejcts to links, replacing string IDs nodes = routers + hosts for link in links: source = next((n for n in nodes if n.id == link.source), None) destination = next((n for n in nodes if n.id == link.destination), None) link.source = source link.destination = destination return (hosts, links, flows, routers)