Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ansible/files/empty_graph.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<LabConnectionGraph>
<PhysicalNetworkGraphDeclaration>
<Devices/>
<DeviceInterfaceLinks/>
</PhysicalNetworkGraphDeclaration>
<DataPlaneGraph/>
</LabConnectionGraph>
7 changes: 7 additions & 0 deletions ansible/files/graph_files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Public graph files
- lab_connection_graph.xml
- example_ixia_connection_graph.xml


# Private graph files
46 changes: 39 additions & 7 deletions ansible/library/conn_graph_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@

'''

LAB_CONNECTION_GRAPH_FILE = 'lab_connection_graph.xml'
LAB_GRAPHFILE_PATH = 'files/'

class Parse_Lab_Graph():
"""
Parse the generated lab physical connection graph and insert Ansible fact of the graph
Expand Down Expand Up @@ -231,28 +228,63 @@ def get_host_connections(self, hostname):
else:
return self.links

def contains_hosts(self, hostnames):
return set(hostnames) <= set(self.devices)


LAB_CONNECTION_GRAPH_FILE = 'graph_files.yml'
EMPTY_GRAPH_FILE = 'empty_graph.xml'
LAB_GRAPHFILE_PATH = 'files/'

"""
Find a graph file contains all devices in testbed.
"""
def find_graph(hostnames, anchor):
filename = LAB_GRAPHFILE_PATH + LAB_CONNECTION_GRAPH_FILE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use os.path.join?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. But improve with another PR?

with open(filename) as fd:
file_list = yaml.load(fd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pyyaml will complain about this when we flip to python3, did you try using safe_load instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try. I prefer to get this merged as-is since it is tested. Then improve.


for fn in file_list:
filename = LAB_GRAPHFILE_PATH + fn
lab_graph = Parse_Lab_Graph(filename)
lab_graph.parse_graph()
if hostnames and lab_graph.contains_hosts(hostnames):
return lab_graph
if anchor and lab_graph.contains_hosts(anchor):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is explained somewhere but what is anchor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explained in the PR comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is still a little confusing to me, but this can be iterated on.

return lab_graph

# Fallback to return an empty connection graph, this is
# needed to bridge the kvm test needs. The KVM test needs
# A graph file, which used to be whatever hardcoded file.
# Here we provide one empty file for the purpose.
lab_graph = Parse_Lab_Graph(LAB_GRAPHFILE_PATH + EMPTY_GRAPH_FILE)
lab_graph.parse_graph()
return lab_graph

def main():
module = AnsibleModule(
argument_spec=dict(
host=dict(required=False),
hosts=dict(required=False, type='list'),
filename=dict(required=False),
anchor=dict(required=False, type='list'),
),
mutually_exclusive=[['host', 'hosts']],
supports_check_mode=True
)
m_args = module.params

hostnames = m_args['hosts']
anchor = m_args['anchor']
if not hostnames:
hostnames = [m_args['host']]
try:
if m_args['filename']:
filename = m_args['filename']
lab_graph = Parse_Lab_Graph(filename)
lab_graph.parse_graph()
else:
filename = LAB_GRAPHFILE_PATH + LAB_CONNECTION_GRAPH_FILE
lab_graph = Parse_Lab_Graph(filename)
lab_graph.parse_graph()
lab_graph = find_graph(hostnames, anchor)

device_info = []
device_conn = []
Expand Down Expand Up @@ -280,7 +312,7 @@ def main():

module.exit_json(ansible_facts=results)
except (IOError, OSError):
module.fail_json(msg="Can not find lab graph file "+LAB_CONNECTION_GRAPH_FILE)
module.fail_json(msg="Can not find lab graph file")
except Exception as e:
module.fail_json(msg=traceback.format_exc())

Expand Down
3 changes: 3 additions & 0 deletions ansible/roles/fanout/tasks/rootfanout_connect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- set_fact: dut="{{ leaf_name }}"
when: deploy_leaf

- debug: msg="Configuring fanout switch for {{ dut }}"

- name: Gathering connection facts about the DUTs or leaffanout device
conn_graph_facts:
host: "{{ dut if ',' not in dut else omit }}"
Expand All @@ -17,6 +19,7 @@

- name: Gathering connection facts about the lab
conn_graph_facts:
anchor: "{{ dut.split(',') | list }}"
delegate_to: localhost
tags: always
register: lab
Expand Down