2013-04-02 00:04:27 +04:00
# Copyright (c) 2013 AnsibleWorks, Inc.
2013-03-24 02:43:11 +04:00
#
# This file is part of Ansible Commander
#
# Ansible Commander is free software: you can redistribute it and/or modify
2013-04-02 00:04:27 +04:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2013-03-24 02:43:11 +04:00
#
2013-04-02 00:04:27 +04:00
# This program 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2013-04-02 00:04:27 +04:00
# GNU Affero General Public License for more details.
2013-03-24 02:43:11 +04:00
#
2013-04-02 00:04:27 +04:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. 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-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 *
@task ( name = ' run_launch_job ' )
2013-03-29 09:02:07 +04:00
def run_launch_job ( launch_job_status_pk ) :
2013-04-02 19:23:58 +04:00
2013-03-29 09:02:07 +04:00
launch_job_status = LaunchJobStatus . objects . get ( pk = launch_job_status_pk )
2013-04-01 01:25:18 +04:00
launch_job_status . status = ' running '
launch_job_status . save ( )
2013-03-29 09:02:07 +04:00
launch_job = launch_job_status . launch_job
2013-04-02 19:23:58 +04:00
plugin_dir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' ,
2013-03-29 09:02:07 +04:00
' plugins ' , ' callback ' ) )
2013-03-23 02:55:10 +04:00
inventory_script = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) ,
2013-03-29 09:02:07 +04:00
' management ' , ' commands ' ,
' acom_inventory.py ' ) )
env = dict ( os . environ . items ( ) )
env [ ' ACOM_LAUNCH_JOB_STATUS_ID ' ] = str ( launch_job_status . pk )
env [ ' ACOM_INVENTORY_ID ' ] = str ( launch_job . inventory . pk )
env [ ' ANSIBLE_CALLBACK_PLUGINS ' ] = plugin_dir
2013-04-02 19:23:58 +04:00
2013-04-01 01:25:18 +04:00
if hasattr ( settings , ' ANSIBLE_TRANSPORT ' ) :
env [ ' ANSIBLE_TRANSPORT ' ] = getattr ( settings , ' ANSIBLE_TRANSPORT ' )
2013-04-02 19:23:58 +04:00
2013-03-23 02:55:10 +04:00
playbook = launch_job . project . default_playbook
2013-04-01 01:25:18 +04:00
cmdline = [ ' ansible-playbook ' , ' -i ' , inventory_script ] #, '-v']
2013-03-29 09:02:07 +04:00
cmdline . append ( playbook )
2013-04-02 21:11:07 +04:00
# FIXME: How to cancel/interrupt job? (not that important for now)
2013-04-01 01:25:18 +04:00
proc = subprocess . Popen ( cmdline , stdout = subprocess . PIPE ,
stderr = subprocess . PIPE , env = env )
stdout , stderr = proc . communicate ( )
2013-04-02 19:23:58 +04:00
2013-04-01 01:25:18 +04:00
# Reload from database before updating/saving.
launch_job_status = LaunchJobStatus . objects . get ( pk = launch_job_status_pk )
if proc . returncode == 0 :
launch_job_status . status = ' successful '
else :
launch_job_status . status = ' failed '
2013-04-02 19:23:58 +04:00
2013-04-01 01:25:18 +04:00
launch_job_status . result_stdout = stdout
launch_job_status . result_stderr = stderr
launch_job_status . save ( )