service 2.64 KB
Newer Older
1 2
#!/usr/bin/python

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 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.  If not, see <http://www.gnu.org/licenses/>.

20 21 22 23 24 25 26 27 28 29
try:
    import json
except ImportError:
    import simplejson as json
import os
import sys
import shlex
import subprocess

# ===========================================
30
# convert arguments of form a=b c=d
31 32 33 34 35 36 37 38 39 40 41
# to a dictionary
# FIXME: make more idiomatic

args = " ".join(sys.argv[1:])
items = shlex.split(args)
params = {}
for x in items:
    (k, v) = x.split("=")
    params[k] = v

name = params['name']
42
state = params.get('state','running')
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

# ===========================================
# get service status

status = os.popen("/sbin/service %s status" % name).read()

# ===========================================
# determine if we are going to change anything

running = False
if status.find("not running") != -1:
    running = False
elif status.find("running") != -1:
    running = True
elif name == 'iptables' and status.find("ACCEPT") != -1:
    # iptables status command output is lame
    # TODO: lookup if we can use a return code for this instead?
    running = True

changed = False
63
if not running and state == "started":
64
    changed = True
65
elif running and state == "stopped":
66
    changed = True
67
elif state == "restarted":
68 69 70 71 72 73 74 75 76 77 78 79 80
    changed = True

# ===========================================
# run change commands if we need to

def _run(cmd):
    return subprocess.call(cmd, 
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)


rc = 0
if changed:
81
    if state == 'started':
82
        rc = _run("/sbin/service %s start" %  name)
83
    elif state == 'stopped':
84
        rc = _run("/sbin/service %s stop" % name)
85
    elif state == 'restarted':
86 87 88 89 90 91 92
        rc1 = _run("/sbin/service %s stop" % name)
        rc2 = _run("/sbin/service %s start" % name)
        rc  = rc1 and rc2

if rc != 0:
    # yeah, should probably include output of failure...
    print json.dumps({
93 94
        "failed" : 1,
        "rc"     : rc
95 96 97 98 99 100 101 102 103 104
    })
    sys.exit(1)

# ===============================================
# success

print json.dumps({
   "changed" : changed
})