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