Created
February 13, 2015 17:25
-
-
Save johndstein/6a6bf702940d9bc56c24 to your computer and use it in GitHub Desktop.
Ansible stop all upstart jobs that begin with a given list of prefixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from glob import glob | |
DOCUMENTATION = ''' | |
--- | |
module: stop_apps | |
short_description: Stop all (upstart) jobs that start with given prefixes. | |
description: | |
- Calls stop on every upstart job that begins with one of the given | |
prefixes. | |
- This will help with adding and removing HIP apps. We can't necessarily | |
know the exact name of all the apps we want to stop. | |
options: | |
prefixes: | |
description: | |
- space delimited list of one or more prefixes to match upstart job | |
name on. | |
required: True | |
author: John Stein | |
Example Usage | |
- name: stop hip apps | |
stop_apps: prefixes=" pupil retina " | |
register: stop_apps_result | |
- debug: msg="YO HERE IT IS!!! {{stop_apps_result.job_names}}" | |
Should output: | |
ok: [whatever] => { | |
"msg": "YO HERE IT IS!!! [u'pupil-rat', u'pupilconf', u'retina', u'retina33']" | |
} | |
''' | |
''' | |
run command and if anything on std err raise Exception | |
else return std out. | |
''' | |
def run_cmd(cmd): | |
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | |
out, err = ps.communicate() | |
if err: | |
raise Exception(err.decode("utf-8").strip()) | |
if out: | |
out = out.decode("utf-8").strip() | |
return out | |
''' | |
stop the upstart jobs and return true if any of them previously running | |
and subsequently stopped. | |
if they were all already stopped return false. | |
if any could not be stopped raise Exception. | |
''' | |
def stop_upstart_jobs(job_names): | |
changed = False | |
commands = [] | |
for job in job_names: | |
cmd = 'status ' + job | |
commands.append(cmd) | |
out = run_cmd(cmd) | |
if 'stop/waiting' in out: | |
continue | |
else: | |
cmd = 'stop ' + job | |
commands.append(cmd) | |
out = run_cmd(cmd) | |
if 'stop/waiting' in out or 'Unknown instance' in out: | |
changed = True | |
else: | |
raise Exception(cmd, ' failure. unable to stop job.') | |
return changed, commands | |
''' | |
list just the upstart job name of all jobs starting with any string in the | |
list of prefixes. | |
''' | |
def list_upstart_jobs(prefixes): | |
job_names = []; | |
for prefix in prefixes.split(): | |
for filename in glob('/etc/init/' + prefix + '*.conf'): | |
job_name = filename[10:-5] | |
job_names.append(job_name) | |
return job_names | |
def main(): | |
module = AnsibleModule( | |
argument_spec = { 'prefixes': { 'required': True } } | |
) | |
job_names = list_upstart_jobs(module.params['prefixes']) | |
changed, commands = stop_upstart_jobs(job_names) | |
module.exit_json(**{ | |
'changed': changed, | |
'job_names': job_names, | |
'commands': commands | |
}) | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment