2017-09-10 21:53:49 +00:00
|
|
|
# (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2017-09-08 18:08:31 +00:00
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2017-09-10 21:53:49 +00:00
|
|
|
callback: timer
|
|
|
|
callback_type: aggregate
|
|
|
|
requirements:
|
|
|
|
- whitelist in configuration
|
|
|
|
short_description: Adds time to play stats
|
|
|
|
version_added: "2.0"
|
|
|
|
description:
|
|
|
|
- This callback just adds total play duration to the play stats.
|
|
|
|
'''
|
2015-10-20 01:36:19 +00:00
|
|
|
|
2015-11-03 16:20:13 +00:00
|
|
|
from datetime import datetime
|
2015-06-29 17:26:30 +00:00
|
|
|
|
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
|
|
|
2015-11-03 16:20:13 +00:00
|
|
|
|
2015-06-29 17:26:30 +00:00
|
|
|
class CallbackModule(CallbackBase):
|
|
|
|
"""
|
|
|
|
This callback module tells you how long your plays ran for.
|
|
|
|
"""
|
|
|
|
CALLBACK_VERSION = 2.0
|
|
|
|
CALLBACK_TYPE = 'aggregate'
|
2015-07-11 03:48:12 +00:00
|
|
|
CALLBACK_NAME = 'timer'
|
2015-10-22 12:27:32 +00:00
|
|
|
CALLBACK_NEEDS_WHITELIST = True
|
2015-06-29 17:26:30 +00:00
|
|
|
|
2015-12-01 21:28:16 +00:00
|
|
|
def __init__(self):
|
2015-06-29 17:26:30 +00:00
|
|
|
|
2015-12-01 21:28:16 +00:00
|
|
|
super(CallbackModule, self).__init__()
|
2015-06-29 17:26:30 +00:00
|
|
|
|
timer callback plugin: handle timezone changes better
A playbook that does `timezone name=Australia/Brisbane` on
a host previously in UTC will appear to take 10 hours.
Improve the seconds handling for playbooks that take longer
than one hour.
Improve the hours handling for playbooks that take longer than
one day.
TZ change before:
```
Playbook run took 0 days, 10 hours, 0 minutes, 36055 seconds
```
After:
```
Playbook run took 0 days, 0 hours, 0 minutes, 55 seconds
```
Sleep for 100s more than one hour before:
```
Playbook run took 0 days, 1 hours, 1 minutes, 3641 seconds
```
After:
```
Playbook run took 0 days, 1 hours, 1 minutes, 41 seconds
```
2017-08-03 04:49:56 +00:00
|
|
|
self.start_time = datetime.utcnow()
|
2015-06-29 17:26:30 +00:00
|
|
|
|
2015-11-03 16:20:13 +00:00
|
|
|
def days_hours_minutes_seconds(self, runtime):
|
|
|
|
minutes = (runtime.seconds // 60) % 60
|
timer callback plugin: handle timezone changes better
A playbook that does `timezone name=Australia/Brisbane` on
a host previously in UTC will appear to take 10 hours.
Improve the seconds handling for playbooks that take longer
than one hour.
Improve the hours handling for playbooks that take longer than
one day.
TZ change before:
```
Playbook run took 0 days, 10 hours, 0 minutes, 36055 seconds
```
After:
```
Playbook run took 0 days, 0 hours, 0 minutes, 55 seconds
```
Sleep for 100s more than one hour before:
```
Playbook run took 0 days, 1 hours, 1 minutes, 3641 seconds
```
After:
```
Playbook run took 0 days, 1 hours, 1 minutes, 41 seconds
```
2017-08-03 04:49:56 +00:00
|
|
|
r_seconds = runtime.seconds % 60
|
2015-11-03 16:20:13 +00:00
|
|
|
return runtime.days, runtime.seconds // 3600, minutes, r_seconds
|
2015-06-29 17:26:30 +00:00
|
|
|
|
|
|
|
def playbook_on_stats(self, stats):
|
|
|
|
self.v2_playbook_on_stats(stats)
|
2015-07-08 23:55:23 +00:00
|
|
|
|
2015-06-29 17:26:30 +00:00
|
|
|
def v2_playbook_on_stats(self, stats):
|
timer callback plugin: handle timezone changes better
A playbook that does `timezone name=Australia/Brisbane` on
a host previously in UTC will appear to take 10 hours.
Improve the seconds handling for playbooks that take longer
than one hour.
Improve the hours handling for playbooks that take longer than
one day.
TZ change before:
```
Playbook run took 0 days, 10 hours, 0 minutes, 36055 seconds
```
After:
```
Playbook run took 0 days, 0 hours, 0 minutes, 55 seconds
```
Sleep for 100s more than one hour before:
```
Playbook run took 0 days, 1 hours, 1 minutes, 3641 seconds
```
After:
```
Playbook run took 0 days, 1 hours, 1 minutes, 41 seconds
```
2017-08-03 04:49:56 +00:00
|
|
|
end_time = datetime.utcnow()
|
2015-11-03 16:20:13 +00:00
|
|
|
runtime = end_time - self.start_time
|
|
|
|
self._display.display("Playbook run took %s days, %s hours, %s minutes, %s seconds" % (self.days_hours_minutes_seconds(runtime)))
|