269
269
"""Get last modification time of directory."""
270
270
return sentry_unit.directory_stat(directory)['mtime']
272
def _get_proc_start_time(self, sentry_unit, service, pgrep_full=False):
273
"""Get process' start time.
275
Determine start time of the process based on the last modification
276
time of the /proc/pid directory. If pgrep_full is True, the process
277
name is matched against the full command line.
272
def _get_proc_start_time(self, sentry_unit, service, pgrep_full=None):
273
"""Get start time of a process based on the last modification time
274
of the /proc/pid directory.
280
cmd = 'pgrep -o -f {}'.format(service)
282
cmd = 'pgrep -o {}'.format(service)
283
cmd = cmd + ' | grep -v pgrep || exit 0'
284
cmd_out = sentry_unit.run(cmd)
285
self.log.debug('CMDout: ' + str(cmd_out))
287
self.log.debug('Pid for %s %s' % (service, str(cmd_out[0])))
288
proc_dir = '/proc/{}'.format(cmd_out[0].strip())
289
return self._get_dir_mtime(sentry_unit, proc_dir)
276
if pgrep_full is True or pgrep_full is False:
277
# /!\ DEPRECATION WARNING (beisner):
278
# No longer implemented, as pidof is now used instead of pgrep.
279
# https://bugs.launchpad.net/charm-helpers/+bug/1474030
280
self.log.warn('/!\\ DEPRECATION WARNING: pgrep_full bool is no '
281
'longer implemented re: lp 1474030.')
283
pid_list = self.get_process_id_list(sentry_unit, service)
285
proc_dir = '/proc/{}'.format(pid)
286
self.log.debug('Pid for {} on {}: {}'.format(
287
service, sentry_unit.info['unit_name'], pid))
289
return self._get_dir_mtime(sentry_unit, proc_dir)
291
291
def service_restarted(self, sentry_unit, service, filename,
292
pgrep_full=False, sleep_time=20):
292
pgrep_full=None, sleep_time=20):
293
293
"""Check if service was restarted.
295
295
Compare a service's start time vs a file's last modification time
299
299
# /!\ DEPRECATION WARNING (beisner):
300
300
# This is prone to races in that no before-time is known.
301
301
# Use validate_service_config_changed instead.
303
# NOTE(beisner) pgrep_full is no longer implemented, as pidof is now
304
# used instead of pgrep. pgrep_full is still passed through to ensure
305
# deprecation WARNS. lp1474030
302
307
self.log.warn('/!\\ DEPRECATION WARNING: use '
303
308
'validate_service_config_changed instead of '
304
309
'service_restarted due to known races.')
313
318
def service_restarted_since(self, sentry_unit, mtime, service,
314
pgrep_full=False, sleep_time=20,
319
pgrep_full=None, sleep_time=20,
315
320
retry_count=2, retry_sleep_time=30):
316
321
"""Check if service was been started after a given time.
319
324
sentry_unit (sentry): The sentry unit to check for the service on
320
325
mtime (float): The epoch time to check against
321
326
service (string): service name to look for in process table
322
pgrep_full (boolean): Use full command line search mode with pgrep
327
pgrep_full: No longer implemented, passed for WARNs
323
328
sleep_time (int): Seconds to sleep before looking for process
324
329
retry_count (int): If service is not found, how many times to retry
328
333
False if service is older than mtime or if service was
336
# NOTE(beisner) pgrep_full is no longer implemented, as pidof is now
337
# used instead of pgrep. pgrep_full is still passed through to ensure
338
# deprecation WARNS. lp1474030
331
340
unit_name = sentry_unit.info['unit_name']
332
self.log.debug('Checking %s restarted since %s on '
341
self.log.debug('Checking that %s service restarted since %s on '
333
342
'%s' % (service, mtime, unit_name))
334
343
time.sleep(sleep_time)
335
344
proc_start_time = None
378
387
bool: True if file was modified more recently than mtime, False if
379
388
file was modified before mtime,
381
self.log.debug('Checking %s updated since %s' % (filename, mtime))
390
self.log.debug('Checking that %s file updated since '
391
'%s' % (filename, mtime))
392
unit_name = sentry_unit.info['unit_name']
382
393
time.sleep(sleep_time)
383
394
file_mtime = self._get_file_mtime(sentry_unit, filename)
384
395
if file_mtime >= mtime:
385
396
self.log.debug('File mtime is newer than provided mtime '
386
'(%s >= %s)' % (file_mtime, mtime))
397
'(%s >= %s) on %s (OK)' % (file_mtime, mtime,
389
401
self.log.warn('File mtime %s is older than provided mtime %s'
393
405
def validate_service_config_changed(self, sentry_unit, mtime, service,
394
filename, pgrep_full=False,
406
filename, pgrep_full=None,
395
407
sleep_time=20, retry_count=2,
396
408
retry_sleep_time=30):
397
409
"""Check service and file were updated after mtime
401
413
mtime (float): The epoch time to check against
402
414
service (string): service name to look for in process table
403
415
filename (string): The file to check mtime of
404
pgrep_full (boolean): Use full command line search mode with pgrep
416
pgrep_full: No longer implemented, passed for WARNs
405
417
sleep_time (int): Initial sleep in seconds to pass to test helpers
406
418
retry_count (int): If service is not found, how many times to retry
407
419
retry_sleep_time (int): Time in seconds to wait between retries
421
433
mtime, False if service is older than mtime or if service was
422
434
not found or if filename was modified before mtime.
437
# NOTE(beisner) pgrep_full is no longer implemented, as pidof is now
438
# used instead of pgrep. pgrep_full is still passed through to ensure
439
# deprecation WARNS. lp1474030
424
441
service_restart = self.service_restarted_since(
425
442
sentry_unit, mtime,