]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - docs/examples/generate_interfaces.py
8 """ This script prints to stdout /etc/network/interfaces entries for
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.
15 Example use of this script:
17 generate the swp_defaults file:
18 (bkup existing /etc/network/interfaces.d/swp_defaults file if one exists)
20 #generate_interfaces.py -s > /etc/network/interfaces.d/swp_defaults
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
25 #generate_interfaces.py -s -m /etc/network/interfaces.d/swp_defaults > /etc/network/interfaces.d/swp_defaults.new
27 Include the swp_defaults file in /etc/network/interfaces file
28 (if not already there) using the source command as shown below:
30 source /etc/network/interfaces.d/swp_defaults
34 def get_pci_interfaces():
36 FNULL
= open(os
.devnull
, 'w')
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
43 subprocess
.check_call(cmd
, shell
=True, stdout
=FNULL
)
44 ports
.append(interface
)
53 def get_swp_interfaces():
54 porttab_path
= '/var/lib/cumulus/porttab'
57 with
open(porttab_path
, 'r') as f
:
58 for line
in f
.readlines():
63 ports
.append(line
.split()[0])
68 ports
= get_pci_interfaces()
69 except Exception as e
:
70 print 'Error: Unsupported script: %s' % str(e
)
73 print 'Error: No ports found in %s' % porttab_path
77 def print_swp_defaults_header():
79 # ** This file is autogenerated by /usr/share/doc/ifupdown2/generate_interfaces.py **
81 # This is /etc/network/interfaces section for all available swp
82 # ports on the system.
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.
88 # example entry in /etc/network/interfaces:
89 # source /etc/network/interfaces.d/<filename>
91 # See manpage interfaces(5) for details.
94 def print_bridge_untagged_defaults_header():
96 # ** This file is autogenerated by /usr/share/doc/ifupdown2/generate_interfaces.py **
98 # This is /etc/network/interfaces section for a bridge device with all swp
99 # ports in the system.
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.
106 # example entry in /etc/network/interfaces:
107 # source /etc/network/interfaces.d/filename
109 # See manpage interfaces(5) for details
112 def interfaces_print_swp_default(swp_intf
):
116 cmd
= ['/sbin/ifquery', '%s' %swp
_intf
, '-i', '%s' %args
.mergefile
]
117 outbuf
= subprocess
.check_output(cmd
, stderr
=subprocess
.STDOUT
)
119 # no interface found gen latest
122 outbuf
= 'auto %s\niface %s\n\n' %(swp_intf
, swp_intf
)
125 def interfaces_print_swp_defaults(swp_intfs
):
126 print_swp_defaults_header()
129 outbuf
+= interfaces_print_swp_default(i
)
132 def 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'
139 for i
in range(0, len(swp_intfs
), linen
):
142 ports
+= ' %s' %(' '.join(swp_intfs
[i
:i
+linen
]))
146 def 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')
156 argparser
= argparse
.ArgumentParser(description
='ifupdown interfaces file gen helper')
157 populate_argparser(argparser
)
158 args
= argparser
.parse_args(sys
.argv
[1:])
160 if not args
.swpdefaults
and not args
.bridgedefault
:
161 argparser
.print_help()
164 if args
.bridgedefault
and args
.mergefile
:
165 print 'error: mergefile option currently only supported with -s'
166 argparser
.print_help()
169 swp_intfs
= get_swp_interfaces()
172 interfaces_print_swp_defaults(swp_intfs
)
173 elif args
.bridgedefault
:
174 interfaces_print_bridge_default(swp_intfs
)
176 argparser
.print_help()