Skip to content

Commit

Permalink
Fix chained-comparison and dangerous-default-value pylint warnings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pgombar committed Nov 10, 2020
1 parent 9187165 commit ae20f71
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 238 deletions.
4 changes: 3 additions & 1 deletion azurelinuxagent/agent.py
Expand Up @@ -191,11 +191,13 @@ def collect_logs(self, is_full_mode):
sys.exit(1)


def main(args=[]): # pylint: disable=R0912,W0102
def main(args=None): # pylint: disable=R0912
"""
Parse command line arguments, exit with usage() on error.
Invoke different methods according to different command
"""
if args is None:
args = []
if len(args) <= 0: # pylint: disable=len-as-condition
args = sys.argv[1:]
command, force, verbose, debug, conf_file_path, log_collector_full_mode = parse_args(args)
Expand Down
255 changes: 136 additions & 119 deletions azurelinuxagent/common/osutil/default.py

Large diffs are not rendered by default.

102 changes: 51 additions & 51 deletions azurelinuxagent/common/rdma.py
Expand Up @@ -29,7 +29,7 @@
import azurelinuxagent.common.utils.shellutil as shellutil
from azurelinuxagent.common.utils.textutil import parse_doc, find, getattrib

dapl_config_paths = [ # pylint: disable=invalid-name
dapl_config_paths = [ # pylint: disable=invalid-name
'/etc/dat.conf',
'/etc/rdma/dat.conf',
'/usr/local/etc/dat.conf'
Expand Down Expand Up @@ -71,16 +71,16 @@ def setup_rdma_device(nd_version, shared_conf):
logger.info("RDMA: device is set up")
return

class RDMAHandler(object):

class RDMAHandler(object):
driver_module_name = 'hv_network_direct'
nd_version = None

def get_rdma_version(self): # pylint: disable=R1710
def get_rdma_version(self): # pylint: disable=R1710
"""Retrieve the firmware version information from the system.
This depends on information provided by the Linux kernel."""

if self.nd_version :
if self.nd_version:
return self.nd_version

kvp_key_size = 512
Expand All @@ -94,18 +94,18 @@ def get_rdma_version(self): # pylint: disable=R1710
logger.error(error_msg % driver_info_source)
return

f = open(driver_info_source) # pylint: disable=C0103
while True :
f = open(driver_info_source) # pylint: disable=C0103
while True:
key = f.read(kvp_key_size)
value = f.read(kvp_value_size)
if key and value :
if key and value:
key_0 = key.split("\x00")[0]
value_0 = value.split("\x00")[0]
if key_0 == "NdDriverVersion" :
if key_0 == "NdDriverVersion":
f.close()
self.nd_version = value_0
return self.nd_version
else :
else:
break
f.close()

Expand All @@ -121,18 +121,17 @@ def is_kvp_daemon_running():
# for suse, it uses hv_kvp_daemon
kvp_daemon_names = ['hypervkvpd', 'hv_kvp_daemon']

exitcode, ps_out = shellutil.run_get_output("ps -ef")
exitcode, ps_out = shellutil.run_get_output("ps -ef")
if exitcode != 0:
raise Exception('RDMA: ps -ef failed: %s' % ps_out)
for n in kvp_daemon_names: # pylint: disable=C0103
if n in ps_out: # pylint: disable=R1705
for n in kvp_daemon_names: # pylint: disable=C0103
if n in ps_out: # pylint: disable=R1705
logger.info('RDMA: kvp daemon (%s) is running' % n)
return True
else:
logger.verbose('RDMA: kvp daemon (%s) is not running' % n)
return False


def load_driver_module(self):
"""Load the kernel driver, this depends on the proper driver
to be installed with the install_driver() method"""
Expand Down Expand Up @@ -165,7 +164,7 @@ def install_driver(self):
def is_driver_loaded(self):
"""Check if the network module is loaded in kernel space"""
cmd = 'lsmod | grep ^%s' % self.driver_module_name
status, loaded_modules = shellutil.run_get_output(cmd) # pylint: disable=W0612
status, loaded_modules = shellutil.run_get_output(cmd) # pylint: disable=W0612
logger.info('RDMA: Checking if the module loaded.')
if loaded_modules:
logger.info('RDMA: module loaded.')
Expand All @@ -182,11 +181,11 @@ def reboot_system(self):
logger.error('RDMA: Failed to reboot the system')


dapl_config_paths = [ # pylint: disable=invalid-name
dapl_config_paths = [ # pylint: disable=invalid-name
'/etc/dat.conf', '/etc/rdma/dat.conf', '/usr/local/etc/dat.conf']

class RDMADeviceHandler(object):

class RDMADeviceHandler(object):
"""
Responsible for writing RDMA IP and MAC address to the /dev/hvnd_rdma
interface.
Expand Down Expand Up @@ -215,16 +214,16 @@ def start(self):

def process(self):
try:
if not self.nd_version :
if not self.nd_version:
logger.info("RDMA: provisioning SRIOV RDMA device.")
self.provision_sriov_rdma()
else :
else:
logger.info("RDMA: provisioning Network Direct RDMA device.")
self.provision_network_direct_rdma()
except Exception as e: # pylint: disable=C0103
except Exception as e: # pylint: disable=C0103
logger.error("RDMA: device processing failed: {0}".format(e))

def provision_network_direct_rdma(self) :
def provision_network_direct_rdma(self):
RDMADeviceHandler.update_dat_conf(dapl_config_paths, self.ipv4_addr)

if not conf.enable_check_rdma_driver():
Expand All @@ -234,22 +233,22 @@ def provision_network_direct_rdma(self) :

skip_rdma_device = False
module_name = "hv_network_direct"
retcode,out = shellutil.run_get_output("modprobe -R %s" % module_name, chk_err=False)
retcode, out = shellutil.run_get_output("modprobe -R %s" % module_name, chk_err=False)
if retcode == 0:
module_name = out.strip()
else:
logger.info("RDMA: failed to resolve module name. Use original name")
retcode,out = shellutil.run_get_output("modprobe %s" % module_name)
retcode, out = shellutil.run_get_output("modprobe %s" % module_name)
if retcode != 0:
logger.error("RDMA: failed to load module %s" % module_name)
return
retcode,out = shellutil.run_get_output("modinfo %s" % module_name)
retcode, out = shellutil.run_get_output("modinfo %s" % module_name)
if retcode == 0:
version = re.search("version:\s+(\d+)\.(\d+)\.(\d+)\D", out, re.IGNORECASE) # pylint: disable=W1401
version = re.search("version:\s+(\d+)\.(\d+)\.(\d+)\D", out, re.IGNORECASE) # pylint: disable=W1401
if version:
v1 = int(version.groups(0)[0]) # pylint: disable=C0103
v2 = int(version.groups(0)[1]) # pylint: disable=C0103
if v1>4 or v1==4 and v2>0:
v1 = int(version.groups(0)[0]) # pylint: disable=C0103
v2 = int(version.groups(0)[1]) # pylint: disable=C0103
if v1 > 4 or v1 == 4 and v2 > 0:
logger.info("Skip setting /dev/hvnd_rdma on 4.1 or later")
skip_rdma_device = True
else:
Expand All @@ -265,17 +264,18 @@ def provision_network_direct_rdma(self) :

RDMADeviceHandler.update_network_interface(self.mac_addr, self.ipv4_addr)

def provision_sriov_rdma(self) : # pylint: disable=R1711
def provision_sriov_rdma(self): # pylint: disable=R1711
RDMADeviceHandler.wait_any_rdma_device(
self.sriov_dir, self.device_check_timeout_sec, self.device_check_interval_sec)
RDMADeviceHandler.update_iboip_interface(self.ipv4_addr, self.ipoib_check_timeout_sec, self.ipoib_check_interval_sec)
RDMADeviceHandler.update_iboip_interface(self.ipv4_addr, self.ipoib_check_timeout_sec,
self.ipoib_check_interval_sec)
return

@staticmethod
def update_iboip_interface(ipv4_addr, timeout_sec, check_interval_sec) :
def update_iboip_interface(ipv4_addr, timeout_sec, check_interval_sec):
logger.info("Wait for ib0 become available")
total_retries = timeout_sec/check_interval_sec
n = 0 # pylint: disable=C0103
total_retries = timeout_sec / check_interval_sec
n = 0 # pylint: disable=C0103
found_ib0 = None
while not found_ib0 and n < total_retries:
ret, output = shellutil.run_get_output("ifconfig -a")
Expand All @@ -285,7 +285,7 @@ def update_iboip_interface(ipv4_addr, timeout_sec, check_interval_sec) :
if found_ib0:
break
time.sleep(check_interval_sec)
n += 1 # pylint: disable=C0103
n += 1 # pylint: disable=C0103

if not found_ib0:
raise Exception("ib0 is not available")
Expand All @@ -304,7 +304,7 @@ def update_dat_conf(paths, ipv4_addr):
infiniband interface.
"""
logger.info("Updating DAPL configuration file")
for f in paths: # pylint: disable=C0103
for f in paths: # pylint: disable=C0103
logger.info("RDMA: trying {0}".format(f))
if not os.path.isfile(f):
logger.info(
Expand All @@ -322,7 +322,7 @@ def update_dat_conf(paths, ipv4_addr):

@staticmethod
def replace_dat_conf_contents(cfg, ipv4_addr):
old = "ofa-v2-ib0 u2.0 nonthreadsafe default libdaplofa.so.2 dapl.2.0 \"\S+ 0\"" # pylint: disable=W1401
old = "ofa-v2-ib0 u2.0 nonthreadsafe default libdaplofa.so.2 dapl.2.0 \"\S+ 0\"" # pylint: disable=W1401
new = "ofa-v2-ib0 u2.0 nonthreadsafe default libdaplofa.so.2 dapl.2.0 \"{0} 0\"".format(
ipv4_addr)
return re.sub(old, new, cfg)
Expand All @@ -332,7 +332,7 @@ def write_rdma_config_to_device(path, ipv4_addr, mac_addr):
data = RDMADeviceHandler.generate_rdma_config(ipv4_addr, mac_addr)
logger.info(
"RDMA: Updating device with configuration: {0}".format(data))
with open(path, "w") as f: # pylint: disable=C0103
with open(path, "w") as f: # pylint: disable=C0103
logger.info("RDMA: Device opened for writing")
f.write(data)
logger.info("RDMA: Updated device with IPv4/MAC addr successfully")
Expand All @@ -344,47 +344,47 @@ def generate_rdma_config(ipv4_addr, mac_addr):
@staticmethod
def wait_rdma_device(path, timeout_sec, check_interval_sec):
logger.info("RDMA: waiting for device={0} timeout={1}s".format(path, timeout_sec))
total_retries = timeout_sec/check_interval_sec
n = 0 # pylint: disable=C0103
total_retries = timeout_sec / check_interval_sec
n = 0 # pylint: disable=C0103
while n < total_retries:
if os.path.exists(path):
logger.info("RDMA: device ready")
return
logger.verbose(
"RDMA: device not ready, sleep {0}s".format(check_interval_sec))
time.sleep(check_interval_sec)
n += 1 # pylint: disable=C0103
n += 1 # pylint: disable=C0103
logger.error("RDMA device wait timed out")
raise Exception("The device did not show up in {0} seconds ({1} retries)".format(
timeout_sec, total_retries))

@staticmethod
def wait_any_rdma_device(dir, timeout_sec, check_interval_sec): # pylint: disable=W0622
def wait_any_rdma_device(directory, timeout_sec, check_interval_sec):
logger.info(
"RDMA: waiting for any Infiniband device at directory={0} timeout={1}s".format(
dir, timeout_sec))
total_retries = timeout_sec/check_interval_sec
n = 0 # pylint: disable=C0103
directory, timeout_sec))
total_retries = timeout_sec / check_interval_sec
n = 0 # pylint: disable=C0103
while n < total_retries:
r = os.listdir(dir) # pylint: disable=C0103
r = os.listdir(directory) # pylint: disable=C0103
if r:
logger.info("RDMA: device found in {0}".format(dir))
logger.info("RDMA: device found in {0}".format(directory))
return
logger.verbose(
"RDMA: device not ready, sleep {0}s".format(check_interval_sec))
time.sleep(check_interval_sec)
n += 1 # pylint: disable=C0103
n += 1 # pylint: disable=C0103
logger.error("RDMA device wait timed out")
raise Exception("The device did not show up in {0} seconds ({1} retries)".format(
timeout_sec, total_retries))

@staticmethod
def update_network_interface(mac_addr, ipv4_addr):
netmask=16
netmask = 16

logger.info("RDMA: will update the network interface with IPv4/MAC")

if_name=RDMADeviceHandler.get_interface_by_mac(mac_addr)
if_name = RDMADeviceHandler.get_interface_by_mac(mac_addr)
logger.info("RDMA: network interface found: {0}", if_name)
logger.info("RDMA: bringing network interface up")
if shellutil.run("ifconfig {0} up".format(if_name)) != 0:
Expand All @@ -402,12 +402,12 @@ def get_interface_by_mac(mac):
if ret != 0:
raise Exception("Failed to list network interfaces")
output = output.replace('\n', '')
match = re.search(r"(eth\d).*(HWaddr|ether) {0}".format(mac),
match = re.search(r"(eth\d).*(HWaddr|ether) {0}".format(mac),
output, re.IGNORECASE)
if match is None:
raise Exception("Failed to get ifname with mac: {0}".format(mac))
output = match.group(0)
eths = re.findall(r"eth\d", output)
if eths is None or len(eths) == 0: # pylint: disable=len-as-condition
if eths is None or len(eths) == 0: # pylint: disable=len-as-condition
raise Exception("ifname with mac: {0} not found".format(mac))
return eths[-1]
4 changes: 3 additions & 1 deletion azurelinuxagent/common/utils/fileutil.py
Expand Up @@ -224,11 +224,13 @@ def get_all_files(root_path):
return result


def clean_ioerror(e, paths=[]): # pylint: disable=W0102,C0103
def clean_ioerror(e, paths=None): # pylint: disable=C0103
"""
Clean-up possibly bad files and directories after an IO error.
The code ignores *all* errors since disk state may be unhealthy.
"""
if paths is None:
paths = []
if isinstance(e, IOError) and e.errno in KNOWN_IOERRORS:
for path in paths:
if path is None:
Expand Down

0 comments on commit ae20f71

Please sign in to comment.