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
This commit is contained in:
Ngo Quoc Cuong 2017-07-05 03:29:56 -04:00
parent d0bd3a418f
commit 5904cc2457
10 changed files with 59 additions and 63 deletions

View File

@ -63,7 +63,8 @@ commands =
[flake8] [flake8]
show-source = True show-source = True
enable-extensions = H203,H106 # [H904] Delay string interpolations at logging calls.
enable-extensions = H203,H106,H904
builtins = _ builtins = _
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build

View File

@ -373,7 +373,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:stop") check_policy_on_container(container.as_dict(), "container:stop")
utils.validate_container_state(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) container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
@ -387,7 +387,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:reboot") check_policy_on_container(container.as_dict(), "container:reboot")
utils.validate_container_state(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) container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
@ -400,7 +400,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:pause") check_policy_on_container(container.as_dict(), "container:pause")
utils.validate_container_state(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) container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
@ -413,7 +413,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:unpause") check_policy_on_container(container.as_dict(), "container:unpause")
utils.validate_container_state(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) container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
@ -436,8 +436,7 @@ class ContainersController(base.Controller):
msg = _('Valid stdout, stderr and timestamps values are ''true'', ' msg = _('Valid stdout, stderr and timestamps values are ''true'', '
'"false", True, False, 0 and 1, yes and no') '"false", True, False, 0 and 1, yes and no')
raise exception.InvalidValue(msg) raise exception.InvalidValue(msg)
LOG.debug('Calling compute.container_logs with %s' % LOG.debug('Calling compute.container_logs with %s', container.uuid)
container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
return compute_api.container_logs(context, container, stdout, stderr, return compute_api.container_logs(context, container, stdout, stderr,
@ -457,8 +456,9 @@ class ContainersController(base.Controller):
except ValueError: except ValueError:
msg = _('Valid run values are true, false, 0, 1, yes and no') msg = _('Valid run values are true, false, 0, 1, yes and no')
raise exception.InvalidValue(msg) raise exception.InvalidValue(msg)
LOG.debug('Calling compute.container_exec with %s command %s' LOG.debug('Calling compute.container_exec with %(uuid)s command '
% (container.uuid, kwargs['command'])) '%(command)s',
{'uuid': container.uuid, 'command': kwargs['command']})
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
return compute_api.container_exec(context, container, return compute_api.container_exec(context, container,
@ -488,9 +488,10 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:kill") check_policy_on_container(container.as_dict(), "container:kill")
utils.validate_container_state(container, 'kill') utils.validate_container_state(container, 'kill')
LOG.debug('Calling compute.container_kill with %s signal %s' LOG.debug('Calling compute.container_kill with %(uuid)s '
% (container.uuid, 'signal %(signal)s',
kwargs.get('signal', kwargs.get('signal')))) {'uuid': container.uuid,
'signal': kwargs.get('signal', kwargs.get('signal'))})
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
compute_api.container_kill(context, container, kwargs.get('signal')) compute_api.container_kill(context, container, kwargs.get('signal'))
@ -502,8 +503,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:attach") check_policy_on_container(container.as_dict(), "container:attach")
utils.validate_container_state(container, 'attach') utils.validate_container_state(container, 'attach')
LOG.debug('Checking the status for attach with %s' % LOG.debug('Checking the status for attach with %s', container.uuid)
container.uuid)
if container.interactive: if container.interactive:
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
@ -520,7 +520,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:resize") check_policy_on_container(container.as_dict(), "container:resize")
utils.validate_container_state(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 context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
compute_api.container_resize(context, container, kwargs.get('h', None), compute_api.container_resize(context, container, kwargs.get('h', None),
@ -533,8 +533,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:top") check_policy_on_container(container.as_dict(), "container:top")
utils.validate_container_state(container, 'top') utils.validate_container_state(container, 'top')
LOG.debug('Calling compute.container_top with %s' % LOG.debug('Calling compute.container_top with %s', container.uuid)
container.uuid)
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
return compute_api.container_top(context, container, ps_args) return compute_api.container_top(context, container, ps_args)
@ -545,8 +544,9 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:get_archive") check_policy_on_container(container.as_dict(), "container:get_archive")
utils.validate_container_state(container, 'get_archive') utils.validate_container_state(container, 'get_archive')
LOG.debug('Calling compute.container_get_archive with %s path %s' LOG.debug('Calling compute.container_get_archive with %(uuid)s '
% (container.uuid, kwargs['path'])) 'path %(path)s',
{'uuid': container.uuid, 'path': kwargs['path']})
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
data, stat = compute_api.container_get_archive( data, stat = compute_api.container_get_archive(
@ -559,8 +559,9 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:put_archive") check_policy_on_container(container.as_dict(), "container:put_archive")
utils.validate_container_state(container, 'put_archive') utils.validate_container_state(container, 'put_archive')
LOG.debug('Calling compute.container_put_archive with %s path %s' LOG.debug('Calling compute.container_put_archive with %(uuid)s '
% (container.uuid, kwargs['path'])) 'path %(path)s',
{'uuid': container.uuid, 'path': kwargs['path']})
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
compute_api.container_put_archive(context, container, compute_api.container_put_archive(context, container,
@ -572,8 +573,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:stats") check_policy_on_container(container.as_dict(), "container:stats")
utils.validate_container_state(container, 'stats') utils.validate_container_state(container, 'stats')
LOG.debug('Calling compute.container_stats with %s' LOG.debug('Calling compute.container_stats with %s', container.uuid)
% (container.uuid))
context = pecan.request.context context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
return compute_api.container_stats(context, container) return compute_api.container_stats(context, container)
@ -585,7 +585,7 @@ class ContainersController(base.Controller):
container = _get_container(container_id) container = _get_container(container_id)
check_policy_on_container(container.as_dict(), "container:commit") check_policy_on_container(container.as_dict(), "container:commit")
utils.validate_container_state(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 context = pecan.request.context
compute_api = pecan.request.compute_api compute_api = pecan.request.compute_api
pecan.response.status = 202 pecan.response.status = 202

View File

@ -123,8 +123,7 @@ class ImagesController(base.Controller):
context = pecan.request.context context = pecan.request.context
policy.enforce(context, "image:search", policy.enforce(context, "image:search",
action="image:search") action="image:search")
LOG.debug('Calling compute.image_search with %s' % LOG.debug('Calling compute.image_search with %s', image)
image)
try: try:
exact_match = strutils.bool_from_string(exact_match, strict=True) exact_match = strutils.bool_from_string(exact_match, strict=True)
except ValueError: except ValueError:

View File

@ -117,7 +117,7 @@ def wrap_controller_exception(func, func_server_error, func_client_error):
# log the error message with its associated # log the error message with its associated
# correlation id # correlation id
log_correlation_id = uuidutils.generate_uuid() 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, {'correlation_id': log_correlation_id,
'excp': str(excp)}) 'excp': str(excp)})
# raise a client error with an obfuscated message # raise a client error with an obfuscated message
@ -194,8 +194,8 @@ class ZunException(Exception):
except KeyError: except KeyError:
# kwargs doesn't match a variable in the message # kwargs doesn't match a variable in the message
# log the issue and the kwargs # log the issue and the kwargs
LOG.exception(('Exception in string format operation, ' LOG.exception('Exception in string format operation, '
'kwargs: %s') % kwargs) 'kwargs: %s', kwargs)
try: try:
ferr = CONF.fatal_exception_format_errors ferr = CONF.fatal_exception_format_errors
except cfg.NoSuchOptError: except cfg.NoSuchOptError:

View File

@ -574,7 +574,7 @@ class Manager(object):
LOG.error("Error occurred while calling docker commit API: %s", LOG.error("Error occurred while calling docker commit API: %s",
six.text_type(e)) six.text_type(e))
raise 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, self._do_container_image_upload(context, snapshot_image,
container_image, tag) container_image, tag)

View File

@ -86,12 +86,12 @@ class DockerDriver(driver.ContainerDriver):
def inspect_image(self, image): def inspect_image(self, image):
with docker_utils.docker_client() as docker: 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) image_dict = docker.inspect_image(image)
return image_dict return image_dict
def get_image(self, name): def get_image(self, name):
LOG.debug('Obtaining image %s' % name) LOG.debug('Obtaining image %s', name)
with docker_utils.docker_client() as docker: with docker_utils.docker_client() as docker:
response = docker.get_image(name) response = docker.get_image(name)
return response return response
@ -105,8 +105,8 @@ class DockerDriver(driver.ContainerDriver):
with docker_utils.docker_client() as docker: with docker_utils.docker_client() as docker:
name = container.name name = container.name
image = container.image image = container.image
LOG.debug('Creating container with image %s name %s' LOG.debug('Creating container with image %(image)s name %(name)s',
% (image, name)) {'image': image, 'name': name})
kwargs = { kwargs = {
'name': self.get_container_name(container), 'name': self.get_container_name(container),
@ -779,8 +779,7 @@ class NovaDockerDriver(DockerDriver):
novaclient = nova.NovaClient(elevated) novaclient = nova.NovaClient(elevated)
server_name = self._find_server_by_container_id(sandbox_id) server_name = self._find_server_by_container_id(sandbox_id)
if not server_name: if not server_name:
LOG.warning("Cannot find server name for sandbox %s" % LOG.warning("Cannot find server name for sandbox %s", sandbox_id)
sandbox_id)
return return
server_id = novaclient.delete_server(server_name) server_id = novaclient.delete_server(server_name)
@ -791,8 +790,7 @@ class NovaDockerDriver(DockerDriver):
novaclient = nova.NovaClient(elevated) novaclient = nova.NovaClient(elevated)
server_name = self._find_server_by_container_id(sandbox_id) server_name = self._find_server_by_container_id(sandbox_id)
if not server_name: if not server_name:
LOG.warning("Cannot find server name for sandbox %s" % LOG.warning("Cannot find server name for sandbox %s", sandbox_id)
sandbox_id)
return return
novaclient.stop_server(server_name) novaclient.stop_server(server_name)

View File

@ -36,13 +36,13 @@ class DockerDriver(driver.ContainerImageDriver):
def _search_image_on_host(self, repo, tag): def _search_image_on_host(self, repo, tag):
with docker_utils.docker_client() as docker: with docker_utils.docker_client() as docker:
image = repo + ":" + tag image = repo + ":" + tag
LOG.debug('Inspecting image locally %s' % image) LOG.debug('Inspecting image locally %s', image)
try: try:
image_dict = docker.inspect_image(image) image_dict = docker.inspect_image(image)
if image_dict: if image_dict:
return {'image': repo, 'path': None} return {'image': repo, 'path': None}
except errors.NotFound: except errors.NotFound:
LOG.debug('Image %s not found locally' % image) LOG.debug('Image %s not found locally', image)
return None return None
def _pull_image(self, repo, tag): def _pull_image(self, repo, tag):
@ -57,7 +57,7 @@ class DockerDriver(driver.ContainerImageDriver):
image = self._search_image_on_host(repo, tag) image = self._search_image_on_host(repo, tag)
if not utils.should_pull_image(image_pull_policy, bool(image)): if not utils.should_pull_image(image_pull_policy, bool(image)):
if image: if image:
LOG.debug('Image %s present locally' % repo) LOG.debug('Image %s present locally', repo)
return image, image_loaded return image, image_loaded
else: else:
message = _('Image %s not present with pull policy of Never' message = _('Image %s not present with pull policy of Never'
@ -65,19 +65,18 @@ class DockerDriver(driver.ContainerImageDriver):
raise exception.ImageNotFound(message) raise exception.ImageNotFound(message)
try: try:
LOG.debug('Pulling image from docker %s,' LOG.debug('Pulling image from docker %(repo)s,'
' context %s' % (repo, context)) ' context %(context)s',
{'repo': repo, 'context': context})
self._pull_image(repo, tag) self._pull_image(repo, tag)
return {'image': repo, 'path': None}, image_loaded return {'image': repo, 'path': None}, image_loaded
except exception.ImageNotFound: except exception.ImageNotFound:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
LOG.error( LOG.error('Image %s was not found in docker repo', repo)
'Image %s was not found in docker repo' % repo)
except exception.DockerError: except exception.DockerError:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
LOG.error( LOG.error('Docker API error occurred during downloading '
'Docker API error occurred during downloading\ 'image %s', repo)
image %s' % repo)
except Exception as e: except Exception as e:
msg = _('Cannot download image from docker: {0}') msg = _('Cannot download image from docker: {0}')
raise exception.ZunException(msg.format(e)) raise exception.ZunException(msg.format(e))

View File

@ -36,7 +36,7 @@ class GlanceDriver(driver.ContainerImageDriver):
super(GlanceDriver, self).__init__() super(GlanceDriver, self).__init__()
def _search_image_on_host(self, context, repo): 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 images_directory = CONF.glance.images_directory
try: try:
# TODO(mkrai): Change this to search image entry in zun db # 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 not common_utils.should_pull_image(image_pull_policy, bool(image)):
if image: if image:
LOG.debug('Image %s present locally' % repo) LOG.debug('Image %s present locally', repo)
image_loaded = True image_loaded = True
return image, image_loaded return image, image_loaded
else: else:
@ -86,15 +86,14 @@ class GlanceDriver(driver.ContainerImageDriver):
) % repo ) % repo
raise exception.ImageNotFound(message) raise exception.ImageNotFound(message)
LOG.debug('Pulling image from glance %s' % repo) LOG.debug('Pulling image from glance %s', repo)
try: try:
glance = utils.create_glanceclient(context) glance = utils.create_glanceclient(context)
image_meta = utils.find_image(context, repo) image_meta = utils.find_image(context, repo)
LOG.debug('Image %s was found in glance, downloading now...' LOG.debug('Image %s was found in glance, downloading now...', repo)
% repo)
image_chunks = glance.images.data(image_meta.id) image_chunks = glance.images.data(image_meta.id)
except exception.ImageNotFound: except exception.ImageNotFound:
LOG.error('Image %s was not found in glance' % repo) LOG.error('Image %s was not found in glance', repo)
raise raise
except Exception as e: except Exception as e:
msg = _('Cannot download image from glance: {0}') msg = _('Cannot download image from glance: {0}')
@ -109,14 +108,14 @@ class GlanceDriver(driver.ContainerImageDriver):
except Exception as e: except Exception as e:
msg = _('Error occurred while writing image: {0}') msg = _('Error occurred while writing image: {0}')
raise exception.ZunException(msg.format(e)) raise exception.ZunException(msg.format(e))
LOG.debug('Image %s was downloaded to path : %s' LOG.debug('Image %(repo)s was downloaded to path : %(path)s',
% (repo, out_path)) {'repo': repo, 'path': out_path})
return {'image': repo, 'path': out_path}, image_loaded return {'image': repo, 'path': out_path}, image_loaded
def search_image(self, context, repo, tag, exact_match): def search_image(self, context, repo, tag, exact_match):
# TODO(mkrai): glance driver does not handle tags # TODO(mkrai): glance driver does not handle tags
# once metadata is stored in db then 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: try:
# TODO(hongbin): find image by both repo and tag # TODO(hongbin): find image by both repo and tag
images = utils.find_images(context, repo, exact_match) images = utils.find_images(context, repo, exact_match)
@ -126,7 +125,7 @@ class GlanceDriver(driver.ContainerImageDriver):
def create_image(self, context, image_name): def create_image(self, context, image_name):
"""Create an image.""" """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: try:
img = utils.create_image(context, image_name) img = utils.create_image(context, image_name)
return img return img
@ -136,7 +135,7 @@ class GlanceDriver(driver.ContainerImageDriver):
def update_image(self, context, img_id, disk_format='qcow2', def update_image(self, context, img_id, disk_format='qcow2',
container_format='docker', tag=None): container_format='docker', tag=None):
"""Update an image.""" """Update an image."""
LOG.debug('Updating an image %s in glance' % img_id) LOG.debug('Updating an image %s in glance', img_id)
try: try:
if tag is not None: if tag is not None:
tags = [] tags = []
@ -151,7 +150,7 @@ class GlanceDriver(driver.ContainerImageDriver):
def upload_image_data(self, context, img_id, data): def upload_image_data(self, context, img_id, data):
"""Update an image.""" """Update an image."""
LOG.debug('Uploading an image to glance %s' % img_id) LOG.debug('Uploading an image to glance %s', img_id)
try: try:
img = utils.upload_image_data(context, img_id, data) img = utils.upload_image_data(context, img_id, data)
return img return img

View File

@ -35,7 +35,7 @@ def create_glanceclient(context):
def find_image(context, image_ident): def find_image(context, image_ident):
matches = find_images(context, image_ident, exact_match=True) 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: if len(matches) == 0:
raise exception.ImageNotFound(image=image_ident) raise exception.ImageNotFound(image=image_ident)
if len(matches) > 1: if len(matches) > 1:
@ -93,7 +93,7 @@ def update_image_tags(context, img_id, tags):
def upload_image_data(context, img_id, data): def upload_image_data(context, img_id, data):
"""Upload an image.""" """Upload an image."""
LOG.debug('Upload image %s ' % img_id) LOG.debug('Upload image %s ', img_id)
glance = create_glanceclient(context) glance = create_glanceclient(context)
img = glance.images.upload(img_id, data) img = glance.images.upload(img_id, data)
return img return img

View File

@ -199,7 +199,7 @@ class KuryrNetwork(network.Network):
updated_port = {'security_groups': port['security_groups']} updated_port = {'security_groups': port['security_groups']}
try: try:
LOG.info("Adding security group %(security_group_ids)s " 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, {'security_group_ids': security_group_ids,
'port_id': port['id']}) 'port_id': port['id']})
self.neutron.update_port(port['id'], self.neutron.update_port(port['id'],