From 522bf8e68b5f8225ce71cee5ae0ca8e28488544e Mon Sep 17 00:00:00 2001 From: Roopa Prabhu Date: Fri, 6 Jun 2014 23:00:24 -0700 Subject: [PATCH] Fix bash completion for sudo users (mostly cosmetic) + man page and example fixes Ticket: CM-2911 Reviewed By: CCR-1637 Testing Done: tested ifupdown2 sanity and bash completion The python argcomplete module that i use for ifupdown2 has a limitation that it does not work with sudo when used in the global mode. But there is a workaround for it online (long story short...instead of enabling the global argparse complete ...the author recommends registering argparse complete bash completion individually for your script). This patch does just that. This patch also moves the udev overrides to their respective packages. Two of them are owned by ifupdown2. Conflicts: rootconf/default/home/cumulus/sysroot-complete --- completion/ifup | 9 +++++++++ debian/python-ifupdown2.postinst | 16 +++++++++++++++- debian/python-ifupdown2.postrm | 9 +++++++-- docs/examples/interfaces | 31 ++++++++++++++++++++++++------- man.rst/ifquery.8.rst | 22 +++++++++++++++------- man.rst/ifup.8.rst | 17 +++++++++++++---- sbin/ifupdown | 25 +++++++++++++------------ setup.py | 3 ++- 8 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 completion/ifup diff --git a/completion/ifup b/completion/ifup new file mode 100644 index 0000000..0be547f --- /dev/null +++ b/completion/ifup @@ -0,0 +1,9 @@ + +_python_argcomplete() { + local IFS=' ' + COMPREPLY=( $(IFS="$IFS" COMP_LINE="$COMP_LINE" COMP_POINT="$COMP_POINT" _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" _ARGCOMPLETE=1 "$1" 8>&1 9>&2 1>/dev/null 2>/dev/null) ) + if [[ $? != 0 ]]; then + unset COMPREPLY + fi +} +complete -F _python_argcomplete ifup ifdown ifquery ifreload diff --git a/debian/python-ifupdown2.postinst b/debian/python-ifupdown2.postinst index bff0a81..9247975 100644 --- a/debian/python-ifupdown2.postinst +++ b/debian/python-ifupdown2.postinst @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e MYNAME="${0##*/}" @@ -70,6 +70,20 @@ case "$1" in ;; esac +# override default udev bridge and hotplug rules because they interfere with +# networking init script +udev_user_rulesdir=/etc/udev/rules.d/ +udev_sys_rulesdir=/lib/udev/rules.d/ +if [ -e $udev_user_rulesdir ]; then + udev_ifupdown2_overrides=("80-networking.rules" "60-bridge-network-interface.rules") + for u in ${udev_ifupdown2_overrides[*]} + do + if [ -e ${udev_sys_rulesdir}/$u -a ! -e ${udev_user_rulesdir}/$u ]; then + (cd ${udev_user_rulesdir} && ln -sf /dev/null $u) + fi + done +fi + if [ -x "/etc/init.d/networking" ]; then update-rc.d networking start 40 S . start 35 0 6 . >/dev/null || exit $? fi diff --git a/debian/python-ifupdown2.postrm b/debian/python-ifupdown2.postrm index 42651bc..e0f07d4 100644 --- a/debian/python-ifupdown2.postrm +++ b/debian/python-ifupdown2.postrm @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e @@ -7,7 +7,7 @@ rm -f /sbin/ifup /sbin/ifdown /sbin/ifquery case "$1" in purge) # Note: We don't remove /etc/network/interfaces - rm -f /etc/network/run/ifstate + rm -f /var/tmp/network/ifstatenew if [ -L /etc/network/run ] ; then rm -f /etc/network/run @@ -17,6 +17,11 @@ case "$1" in ;; esac +udevlink=$(readlink /etc/udev/rules.d/80-networking.rules 2>/dev/null) +[ -n "$udevlink" -a "$udevlink" == "/dev/null" ] && rm -f /etc/udev/rules.d/80-networking.rules +udevlink=$(readlink /etc/udev/rules.d/60-bridge-network-interface.rules 2>/dev/null) +[ -n "$udevlink" -a "$udevlink" == "/dev/null" ] && rm -f /etc/udev/rules.d/60-bridge-network-interface.rules + if [ "$1" = "purge" ] ; then update-rc.d networking remove >/dev/null fi diff --git a/docs/examples/interfaces b/docs/examples/interfaces index 35a7a4e..5f41e0c 100644 --- a/docs/examples/interfaces +++ b/docs/examples/interfaces @@ -11,6 +11,19 @@ iface eth0 inet dhcp #source /etc/network/interfaces.d/template.bridges +# swp interface +auto swp30 +iface swp30 + address 12.0.0.4/24 + address 12.0.0.6/24 + address 2000:1000:1000:1000:3::5/128 + mtu 1600 + alias "test network" + duplex full + autoneg on + speed 1000 + +# bond interface auto bond3 iface bond3 inet static address 100.0.0.4/16 @@ -22,6 +35,7 @@ iface bond3 inet static bond-min-links 1 bond-xmit_hash_policy layer3+4 +# bond interface auto bond4 iface bond4 inet static address 100.0.0.6/16 @@ -33,14 +47,16 @@ iface bond4 inet static bond-min-links 1 bond-xmit_hash_policy layer3+4 +# bond interface auto br0 iface br0 address 12.0.0.4/24 address 12.0.0.6/24 - address6 2000:1000:1000:1000:3::5/128 - mstpctl_ports bond3 bond4 swp5 swp8 - mstpctl_stp on + address 2000:1000:1000:1000:3::5/128 + bridge-ports bond3 bond4 swp5 swp8 + bridge-stp on +# vlan interface on bond auto bond3.2000 iface bond3.2000 inet static address 100.1.0.4/16 @@ -51,7 +67,8 @@ iface bond4.2000 inet static auto br2000 iface br2000 inet6 static - address6 2001:dad:beef::4/64 - mstpctl_ports bond3.2000 bond4.2000 swp5.2000 - mstpctl_treeprio 61440 - mstpctl_stp on + address 2001:dad:beef::4/64 + bridge-ports bond3.2000 bond4.2000 swp5.2000 + bridge-stp on + mstpctl-treeprio 61440 + mstpctl-portp2p bond3.2000=yes bond4.2000=yes diff --git a/man.rst/ifquery.8.rst b/man.rst/ifquery.8.rst index 8a09838..8072702 100644 --- a/man.rst/ifquery.8.rst +++ b/man.rst/ifquery.8.rst @@ -6,16 +6,12 @@ ifquery query network interface configuration ------------------------------------- -:Author: roopa@cumulusnetworks.com +:Author: Roopa Prabhu :Date: 2014-02-05 :Copyright: Copyright 2014 Cumulus Networks, Inc. All rights reserved. :Version: 0.1 :Manual section: 8 -NAME -==== - **ifquery** - query interface configuration - SYNOPSIS ======== @@ -36,7 +32,8 @@ DESCRIPTION in **/etc/network/interfaces** file. **ifquery** always works on the current **interfaces(5)** file - **/etc/network/interfaces**. + **/etc/network/interfaces** unless an alternate interfaces file is + provided with the **-i** option. OPTIONS ======= @@ -65,13 +62,24 @@ OPTIONS Exclude interfaces from the list of interfaces to operate on. Can be specified multiple times + -i INTERFACESFILE, --interfaces INTERFACESFILE + Use interfaces file instead of default + /etc/network/interfaces + -r, --running print raw interfaces file entries -c, --check check interface file contents against running state of an interface. Returns exit code 0 on success and 1 on error - -p, --print-dependency {list,dot} print iface dependency in list or dot format + -x, --raw print raw config file entries + + + -o {native,json}, --format {native,json} + interface display format + + -p, --print-dependency {list,dot} + print iface dependency in list or dot format -s, --syntax-help print supported interface config syntax. Scans all addon modules and dumps supported syntax from them diff --git a/man.rst/ifup.8.rst b/man.rst/ifup.8.rst index 1e96501..72831c5 100644 --- a/man.rst/ifup.8.rst +++ b/man.rst/ifup.8.rst @@ -6,7 +6,7 @@ ifup network interface management commands ------------------------------------- -:Author: roopa@cumulusnetworks.com +:Author: Roopa Prabhu :Date: 2014-02-05 :Copyright: Copyright 2014 Cumulus Networks, Inc. All rights reserved. :Version: 0.1 @@ -85,10 +85,19 @@ OPTIONS -n, --no-act print out what would happen,but don't do it - -p, --print-dependency {list,dot} print iface dependency in list or dot format. + -p, --print-dependency {list,dot} + print iface dependency in list or dot format - --no-scripts, --no-addons dont run any addon modules/scripts. Only bring - the interface administratively up/down + -m, --admin-state, --no-scripts + dont run any addon modules/scripts. Only bring + the interface administratively up/down + + -u, --use-current-config + By default ifdown looks at the saved state for + interfaces to bring down. This option allows ifdown + to look at the current interfaces file. Useful when + your state file is corrupted or you want down to use + the latest from the interfaces file EXAMPLES ======== diff --git a/sbin/ifupdown b/sbin/ifupdown index b49f283..d6aaa0f 100755 --- a/sbin/ifupdown +++ b/sbin/ifupdown @@ -1,5 +1,4 @@ #!/usr/bin/python -# PYTHON_ARGCOMPLETE_OK import sys import os @@ -231,7 +230,7 @@ def update_ifup_argparser(argparser): def update_ifdown_argparser(argparser): update_ifupdown_argparser(argparser) - argparser.add_argument('--use-current-config', + argparser.add_argument('-u', '--use-current-config', dest='usecurrentconfig', action='store_true', help='By default ifdown looks at the saved state for ' + 'interfaces to bring down. This option allows ifdown to ' + @@ -253,12 +252,12 @@ def update_ifquery_argparser(argparser): action='store_true', help='check interface file contents against ' + 'running state of an interface') - group.add_argument('--raw', action='store_true', dest='raw', + group.add_argument(-x', '--raw', action='store_true', dest='raw', help='print raw config file entries') group.add_argument('--print-savedstate', action='store_true', dest='printsavedstate', help=argparse.SUPPRESS) - argparser.add_argument('-m', '--format', dest='format', default='native', + argparser.add_argument('-o', '--format', dest='format', default='native', choices=['native', 'json'], help='interface display format') argparser.add_argument('-p', '--print-dependency', @@ -296,7 +295,7 @@ def update_ifreload_argparser(argparser): # default='/etc/network/interfaces', # help='use interfaces file instead of default ' + # '/etc/network/interfaces') - argparser.add_argument('--use-current-config', + argparser.add_argument('-u', '--use-current-config', dest='usecurrentconfig', action='store_true', help='By default ifreload looks at saved state for ' + 'interfaces to bring down. With this option ifreload will' @@ -378,6 +377,15 @@ def main(argv): args = parse_args(argv[1:], op) if not validate_args(op, args): exit(1) + + if not sys.argv[0].endswith('ifquery') and not os.geteuid() == 0: + print 'error: must be root to run this command' + exit(1) + + if not sys.argv[0].endswith('ifquery') and not utils.lockFile(lockfile): + print 'Another instance of this program is already running.' + exit(0) + read_config() init(args) handlers.get(op)(args) @@ -399,13 +407,6 @@ def main(argv): if __name__ == "__main__": - if not sys.argv[0].endswith('ifquery') and not os.geteuid() == 0: - print 'error: must be root to run this command' - exit(1) - - if not utils.lockFile(lockfile): - print 'Another instance of this program is already running.' - exit(0) # required during boot os.putenv('PATH', ENVPATH) diff --git a/setup.py b/setup.py index ca04742..b83fbe5 100755 --- a/setup.py +++ b/setup.py @@ -24,5 +24,6 @@ setup(name='ifupdown2', ('/usr/share/doc/python-ifupdown2/examples/', ['docs/examples/interfaces', 'docs/examples/interfaces_bridge_template_func', - 'docs/examples/interfaces_with_template'])] + 'docs/examples/interfaces_with_template']), + ('/etc/bash_completion.d/', ['completion/ifup'])] )