Files
awx/ansibleworks/main/management/commands/acom_callback_event.py
Chris Church cba55a061a Numerous model-related updates and supporing changes, including:
- Add variables field on Host/Group models and remove separate VariableData model.
- Add data migrations for existing variable data.
- Update views, serializers and tests to keep roughly the same API interface for variable data.
- Add has_active_failures properties on Group/Host models to provide indication of last job status.
- Add job_tags field on JobTemplate/Job models to specify tags to ansible-playbook.
- Add host_config_key field to JobTemplate model for use by empheral hosts.
- Add job_args, job_cwd and job_env fields to Job model to capture more info from running the job.
- Add failed flag on JobHostSummary model.
- Add play/task fields on JobEvent model to capture new context variables from callback.
- Add parent field on JobEvent model to capture hierarchy of job events.
- Add hosts field on JobEvent model to capture all hosts associated with the event (especially useful for parent events in the hierarchy).
- Removed existing Tag model, replace with django-taggit instead.
- Removed existing AuditLog model, replacement TBD.
2013-06-10 17:34:37 -04:00

79 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright (c) 2013 AnsibleWorks, Inc.
# All Rights Reserved.
import json
from optparse import make_option
import os
import sys
from django.core.management.base import NoArgsCommand, CommandError
from django.db import transaction
class Command(NoArgsCommand):
'''
Management command to log callback events from ansible-playbook.
'''
help = 'Ansible Commander Callback Event Capture'
option_list = NoArgsCommand.option_list + (
make_option('-j', '--job', dest='job_id',
type='int', default=0,
help='Job ID (can also be specified using ACOM_JOB_ID '
'environment variable)'),
make_option('-e', '--event', dest='event_type', default=None,
help='Event type'),
make_option('-f', '--file', dest='event_data_file', default=None,
help='JSON-formatted data file containing callback event '
'data (specify "-" to read from stdin)'),
make_option('-d', '--data', dest='event_data_json', default=None,
help='JSON-formatted callback event data'),
)
@transaction.commit_on_success
def handle_noargs(self, **options):
from ansibleworks.main.models import Job, JobEvent
event_type = options.get('event_type', None)
if not event_type:
raise CommandError('No event specified')
if event_type not in [x[0] for x in JobEvent.EVENT_CHOICES]:
raise CommandError('Unsupported event')
event_data_file = options.get('event_data_file', None)
event_data_json = options.get('event_data_json', None)
if event_data_file is None and event_data_json is None:
raise CommandError('Either --file or --data must be specified')
try:
job_id = int(os.getenv('ACOM_JOB_ID', options.get('job_id', 0)))
except ValueError:
raise CommandError('Job ID must be an integer')
if not job_id:
raise CommandError('No Job ID specified')
try:
job = Job.objects.get(id=job_id)
except Job.DoesNotExist:
raise CommandError('Job with ID %d not found' % job_id)
if job.status != 'running':
raise CommandError('Unable to add event except when job is running'
', status is currently %s' % job.status)
try:
if event_data_json is None:
try:
if event_data_file == '-':
event_data_fileobj = sys.stdin
else:
event_data_fileobj = file(event_data_file, 'rb')
event_data = json.load(event_data_fileobj)
except IOError, e:
raise CommandError('Error %r reading from %s' % (e, event_data_file))
else:
event_data = json.loads(event_data_json)
except ValueError:
raise CommandError('Error parsing JSON data')
job.job_events.create(event=event_type, event_data=event_data)
if __name__ == '__main__':
from __init__ import run_command_as_script
command_name = os.path.splitext(os.path.basename(__file__))[0]
run_command_as_script(command_name)