Browse Source

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
tags/0.2.0
Ngo Quoc Cuong 2 years ago
parent
commit
5904cc2457

+ 2
- 1
tox.ini View File

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

+ 23
- 23
zun/api/controllers/v1/containers.py View File

@@ -373,7 +373,7 @@ class ContainersController(base.Controller):
373 373
         container = _get_container(container_id)
374 374
         check_policy_on_container(container.as_dict(), "container:stop")
375 375
         utils.validate_container_state(container, 'stop')
376
-        LOG.debug('Calling compute.container_stop with %s' %
376
+        LOG.debug('Calling compute.container_stop with %s',
377 377
                   container.uuid)
378 378
         context = pecan.request.context
379 379
         compute_api = pecan.request.compute_api
@@ -387,7 +387,7 @@ class ContainersController(base.Controller):
387 387
         container = _get_container(container_id)
388 388
         check_policy_on_container(container.as_dict(), "container:reboot")
389 389
         utils.validate_container_state(container, 'reboot')
390
-        LOG.debug('Calling compute.container_reboot with %s' %
390
+        LOG.debug('Calling compute.container_reboot with %s',
391 391
                   container.uuid)
392 392
         context = pecan.request.context
393 393
         compute_api = pecan.request.compute_api
@@ -400,7 +400,7 @@ class ContainersController(base.Controller):
400 400
         container = _get_container(container_id)
401 401
         check_policy_on_container(container.as_dict(), "container:pause")
402 402
         utils.validate_container_state(container, 'pause')
403
-        LOG.debug('Calling compute.container_pause with %s' %
403
+        LOG.debug('Calling compute.container_pause with %s',
404 404
                   container.uuid)
405 405
         context = pecan.request.context
406 406
         compute_api = pecan.request.compute_api
@@ -413,7 +413,7 @@ class ContainersController(base.Controller):
413 413
         container = _get_container(container_id)
414 414
         check_policy_on_container(container.as_dict(), "container:unpause")
415 415
         utils.validate_container_state(container, 'unpause')
416
-        LOG.debug('Calling compute.container_unpause with %s' %
416
+        LOG.debug('Calling compute.container_unpause with %s',
417 417
                   container.uuid)
418 418
         context = pecan.request.context
419 419
         compute_api = pecan.request.compute_api
@@ -436,8 +436,7 @@ class ContainersController(base.Controller):
436 436
             msg = _('Valid stdout, stderr and timestamps values are ''true'', '
437 437
                     '"false", True, False, 0 and 1, yes and no')
438 438
             raise exception.InvalidValue(msg)
439
-        LOG.debug('Calling compute.container_logs with %s' %
440
-                  container.uuid)
439
+        LOG.debug('Calling compute.container_logs with %s', container.uuid)
441 440
         context = pecan.request.context
442 441
         compute_api = pecan.request.compute_api
443 442
         return compute_api.container_logs(context, container, stdout, stderr,
@@ -457,8 +456,9 @@ class ContainersController(base.Controller):
457 456
         except ValueError:
458 457
             msg = _('Valid run values are true, false, 0, 1, yes and no')
459 458
             raise exception.InvalidValue(msg)
460
-        LOG.debug('Calling compute.container_exec with %s command %s'
461
-                  % (container.uuid, kwargs['command']))
459
+        LOG.debug('Calling compute.container_exec with %(uuid)s command '
460
+                  '%(command)s',
461
+                  {'uuid': container.uuid, 'command': kwargs['command']})
462 462
         context = pecan.request.context
463 463
         compute_api = pecan.request.compute_api
464 464
         return compute_api.container_exec(context, container,
@@ -488,9 +488,10 @@ class ContainersController(base.Controller):
488 488
         container = _get_container(container_id)
489 489
         check_policy_on_container(container.as_dict(), "container:kill")
490 490
         utils.validate_container_state(container, 'kill')
491
-        LOG.debug('Calling compute.container_kill with %s signal %s'
492
-                  % (container.uuid,
493
-                     kwargs.get('signal', kwargs.get('signal'))))
491
+        LOG.debug('Calling compute.container_kill with %(uuid)s '
492
+                  'signal %(signal)s',
493
+                  {'uuid': container.uuid,
494
+                   'signal': kwargs.get('signal', kwargs.get('signal'))})
494 495
         context = pecan.request.context
495 496
         compute_api = pecan.request.compute_api
496 497
         compute_api.container_kill(context, container, kwargs.get('signal'))
@@ -502,8 +503,7 @@ class ContainersController(base.Controller):
502 503
         container = _get_container(container_id)
503 504
         check_policy_on_container(container.as_dict(), "container:attach")
504 505
         utils.validate_container_state(container, 'attach')
505
-        LOG.debug('Checking the status for attach with %s' %
506
-                  container.uuid)
506
+        LOG.debug('Checking the status for attach with %s', container.uuid)
507 507
         if container.interactive:
508 508
             context = pecan.request.context
509 509
             compute_api = pecan.request.compute_api
@@ -520,7 +520,7 @@ class ContainersController(base.Controller):
520 520
         container = _get_container(container_id)
521 521
         check_policy_on_container(container.as_dict(), "container:resize")
522 522
         utils.validate_container_state(container, 'resize')
523
-        LOG.debug('Calling tty resize with %s ' % (container.uuid))
523
+        LOG.debug('Calling tty resize with %s ', container.uuid)
524 524
         context = pecan.request.context
525 525
         compute_api = pecan.request.compute_api
526 526
         compute_api.container_resize(context, container, kwargs.get('h', None),
@@ -533,8 +533,7 @@ class ContainersController(base.Controller):
533 533
         container = _get_container(container_id)
534 534
         check_policy_on_container(container.as_dict(), "container:top")
535 535
         utils.validate_container_state(container, 'top')
536
-        LOG.debug('Calling compute.container_top with %s' %
537
-                  container.uuid)
536
+        LOG.debug('Calling compute.container_top with %s', container.uuid)
538 537
         context = pecan.request.context
539 538
         compute_api = pecan.request.compute_api
540 539
         return compute_api.container_top(context, container, ps_args)
@@ -545,8 +544,9 @@ class ContainersController(base.Controller):
545 544
         container = _get_container(container_id)
546 545
         check_policy_on_container(container.as_dict(), "container:get_archive")
547 546
         utils.validate_container_state(container, 'get_archive')
548
-        LOG.debug('Calling compute.container_get_archive with %s path %s'
549
-                  % (container.uuid, kwargs['path']))
547
+        LOG.debug('Calling compute.container_get_archive with %(uuid)s '
548
+                  'path %(path)s',
549
+                  {'uuid': container.uuid, 'path': kwargs['path']})
550 550
         context = pecan.request.context
551 551
         compute_api = pecan.request.compute_api
552 552
         data, stat = compute_api.container_get_archive(
@@ -559,8 +559,9 @@ class ContainersController(base.Controller):
559 559
         container = _get_container(container_id)
560 560
         check_policy_on_container(container.as_dict(), "container:put_archive")
561 561
         utils.validate_container_state(container, 'put_archive')
562
-        LOG.debug('Calling compute.container_put_archive with %s path %s'
563
-                  % (container.uuid, kwargs['path']))
562
+        LOG.debug('Calling compute.container_put_archive with %(uuid)s '
563
+                  'path %(path)s',
564
+                  {'uuid': container.uuid, 'path': kwargs['path']})
564 565
         context = pecan.request.context
565 566
         compute_api = pecan.request.compute_api
566 567
         compute_api.container_put_archive(context, container,
@@ -572,8 +573,7 @@ class ContainersController(base.Controller):
572 573
         container = _get_container(container_id)
573 574
         check_policy_on_container(container.as_dict(), "container:stats")
574 575
         utils.validate_container_state(container, 'stats')
575
-        LOG.debug('Calling compute.container_stats with %s'
576
-                  % (container.uuid))
576
+        LOG.debug('Calling compute.container_stats with %s', container.uuid)
577 577
         context = pecan.request.context
578 578
         compute_api = pecan.request.compute_api
579 579
         return compute_api.container_stats(context, container)
@@ -585,7 +585,7 @@ class ContainersController(base.Controller):
585 585
         container = _get_container(container_id)
586 586
         check_policy_on_container(container.as_dict(), "container:commit")
587 587
         utils.validate_container_state(container, 'commit')
588
-        LOG.debug('Calling compute.container_commit %s ' % (container.uuid))
588
+        LOG.debug('Calling compute.container_commit %s ', container.uuid)
589 589
         context = pecan.request.context
590 590
         compute_api = pecan.request.compute_api
591 591
         pecan.response.status = 202

+ 1
- 2
zun/api/controllers/v1/images.py View File

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

+ 3
- 3
zun/common/exception.py View File

@@ -117,7 +117,7 @@ def wrap_controller_exception(func, func_server_error, func_client_error):
117 117
                 # log the error message with its associated
118 118
                 # correlation id
119 119
                 log_correlation_id = uuidutils.generate_uuid()
120
-                LOG.exception("%(correlation_id)s:%(excp)s" %
120
+                LOG.exception("%(correlation_id)s:%(excp)s",
121 121
                               {'correlation_id': log_correlation_id,
122 122
                                'excp': str(excp)})
123 123
                 # raise a client error with an obfuscated message
@@ -194,8 +194,8 @@ class ZunException(Exception):
194 194
         except KeyError:
195 195
             # kwargs doesn't match a variable in the message
196 196
             # log the issue and the kwargs
197
-            LOG.exception(('Exception in string format operation, '
198
-                           'kwargs: %s') % kwargs)
197
+            LOG.exception('Exception in string format operation, '
198
+                          'kwargs: %s', kwargs)
199 199
             try:
200 200
                 ferr = CONF.fatal_exception_format_errors
201 201
             except cfg.NoSuchOptError:

+ 1
- 1
zun/compute/manager.py View File

@@ -574,7 +574,7 @@ class Manager(object):
574 574
             LOG.error("Error occurred while calling docker commit API: %s",
575 575
                       six.text_type(e))
576 576
             raise
577
-        LOG.debug('Upload image %s to glance' % container_image_id)
577
+        LOG.debug('Upload image %s to glance', container_image_id)
578 578
         self._do_container_image_upload(context, snapshot_image,
579 579
                                         container_image, tag)
580 580
 

+ 6
- 8
zun/container/docker/driver.py View File

@@ -86,12 +86,12 @@ class DockerDriver(driver.ContainerDriver):
86 86
 
87 87
     def inspect_image(self, image):
88 88
         with docker_utils.docker_client() as docker:
89
-            LOG.debug('Inspecting image %s' % image)
89
+            LOG.debug('Inspecting image %s', image)
90 90
             image_dict = docker.inspect_image(image)
91 91
             return image_dict
92 92
 
93 93
     def get_image(self, name):
94
-        LOG.debug('Obtaining image %s' % name)
94
+        LOG.debug('Obtaining image %s', name)
95 95
         with docker_utils.docker_client() as docker:
96 96
             response = docker.get_image(name)
97 97
             return response
@@ -105,8 +105,8 @@ class DockerDriver(driver.ContainerDriver):
105 105
         with docker_utils.docker_client() as docker:
106 106
             name = container.name
107 107
             image = container.image
108
-            LOG.debug('Creating container with image %s name %s'
109
-                      % (image, name))
108
+            LOG.debug('Creating container with image %(image)s name %(name)s',
109
+                      {'image': image, 'name': name})
110 110
 
111 111
             kwargs = {
112 112
                 'name': self.get_container_name(container),
@@ -779,8 +779,7 @@ class NovaDockerDriver(DockerDriver):
779 779
         novaclient = nova.NovaClient(elevated)
780 780
         server_name = self._find_server_by_container_id(sandbox_id)
781 781
         if not server_name:
782
-            LOG.warning("Cannot find server name for sandbox %s" %
783
-                        sandbox_id)
782
+            LOG.warning("Cannot find server name for sandbox %s", sandbox_id)
784 783
             return
785 784
 
786 785
         server_id = novaclient.delete_server(server_name)
@@ -791,8 +790,7 @@ class NovaDockerDriver(DockerDriver):
791 790
         novaclient = nova.NovaClient(elevated)
792 791
         server_name = self._find_server_by_container_id(sandbox_id)
793 792
         if not server_name:
794
-            LOG.warning("Cannot find server name for sandbox %s" %
795
-                        sandbox_id)
793
+            LOG.warning("Cannot find server name for sandbox %s", sandbox_id)
796 794
             return
797 795
         novaclient.stop_server(server_name)
798 796
 

+ 9
- 10
zun/image/docker/driver.py View File

@@ -36,13 +36,13 @@ class DockerDriver(driver.ContainerImageDriver):
36 36
     def _search_image_on_host(self, repo, tag):
37 37
         with docker_utils.docker_client() as docker:
38 38
             image = repo + ":" + tag
39
-            LOG.debug('Inspecting image locally %s' % image)
39
+            LOG.debug('Inspecting image locally %s', image)
40 40
             try:
41 41
                 image_dict = docker.inspect_image(image)
42 42
                 if image_dict:
43 43
                     return {'image': repo, 'path': None}
44 44
             except errors.NotFound:
45
-                LOG.debug('Image %s not found locally' % image)
45
+                LOG.debug('Image %s not found locally', image)
46 46
                 return None
47 47
 
48 48
     def _pull_image(self, repo, tag):
@@ -57,7 +57,7 @@ class DockerDriver(driver.ContainerImageDriver):
57 57
         image = self._search_image_on_host(repo, tag)
58 58
         if not utils.should_pull_image(image_pull_policy, bool(image)):
59 59
             if image:
60
-                LOG.debug('Image  %s present locally' % repo)
60
+                LOG.debug('Image  %s present locally', repo)
61 61
                 return image, image_loaded
62 62
             else:
63 63
                 message = _('Image %s not present with pull policy of Never'
@@ -65,19 +65,18 @@ class DockerDriver(driver.ContainerImageDriver):
65 65
                 raise exception.ImageNotFound(message)
66 66
 
67 67
         try:
68
-            LOG.debug('Pulling image from docker %s,'
69
-                      ' context %s' % (repo, context))
68
+            LOG.debug('Pulling image from docker %(repo)s,'
69
+                      ' context %(context)s',
70
+                      {'repo': repo, 'context': context})
70 71
             self._pull_image(repo, tag)
71 72
             return {'image': repo, 'path': None}, image_loaded
72 73
         except exception.ImageNotFound:
73 74
             with excutils.save_and_reraise_exception():
74
-                LOG.error(
75
-                    'Image %s was not found in docker repo' % repo)
75
+                LOG.error('Image %s was not found in docker repo', repo)
76 76
         except exception.DockerError:
77 77
             with excutils.save_and_reraise_exception():
78
-                LOG.error(
79
-                    'Docker API error occurred during downloading\
80
-                    image %s' % repo)
78
+                LOG.error('Docker API error occurred during downloading '
79
+                          'image %s', repo)
81 80
         except Exception as e:
82 81
             msg = _('Cannot download image from docker: {0}')
83 82
             raise exception.ZunException(msg.format(e))

+ 11
- 12
zun/image/glance/driver.py View File

@@ -36,7 +36,7 @@ class GlanceDriver(driver.ContainerImageDriver):
36 36
         super(GlanceDriver, self).__init__()
37 37
 
38 38
     def _search_image_on_host(self, context, repo):
39
-        LOG.debug('Searching for image %s locally' % repo)
39
+        LOG.debug('Searching for image %s locally', repo)
40 40
         images_directory = CONF.glance.images_directory
41 41
         try:
42 42
             # TODO(mkrai): Change this to search image entry in zun db
@@ -78,7 +78,7 @@ class GlanceDriver(driver.ContainerImageDriver):
78 78
 
79 79
         if not common_utils.should_pull_image(image_pull_policy, bool(image)):
80 80
             if image:
81
-                LOG.debug('Image  %s present locally' % repo)
81
+                LOG.debug('Image  %s present locally', repo)
82 82
                 image_loaded = True
83 83
                 return image, image_loaded
84 84
             else:
@@ -86,15 +86,14 @@ class GlanceDriver(driver.ContainerImageDriver):
86 86
                             ) % repo
87 87
                 raise exception.ImageNotFound(message)
88 88
 
89
-        LOG.debug('Pulling image from glance %s' % repo)
89
+        LOG.debug('Pulling image from glance %s', repo)
90 90
         try:
91 91
             glance = utils.create_glanceclient(context)
92 92
             image_meta = utils.find_image(context, repo)
93
-            LOG.debug('Image %s was found in glance, downloading now...'
94
-                      % repo)
93
+            LOG.debug('Image %s was found in glance, downloading now...', repo)
95 94
             image_chunks = glance.images.data(image_meta.id)
96 95
         except exception.ImageNotFound:
97
-            LOG.error('Image %s was not found in glance' % repo)
96
+            LOG.error('Image %s was not found in glance', repo)
98 97
             raise
99 98
         except Exception as e:
100 99
             msg = _('Cannot download image from glance: {0}')
@@ -109,14 +108,14 @@ class GlanceDriver(driver.ContainerImageDriver):
109 108
         except Exception as e:
110 109
             msg = _('Error occurred while writing image: {0}')
111 110
             raise exception.ZunException(msg.format(e))
112
-        LOG.debug('Image %s was downloaded to path : %s'
113
-                  % (repo, out_path))
111
+        LOG.debug('Image %(repo)s was downloaded to path : %(path)s',
112
+                  {'repo': repo, 'path': out_path})
114 113
         return {'image': repo, 'path': out_path}, image_loaded
115 114
 
116 115
     def search_image(self, context, repo, tag, exact_match):
117 116
         # TODO(mkrai): glance driver does not handle tags
118 117
         #       once metadata is stored in db then handle tags
119
-        LOG.debug('Searching image in glance %s' % repo)
118
+        LOG.debug('Searching image in glance %s', repo)
120 119
         try:
121 120
             # TODO(hongbin): find image by both repo and tag
122 121
             images = utils.find_images(context, repo, exact_match)
@@ -126,7 +125,7 @@ class GlanceDriver(driver.ContainerImageDriver):
126 125
 
127 126
     def create_image(self, context, image_name):
128 127
         """Create an image."""
129
-        LOG.debug('Creating a new image in glance %s' % image_name)
128
+        LOG.debug('Creating a new image in glance %s', image_name)
130 129
         try:
131 130
             img = utils.create_image(context, image_name)
132 131
             return img
@@ -136,7 +135,7 @@ class GlanceDriver(driver.ContainerImageDriver):
136 135
     def update_image(self, context, img_id, disk_format='qcow2',
137 136
                      container_format='docker', tag=None):
138 137
         """Update an image."""
139
-        LOG.debug('Updating an image %s in glance' % img_id)
138
+        LOG.debug('Updating an image %s in glance', img_id)
140 139
         try:
141 140
             if tag is not None:
142 141
                 tags = []
@@ -151,7 +150,7 @@ class GlanceDriver(driver.ContainerImageDriver):
151 150
 
152 151
     def upload_image_data(self, context, img_id, data):
153 152
         """Update an image."""
154
-        LOG.debug('Uploading an image to glance %s' % img_id)
153
+        LOG.debug('Uploading an image to glance %s', img_id)
155 154
         try:
156 155
             img = utils.upload_image_data(context, img_id, data)
157 156
             return img

+ 2
- 2
zun/image/glance/utils.py View File

@@ -35,7 +35,7 @@ def create_glanceclient(context):
35 35
 
36 36
 def find_image(context, image_ident):
37 37
     matches = find_images(context, image_ident, exact_match=True)
38
-    LOG.debug('Found matches %s ' % matches)
38
+    LOG.debug('Found matches %s ', matches)
39 39
     if len(matches) == 0:
40 40
         raise exception.ImageNotFound(image=image_ident)
41 41
     if len(matches) > 1:
@@ -93,7 +93,7 @@ def update_image_tags(context, img_id, tags):
93 93
 
94 94
 def upload_image_data(context, img_id, data):
95 95
     """Upload an image."""
96
-    LOG.debug('Upload image %s ' % img_id)
96
+    LOG.debug('Upload image %s ', img_id)
97 97
     glance = create_glanceclient(context)
98 98
     img = glance.images.upload(img_id, data)
99 99
     return img

+ 1
- 1
zun/network/kuryr_network.py View File

@@ -199,7 +199,7 @@ class KuryrNetwork(network.Network):
199 199
             updated_port = {'security_groups': port['security_groups']}
200 200
             try:
201 201
                 LOG.info("Adding security group %(security_group_ids)s "
202
-                         "to port %(port_id)s" %
202
+                         "to port %(port_id)s",
203 203
                          {'security_group_ids': security_group_ids,
204 204
                           'port_id': port['id']})
205 205
                 self.neutron.update_port(port['id'],

Loading…
Cancel
Save