mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
Updated callback module to delegate to acom_callback_event management command.
This commit is contained in:
56
lib/main/management/commands/acom_callback_event.py
Normal file → Executable file
56
lib/main/management/commands/acom_callback_event.py
Normal file → Executable file
@@ -22,23 +22,40 @@ import json
|
||||
from optparse import make_option
|
||||
import os
|
||||
import sys
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.base import NoArgsCommand, CommandError
|
||||
|
||||
class Command(BaseCommmand):
|
||||
class Command(NoArgsCommand):
|
||||
'''
|
||||
Management command to log callback events from ansible-playbook.
|
||||
'''
|
||||
|
||||
help = 'Ansible Commander Callback Event Capture'
|
||||
|
||||
option_list = BaseCommmand.option_list + (
|
||||
option_list = NoArgsCommand.option_list + (
|
||||
make_option('-i', '--launch-job-status', dest='launch_job_status_id',
|
||||
type='int', default=0,
|
||||
help='Inventory ID (can also be specified using '
|
||||
'ACOM_INVENTORY_ID environment variable)'),
|
||||
#make_option('--indent', dest='indent', type='int', default=None,
|
||||
# help='Indentation level for pretty printing output'),
|
||||
help='Launch job status ID (can also be specified using '
|
||||
'ACOM_LAUNCH_JOB_STATUS_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'),
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
from lib.main.models import LaunchJobStatus
|
||||
def handle_noargs(self, **options):
|
||||
from lib.main.models import LaunchJobStatus, LaunchJobStatusEvent
|
||||
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 LaunchJobStatusEvent.EVENT_TYPES]:
|
||||
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:
|
||||
launch_job_status_id = int(os.getenv('ACOM_LAUNCH_JOB_STATUS_ID',
|
||||
options.get('launch_job_status_id', 0)))
|
||||
@@ -48,9 +65,26 @@ class Command(BaseCommmand):
|
||||
raise CommandError('No launch job status ID specified')
|
||||
try:
|
||||
launch_job_status = LaunchJobStatus.objects.get(id=launch_job_status_id)
|
||||
except Inventory.DoesNotExist:
|
||||
except LaunchJobStatus.DoesNotExist:
|
||||
raise CommandError('Launch job status with ID %d not found' % launch_job_status_id)
|
||||
# FIXME: Do stuff here.
|
||||
if launch_job_status.status != 'running':
|
||||
raise CommandError('Unable to add event except when launch job is running')
|
||||
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')
|
||||
launch_job_status.launch_job_status_events.create(event=event_type,
|
||||
event_data=event_data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
from __init__ import run_command_as_script
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
import json
|
||||
from optparse import make_option
|
||||
import os
|
||||
import sys
|
||||
from django.core.management.base import NoArgsCommand, CommandError
|
||||
|
||||
class Command(NoArgsCommand):
|
||||
|
||||
Reference in New Issue
Block a user