2013-04-02 00:04:27 +04:00
# Copyright (c) 2013 AnsibleWorks, Inc.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# This file is part of Ansible Commander.
#
2013-03-24 02:43:11 +04:00
# Ansible Commander is free software: you can redistribute it and/or modify
2013-04-09 09:05:55 +04:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# Ansible Commander is distributed in the hope that it will be useful,
2013-03-24 02:43:11 +04:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2013-04-09 09:05:55 +04:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible Commander. If not, see <http://www.gnu.org/licenses/>.
2013-03-24 02:43:11 +04:00
2013-03-23 02:55:10 +04:00
import os
import subprocess
2013-04-05 00:53:20 +04:00
import traceback
2013-03-15 00:11:14 +04:00
from celery import task
2013-04-01 01:25:18 +04:00
from django . conf import settings
2013-03-15 00:11:14 +04:00
from lib . main . models import *
2013-04-18 02:59:21 +04:00
__all__ = [ ' run_job ' ]
@task ( name = ' run_job ' )
def run_job ( job_pk ) :
job = Job . objects . get ( pk = job_pk )
job . status = ' running '
job . save ( update_fields = [ ' status ' ] )
2013-04-05 00:53:20 +04:00
try :
status , stdout , stderr , tb = ' error ' , ' ' , ' ' , ' '
plugin_dir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' ,
' plugins ' , ' callback ' ) )
inventory_script = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) ,
' management ' , ' commands ' ,
' acom_inventory.py ' ) )
callback_script = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) ,
' management ' , ' commands ' ,
' acom_callback_event.py ' ) )
env = dict ( os . environ . items ( ) )
2013-04-09 08:55:25 +04:00
# question: when running over CLI, generate a random ID or grab next, etc?
2013-04-18 02:59:21 +04:00
# answer: TBD
env [ ' ACOM_JOB_ID ' ] = str ( job . pk )
env [ ' ACOM_INVENTORY_ID ' ] = str ( job . inventory . pk )
2013-04-05 00:53:20 +04:00
env [ ' ANSIBLE_CALLBACK_PLUGINS ' ] = plugin_dir
env [ ' ACOM_CALLBACK_EVENT_SCRIPT ' ] = callback_script
2013-04-02 19:23:58 +04:00
2013-04-05 00:53:20 +04:00
if hasattr ( settings , ' ANSIBLE_TRANSPORT ' ) :
env [ ' ANSIBLE_TRANSPORT ' ] = getattr ( settings , ' ANSIBLE_TRANSPORT ' )
2013-04-19 23:40:08 +04:00
cwd = job . project . local_path
2013-04-05 00:53:20 +04:00
cmdline = [ ' ansible-playbook ' , ' -i ' , inventory_script ]
2013-04-18 02:59:21 +04:00
if job . job_type == ' check ' :
2013-04-05 00:53:20 +04:00
cmdline . append ( ' --check ' )
2013-04-19 23:40:08 +04:00
cmdline . append ( job . playbook ) # relative path to project.local_path
2013-04-02 21:11:07 +04:00
2013-04-05 00:53:20 +04:00
# FIXME: How to cancel/interrupt job? (not that important for now)
proc = subprocess . Popen ( cmdline , stdout = subprocess . PIPE ,
2013-04-19 23:40:08 +04:00
stderr = subprocess . PIPE , cwd = cwd , env = env )
2013-04-05 00:53:20 +04:00
stdout , stderr = proc . communicate ( )
status = ' successful ' if proc . returncode == 0 else ' failed '
except Exception :
tb = traceback . format_exc ( )
2013-04-02 19:23:58 +04:00
2013-04-01 01:25:18 +04:00
# Reload from database before updating/saving.
2013-04-18 02:59:21 +04:00
job = Job . objects . get ( pk = job_pk )
job . status = status
job . result_stdout = stdout
job . result_stderr = stderr
job . result_traceback = tb
job . save ( update_fields = [ ' status ' , ' result_stdout ' , ' result_stderr ' ,
' result_traceback ' ] )