4ba3a833d04e8abe521d0fcd8bbc7008fb6bac75
[csit.git] / resources / tools / topology / update_topology.py
1 #!/usr/bin/env python2.7
2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """This executable python module gathers MAC address data from topology nodes.
16 It requires that all interfaces/port elements in topology have driver field.
17 This script binds the port in given node to set linux kernel driver and
18 extracts MAC address from it."""
19
20 import sys
21 import os
22 import re
23
24 from argparse import ArgumentParser
25
26 import yaml
27
28 from resources.libraries.python.ssh import SSH
29
30
31 def load_topology(args):
32     """Load topology file referenced to by parameter passed to this script.
33
34     :param args: Arguments parsed from commandline.
35     :type args: ArgumentParser().parse_args()
36     :return: Python representation of topology YAML.
37     :rtype: dict
38     """
39     data = None
40     with open(args.topology, "r") as stream:
41         try:
42             data = yaml.safe_load(stream)
43         except yaml.YAMLError as exc:
44             print(f"Failed to load topology file: {args.topology}")
45             print(exc)
46             raise
47
48     return data
49
50
51 def ssh_no_error(ssh, cmd):
52     """Execute a command over ssh channel, and log and exit if the command
53     fails.
54
55     :param ssh: SSH() object connected to a node.
56     :param cmd: Command line to execute on remote node.
57     :type ssh: SSH() object
58     :type cmd: str
59     :return: stdout from the SSH command.
60     :rtype: str
61     """
62     ret, stdo, stde = ssh.exec_command(cmd)
63     if ret != 0:
64         print(f"Command execution failed: '{cmd}'")
65         print(f"stdout: {stdo}")
66         print(f"stderr: {stde}")
67         raise RuntimeError(u"Unexpected ssh command failure")
68
69     return stdo
70
71
72 def update_mac_addresses_for_node(node):
73     """For given node loop over all ports with PCI address and look for its MAC
74     address.
75
76     This function firstly unbinds the PCI device from its current driver
77     and binds it to linux kernel driver. After the device is bound to specific
78     linux kernel driver the MAC address is extracted from /sys/bus/pci location
79     and stored within the node dictionary that was passed to this function.
80
81     :param node: Node from topology.
82     :type node: dict
83     """
84     for port_name, port in node[u"interfaces"].items():
85         if u"driver" not in port:
86             raise RuntimeError(
87                 f"{node[u'host']} port {port_name} has no driver element, "
88                 f"exiting"
89             )
90
91         ssh = SSH()
92         ssh.connect(node)
93
94         # TODO: make following SSH commands into one-liner to save on SSH opers
95
96         # First unbind from current driver
97         drvr_dir_path = f"/sys/bus/pci/devices/{port[u'pci_address']}/driver"
98         cmd = f'''\
99             if [ -d {drvr_dir_path} ]; then
100                 echo {port[u'pci_address']} | sudo tee {drvr_dir_path}/unbind ;
101             else
102                 true Do not have to do anything, port already unbound ;
103             fi'''
104         ssh_no_error(ssh, cmd)
105
106         # Then bind to the 'driver' from topology for given port
107         cmd = f"echo {port[u'pci_address']} | " \
108             f"sudo tee /sys/bus/pci/drivers/{port[u'driver']}/bind"
109         ssh_no_error(ssh, cmd)
110
111         # Then extract the mac address and store it in the topology
112         cmd = f"cat /sys/bus/pci/devices/{port['pci_address']}/net/*/address"
113         mac = ssh_no_error(ssh, cmd).strip()
114         pattern = re.compile(u"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$")
115         if not pattern.match(mac):
116             raise RuntimeError(
117                 f"MAC address read from host {node[u'host']} "
118                 f"{port[u'pci_address']} is in bad format '{mac}'"
119             )
120         print(
121             f"{node[u'host']}: Found MAC address of PCI device "
122             f"{port[u'pci_address']}: {mac}"
123         )
124         port[u"mac_address"] = mac
125
126
127 def update_nodes_mac_addresses(topology):
128     """Loop over nodes in topology and get mac addresses for all listed ports
129     based on PCI addresses.
130
131     :param topology: Topology information with nodes.
132     :type topology: dict
133     """
134     for node in topology[u"nodes"].values():
135         update_mac_addresses_for_node(node)
136
137
138 def dump_updated_topology(topology, args):
139     """Writes or prints out updated topology file.
140
141     :param topology: Topology information with nodes.
142     :param args: Arguments parsed from command line.
143     :type topology: dict
144     :type args: ArgumentParser().parse_args()
145     :return: 1 if error occurred, 0 if successful.
146     :rtype: int
147     """
148     if args.output_file:
149         if not args.force:
150             if os.path.isfile(args.output_file):
151                 print (
152                     f"File {args.output_file} already exists. If you want to "
153                     f"overwrite this file, add -f as a parameter to this script"
154                 )
155                 return 1
156         with open(args.output_file, "w") as stream:
157             yaml.dump(topology, stream, default_flow_style=False)
158     else:
159         print(yaml.dump(topology, default_flow_style=False))
160     return 0
161
162
163 def main():
164     """Main function"""
165     parser = ArgumentParser()
166     parser.add_argument(u"topology", help=u"Topology yaml file to read")
167     parser.add_argument(u"--output-file", u"-o", help=u"Output file")
168     parser.add_argument(
169         u"-f", u"--force", help=u"Overwrite existing file",
170         action=u"store_const", const=True
171     )
172     parser.add_argument(u"--verbose", u"-v", action=u"store_true")
173     args = parser.parse_args()
174
175     topology = load_topology(args)
176     update_nodes_mac_addresses(topology)
177     ret = dump_updated_topology(topology, args)
178
179     return ret
180
181
182 if __name__ == u"__main__":
183     sys.exit(main())