Commit dcee09ba authored by Jürgen Haas's avatar Jürgen Haas

Validate drush command and format option before executing it

parent 607c8328
......@@ -11,6 +11,7 @@ __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '0.1.0',
'status': ['preview']}
import json
import os
import sys
import tempfile
......@@ -20,6 +21,15 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
def _fail(module, cmd, out, err):
msg = ''
if out:
msg += "stdout: %s" % (out,)
if err:
msg += "\n:stderr: %s" % (err,)
module.fail_json(cmd=cmd, msg=msg)
def _get_drush(module, executable=None):
candidate_drush_basenames = ('drush',)
......@@ -47,13 +57,43 @@ def _get_drush(module, executable=None):
return drush
def _fail(module, cmd, out, err):
msg = ''
if out:
msg += "stdout: %s" % (out,)
if err:
msg += "\n:stderr: %s" % (err,)
module.fail_json(cmd=cmd, msg=msg)
def _run_drush(module, drush, chdir, alias, command, extra_args, format):
err = ''
out = ''
cmd = [drush]
if format is not None:
cmd.extend(['--format=%s' % format, '--yes'])
if module.check_mode:
cmd.extend(['--simulate'])
if alias is not None:
cmd.append(alias)
cmd.append(command)
if extra_args:
cmd.extend(shlex.split(extra_args))
rc, out_drush, err_drush = module.run_command(cmd, use_unsafe_shell=True, cwd=chdir)
out += out_drush
err += err_drush
if rc != 0:
_fail(module, cmd, out, err)
return cmd, out, err
def _get_format(module, drush, chdir, alias, command, format):
cmd, out, err = _run_drush(module, drush, chdir, alias,'help', command, 'json')
response = json.loads(out)
if 'options' not in response:
_fail(module, cmd, '', 'Unknown exception when validating drush syntax')
if 'format' in response['options']:
if format is None:
format = 'json'
if format in response['options']['format']['description']:
return format
_fail(module, cmd, '', 'Format %s is not supported')
if format is not None:
_fail(module, cmd, '', 'Option format is not supported')
return None
def main():
......@@ -62,7 +102,7 @@ def main():
alias=dict(type='str'),
command=dict(type='str'),
extra_args=dict(type='str'),
format=dict(type='str', default='json'),
format=dict(type='str'),
chdir=dict(type='path'),
executable=dict(type='path'),
umask=dict(type='str'),
......@@ -72,11 +112,16 @@ def main():
)
alias = module.params['alias']
command = module.params['command']
output_format = module.params['format']
extra_args = module.params['extra_args']
chdir = module.params['chdir']
umask = module.params['umask']
if alias is not None:
if alias[0] != '@':
alias = '@' + alias
if umask and not isinstance(umask, int):
try:
umask = int(umask, 8)
......@@ -92,31 +137,10 @@ def main():
# this is done to avoid permissions issues with privilege escalation
chdir = tempfile.gettempdir()
err = ''
out = ''
drush = _get_drush(module, module.params['executable'])
format = _get_format(module, drush, chdir, alias, command, output_format)
cmd = [drush, '--format=%s' % output_format, '--yes']
if module.check_mode:
cmd.extend(['--simulate'])
if alias is not None:
if alias[0] != '@':
alias = '@' + alias
cmd.append(alias)
cmd.append(module.params['command'])
if extra_args:
cmd.extend(shlex.split(extra_args))
rc, out_drush, err_druch = module.run_command(cmd, use_unsafe_shell=True, cwd=chdir)
out += out_drush
err += err_druch
if rc != 0:
_fail(module, cmd, out, err)
cmd, out, err = _run_drush(module, drush, chdir, alias, command, extra_args, format)
changed = True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment