From 5904cc24573fd93a6cf91f84875ea0b298ae84bc Mon Sep 17 00:00:00 2001 From: Ngo Quoc Cuong Date: Wed, 5 Jul 2017 03:29:56 -0400 Subject: [PATCH] Enable H904 check H904 String interpolation should be delayed to be handled by the logging code, rather than being done at the point of the logging call. Use ',' instead of '%'. See: https://docs.openstack.org/oslo.i18n/latest/user/guidelines.html#adding-variables-to-log-messages Related-Bug: #1596829 Change-Id: If986ca58517876d65e04b5e63ba8bb0c19793f01 --- tox.ini | 3 +- zun/api/controllers/v1/containers.py | 46 ++++++++++++++-------------- zun/api/controllers/v1/images.py | 3 +- zun/common/exception.py | 6 ++-- zun/compute/manager.py | 2 +- zun/container/docker/driver.py | 14 ++++----- zun/image/docker/driver.py | 19 ++++++------ zun/image/glance/driver.py | 23 +++++++------- zun/image/glance/utils.py | 4 +-- zun/network/kuryr_network.py | 2 +- 10 files changed, 59 insertions(+), 63 deletions(-) diff --git a/tox.ini b/tox.ini index 7de820733..4e7abe81f 100644 --- a/tox.ini +++ b/tox.ini @@ -63,7 +63,8 @@ commands = [flake8] show-source = True -enable-extensions = H203,H106 +# [H904] Delay string interpolations at logging calls. +enable-extensions = H203,H106,H904 builtins = _ exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index 02e9b1a66..65f8c60dd 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -373,7 +373,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:stop") utils.validate_container_state(container, 'stop') - LOG.debug('Calling compute.container_stop with %s' % + LOG.debug('Calling compute.container_stop with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api @@ -387,7 +387,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:reboot") utils.validate_container_state(container, 'reboot') - LOG.debug('Calling compute.container_reboot with %s' % + LOG.debug('Calling compute.container_reboot with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api @@ -400,7 +400,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:pause") utils.validate_container_state(container, 'pause') - LOG.debug('Calling compute.container_pause with %s' % + LOG.debug('Calling compute.container_pause with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api @@ -413,7 +413,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:unpause") utils.validate_container_state(container, 'unpause') - LOG.debug('Calling compute.container_unpause with %s' % + LOG.debug('Calling compute.container_unpause with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api @@ -436,8 +436,7 @@ class ContainersController(base.Controller): msg = _('Valid stdout, stderr and timestamps values are ''true'', ' '"false", True, False, 0 and 1, yes and no') raise exception.InvalidValue(msg) - LOG.debug('Calling compute.container_logs with %s' % - container.uuid) + LOG.debug('Calling compute.container_logs with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api return compute_api.container_logs(context, container, stdout, stderr, @@ -457,8 +456,9 @@ class ContainersController(base.Controller): except ValueError: msg = _('Valid run values are true, false, 0, 1, yes and no') raise exception.InvalidValue(msg) - LOG.debug('Calling compute.container_exec with %s command %s' - % (container.uuid, kwargs['command'])) + LOG.debug('Calling compute.container_exec with %(uuid)s command ' + '%(command)s', + {'uuid': container.uuid, 'command': kwargs['command']}) context = pecan.request.context compute_api = pecan.request.compute_api return compute_api.container_exec(context, container, @@ -488,9 +488,10 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:kill") utils.validate_container_state(container, 'kill') - LOG.debug('Calling compute.container_kill with %s signal %s' - % (container.uuid, - kwargs.get('signal', kwargs.get('signal')))) + LOG.debug('Calling compute.container_kill with %(uuid)s ' + 'signal %(signal)s', + {'uuid': container.uuid, + 'signal': kwargs.get('signal', kwargs.get('signal'))}) context = pecan.request.context compute_api = pecan.request.compute_api compute_api.container_kill(context, container, kwargs.get('signal')) @@ -502,8 +503,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:attach") utils.validate_container_state(container, 'attach') - LOG.debug('Checking the status for attach with %s' % - container.uuid) + LOG.debug('Checking the status for attach with %s', container.uuid) if container.interactive: context = pecan.request.context compute_api = pecan.request.compute_api @@ -520,7 +520,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:resize") utils.validate_container_state(container, 'resize') - LOG.debug('Calling tty resize with %s ' % (container.uuid)) + LOG.debug('Calling tty resize with %s ', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api compute_api.container_resize(context, container, kwargs.get('h', None), @@ -533,8 +533,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:top") utils.validate_container_state(container, 'top') - LOG.debug('Calling compute.container_top with %s' % - container.uuid) + LOG.debug('Calling compute.container_top with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api return compute_api.container_top(context, container, ps_args) @@ -545,8 +544,9 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:get_archive") utils.validate_container_state(container, 'get_archive') - LOG.debug('Calling compute.container_get_archive with %s path %s' - % (container.uuid, kwargs['path'])) + LOG.debug('Calling compute.container_get_archive with %(uuid)s ' + 'path %(path)s', + {'uuid': container.uuid, 'path': kwargs['path']}) context = pecan.request.context compute_api = pecan.request.compute_api data, stat = compute_api.container_get_archive( @@ -559,8 +559,9 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:put_archive") utils.validate_container_state(container, 'put_archive') - LOG.debug('Calling compute.container_put_archive with %s path %s' - % (container.uuid, kwargs['path'])) + LOG.debug('Calling compute.container_put_archive with %(uuid)s ' + 'path %(path)s', + {'uuid': container.uuid, 'path': kwargs['path']}) context = pecan.request.context compute_api = pecan.request.compute_api compute_api.container_put_archive(context, container, @@ -572,8 +573,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:stats") utils.validate_container_state(container, 'stats') - LOG.debug('Calling compute.container_stats with %s' - % (container.uuid)) + LOG.debug('Calling compute.container_stats with %s', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api return compute_api.container_stats(context, container) @@ -585,7 +585,7 @@ class ContainersController(base.Controller): container = _get_container(container_id) check_policy_on_container(container.as_dict(), "container:commit") utils.validate_container_state(container, 'commit') - LOG.debug('Calling compute.container_commit %s ' % (container.uuid)) + LOG.debug('Calling compute.container_commit %s ', container.uuid) context = pecan.request.context compute_api = pecan.request.compute_api pecan.response.status = 202 diff --git a/zun/api/controllers/v1/images.py b/zun/api/controllers/v1/images.py index 6e36f2cdb..9cb001ba5 100644 --- a/zun/api/controllers/v1/images.py +++ b/zun/api/controllers/v1/images.py @@ -123,8 +123,7 @@ class ImagesController(base.Controller): context = pecan.request.context policy.enforce(context, "image:search", action="image:search") - LOG.debug('Calling compute.image_search with %s' % - image) + LOG.debug('Calling compute.image_search with %s', image) try: exact_match = strutils.bool_from_string(exact_match, strict=True) except ValueError: diff --git a/zun/common/exception.py b/zun/common/exception.py index a55213936..eef918b6f 100644 --- a/zun/common/exception.py +++ b/zun/common/exception.py @@ -117,7 +117,7 @@ def wrap_controller_exception(func, func_server_error, func_client_error): # log the error message with its associated # correlation id log_correlation_id = uuidutils.generate_uuid() - LOG.exception("%(correlation_id)s:%(excp)s" % + LOG.exception("%(correlation_id)s:%(excp)s", {'correlation_id': log_correlation_id, 'excp': str(excp)}) # raise a client error with an obfuscated message @@ -194,8 +194,8 @@ class ZunException(Exception): except KeyError: # kwargs doesn't match a variable in the message # log the issue and the kwargs - LOG.exception(('Exception in string format operation, ' - 'kwargs: %s') % kwargs) + LOG.exception('Exception in string format operation, ' + 'kwargs: %s', kwargs) try: ferr = CONF.fatal_exception_format_errors except cfg.NoSuchOptError: diff --git a/zun/compute/manager.py b/zun/compute/manager.py index 6ce320072..d4107acdb 100644 --- a/zun/compute/manager.py +++ b/zun/compute/manager.py @@ -574,7 +574,7 @@ class Manager(object): LOG.error("Error occurred while calling docker commit API: %s", six.text_type(e)) raise - LOG.debug('Upload image %s to glance' % container_image_id) + LOG.debug('Upload image %s to glance', container_image_id) self._do_container_image_upload(context, snapshot_image, container_image, tag) diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index b5e2a8d14..88766e84f 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -86,12 +86,12 @@ class DockerDriver(driver.ContainerDriver): def inspect_image(self, image): with docker_utils.docker_client() as docker: - LOG.debug('Inspecting image %s' % image) + LOG.debug('Inspecting image %s', image) image_dict = docker.inspect_image(image) return image_dict def get_image(self, name): - LOG.debug('Obtaining image %s' % name) + LOG.debug('Obtaining image %s', name) with docker_utils.docker_client() as docker: response = docker.get_image(name) return response @@ -105,8 +105,8 @@ class DockerDriver(driver.ContainerDriver): with docker_utils.docker_client() as docker: name = container.name image = container.image - LOG.debug('Creating container with image %s name %s' - % (image, name)) + LOG.debug('Creating container with image %(image)s name %(name)s', + {'image': image, 'name': name}) kwargs = { 'name': self.get_container_name(container), @@ -779,8 +779,7 @@ class NovaDockerDriver(DockerDriver): novaclient = nova.NovaClient(elevated) server_name = self._find_server_by_container_id(sandbox_id) if not server_name: - LOG.warning("Cannot find server name for sandbox %s" % - sandbox_id) + LOG.warning("Cannot find server name for sandbox %s", sandbox_id) return server_id = novaclient.delete_server(server_name) @@ -791,8 +790,7 @@ class NovaDockerDriver(DockerDriver): novaclient = nova.NovaClient(elevated) server_name = self._find_server_by_container_id(sandbox_id) if not server_name: - LOG.warning("Cannot find server name for sandbox %s" % - sandbox_id) + LOG.warning("Cannot find server name for sandbox %s", sandbox_id) return novaclient.stop_server(server_name) diff --git a/zun/image/docker/driver.py b/zun/image/docker/driver.py index 15eb8e9b2..efc766e67 100644 --- a/zun/image/docker/driver.py +++ b/zun/image/docker/driver.py @@ -36,13 +36,13 @@ class DockerDriver(driver.ContainerImageDriver): def _search_image_on_host(self, repo, tag): with docker_utils.docker_client() as docker: image = repo + ":" + tag - LOG.debug('Inspecting image locally %s' % image) + LOG.debug('Inspecting image locally %s', image) try: image_dict = docker.inspect_image(image) if image_dict: return {'image': repo, 'path': None} except errors.NotFound: - LOG.debug('Image %s not found locally' % image) + LOG.debug('Image %s not found locally', image) return None def _pull_image(self, repo, tag): @@ -57,7 +57,7 @@ class DockerDriver(driver.ContainerImageDriver): image = self._search_image_on_host(repo, tag) if not utils.should_pull_image(image_pull_policy, bool(image)): if image: - LOG.debug('Image %s present locally' % repo) + LOG.debug('Image %s present locally', repo) return image, image_loaded else: message = _('Image %s not present with pull policy of Never' @@ -65,19 +65,18 @@ class DockerDriver(driver.ContainerImageDriver): raise exception.ImageNotFound(message) try: - LOG.debug('Pulling image from docker %s,' - ' context %s' % (repo, context)) + LOG.debug('Pulling image from docker %(repo)s,' + ' context %(context)s', + {'repo': repo, 'context': context}) self._pull_image(repo, tag) return {'image': repo, 'path': None}, image_loaded except exception.ImageNotFound: with excutils.save_and_reraise_exception(): - LOG.error( - 'Image %s was not found in docker repo' % repo) + LOG.error('Image %s was not found in docker repo', repo) except exception.DockerError: with excutils.save_and_reraise_exception(): - LOG.error( - 'Docker API error occurred during downloading\ - image %s' % repo) + LOG.error('Docker API error occurred during downloading ' + 'image %s', repo) except Exception as e: msg = _('Cannot download image from docker: {0}') raise exception.ZunException(msg.format(e)) diff --git a/zun/image/glance/driver.py b/zun/image/glance/driver.py index 104f6f949..f6d872714 100644 --- a/zun/image/glance/driver.py +++ b/zun/image/glance/driver.py @@ -36,7 +36,7 @@ class GlanceDriver(driver.ContainerImageDriver): super(GlanceDriver, self).__init__() def _search_image_on_host(self, context, repo): - LOG.debug('Searching for image %s locally' % repo) + LOG.debug('Searching for image %s locally', repo) images_directory = CONF.glance.images_directory try: # TODO(mkrai): Change this to search image entry in zun db @@ -78,7 +78,7 @@ class GlanceDriver(driver.ContainerImageDriver): if not common_utils.should_pull_image(image_pull_policy, bool(image)): if image: - LOG.debug('Image %s present locally' % repo) + LOG.debug('Image %s present locally', repo) image_loaded = True return image, image_loaded else: @@ -86,15 +86,14 @@ class GlanceDriver(driver.ContainerImageDriver): ) % repo raise exception.ImageNotFound(message) - LOG.debug('Pulling image from glance %s' % repo) + LOG.debug('Pulling image from glance %s', repo) try: glance = utils.create_glanceclient(context) image_meta = utils.find_image(context, repo) - LOG.debug('Image %s was found in glance, downloading now...' - % repo) + LOG.debug('Image %s was found in glance, downloading now...', repo) image_chunks = glance.images.data(image_meta.id) except exception.ImageNotFound: - LOG.error('Image %s was not found in glance' % repo) + LOG.error('Image %s was not found in glance', repo) raise except Exception as e: msg = _('Cannot download image from glance: {0}') @@ -109,14 +108,14 @@ class GlanceDriver(driver.ContainerImageDriver): except Exception as e: msg = _('Error occurred while writing image: {0}') raise exception.ZunException(msg.format(e)) - LOG.debug('Image %s was downloaded to path : %s' - % (repo, out_path)) + LOG.debug('Image %(repo)s was downloaded to path : %(path)s', + {'repo': repo, 'path': out_path}) return {'image': repo, 'path': out_path}, image_loaded def search_image(self, context, repo, tag, exact_match): # TODO(mkrai): glance driver does not handle tags # once metadata is stored in db then handle tags - LOG.debug('Searching image in glance %s' % repo) + LOG.debug('Searching image in glance %s', repo) try: # TODO(hongbin): find image by both repo and tag images = utils.find_images(context, repo, exact_match) @@ -126,7 +125,7 @@ class GlanceDriver(driver.ContainerImageDriver): def create_image(self, context, image_name): """Create an image.""" - LOG.debug('Creating a new image in glance %s' % image_name) + LOG.debug('Creating a new image in glance %s', image_name) try: img = utils.create_image(context, image_name) return img @@ -136,7 +135,7 @@ class GlanceDriver(driver.ContainerImageDriver): def update_image(self, context, img_id, disk_format='qcow2', container_format='docker', tag=None): """Update an image.""" - LOG.debug('Updating an image %s in glance' % img_id) + LOG.debug('Updating an image %s in glance', img_id) try: if tag is not None: tags = [] @@ -151,7 +150,7 @@ class GlanceDriver(driver.ContainerImageDriver): def upload_image_data(self, context, img_id, data): """Update an image.""" - LOG.debug('Uploading an image to glance %s' % img_id) + LOG.debug('Uploading an image to glance %s', img_id) try: img = utils.upload_image_data(context, img_id, data) return img diff --git a/zun/image/glance/utils.py b/zun/image/glance/utils.py index 5846fdb7d..558742f8a 100644 --- a/zun/image/glance/utils.py +++ b/zun/image/glance/utils.py @@ -35,7 +35,7 @@ def create_glanceclient(context): def find_image(context, image_ident): matches = find_images(context, image_ident, exact_match=True) - LOG.debug('Found matches %s ' % matches) + LOG.debug('Found matches %s ', matches) if len(matches) == 0: raise exception.ImageNotFound(image=image_ident) if len(matches) > 1: @@ -93,7 +93,7 @@ def update_image_tags(context, img_id, tags): def upload_image_data(context, img_id, data): """Upload an image.""" - LOG.debug('Upload image %s ' % img_id) + LOG.debug('Upload image %s ', img_id) glance = create_glanceclient(context) img = glance.images.upload(img_id, data) return img diff --git a/zun/network/kuryr_network.py b/zun/network/kuryr_network.py index 801fbbb6d..3f38a4847 100644 --- a/zun/network/kuryr_network.py +++ b/zun/network/kuryr_network.py @@ -199,7 +199,7 @@ class KuryrNetwork(network.Network): updated_port = {'security_groups': port['security_groups']} try: LOG.info("Adding security group %(security_group_ids)s " - "to port %(port_id)s" % + "to port %(port_id)s", {'security_group_ids': security_group_ids, 'port_id': port['id']}) self.neutron.update_port(port['id'],