Skip to content

Commit 079bc1d

Browse files
authored
fix: remove useless code from client collection (#4438)
- this is a follow up of #4250 Signed-off-by: Xiangce Liu <[email protected]>
1 parent df078fc commit 079bc1d

File tree

6 files changed

+152
-168
lines changed

6 files changed

+152
-168
lines changed

insights/client/client.py

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,31 @@
1010
import six
1111
from distutils.version import LooseVersion
1212

13-
from .utilities import (generate_machine_id,
14-
write_to_disk,
15-
write_registered_file,
16-
write_unregistered_file,
17-
delete_cache_files,
18-
determine_hostname,
19-
get_version_info)
13+
from .utilities import (
14+
generate_machine_id,
15+
write_to_disk,
16+
write_registered_file,
17+
write_unregistered_file,
18+
delete_cache_files,
19+
determine_hostname,
20+
get_version_info,
21+
)
2022
from .collection_rules import InsightsUploadConf
2123
from .core_collector import CoreCollector
2224
from .connection import InsightsConnection
2325
from .support import registration_check
2426
from .constants import InsightsConstants as constants
2527

2628
NETWORK = constants.custom_network_log_level
27-
LOG_FORMAT = ("%(asctime)s %(levelname)8s %(name)s:%(lineno)s %(message)s")
29+
LOG_FORMAT = "%(asctime)s %(levelname)8s %(name)s:%(lineno)s %(message)s"
2830
logger = logging.getLogger(__name__)
2931

3032

3133
class RotatingFileHandlerWithUMask(logging.handlers.RotatingFileHandler, object):
32-
""" logging.handlers.RotatingFileHandler subclass with a modified
33-
file permission mask.
34+
"""logging.handlers.RotatingFileHandler subclass with a modified
35+
file permission mask.
3436
"""
37+
3538
def __init__(self, umask, *args, **kwargs):
3639
self._umask = umask
3740
super(RotatingFileHandlerWithUMask, self).__init__(*args, **kwargs)
@@ -49,9 +52,10 @@ def _open(self):
4952

5053

5154
class FileHandlerWithUMask(logging.FileHandler, object):
52-
""" logging.FileHandler subclass with a modified
53-
file permission mask.
55+
"""logging.FileHandler subclass with a modified
56+
file permission mask.
5457
"""
58+
5559
def __init__(self, umask, *args, **kwargs):
5660
self._umask = umask
5761
super(FileHandlerWithUMask, self).__init__(*args, **kwargs)
@@ -89,7 +93,9 @@ def get_file_handler(config):
8993
# ensure the legacy rotating file handler is only used in older client versions
9094
# or if there is a problem retrieving the rpm version.
9195
rpm_version = get_version_info()['client_version']
92-
if not rpm_version or (LooseVersion(rpm_version) < LooseVersion(constants.rpm_version_before_logrotate)):
96+
if not rpm_version or (
97+
LooseVersion(rpm_version) < LooseVersion(constants.rpm_version_before_logrotate)
98+
):
9399
file_handler = RotatingFileHandlerWithUMask(0o077, log_file, backupCount=3)
94100
else:
95101
file_handler = FileHandlerWithUMask(0o077, log_file)
@@ -153,7 +159,9 @@ def register(config, pconn):
153159
authmethod = config.authmethod
154160
auto_config = config.auto_config
155161
if not username and not password and not auto_config and authmethod == 'BASIC':
156-
logger.debug('Username and password must be defined in configuration file with BASIC authentication method.')
162+
logger.debug(
163+
'Username and password must be defined in configuration file with BASIC authentication method.'
164+
)
157165
return False
158166
return pconn.register()
159167

@@ -174,8 +182,10 @@ def _legacy_handle_registration(config, pconn):
174182
machine_id_present = isfile(constants.machine_id_file)
175183

176184
if machine_id_present and check['status'] is False:
177-
logger.info("Machine-id found, insights-client can not be registered."
178-
" Please, unregister insights-client first: `insights-client --unregister`")
185+
logger.info(
186+
"Machine-id found, insights-client can not be registered."
187+
" Please, unregister insights-client first: `insights-client --unregister`"
188+
)
179189
return False
180190

181191
logger.debug('Machine-id: %s', generate_machine_id())
@@ -204,11 +214,11 @@ def _legacy_handle_registration(config, pconn):
204214
if config.display_name is None and config.group is None:
205215
logger.info('Successfully registered host %s', hostname)
206216
elif config.display_name is None:
207-
logger.info('Successfully registered host %s in group %s',
208-
hostname, group)
217+
logger.info('Successfully registered host %s in group %s', hostname, group)
209218
else:
210-
logger.info('Successfully registered host %s as %s in group %s',
211-
hostname, display_name, group)
219+
logger.info(
220+
'Successfully registered host %s as %s in group %s', hostname, display_name, group
221+
)
212222
if message:
213223
logger.info(message)
214224
write_registered_file()
@@ -219,13 +229,17 @@ def _legacy_handle_registration(config, pconn):
219229
# print messaging and exit
220230
if check['unreg_date']:
221231
# registered and then unregistered
222-
logger.info('This machine has been unregistered. '
223-
'Use --register if you would like to '
224-
're-register this machine.')
232+
logger.info(
233+
'This machine has been unregistered. '
234+
'Use --register if you would like to '
235+
're-register this machine.'
236+
)
225237
else:
226238
# not yet registered
227-
logger.info('This machine has not yet been registered. '
228-
'Use --register to register this machine.')
239+
logger.info(
240+
'This machine has not yet been registered. '
241+
'Use --register to register this machine.'
242+
)
229243
return False
230244

231245

@@ -239,11 +253,11 @@ def handle_registration(config, pconn):
239253

240254
def get_registration_status(config, pconn):
241255
'''
242-
Handle the registration process
243-
Returns:
244-
True - machine is registered
245-
False - machine is unregistered
246-
None - could not reach the API
256+
Handle the registration process
257+
Returns:
258+
True - machine is registered
259+
False - machine is unregistered
260+
None - could not reach the API
247261
'''
248262
return registration_check(pconn)
249263

@@ -258,7 +272,7 @@ def __cleanup_local_files():
258272
# -LEGACY-
259273
def _legacy_handle_unregistration(config, pconn):
260274
"""
261-
returns (bool): True success, False failure
275+
returns (bool): True success, False failure
262276
"""
263277

264278
check = get_registration_status(config, pconn)
@@ -334,7 +348,7 @@ def collect(config):
334348
'Starting to collect Insights data for %s' % determine_hostname(config.display_name)
335349
)
336350

