Merge lp:~gnuoy/charm-helpers/1422386 into lp:charm-helpers

Proposed by Liam Young on 2015-02-16
Status: Merged
Merged at revision: 319
Proposed branch: lp:~gnuoy/charm-helpers/1422386
Merge into: lp:charm-helpers
Diff against target: 140 lines (+122/-2)
1 file modified
charmhelpers/contrib/amulet/utils.py (+122/-2)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/1422386
Reviewer Review Type Date Requested Status
James Page 2015-02-16 Approve on 2015-02-19
Review via email: mp+249837@code.launchpad.net
To post a comment you must log in.
lp:~gnuoy/charm-helpers/1422386 updated on 2015-02-19
313. By Liam Young on 2015-02-18

Add some logging

314. By Liam Young on 2015-02-18

Merge trunk in

315. By Liam Young on 2015-02-18

Up sleep time in retry loop to avoid blocking hook execution

316. By Liam Young on 2015-02-18

Improve logging

317. By Liam Young on 2015-02-18

Fix typo in log message

318. By Liam Young on 2015-02-18

Fix more logging message

319. By Liam Young on 2015-02-19

Add functions for checking service and config changes after an arbitraty time

320. By Liam Young on 2015-02-19

Revert service_restarted as the new functions replace it

321. By Liam Young on 2015-02-19

Add usage info to new amulet test function (and fix lint)

322. By Liam Young on 2015-02-19

Merge trunk in

James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/amulet/utils.py'
2--- charmhelpers/contrib/amulet/utils.py 2015-01-22 06:06:03 +0000
3+++ charmhelpers/contrib/amulet/utils.py 2015-02-19 08:43:58 +0000
4@@ -169,8 +169,13 @@
5 cmd = 'pgrep -o -f {}'.format(service)
6 else:
7 cmd = 'pgrep -o {}'.format(service)
8- proc_dir = '/proc/{}'.format(sentry_unit.run(cmd)[0].strip())
9- return self._get_dir_mtime(sentry_unit, proc_dir)
10+ cmd = cmd + ' | grep -v pgrep || exit 0'
11+ cmd_out = sentry_unit.run(cmd)
12+ self.log.debug('CMDout: ' + str(cmd_out))
13+ if cmd_out[0]:
14+ self.log.debug('Pid for %s %s' % (service, str(cmd_out[0])))
15+ proc_dir = '/proc/{}'.format(cmd_out[0].strip())
16+ return self._get_dir_mtime(sentry_unit, proc_dir)
17
18 def service_restarted(self, sentry_unit, service, filename,
19 pgrep_full=False, sleep_time=20):
20@@ -187,6 +192,121 @@
21 else:
22 return False
23
24+ def service_restarted_since(self, sentry_unit, mtime, service,
25+ pgrep_full=False, sleep_time=20,
26+ retry_count=2):
27+ """Check if service was been started after a given time.
28+
29+ Args:
30+ sentry_unit (sentry): The sentry unit to check for the service on
31+ mtime (float): The epoch time to check against
32+ service (string): service name to look for in process table
33+ pgrep_full (boolean): Use full command line search mode with pgrep
34+ sleep_time (int): Seconds to sleep before looking for process
35+ retry_count (int): If service is not found, how many times to retry
36+
37+ Returns:
38+ bool: True if service found and its start time it newer than mtime,
39+ False if service is older than mtime or if service was
40+ not found.
41+ """
42+ self.log.debug('Checking %s restarted since %s' % (service, mtime))
43+ time.sleep(sleep_time)
44+ proc_start_time = self._get_proc_start_time(sentry_unit, service,
45+ pgrep_full)
46+ while retry_count > 0 and not proc_start_time:
47+ self.log.debug('No pid file found for service %s, will retry %i '
48+ 'more times' % (service, retry_count))
49+ time.sleep(30)
50+ proc_start_time = self._get_proc_start_time(sentry_unit, service,
51+ pgrep_full)
52+ retry_count = retry_count - 1
53+
54+ if not proc_start_time:
55+ self.log.warn('No proc start time found, assuming service did '
56+ 'not start')
57+ return False
58+ if proc_start_time >= mtime:
59+ self.log.debug('proc start time is newer than provided mtime'
60+ '(%s >= %s)' % (proc_start_time, mtime))
61+ return True
62+ else:
63+ self.log.warn('proc start time (%s) is older than provided mtime '
64+ '(%s), service did not restart' % (proc_start_time,
65+ mtime))
66+ return False
67+
68+ def config_updated_since(self, sentry_unit, filename, mtime,
69+ sleep_time=20):
70+ """Check if file was modified after a given time.
71+
72+ Args:
73+ sentry_unit (sentry): The sentry unit to check the file mtime on
74+ filename (string): The file to check mtime of
75+ mtime (float): The epoch time to check against
76+ sleep_time (int): Seconds to sleep before looking for process
77+
78+ Returns:
79+ bool: True if file was modified more recently than mtime, False if
80+ file was modified before mtime,
81+ """
82+ self.log.debug('Checking %s updated since %s' % (filename, mtime))
83+ time.sleep(sleep_time)
84+ file_mtime = self._get_file_mtime(sentry_unit, filename)
85+ if file_mtime >= mtime:
86+ self.log.debug('File mtime is newer than provided mtime '
87+ '(%s >= %s)' % (file_mtime, mtime))
88+ return True
89+ else:
90+ self.log.warn('File mtime %s is older than provided mtime %s'
91+ % (file_mtime, mtime))
92+ return False
93+
94+ def validate_service_config_changed(self, sentry_unit, mtime, service,
95+ filename, pgrep_full=False,
96+ sleep_time=20, retry_count=2):
97+ """Check service and file were updated after mtime
98+
99+ Args:
100+ sentry_unit (sentry): The sentry unit to check for the service on
101+ mtime (float): The epoch time to check against
102+ service (string): service name to look for in process table
103+ filename (string): The file to check mtime of
104+ pgrep_full (boolean): Use full command line search mode with pgrep
105+ sleep_time (int): Seconds to sleep before looking for process
106+ retry_count (int): If service is not found, how many times to retry
107+
108+ Typical Usage:
109+ u = OpenStackAmuletUtils(ERROR)
110+ ...
111+ mtime = u.get_sentry_time(self.cinder_sentry)
112+ self.d.configure('cinder', {'verbose': 'True', 'debug': 'True'})
113+ if not u.validate_service_config_changed(self.cinder_sentry,
114+ mtime,
115+ 'cinder-api',
116+ '/etc/cinder/cinder.conf')
117+ amulet.raise_status(amulet.FAIL, msg='update failed')
118+ Returns:
119+ bool: True if both service and file where updated/restarted after
120+ mtime, False if service is older than mtime or if service was
121+ not found or if filename was modified before mtime.
122+ """
123+ self.log.debug('Checking %s restarted since %s' % (service, mtime))
124+ time.sleep(sleep_time)
125+ service_restart = self.service_restarted_since(sentry_unit, mtime,
126+ service,
127+ pgrep_full=pgrep_full,
128+ sleep_time=0,
129+ retry_count=retry_count)
130+ config_update = self.config_updated_since(sentry_unit, filename, mtime,
131+ sleep_time=0)
132+ return service_restart and config_update
133+
134+ def get_sentry_time(self, sentry_unit):
135+ """Return current epoch time on a sentry"""
136+ cmd = "date +'%s'"
137+ return float(sentry_unit.run(cmd)[0])
138+
139 def relation_error(self, name, data):
140 return 'unexpected relation data in {} - {}'.format(name, data)
141

Subscribers

People subscribed via source and target branches