]> git.proxmox.com Git - mirror_ifupdown2.git/blame - docs/examples/generate_interfaces.py
.gitignore: pycharm remote execution update
[mirror_ifupdown2.git] / docs / examples / generate_interfaces.py
CommitLineData
2c8c4ce7
RP
1#!/usr/bin/python
2
3import argparse
4import sys
5import subprocess
e909bd85 6import os
2c8c4ce7
RP
7
8""" This script prints to stdout /etc/network/interfaces entries for
9 requested interfaces.
10
11 Currently it supports generation of interfaces(5) section for all
12 swp interfaces on the system. And also an interface section
13 for a bridge with all swp ports.
14
15 Example use of this script:
16
17 generate the swp_defaults file:
18 (bkup existing /etc/network/interfaces.d/swp_defaults file if one exists)
19
20 #generate_interfaces.py -s > /etc/network/interfaces.d/swp_defaults
21
22 User -m option if you want the new swp_defaults to be auto merged
23 with the contents from the old file, use -m option
24
25 #generate_interfaces.py -s -m /etc/network/interfaces.d/swp_defaults > /etc/network/interfaces.d/swp_defaults.new
26
27 Include the swp_defaults file in /etc/network/interfaces file
28 (if not already there) using the source command as shown below:
29
30 source /etc/network/interfaces.d/swp_defaults
31
32"""
33
e909bd85
JF
34def get_pci_interfaces():
35 ports = []
36 FNULL = open(os.devnull, 'w')
37 try:
38 cmd = '(ip -o link show | grep -v "@" | cut -d" " -f2 | sed \'s/:$//\')'
39 output = subprocess.check_output(cmd, shell=True).split()
40 for interface in output:
41 cmd = 'udevadm info -a -p /sys/class/net/%s | grep \'SUBSYSTEMS=="pci"\'' % interface
42 try:
43 subprocess.check_call(cmd, shell=True, stdout=FNULL)
44 ports.append(interface)
45 except:
46 pass
47 except:
48 pass
49 finally:
50 FNULL.close()
51 return ports
52
883f7f83 53def get_swp_interfaces():
54 porttab_path = '/var/lib/cumulus/porttab'
55 ports = []
3f917631
JF
56 try:
57 with open(porttab_path, 'r') as f:
58 for line in f.readlines():
59 line = line.strip()
60 if '#' in line:
61 continue
62 try:
63 ports.append(line.split()[0])
64 except ValueError:
65 continue
e909bd85
JF
66 except:
67 try:
68 ports = get_pci_interfaces()
69 except Exception as e:
70 print 'Error: Unsupported script: %s' % str(e)
71 exit(1)
3f917631
JF
72 if not ports:
73 print 'Error: No ports found in %s' % porttab_path
74 exit(1)
2c8c4ce7
RP
75 return ports
76
77def print_swp_defaults_header():
78 print '''
d486dd0d 79# ** This file is autogenerated by /usr/share/doc/ifupdown2/generate_interfaces.py **
2c8c4ce7
RP
80#
81# This is /etc/network/interfaces section for all available swp
82# ports on the system.
83#
84# To include this file in the main /etc/network/interfaces file,
85# copy this file under /etc/network/interfaces.d/ and use the
86# source line in the /etc/network/interfaces file.
87#
88# example entry in /etc/network/interfaces:
89# source /etc/network/interfaces.d/<filename>
90#
91# See manpage interfaces(5) for details.
92'''
93
94def print_bridge_untagged_defaults_header():
95 print '''
d486dd0d 96# ** This file is autogenerated by /usr/share/doc/ifupdown2/generate_interfaces.py **
2c8c4ce7
RP
97#
98# This is /etc/network/interfaces section for a bridge device with all swp
99# ports in the system.
100#
101# To include this file in the main /etc/network/interfaces file,
102# copy this file under /etc/network/interfaces.d/ and use the
103# source line in the /etc/network/interfaces file as shown below.
104# details.
105#
106# example entry in /etc/network/interfaces:
107# source /etc/network/interfaces.d/filename
108#
109# See manpage interfaces(5) for details
110'''
111
112def interfaces_print_swp_default(swp_intf):
113 outbuf = None
114 if args.mergefile:
115 try:
116 cmd = ['/sbin/ifquery', '%s' %swp_intf, '-i', '%s' %args.mergefile]
117 outbuf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
118 except Exception, e:
119 # no interface found gen latest
120 pass
121 if not outbuf:
122 outbuf = 'auto %s\niface %s\n\n' %(swp_intf, swp_intf)
123 return outbuf
124
125def interfaces_print_swp_defaults(swp_intfs):
126 print_swp_defaults_header()
127 outbuf = ''
128 for i in swp_intfs:
129 outbuf += interfaces_print_swp_default(i)
130 print outbuf
131
132def interfaces_print_bridge_default(swp_intfs):
133 print_bridge_untagged_defaults_header()
134 outbuf = 'auto bridge-untagged\n'
135 outbuf += 'iface bridge-untagged\n'
136 outbuf += ' bridge-ports \\\n'
137 linen = 5
138 ports = ''
139 for i in range(0, len(swp_intfs), linen):
140 if ports:
141 ports += ' \\\n'
142 ports += ' %s' %(' '.join(swp_intfs[i:i+linen]))
143 outbuf += ports
144 print outbuf
145
146def populate_argparser(argparser):
147 group = argparser.add_mutually_exclusive_group(required=False)
148 group.add_argument('-s', '--swp-defaults', action='store_true',
149 dest='swpdefaults', help='generate swp defaults file')
150 group.add_argument('-b', '--bridge-default', action='store_true',
151 dest='bridgedefault',
152 help='generate default untagged bridge')
153 argparser.add_argument('-m', '--merge', dest='mergefile', help='merge ' +
154 'new generated iface content with the old one')
155
156argparser = argparse.ArgumentParser(description='ifupdown interfaces file gen helper')
157populate_argparser(argparser)
158args = argparser.parse_args(sys.argv[1:])
159
160if not args.swpdefaults and not args.bridgedefault:
161 argparser.print_help()
162 exit(1)
163
164if args.bridgedefault and args.mergefile:
165 print 'error: mergefile option currently only supported with -s'
166 argparser.print_help()
167 exit(1)
168
169swp_intfs = get_swp_interfaces()
2c8c4ce7
RP
170
171if args.swpdefaults:
172 interfaces_print_swp_defaults(swp_intfs)
173elif args.bridgedefault:
174 interfaces_print_bridge_default(swp_intfs)
175else:
176 argparser.print_help()