()
| 920 | |
| 921 | |
| 922 | def cleanup_rules(): |
| 923 | try: |
| 924 | states = ['running', 'paused'] |
| 925 | vmsInHost = virshlist(states) |
| 926 | |
| 927 | logging.debug(" Vms on the host : %s ", vmsInHost) |
| 928 | |
| 929 | cleanup = [] |
| 930 | chainscmd = """iptables-save | grep -P '^:(?!.*-(def|eg))' | awk '{sub(/^:/, "", $1) ; print $1}' | sort | uniq""" |
| 931 | chains = execute(chainscmd).split('\n') |
| 932 | |
| 933 | logging.debug(" iptables chains in the host :%s ", chains) |
| 934 | |
| 935 | for chain in chains: |
| 936 | if 1 in [ chain.startswith(c) for c in ['r-', 'i-', 's-', 'v-'] ]: |
| 937 | vm_name = chain |
| 938 | vmpresent = False |
| 939 | |
| 940 | for vm in vmsInHost: |
| 941 | if vm_name in vm: |
| 942 | vmpresent = True |
| 943 | break |
| 944 | |
| 945 | if vmpresent is False: |
| 946 | logging.debug("vm " + vm_name + " is not running or paused, cleaning up iptables rules") |
| 947 | cleanup.append(vm_name) |
| 948 | |
| 949 | bridge_tables = execute("""grep -E '^ebtable_' /proc/modules | cut -f1 -d' ' | sed s/ebtable_//""").split('\n') |
| 950 | for table in [_f for _f in bridge_tables if _f]: |
| 951 | chainscmd = """ebtables -t %s -L | awk '/chain:/ { gsub(/(^.*chain: |-(in|out|ips).*)/, ""); print $1}' | sort | uniq""" % table |
| 952 | chains = execute(chainscmd).split('\n') |
| 953 | |
| 954 | logging.debug(" ebtables chains in the host: %s ", chains) |
| 955 | |
| 956 | for chain in [_f for _f in chains if _f]: |
| 957 | if 1 in [chain.startswith(c) for c in ['r-', 'i-', 's-', 'v-']]: |
| 958 | vm_name = chain |
| 959 | vmpresent = False |
| 960 | for vm in vmsInHost: |
| 961 | if vm_name in vm: |
| 962 | vmpresent = True |
| 963 | break |
| 964 | |
| 965 | if vmpresent is False: |
| 966 | logging.debug("vm " + vm_name + " is not running or paused, cleaning up ebtables rules") |
| 967 | cleanup.append(vm_name) |
| 968 | |
| 969 | cleanup = list(set(cleanup)) # remove duplicates |
| 970 | for vmname in cleanup: |
| 971 | destroy_network_rules_for_vm(vmname) |
| 972 | |
| 973 | logging.debug("Cleaned up rules for " + str(len(cleanup)) + " chains") |
| 974 | except: |
| 975 | logging.debug("Failed to cleanup rules !") |
| 976 | |
| 977 | |
| 978 | def check_rule_log_for_vm(vmName, vmId, vmIP, domID, signature, seqno): |
no test coverage detected