def node_stop(force): if force or len(client.get_endpoints(hostname=hostname, orchestrator_id=ORCHESTRATOR_ID)) == 0: client.remove_host(hostname) try: docker_client.stop("calico-node") except docker.errors.APIError as err: if err.response.status_code != 404: raise print "Node stopped and all configuration removed" else: print "Current host has active endpoints so can't be stopped." + \ " Force with --force"
def node_stop(force): if force or len( client.get_endpoints(hostname=hostname, orchestrator_id=ORCHESTRATOR_ID)) == 0: client.remove_host(hostname) try: docker_client.stop("calico-node") except docker.errors.APIError as err: if err.response.status_code != 404: raise print "Node stopped and all configuration removed" else: print "Current host has active endpoints so can't be stopped." + \ " Force with --force"
def endpoint_show(hostname, orchestrator_id, workload_id, endpoint_id, detailed): """ List the profiles for a given endpoint. All parameters will be used to filter down which endpoints should be shown. :param endpoint_id: The endpoint ID. :param workload_id: The workload ID. :param orchestrator_id: The orchestrator ID. :param hostname: The hostname. :param detailed: Optional flag, when set to True, will provide more information in the shown table :return: Nothing """ endpoints = client.get_endpoints(hostname=hostname, orchestrator_id=orchestrator_id, workload_id=workload_id, endpoint_id=endpoint_id) if detailed: headings = [ "Hostname", "Orchestrator ID", "Workload ID", "Endpoint ID", "Addresses", "MAC", "Profiles", "State" ] x = PrettyTable(headings, sortby="Hostname") for endpoint in endpoints: addresses = "\n".join( [str(net) for net in endpoint.ipv4_nets | endpoint.ipv6_nets]) x.add_row([ endpoint.hostname, endpoint.orchestrator_id, endpoint.workload_id, endpoint.endpoint_id, addresses, endpoint.mac, ','.join(endpoint.profile_ids), endpoint.state ]) else: headings = [ "Hostname", "Orchestrator ID", "NumWorkloads", "NumEndpoints" ] x = PrettyTable(headings, sortby="Hostname") """ To calculate the number of unique endpoints, and unique workloads on each host, we first create a dictionary in the following format: { host1: { workload1: num_workload1_endpoints, workload2: num_workload2_endpoints, ... }, host2: { workload3: num_workload3_endpoints, workload4: num_workload4_endpoints, ... } """ # Use a vividict so the host key is automatically set table_dict = Vividict() for endpoint in endpoints: if endpoint.workload_id not in table_dict[endpoint.hostname]: table_dict[endpoint.hostname][endpoint.workload_id] = 0 table_dict[endpoint.hostname][endpoint.workload_id] += 1 # This table has one entry for each host. So loop through the hosts for host in table_dict: # Check how many workloads belong to each host num_workloads = len(table_dict[host]) # Add up how many endpoints each workload on this host has num_endpoints = 0 for workload, endpoints in iter(table_dict[host].items()): num_endpoints += endpoints # Add the results to this table new_row = [ endpoint.hostname, endpoint.orchestrator_id, num_workloads, num_endpoints ] x.add_row(new_row) print str(x) + "\n"
def endpoint_show(hostname, orchestrator_id, workload_id, endpoint_id, detailed): """ List the profiles for a given endpoint. All parameters will be used to filter down which endpoints should be shown. :param endpoint_id: The endpoint ID. :param workload_id: The workload ID. :param orchestrator_id: The orchestrator ID. :param hostname: The hostname. :param detailed: Optional flag, when set to True, will provide more information in the shown table :return: Nothing """ endpoints = client.get_endpoints(hostname=hostname, orchestrator_id=orchestrator_id, workload_id=workload_id, endpoint_id=endpoint_id) if detailed: headings = ["Hostname", "Orchestrator ID", "Workload ID", "Endpoint ID", "Addresses", "MAC", "Profiles", "State"] x = PrettyTable(headings, sortby="Hostname") for endpoint in endpoints: addresses = "\n".join([str(net) for net in endpoint.ipv4_nets | endpoint.ipv6_nets]) x.add_row([endpoint.hostname, endpoint.orchestrator_id, endpoint.workload_id, endpoint.endpoint_id, addresses, endpoint.mac, ','.join(endpoint.profile_ids), endpoint.state]) else: headings = ["Hostname", "Orchestrator ID", "Number of Workloads", "Number of Endpoints"] x = PrettyTable(headings, sortby="Hostname") # The summary table has one entry for each host/orchestrator # combination. We create a dictionary to maintain the summary # information, using the (hostname, orchestrator_id) as the unique # key, with a value of an EndpointSummary object to store unique # workload IDs and a count of endpoint IDs. # # We use a default dict to automatically create an "empty" (zero count) # EndpointSummary for each new table entry. host_orch_summary = defaultdict(EndpointSummary) for endpoint in endpoints: key = (endpoint.hostname, endpoint.orchestrator_id) summary = host_orch_summary[key] summary.add_endpoint(endpoint) # This table has one entry for each host/orchestrator combination. for key, summary in host_orch_summary.iteritems(): hostname, orchestrator_id = key x.add_row([hostname, orchestrator_id, len(summary.workload_ids), summary.num_endpoints]) print str(x) + "\n"