337-
dc.run_collection(rm_conf, get_branch_info(config), pc.create_report())
351+
dc.run_collection(rm_conf)
338352

339353
return dc.done()
340354

@@ -370,20 +384,25 @@ def _legacy_upload(config, pconn, tar_file, content_type, collection_duration=No
370384
msg_name = determine_hostname(config.display_name)
371385
account_number = config.account_number
372386
if account_number:
373-
logger.info("Successfully uploaded report from %s to account %s.",
374-
msg_name, account_number)
387+
logger.info(
388+
"Successfully uploaded report from %s to account %s.", msg_name, account_number
389+
)
375390
else:
376391
logger.info("Successfully uploaded report for %s.", msg_name)
377392
if config.register:
378393
# direct to console after register + upload
379-
logger.info('View the Red Hat Insights console at https://console.redhat.com/insights/')
394+
logger.info(
395+
'View the Red Hat Insights console at https://console.redhat.com/insights/'
396+
)
380397
break
381398

382399
elif upload.status_code in (412, 413):
383400
pconn.handle_fail_rcs(upload)
384401
raise RuntimeError('Upload failed.')
385402
else:
386-
display_upload_error_and_retry(config, tries, "%s: %s" % (upload.status_code, upload.reason))
403+
display_upload_error_and_retry(
404+
config, tries, "%s: %s" % (upload.status_code, upload.reason)
405+
)
387406
return api_response
388407

389408

@@ -422,11 +441,11 @@ def upload(config, pconn, tar_file, content_type, collection_duration=None):
422441

423442

424443
def display_upload_error_and_retry(config, tries, error_message):
425-
logger.error("Upload attempt %d of %d failed! Reason: %s",
426-
tries + 1, config.retries, error_message)
444+
logger.error(
445+
"Upload attempt %d of %d failed! Reason: %s", tries + 1, config.retries, error_message
446+
)
427447
if tries + 1 < config.retries:
428-
logger.info("Waiting %d seconds then retrying",
429-
constants.sleep_time)
448+
logger.info("Waiting %d seconds then retrying", constants.sleep_time)
430449
time.sleep(constants.sleep_time)
431450
else:
432451
logger.error("All attempts to upload have failed!")

0 commit comments

Comments
 (0)