From 64ab3554a14afa48bb33839631ed4f8e71945874 Mon Sep 17 00:00:00 2001 From: Rohit Yadav Date: Tue, 21 Apr 2015 17:33:18 +0200 Subject: [PATCH] scripts: filter output instead of popping string from list This is a defensive enhancement for KVM SG script that filters out empty string instead of popping last item which may or may not be an empty string. Signed-off-by: Rohit Yadav --- scripts/vm/network/security_group.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/scripts/vm/network/security_group.py b/scripts/vm/network/security_group.py index b4a355d9ecc..5e022d8928d 100755 --- a/scripts/vm/network/security_group.py +++ b/scripts/vm/network/security_group.py @@ -234,16 +234,14 @@ def destroy_ebtables_rules(vm_name, vif): delcmd = "ebtables -t nat -L PREROUTING | grep " + vm_name delcmds = [] try: - delcmds = execute(delcmd).split('\n') - delcmds.pop() + delcmds = filter(None, execute(delcmd).split('\n')) delcmds = ["-D PREROUTING " + x for x in delcmds ] except: pass postcmds = [] try: postcmd = "ebtables -t nat -L POSTROUTING | grep " + vm_name - postcmds = execute(postcmd).split('\n') - postcmds.pop() + postcmds = filter(None, execute(postcmd).split('\n')) postcmds = ["-D POSTROUTING " + x for x in postcmds] except: pass @@ -544,13 +542,12 @@ def post_default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname, dhcpS def delete_rules_for_vm_in_bridge_firewall_chain(vmName): vm_name = vmName if vm_name.startswith('i-') or vm_name.startswith('r-'): - vm_name = '-'.join(vm_name.split('-')[:-1]) + "-def" + vm_name = '-'.join(vm_name.split('-')[:-1]) + "-def" vmchain = vm_name delcmd = """iptables-save | awk '/BF(.*)physdev-is-bridged(.*)%s/ { sub(/-A/, "-D", $1) ; print }'""" % vmchain - delcmds = execute(delcmd).split('\n') - delcmds.pop() + delcmds = filter(None, execute(delcmd).split('\n')) for cmd in delcmds: try: execute("iptables " + cmd) @@ -647,8 +644,7 @@ def network_rules_for_rebooted_vm(vmName): ipts = [] for cmd in [delcmd, inscmd]: logging.debug(cmd) - cmds = execute(cmd).split('\n') - cmds.pop() + cmds = filter(None, execute(cmd).split('\n')) for c in cmds: ipt = "iptables " + c ipts.append(ipt)