From e28beaec7a25f7de7289a4eb93c2dbe48eb368f5 Mon Sep 17 00:00:00 2001 From: hlange4 <52819910+hlange4@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:39:31 +0200 Subject: [PATCH 1/2] Update bms-network-setup.py - removed duplicate statement Removed duplicate code from the man himself :P --- bms-network-setup.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bms-network-setup.py b/bms-network-setup.py index 22cef70..b74567a 100755 --- a/bms-network-setup.py +++ b/bms-network-setup.py @@ -545,8 +545,6 @@ def process_template(template, ljson, njson, sjson, note = True): out += "%s" % nameservers(sjson) elif mode == VLANNAME: out += SFMT % (key, vlanname(ljson)) - elif mode == VLANNAME: - out += SFMT % (key, vlanname(ljson)) elif mode == DEBNMMODE: out += debiface(ljson, net, sjson) elif mode == NETPLMODE: From fbc0f3028252b769db05f33ff288bbd730e9b1e4 Mon Sep 17 00:00:00 2001 From: hlange4 <52819910+hlange4@users.noreply.github.com> Date: Wed, 18 Oct 2023 13:07:53 +0200 Subject: [PATCH 2/2] Update bms-network-setup.py - some more refactoring Fixed some Exception stylings - it would be best to define custom Exceptions. added ```startswith``` and ```endswith``` for better readability removed several unused parameters in functions: Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same. rewrote the code snippet that checks if jval is not None to a more concise way to express this Defined a constant instead of duplicating this literal ".yaml" 3 times. --- bms-network-setup.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/bms-network-setup.py b/bms-network-setup.py index b74567a..a015c38 100755 --- a/bms-network-setup.py +++ b/bms-network-setup.py @@ -389,7 +389,7 @@ def bondslavex(btpl): def bondnm(iface): "derive bondN name from interfaceN string" - if iface[:9] == "interface": + if iface.startswith("interface"): return "bond%s" % iface[9:] else: return iface @@ -425,7 +425,7 @@ def bonddhcp(njson, sjson): "BOOTPROTO=dhcp or static config from network settings" global FIRST #six.print_(njson) - if njson["type"][-4:] == "dhcp": + if njson["type"].endswith("dhcp"): if IS_SUSE and "gateway" in njson and FIRST: FIRST = False return "BOOTPROTO=dhcp\nDHCLIENT_PRIMARY_DEVICE=yes\n" @@ -466,7 +466,7 @@ def debiface(ljson, njson, sjson): return "auto %s\niface %s inet static\n%s" % \ (nm, nm, process_template(IFCFG_STAT, njson, njson, sjson, False)) -def netpliface(ljson, njson, sjson): +def netpliface(ljson): "generate interface yaml layout, incl. static network config if needed" np_bond_slaves="" for npslave in bond_slaves: @@ -491,7 +491,7 @@ def process_template(template, ljson, njson, sjson, note = True): # Fill default name try: nm = ljson["name"] - except: + except Exception: pass # Find network of interface net = None @@ -501,7 +501,7 @@ def process_template(template, ljson, njson, sjson, note = True): if nets["link"] == link: net = nets break - except: + except Exception: pass #six.print_("Device ID %s: network %s" % (link, net)) @@ -511,11 +511,9 @@ def process_template(template, ljson, njson, sjson, note = True): elif mode == OPT or mode == MAND: try: jval = ljson[val] - if jval == None: - pass - else: - out += SFMT % (key, jval) - except: + if jval is not None: + out += SFMT % (key, jval) + except Exception: if mode == MAND: LOG.error("Mandatory value %s not found for %s setting" % (val, key)) elif mode == MAYBEDHCP: @@ -548,7 +546,7 @@ def process_template(template, ljson, njson, sjson, note = True): elif mode == DEBNMMODE: out += debiface(ljson, net, sjson) elif mode == NETPLMODE: - out += netpliface(ljson, net, sjson) + out += netpliface(ljson) elif mode == BONDSLAVES: out += "\t%s %s\n\tbond-primary %s\n" % (key, splist(ljson["bond_links"]), ljson["bond_links"][0]) @@ -614,7 +612,7 @@ def rename_if(old, new): try: out = subprocess.check_output(cmd1.split(" "), stderr=subprocess.STDOUT) out = subprocess.check_output(cmd2.split(" "), stderr=subprocess.STDOUT) - except: + except Exception: six.print_("FAIL: %s" % out, file=sys.stderr) @@ -638,7 +636,7 @@ def find_name(mac, retry = 1): portid = open("/sys/class/net/%s/phys_port_id" % dev, "r").read().rstrip() if shortmac == portid: return dev - except: + except Exception: pass if ctr < retry: ctr += 1 @@ -694,7 +692,7 @@ def ifdown(ifnm): try: six.print_(cmd, file=sys.stderr) out = subprocess.check_output(cmd.split(" "), stderr=subprocess.STDOUT) - except: + except Exception: six.print_("FAIL: %s" % out, file=sys.stderr) @@ -728,7 +726,7 @@ def clean_miss_ifaces(njson): six.print_("Info: Remove %s" % cand, file=sys.stderr) os.unlink(cand) -def clean_netplan_ifaces(njson): +def clean_netplan_ifaces(): ci_bond0_file = "/etc/netplan/50-cloud-init.yaml" if os.path.exists(ci_bond0_file): os.remove(ci_bond0_file) @@ -743,7 +741,7 @@ def process_network_hw(): if ret: try: os.unlink("%s/is_bms" % CONFDIR) - except: + except Exception: pass six.print_("Not running on BMS, exiting", file=sys.stderr) sys.exit(0) @@ -764,26 +762,27 @@ def process_network_hw(): try: sjson = network_json["services"] njson = network_json["networks"] - except: + except Exception: pass open("%s/is_bms" % CONFDIR, "w") #six.print_(network_json) - clean_netplan_ifaces(njson) + clean_netplan_ifaces() for ljson in network_json["links"]: tp = ljson["type"] nm = ifname(ljson) + dot_yaml = ".yaml" if tp == "phy": IFCFG_TMPL = IFCFG_PHY PRE = "60-" if IS_DEB else "" - POST = ".yaml" if IS_NETPLAN else "" + POST = dot_yaml if IS_NETPLAN else "" elif tp == "bond": IFCFG_TMPL = IFCFG_BOND PRE = "61-" if IS_DEB else "" - POST = ".yaml" if IS_NETPLAN else "" + POST = dot_yaml if IS_NETPLAN else "" elif tp == "vlan": IFCFG_TMPL = IFCFG_VLAN PRE = "62-" if IS_DEB else "" - POST = ".yaml" if IS_NETPLAN else "" + POST = dot_yaml if IS_NETPLAN else "" else: six.print_("Unknown network type %s" % tp, file=sys.stderr)