diff --git a/octavia/amphorae/backends/agent/agent_jinja_cfg.py b/octavia/amphorae/backends/agent/agent_jinja_cfg.py index 210c5f5c15..04cb83565b 100644 --- a/octavia/amphorae/backends/agent/agent_jinja_cfg.py +++ b/octavia/amphorae/backends/agent/agent_jinja_cfg.py @@ -25,7 +25,7 @@ TEMPLATES_DIR = (os.path.dirname(os.path.realpath(__file__)) + constants.AGENT_API_TEMPLATES + '/') -class AgentJinjaTemplater(object): +class AgentJinjaTemplater: def __init__(self): template_loader = jinja2.FileSystemLoader(searchpath=os.path.dirname( diff --git a/octavia/amphorae/backends/agent/api_server/amphora_info.py b/octavia/amphorae/backends/agent/api_server/amphora_info.py index 34381bd34b..27ed98d23c 100644 --- a/octavia/amphorae/backends/agent/api_server/amphora_info.py +++ b/octavia/amphorae/backends/agent/api_server/amphora_info.py @@ -30,7 +30,7 @@ from octavia.common import exceptions LOG = logging.getLogger(__name__) -class AmphoraInfo(object): +class AmphoraInfo: def __init__(self, osutils): self._osutils = osutils @@ -135,7 +135,7 @@ class AmphoraInfo(object): def _get_meminfo(self): re_parser = re.compile(r'^(?P\S*):\s*(?P\d*)\s*kB') result = {} - with open('/proc/meminfo', 'r', encoding='utf-8') as meminfo: + with open('/proc/meminfo', encoding='utf-8') as meminfo: for line in meminfo: match = re_parser.match(line) if not match: @@ -199,7 +199,7 @@ class AmphoraInfo(object): def _get_active_tuned_profiles(self) -> str: """Returns the active TuneD profile(s)""" try: - with open("/etc/tuned/active_profile", "r", encoding="utf-8") as f: + with open("/etc/tuned/active_profile", encoding="utf-8") as f: return f.read(1024).strip() except OSError as ex: LOG.debug("Reading active TuneD profiles failed: %r", ex) diff --git a/octavia/amphorae/backends/agent/api_server/keepalived.py b/octavia/amphorae/backends/agent/api_server/keepalived.py index 3ed61e22a0..7ae2aae8e8 100644 --- a/octavia/amphorae/backends/agent/api_server/keepalived.py +++ b/octavia/amphorae/backends/agent/api_server/keepalived.py @@ -40,7 +40,7 @@ SYSTEMD_TEMPLATE = j2_env.get_template(consts.KEEPALIVED_JINJA2_SYSTEMD) check_script_template = j2_env.get_template(consts.CHECK_SCRIPT_CONF) -class Keepalived(object): +class Keepalived: def upload_keepalived_config(self): stream = loadbalancer.Wrapped(flask.request.stream) @@ -76,7 +76,7 @@ class Keepalived(object): template = UPSTART_TEMPLATE elif init_system == consts.INIT_SYSVINIT: template = SYSVINIT_TEMPLATE - init_enable_cmd = "insserv {file}".format(file=file_path) + init_enable_cmd = f"insserv {file_path}" else: raise util.UnknownInitError() @@ -140,25 +140,23 @@ class Keepalived(object): consts.AMP_ACTION_RELOAD]: return webob.Response(json={ 'message': 'Invalid Request', - 'details': "Unknown action: {0}".format(action)}, status=400) + 'details': f"Unknown action: {action}"}, status=400) if action == consts.AMP_ACTION_START: keepalived_pid_path = util.keepalived_pid_path() try: # Is there a pid file for keepalived? - with open(keepalived_pid_path, - 'r', encoding='utf-8') as pid_file: + with open(keepalived_pid_path, encoding='utf-8') as pid_file: pid = int(pid_file.readline()) os.kill(pid, 0) # If we got here, it means the keepalived process is running. # We should reload it instead of trying to start it again. action = consts.AMP_ACTION_RELOAD - except (IOError, OSError): + except OSError: pass - cmd = ("/usr/sbin/service octavia-keepalived {action}".format( - action=action)) + cmd = f"/usr/sbin/service octavia-keepalived {action}" try: subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) @@ -166,11 +164,10 @@ class Keepalived(object): LOG.debug('Failed to %s octavia-keepalived service: %s %s', action, e, e.output) return webob.Response(json={ - 'message': "Failed to {0} octavia-keepalived service".format( - action), + 'message': f"Failed to {action} octavia-keepalived service", 'details': e.output}, status=500) return webob.Response( json={'message': 'OK', - 'details': 'keepalived {action}ed'.format(action=action)}, + 'details': f'keepalived {action}ed'}, status=202) diff --git a/octavia/amphorae/backends/agent/api_server/keepalivedlvs.py b/octavia/amphorae/backends/agent/api_server/keepalivedlvs.py index d5a619a321..531711051e 100644 --- a/octavia/amphorae/backends/agent/api_server/keepalivedlvs.py +++ b/octavia/amphorae/backends/agent/api_server/keepalivedlvs.py @@ -140,7 +140,7 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): util.run_systemctl_command( consts.ENABLE, "octavia-keepalivedlvs-%s" % str(listener_id)) elif init_system == consts.INIT_SYSVINIT: - init_enable_cmd = "insserv {file}".format(file=file_path) + init_enable_cmd = f"insserv {file_path}" try: subprocess.check_output(init_enable_cmd.split(), stderr=subprocess.STDOUT) @@ -180,8 +180,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): raise exceptions.HTTPException( response=webob.Response(json={ 'message': 'UDP Listener Not Found', - 'details': "No UDP listener with UUID: {0}".format( - listener_id)}, status=404)) + 'details': f"No UDP listener with UUID: {listener_id}"}, + status=404)) def get_lvs_listener_config(self, listener_id): """Gets the keepalivedlvs config @@ -190,7 +190,7 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): """ self._check_lvs_listener_exists(listener_id) with open(util.keepalived_lvs_cfg_path(listener_id), - 'r', encoding='utf-8') as file: + encoding='utf-8') as file: cfg = file.read() resp = webob.Response(cfg, content_type='text/plain') return resp @@ -202,7 +202,7 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): consts.AMP_ACTION_RELOAD]: return webob.Response(json={ 'message': 'Invalid Request', - 'details': "Unknown action: {0}".format(action)}, status=400) + 'details': f"Unknown action: {action}"}, status=400) # When octavia requests a reload of keepalived, force a restart since # a keepalived reload doesn't restore members in their initial state. @@ -217,24 +217,22 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): action = consts.AMP_ACTION_START cmd = ("/usr/sbin/service " - "octavia-keepalivedlvs-{listener_id} " - "{action}".format(listener_id=listener_id, action=action)) + f"octavia-keepalivedlvs-{listener_id} {action}") try: subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: LOG.debug('Failed to %s keepalivedlvs listener %s', - listener_id + ' : ' + action, str(e)) + listener_id + ' : ' + action, e) return webob.Response(json={ - 'message': ("Failed to {0} keepalivedlvs listener {1}" - .format(action, listener_id)), + 'message': (f"Failed to {action} keepalivedlvs listener " + f"{listener_id}"), 'details': e.output}, status=500) return webob.Response( json={'message': 'OK', - 'details': 'keepalivedlvs listener {listener_id} ' - '{action}ed'.format(listener_id=listener_id, - action=action)}, + 'details': (f'keepalivedlvs listener {listener_id} ' + f'{action}ed')}, status=202) def _check_lvs_listener_status(self, listener_id): @@ -243,7 +241,7 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): '/proc', util.get_keepalivedlvs_pid(listener_id))): # Check if the listener is disabled with open(util.keepalived_lvs_cfg_path(listener_id), - 'r', encoding='utf-8') as file: + encoding='utf-8') as file: cfg = file.read() m = re.search('virtual_server', cfg) if m: @@ -280,12 +278,12 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): if os.path.exists(keepalived_pid) and os.path.exists( os.path.join('/proc', util.get_keepalivedlvs_pid(listener_id))): - cmd = ("/usr/sbin/service " - "octavia-keepalivedlvs-{0} stop".format(listener_id)) + cmd = (f"/usr/sbin/service " + f"octavia-keepalivedlvs-{listener_id} stop") try: subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - LOG.error("Failed to stop keepalivedlvs service: %s", str(e)) + LOG.error("Failed to stop keepalivedlvs service: %s", e) return webob.Response(json={ 'message': "Error stopping keepalivedlvs", 'details': e.output}, status=500) @@ -305,9 +303,9 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): if init_system == consts.INIT_SYSTEMD: util.run_systemctl_command( - consts.DISABLE, "octavia-keepalivedlvs-%s" % str(listener_id)) + consts.DISABLE, f"octavia-keepalivedlvs-{listener_id}") elif init_system == consts.INIT_SYSVINIT: - init_disable_cmd = "insserv -r {file}".format(file=init_path) + init_disable_cmd = f"insserv -r {init_path}" elif init_system != consts.INIT_UPSTART: raise util.UnknownInitError() @@ -322,7 +320,7 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase): return webob.Response(json={ 'message': ( "Error disabling octavia-keepalivedlvs-" - "{0} service".format(listener_id)), + "{} service".format(listener_id)), 'details': e.output}, status=500) # delete init script ,config file and log file for that listener diff --git a/octavia/amphorae/backends/agent/api_server/loadbalancer.py b/octavia/amphorae/backends/agent/api_server/loadbalancer.py index 26d884ba54..2029fbcf98 100644 --- a/octavia/amphorae/backends/agent/api_server/loadbalancer.py +++ b/octavia/amphorae/backends/agent/api_server/loadbalancer.py @@ -53,7 +53,7 @@ SYSTEMD_TEMPLATE = JINJA_ENV.get_template(SYSTEMD_CONF) # Wrap a stream so we can compute the md5 while reading -class Wrapped(object): +class Wrapped: def __init__(self, stream_): self.stream = stream_ self.hash = md5(usedforsecurity=False) # nosec @@ -71,7 +71,7 @@ class Wrapped(object): return getattr(self.stream, attr) -class Loadbalancer(object): +class Loadbalancer: def get_haproxy_config(self, lb_id): """Gets the haproxy config @@ -79,7 +79,7 @@ class Loadbalancer(object): :param listener_id: the id of the listener """ self._check_lb_exists(lb_id) - with open(util.config_path(lb_id), 'r', encoding='utf-8') as file: + with open(util.config_path(lb_id), encoding='utf-8') as file: cfg = file.read() resp = webob.Response(cfg, content_type='text/plain') resp.headers['ETag'] = ( @@ -161,7 +161,7 @@ class Loadbalancer(object): template = UPSTART_TEMPLATE elif init_system == consts.INIT_SYSVINIT: template = SYSVINIT_TEMPLATE - init_enable_cmd = "insserv {file}".format(file=init_path) + init_enable_cmd = f"insserv {init_path}" else: raise util.UnknownInitError() @@ -206,7 +206,7 @@ class Loadbalancer(object): # Make sure the new service is enabled on boot if init_system == consts.INIT_SYSTEMD: util.run_systemctl_command( - consts.ENABLE, "haproxy-{lb_id}".format(lb_id=lb_id)) + consts.ENABLE, f"haproxy-{lb_id}") elif init_system == consts.INIT_SYSVINIT: try: subprocess.check_output(init_enable_cmd.split(), @@ -216,7 +216,7 @@ class Loadbalancer(object): "%(err)s %(out)s", {'lb_id': lb_id, 'err': e, 'out': e.output}) return webob.Response(json={ - 'message': "Error enabling haproxy-{0} service".format( + 'message': "Error enabling haproxy-{} service".format( lb_id), 'details': e.output}, status=500) res = webob.Response(json={'message': 'OK'}, status=202) @@ -231,7 +231,7 @@ class Loadbalancer(object): consts.AMP_ACTION_RELOAD]: return webob.Response(json={ 'message': 'Invalid Request', - 'details': "Unknown action: {0}".format(action)}, status=400) + 'details': f"Unknown action: {action}"}, status=400) self._check_lb_exists(lb_id) is_vrrp = (CONF.controller_worker.loadbalancer_topology == @@ -269,7 +269,7 @@ class Loadbalancer(object): "%(out)s", {'action': action, 'lb_id': lb_id, 'err': e, 'out': e.output}) return webob.Response(json={ - 'message': "Error {0}ing haproxy".format(action), + 'message': f"Error {action}ing haproxy", 'details': e.output}, status=500) # If we are not in active/standby we need to send an IP @@ -288,7 +288,7 @@ class Loadbalancer(object): details = ( 'Configuration file is valid\n' - 'haproxy daemon for {0} started'.format(lb_id) + 'haproxy daemon for {} started'.format(lb_id) ) return webob.Response(json={'message': 'OK', 'details': details}, @@ -303,7 +303,7 @@ class Loadbalancer(object): # check if that haproxy is still running and if stop it if os.path.exists(util.pid_path(lb_id)) and os.path.exists( os.path.join('/proc', util.get_haproxy_pid(lb_id))): - cmd = "/usr/sbin/service haproxy-{0} stop".format(lb_id) + cmd = f"/usr/sbin/service haproxy-{lb_id} stop" try: subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: @@ -342,7 +342,7 @@ class Loadbalancer(object): consts.DISABLE, "haproxy-{lb_id}".format( lb_id=lb_id)) elif init_system == consts.INIT_SYSVINIT: - init_disable_cmd = "insserv -r {file}".format(file=init_path) + init_disable_cmd = f"insserv -r {init_path}" elif init_system != consts.INIT_UPSTART: raise util.UnknownInitError() @@ -355,7 +355,7 @@ class Loadbalancer(object): "%(err)s %(out)s", {'lb_id': lb_id, 'err': e, 'out': e.output}) return webob.Response(json={ - 'message': "Error disabling haproxy-{0} service".format( + 'message': "Error disabling haproxy-{} service".format( lb_id), 'details': e.output}, status=500) # delete the directory + init script for that listener @@ -423,7 +423,7 @@ class Loadbalancer(object): 'details': "No certificate with filename: {f}".format( f=filename)}, status=404) - with open(cert_path, 'r', encoding='utf-8') as crt_file: + with open(cert_path, encoding='utf-8') as crt_file: cert = crt_file.read() md5sum = md5(octavia_utils.b(cert), usedforsecurity=False).hexdigest() # nosec @@ -442,8 +442,7 @@ class Loadbalancer(object): if os.path.exists( os.path.join('/proc', util.get_haproxy_pid(lb_id))): # Check if the listener is disabled - with open(util.config_path(lb_id), - 'r', encoding='utf-8') as file: + with open(util.config_path(lb_id), encoding='utf-8') as file: cfg = file.read() m = re.findall('^frontend (.*)$', cfg, re.MULTILINE) return m or [] @@ -458,7 +457,7 @@ class Loadbalancer(object): raise exceptions.HTTPException( response=webob.Response(json={ 'message': 'Loadbalancer Not Found', - 'details': "No loadbalancer with UUID: {0}".format( + 'details': "No loadbalancer with UUID: {}".format( lb_id)}, status=404)) def _check_ssl_filename_format(self, filename): diff --git a/octavia/amphorae/backends/agent/api_server/lvs_listener_base.py b/octavia/amphorae/backends/agent/api_server/lvs_listener_base.py index 0687c74997..ec068248a3 100644 --- a/octavia/amphorae/backends/agent/api_server/lvs_listener_base.py +++ b/octavia/amphorae/backends/agent/api_server/lvs_listener_base.py @@ -20,7 +20,7 @@ from oslo_config import cfg CONF = cfg.CONF -class LvsListenerApiServerBase(object, metaclass=abc.ABCMeta): +class LvsListenerApiServerBase(metaclass=abc.ABCMeta): """Base LVS Listener Server API """ diff --git a/octavia/amphorae/backends/agent/api_server/osutils.py b/octavia/amphorae/backends/agent/api_server/osutils.py index e28826e4de..a1e55584e6 100644 --- a/octavia/amphorae/backends/agent/api_server/osutils.py +++ b/octavia/amphorae/backends/agent/api_server/osutils.py @@ -29,7 +29,7 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -class BaseOS(object): +class BaseOS: def __init__(self, os_name): self.os_name = os_name @@ -97,7 +97,7 @@ class BaseOS(object): e, e.output) raise exceptions.HTTPException( response=webob.Response(json={ - 'message': 'Error plugging {0}'.format(name), + 'message': f'Error plugging {name}', 'details': e.output}, status=500)) @@ -109,7 +109,7 @@ class Ubuntu(BaseOS): def cmd_get_version_of_installed_package(self, package_name): name = self._map_package_name(package_name) - return "dpkg-query -W -f=${{Version}} {name}".format(name=name) + return f"dpkg-query -W -f=${{Version}} {name}" class RH(BaseOS): @@ -120,7 +120,7 @@ class RH(BaseOS): def cmd_get_version_of_installed_package(self, package_name): name = self._map_package_name(package_name) - return "rpm -q --queryformat %{{VERSION}} {name}".format(name=name) + return f"rpm -q --queryformat %{{VERSION}} {name}" class CentOS(RH): diff --git a/octavia/amphorae/backends/agent/api_server/plug.py b/octavia/amphorae/backends/agent/api_server/plug.py index 92c29cfad2..06949e141c 100644 --- a/octavia/amphorae/backends/agent/api_server/plug.py +++ b/octavia/amphorae/backends/agent/api_server/plug.py @@ -34,7 +34,7 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -class Plug(object): +class Plug: def __init__(self, osutils): self._osutils = osutils @@ -89,7 +89,7 @@ class Plug(object): try: rendered_vips = self.render_vips(vips) except ValueError as e: - vip_error_message = "Invalid VIP: {}".format(e) + vip_error_message = f"Invalid VIP: {e}" return webob.Response(json={'message': vip_error_message}, status=400) @@ -98,7 +98,7 @@ class Plug(object): gateway, host_routes) except ValueError as e: return webob.Response( - json={'message': "Invalid VRRP Address: {}".format(e)}, + json={'message': f"Invalid VRRP Address: {e}"}, status=400) # Check if the interface is already in the network namespace @@ -148,14 +148,14 @@ class Plug(object): for ip in fixed_ips: try: socket.inet_pton(socket.AF_INET, ip.get('ip_address')) - except socket.error: + except OSError: socket.inet_pton(socket.AF_INET6, ip.get('ip_address')) def plug_network(self, mac_address, fixed_ips, mtu=None, vip_net_info=None): try: self._check_ip_addresses(fixed_ips=fixed_ips) - except socket.error: + except OSError: return webob.Response(json={ 'message': "Invalid network port"}, status=400) diff --git a/octavia/amphorae/backends/agent/api_server/server.py b/octavia/amphorae/backends/agent/api_server/server.py index 1d8686783c..55d17428a7 100644 --- a/octavia/amphorae/backends/agent/api_server/server.py +++ b/octavia/amphorae/backends/agent/api_server/server.py @@ -55,7 +55,7 @@ def register_app_error_handler(app): app.register_error_handler(code, make_json_error) -class Server(object): +class Server: def __init__(self): self.app = flask.Flask(__name__) self._osutils = osutils.BaseOS.get_os_util() diff --git a/octavia/amphorae/backends/agent/api_server/util.py b/octavia/amphorae/backends/agent/api_server/util.py index 5fcb51d261..77500700ea 100644 --- a/octavia/amphorae/backends/agent/api_server/util.py +++ b/octavia/amphorae/backends/agent/api_server/util.py @@ -48,13 +48,13 @@ class UnknownInitError(Exception): def init_path(lb_id, init_system): if init_system == consts.INIT_SYSTEMD: return os.path.join(consts.SYSTEMD_DIR, - 'haproxy-{0}.service'.format(lb_id)) + f'haproxy-{lb_id}.service') if init_system == consts.INIT_UPSTART: return os.path.join(consts.UPSTART_DIR, - 'haproxy-{0}.conf'.format(lb_id)) + f'haproxy-{lb_id}.conf') if init_system == consts.INIT_SYSVINIT: return os.path.join(consts.SYSVINIT_DIR, - 'haproxy-{0}'.format(lb_id)) + f'haproxy-{lb_id}') raise UnknownInitError() @@ -120,13 +120,13 @@ def state_file_path(lb_id): def get_haproxy_pid(lb_id): - with open(pid_path(lb_id), 'r', encoding='utf-8') as f: + with open(pid_path(lb_id), encoding='utf-8') as f: return f.readline().rstrip() def get_keepalivedlvs_pid(listener_id): pid_file = keepalived_lvs_pids_path(listener_id)[0] - with open(pid_file, 'r', encoding='utf-8') as f: + with open(pid_file, encoding='utf-8') as f: return f.readline().rstrip() @@ -228,8 +228,7 @@ def is_lvs_listener_running(listener_id): def get_os_init_system(): if os.path.exists(consts.INIT_PROC_COMM_PATH): - with open(consts.INIT_PROC_COMM_PATH, - 'r', encoding='utf-8') as init_comm: + with open(consts.INIT_PROC_COMM_PATH, encoding='utf-8') as init_comm: init_proc_name = init_comm.read().rstrip('\n') if init_proc_name == consts.INIT_SYSTEMD: return consts.INIT_SYSTEMD @@ -268,7 +267,7 @@ def install_netns_systemd_service(): def run_systemctl_command(command, service): - cmd = "systemctl {cmd} {srvc}".format(cmd=command, srvc=service) + cmd = f"systemctl {command} {service}" try: subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: @@ -295,7 +294,7 @@ def get_backend_for_lb_object(object_id): def parse_haproxy_file(lb_id): - with open(config_path(lb_id), 'r', encoding='utf-8') as file: + with open(config_path(lb_id), encoding='utf-8') as file: cfg = file.read() listeners = {} @@ -376,7 +375,7 @@ def get_haproxy_vip_addresses(lb_id): :returns: List of VIP addresses (IPv4 and IPv6) """ vips = [] - with open(config_path(lb_id), 'r', encoding='utf-8') as file: + with open(config_path(lb_id), encoding='utf-8') as file: for line in file: current_line = line.strip() if current_line.startswith('bind'): diff --git a/octavia/amphorae/backends/health_daemon/health_daemon.py b/octavia/amphorae/backends/health_daemon/health_daemon.py index f4247682af..50774ac526 100644 --- a/octavia/amphorae/backends/health_daemon/health_daemon.py +++ b/octavia/amphorae/backends/health_daemon/health_daemon.py @@ -118,14 +118,13 @@ def run_sender(cmd_queue): # heartbeat if os.path.isfile(keepalived_cfg_path): # Is there a pid file for keepalived? - with open(keepalived_pid_path, - 'r', encoding='utf-8') as pid_file: + with open(keepalived_pid_path, encoding='utf-8') as pid_file: pid = int(pid_file.readline()) os.kill(pid, 0) message = build_stats_message() sender.dosend(message) - except (IOError, OSError) as e: + except OSError as e: if e.errno == errno.ENOENT: # Missing PID file, skip health heartbeat. LOG.error('Missing keepalived PID file %s, skipping health ' diff --git a/octavia/amphorae/backends/health_daemon/health_sender.py b/octavia/amphorae/backends/health_daemon/health_sender.py index 5608d57016..8bc61eb59d 100644 --- a/octavia/amphorae/backends/health_daemon/health_sender.py +++ b/octavia/amphorae/backends/health_daemon/health_sender.py @@ -31,7 +31,7 @@ def round_robin_addr(addrinfo_list): return addrinfo -class UDPStatusSender(object): +class UDPStatusSender: def __init__(self): self._update_dests() self.v4sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -56,7 +56,7 @@ class UDPStatusSender(object): self.v4sock.sendto(envelope_str, dest[4]) elif dest[0] == socket.AF_INET6: self.v6sock.sendto(envelope_str, dest[4]) - except socket.error: + except OSError: # Pass here as on amp boot it will get one or more # error: [Errno 101] Network is unreachable # while the networks are coming up diff --git a/octavia/amphorae/backends/utils/haproxy_query.py b/octavia/amphorae/backends/utils/haproxy_query.py index d0aa00dee8..9403df5621 100644 --- a/octavia/amphorae/backends/utils/haproxy_query.py +++ b/octavia/amphorae/backends/utils/haproxy_query.py @@ -24,7 +24,7 @@ from octavia.i18n import _ LOG = logging.getLogger(__name__) -class HAProxyQuery(object): +class HAProxyQuery: """Class used for querying the HAProxy statistics socket. The CSV output is defined in the HAProxy documentation: @@ -51,7 +51,7 @@ class HAProxyQuery(object): try: sock.connect(self.socket) - except socket.error as e: + except OSError as e: raise Exception( _("HAProxy '{0}' query failed.").format(query)) from e diff --git a/octavia/amphorae/backends/utils/interface.py b/octavia/amphorae/backends/utils/interface.py index f2fbf8d828..58081e6dd6 100644 --- a/octavia/amphorae/backends/utils/interface.py +++ b/octavia/amphorae/backends/utils/interface.py @@ -37,7 +37,7 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -class InterfaceController(object): +class InterfaceController: ADD = 'add' DELETE = 'delete' SET = 'set' @@ -109,7 +109,7 @@ class InterfaceController(object): "/var/lib/dhclient/dhclient-{}.leases".format( interface_name), "-pf", - "/run/dhclient-{}.pid".format(interface_name), + f"/run/dhclient-{interface_name}.pid", interface_name] LOG.debug("Running '%s'", cmd) subprocess.check_output(cmd, stderr=subprocess.STDOUT) @@ -121,7 +121,7 @@ class InterfaceController(object): "/var/lib/dhclient/dhclient-{}.leases".format( interface_name), "-pf", - "/run/dhclient-{}.pid".format(interface_name), + f"/run/dhclient-{interface_name}.pid", interface_name] LOG.debug("Running '%s'", cmd) subprocess.check_output(cmd, stderr=subprocess.STDOUT) diff --git a/octavia/amphorae/backends/utils/interface_file.py b/octavia/amphorae/backends/utils/interface_file.py index b627aca99d..1da9bc0347 100644 --- a/octavia/amphorae/backends/utils/interface_file.py +++ b/octavia/amphorae/backends/utils/interface_file.py @@ -24,7 +24,7 @@ from octavia.common import constants as consts CONF = cfg.CONF -class InterfaceFile(object): +class InterfaceFile: def __init__(self, name, if_type, mtu=None, addresses=None, routes=None, rules=None, scripts=None, is_sriov=False): self.name = name @@ -88,7 +88,7 @@ class InterfaceFile(object): except OSError: pass - interface_file = "{}.json".format(self.name) + interface_file = f"{self.name}.json" with os.fdopen(os.open(os.path.join(net_dir, interface_file), flags, mode), 'w') as fp: diff --git a/octavia/amphorae/backends/utils/keepalivedlvs_query.py b/octavia/amphorae/backends/utils/keepalivedlvs_query.py index abcb946de2..153cc0afdc 100644 --- a/octavia/amphorae/backends/utils/keepalivedlvs_query.py +++ b/octavia/amphorae/backends/utils/keepalivedlvs_query.py @@ -147,7 +147,7 @@ def get_lvs_listener_resource_ipports_nsname(listener_id): # 'HealthMonitor': {'id': healthmonitor-id}} resource_ipport_mapping = {} with open(util.keepalived_lvs_cfg_path(listener_id), - 'r', encoding='utf-8') as f: + encoding='utf-8') as f: cfg = f.read() ret = VS_ADDRESS_REGEX.findall(cfg) @@ -260,7 +260,7 @@ def get_lvs_listener_pool_status(listener_id): restarting = config_stat.st_mtime > check_pid_stat.st_mtime with open(util.keepalived_lvs_cfg_path(listener_id), - 'r', encoding='utf-8') as f: + encoding='utf-8') as f: cfg = f.read() hm_enabled = len(CHECKER_REGEX.findall(cfg)) > 0 diff --git a/octavia/amphorae/backends/utils/network_namespace.py b/octavia/amphorae/backends/utils/network_namespace.py index f03080e8ad..8b5e6b1e58 100644 --- a/octavia/amphorae/backends/utils/network_namespace.py +++ b/octavia/amphorae/backends/utils/network_namespace.py @@ -15,7 +15,7 @@ import ctypes import os -class NetworkNamespace(object): +class NetworkNamespace: """A network namespace context manager. Runs wrapped code inside the specified network namespace. @@ -32,8 +32,8 @@ class NetworkNamespace(object): raise OSError(errno, os.strerror(errno)) def __init__(self, netns): - self.current_netns = '/proc/{pid}/ns/net'.format(pid=os.getpid()) - self.target_netns = '/var/run/netns/{netns}'.format(netns=netns) + self.current_netns = f'/proc/{os.getpid()}/ns/net' + self.target_netns = f'/var/run/netns/{netns}' # reference: man setns(2) self.set_netns = ctypes.CDLL('libc.so.6', use_errno=True).setns self.set_netns.errcheck = self._error_handler diff --git a/octavia/amphorae/drivers/driver_base.py b/octavia/amphorae/drivers/driver_base.py index 6f6b4253e8..e06cb01aaf 100644 --- a/octavia/amphorae/drivers/driver_base.py +++ b/octavia/amphorae/drivers/driver_base.py @@ -19,7 +19,7 @@ from typing import Optional from octavia.db import models as db_models -class AmphoraLoadBalancerDriver(object, metaclass=abc.ABCMeta): +class AmphoraLoadBalancerDriver(metaclass=abc.ABCMeta): @abc.abstractmethod def update_amphora_listeners(self, loadbalancer, amphora, timeout_dict): @@ -264,7 +264,7 @@ class AmphoraLoadBalancerDriver(object, metaclass=abc.ABCMeta): """ -class VRRPDriverMixin(object, metaclass=abc.ABCMeta): +class VRRPDriverMixin(metaclass=abc.ABCMeta): """Abstract mixin class for VRRP support in loadbalancer amphorae Usage: To plug VRRP support in another service driver XYZ, use: diff --git a/octavia/amphorae/drivers/haproxy/rest_api_driver.py b/octavia/amphorae/drivers/haproxy/rest_api_driver.py index 3b941ec745..a1d85fc35c 100644 --- a/octavia/amphorae/drivers/haproxy/rest_api_driver.py +++ b/octavia/amphorae/drivers/haproxy/rest_api_driver.py @@ -308,7 +308,7 @@ class HaproxyAmphoraLoadBalancerDriver( for cert_id in certs_to_delete: self.clients[amphora.api_version].delete_cert_pem( amphora, listener.load_balancer.id, - '{id}.pem'.format(id=cert_id)) + f'{cert_id}.pem') # See how many non-UDP/SCTP listeners we have left non_lvs_listener_count = len([ @@ -451,7 +451,7 @@ class HaproxyAmphoraLoadBalancerDriver( for cert in certs: pem = cert_parser.build_pem(cert) md5sum = md5(pem, usedforsecurity=False).hexdigest() # nosec - name = '{id}.pem'.format(id=cert.id) + name = f'{cert.id}.pem' cert_filename_list.append( os.path.join( CONF.haproxy_amphora.base_cert_dir, obj_id, name)) @@ -460,10 +460,10 @@ class HaproxyAmphoraLoadBalancerDriver( if certs: # Build and upload the crt-list file for haproxy crt_list = "\n".join(cert_filename_list) - crt_list = f'{crt_list}\n'.encode('utf-8') + crt_list = f'{crt_list}\n'.encode() md5sum = md5(crt_list, usedforsecurity=False).hexdigest() # nosec - name = '{id}.pem'.format(id=listener.id) + name = f'{listener.id}.pem' self._upload_cert(amphora, obj_id, crt_list, md5sum, name) return {'tls_cert': tls_cert, 'sni_certs': sni_certs} @@ -482,7 +482,7 @@ class HaproxyAmphoraLoadBalancerDriver( pass md5sum = md5(secret, usedforsecurity=False).hexdigest() # nosec id = hashlib.sha1(secret).hexdigest() # nosec - name = '{id}.pem'.format(id=id) + name = f'{id}.pem' if amphora and obj_id: self._upload_cert( @@ -520,7 +520,7 @@ class HaproxyAmphoraLoadBalancerDriver( except AttributeError: pass md5sum = md5(pem, usedforsecurity=False).hexdigest() # nosec - name = '{id}.pem'.format(id=tls_cert.id) + name = f'{tls_cert.id}.pem' if amphora and obj_id: self._upload_cert(amphora, obj_id, pem=pem, md5sum=md5sum, name=name) @@ -629,7 +629,7 @@ class CustomHostNameCheckingAdapter(requests.adapters.HTTPAdapter): return super().init_poolmanager(*pool_args, **pool_kwargs) -class AmphoraAPIClientBase(object): +class AmphoraAPIClientBase: def __init__(self): super().__init__() @@ -650,7 +650,7 @@ class AmphoraAPIClientBase(object): ip=ip, interface=CONF.haproxy_amphora.lb_network_interface) elif utils.is_ipv6(ip): - ip = '[{ip}]'.format(ip=ip) + ip = f'[{ip}]' if api_version: return "https://{ip}:{port}/{version}/".format( ip=ip, @@ -782,7 +782,7 @@ class AmphoraAPIClient1_0(AmphoraAPIClientBase): def get_listener_status(self, amp, listener_id): r = self.get( amp, - 'listeners/{listener_id}'.format(listener_id=listener_id)) + f'listeners/{listener_id}') if exc.check_exception(r): return r.json() return None @@ -825,7 +825,7 @@ class AmphoraAPIClient1_0(AmphoraAPIClientBase): def delete_listener(self, amp, object_id): r = self.delete( - amp, 'listeners/{object_id}'.format(object_id=object_id)) + amp, f'listeners/{object_id}') return exc.check_exception(r, (404,)) def get_info(self, amp, raise_retry_exception=False, @@ -855,7 +855,7 @@ class AmphoraAPIClient1_0(AmphoraAPIClientBase): def plug_vip(self, amp, vip, net_info): r = self.post(amp, - 'plug/vip/{vip}'.format(vip=vip), + f'plug/vip/{vip}', json=net_info) return exc.check_exception(r) @@ -864,12 +864,12 @@ class AmphoraAPIClient1_0(AmphoraAPIClientBase): return exc.check_exception(r) def _vrrp_action(self, action, amp, timeout_dict=None): - r = self.put(amp, 'vrrp/{action}'.format(action=action), + r = self.put(amp, f'vrrp/{action}', timeout_dict=timeout_dict) return exc.check_exception(r) def get_interface(self, amp, ip_addr, timeout_dict=None, log_error=True): - r = self.get(amp, 'interface/{ip_addr}'.format(ip_addr=ip_addr), + r = self.get(amp, f'interface/{ip_addr}', timeout_dict=timeout_dict) return exc.check_exception(r, log_error=log_error).json() diff --git a/octavia/amphorae/drivers/health/heartbeat_udp.py b/octavia/amphorae/drivers/health/heartbeat_udp.py index e81cd09be9..01eec85c7e 100644 --- a/octavia/amphorae/drivers/health/heartbeat_udp.py +++ b/octavia/amphorae/drivers/health/heartbeat_udp.py @@ -37,7 +37,7 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -class UDPStatusGetter(object): +class UDPStatusGetter: """This class defines methods that will gather heartbeats The heartbeats are transmitted via UDP and this class will bind to a port diff --git a/octavia/amphorae/drivers/keepalived/jinja/jinja_cfg.py b/octavia/amphorae/drivers/keepalived/jinja/jinja_cfg.py index dce571f9f5..9b258c5fb8 100644 --- a/octavia/amphorae/drivers/keepalived/jinja/jinja_cfg.py +++ b/octavia/amphorae/drivers/keepalived/jinja/jinja_cfg.py @@ -30,7 +30,7 @@ CONF = cfg.CONF LOG = logging.getLogger(__name__) -class KeepalivedJinjaTemplater(object): +class KeepalivedJinjaTemplater: def __init__(self, keepalived_template=None): """Keepalived configuration generation diff --git a/octavia/amphorae/drivers/noop_driver/driver.py b/octavia/amphorae/drivers/noop_driver/driver.py index 8b3cdff5b0..98906886da 100644 --- a/octavia/amphorae/drivers/noop_driver/driver.py +++ b/octavia/amphorae/drivers/noop_driver/driver.py @@ -23,7 +23,7 @@ from octavia.db import repositories LOG = logging.getLogger(__name__) -class NoopManager(object): +class NoopManager: def __init__(self): super().__init__()