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