update to use time function

This commit is contained in:
sean-m-sullivan 2020-08-26 23:30:18 -05:00
parent 243c2cfe15
commit 0c18587851
4 changed files with 61 additions and 63 deletions

View File

@ -7,6 +7,7 @@ from ansible.module_utils.six import PY2
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.six.moves.http_cookiejar import CookieJar
import time
import re
from json import loads, dumps
@ -588,3 +589,26 @@ class TowerAPIModule(TowerModule):
return False
else:
return True
def wait_on_url(self, object_name=None, object_type=None, url=None, timeout=None, interval=None):
# Grab our start time to compare against for the timeout
start = time.time()
result = self.get_endpoint(url)
while not result['json']['finished']:
# If we are past our time out fail with a message
if timeout and timeout < time.time() - start:
self.json_output['msg'] = 'Monitoring of {0} "{1}" aborted due to timeout'.format(object_type, object_name)
self.fail_json(**self.json_output)
# Put the process to sleep for our interval
time.sleep(interval)
result = self.get_endpoint(url)
self.json_output['status'] = result['json']['status']
# If the job has failed, we want to raise a task failure for that so we get a non-zero response.
if result['json']['failed']:
self.json_output['msg'] = 'The {0} "{1}" failed'.format(object_type, object_name)
self.fail_json(**self.json_output)
return result

View File

@ -143,6 +143,9 @@ def main():
verbosity=dict(type='int', choices=[0, 1, 2, 3, 4, 5]),
diff_mode=dict(type='bool'),
credential_passwords=dict(type='dict'),
wait=dict(default=False, type='bool'),
interval=dict(default=1.0, type='float'),
timeout=dict(default=None, type='int'),
)
# Create a module for ourselves
@ -162,6 +165,9 @@ def main():
optional_args['verbosity'] = module.params.get('verbosity')
optional_args['diff_mode'] = module.params.get('diff_mode')
optional_args['credential_passwords'] = module.params.get('credential_passwords')
wait = module.params.get('wait')
interval = module.params.get('interval')
timeout = module.params.get('timeout')
# Create a datastructure to pass into our job launch
post_data = {}
@ -216,12 +222,26 @@ def main():
if results['status_code'] != 201:
module.fail_json(msg="Failed to launch job, see response for details", **{'response': results})
if not wait:
module.exit_json(**{
'changed': True,
'id': results['json']['id'],
'status': results['json']['status'],
})
# Invoke wait function
results = module.wait_on_url(
object_name=name,
object_type='job',
url=results['json']['url'],
timeout=timeout, interval=interval
)
module.exit_json(**{
'changed': True,
'id': results['json']['id'],
'status': results['json']['status'],
})
if __name__ == '__main__':
main()

View File

@ -55,7 +55,7 @@ EXAMPLES = '''
- name: Launch a job
tower_job_launch:
job_template: "My Job Template"
register: job
register: job
- name: Wait for job max 120s
tower_job_wait:
@ -93,20 +93,6 @@ status:
from ..module_utils.tower_api import TowerAPIModule
import time
def check_job(module, job_url):
response = module.get_endpoint(job_url)
if response['status_code'] != 200:
module.fail_json(msg="Unable to read job from Tower {0}: {1}".format(response['status_code'], module.extract_errors_from_response(response)))
# Since we were successful, extract the fields we want to return
for k in ('id', 'status', 'elapsed', 'started', 'finished'):
module.json_output[k] = response['json'].get(k)
# And finally return the payload
return response['json']
def main():
@ -153,31 +139,13 @@ def main():
if job is None:
module.fail_json(msg='Unable to wait on job {0}; that ID does not exist in Tower.'.format(job_id))
job_url = job['url']
# Grab our start time to compare against for the timeout
start = time.time()
# Get the initial job status from Tower, this will exit if there are any issues with the HTTP call
result = check_job(module, job_url)
# Loop while the job is not yet completed
while not result['finished']:
# If we are past our time out fail with a message
if timeout and timeout < time.time() - start:
module.json_output['msg'] = "Monitoring aborted due to timeout"
module.fail_json(**module.json_output)
# Put the process to sleep for our interval
time.sleep(interval)
# Check the job again
result = check_job(module, job_url)
# If the job has failed, we want to raise an Exception for that so we get a non-zero response.
if result['failed']:
module.json_output['msg'] = 'Job with id {0} failed'.format(job_id)
module.fail_json(**module.json_output)
# Invoke wait function
result = module.wait_on_url(
object_name=job_id,
object_type='job',
url=job['url'],
timeout=timeout, interval=interval
)
module.exit_json(**module.json_output)

View File

@ -93,7 +93,6 @@ EXAMPLES = '''
from ..module_utils.tower_api import TowerAPIModule
import json
import time
def main():
@ -177,27 +176,14 @@ def main():
if not wait:
module.exit_json(**module.json_output)
# Grab our start time to compare against for the timeout
start = time.time()
job_url = result['json']['url']
while not result['json']['finished']:
# If we are past our time out fail with a message
if timeout and timeout < time.time() - start:
module.json_output['msg'] = "Monitoring aborted due to timeout"
module.fail_json(**module.json_output)
# Put the process to sleep for our interval
time.sleep(interval)
result = module.get_endpoint(job_url)
module.json_output['status'] = result['json']['status']
# If the job has failed, we want to raise a task failure for that so we get a non-zero response.
if result['json']['failed']:
module.json_output['msg'] = 'The workflow "{0}" failed'.format(name)
module.fail_json(**module.json_output)
# Invoke wait function
module.wait_on_url(
object_name=name,
object_type='workflow_job',
url=result['json']['url'],
timeout=timeout, interval=interval
)
module.exit_json(**module.json_output)