Commit 196e3f7a authored by Jürgen Haas's avatar Jürgen Haas

Add documentation

parent dcee09ba
......@@ -11,6 +11,82 @@ __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '0.1.0',
'status': ['preview']}
DOCUMENTATION = '''
---
module: drush
short_description: Execute drush commands on remote Drupal sites
description:
- Execute drush commands on remote Drupal sites. To use this module,
Drush must already be installed on the remote host.
options:
alias:
description:
- The drush alias on which the command should be executed. Can be
provided with or without leading @-sign, e.g. "default" or "@default".
command:
required: true
description:
- Drush command to execute.
extra_args:
description:
- Extra arguments passed to drush.
format:
description:
- Output format if supported by the command.
default: 'json'
chdir:
description:
- cd into this directory before running the command. This should be
inside the Drupal project for which the command should be executed.
executable:
description:
- The explicit executable or a pathname to the executable to be used to
run drush for a specific version installed in the system.
umask:
description:
- The system umask to apply before executing the drush command. This is
useful, for example, when installing on systems that have a very
restrictive umask by default (e.g., "0077") and you want to execute
drush commands that create, modify or delete files. Note that this
requires you to specify desired umask mode as an octal string,
(e.g., "0022").
requirements:
- drush
author:
- Jürgen Haas (@jurgenhaas)
'''
EXAMPLES = '''
# Receive site status.
- drush:
command: status
chdir: /var/www/html
# Rebuild site registry and flush cache.
- drush:
command: cache-rebuild
chdir: /var/www/html
# Enable maintenance mode.
- drush:
command: state:set
chdir: /var/www/html
extra_args: system.maintenance_mode 1
'''
RETURN = '''
chdir:
description: directory from which the command was executed
returned: success
type: str
sample: /var/www/html
cmd:
description: drush command used by the module
returned: success
type: str
sample: /usr/local/bin/drush --yes cache-rebuild
'''
import json
import os
import sys
......@@ -57,13 +133,10 @@ def _get_drush(module, executable=None):
return drush
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'])
def _run_drush(module, drush, chdir, alias, command, extra_args, output_format):
cmd = [drush, '--yes']
if output_format is not None:
cmd.extend(['--format=%s' % output_format])
if module.check_mode:
cmd.extend(['--simulate'])
if alias is not None:
......@@ -72,6 +145,8 @@ def _run_drush(module, drush, chdir, alias, command, extra_args, format):
if extra_args:
cmd.extend(shlex.split(extra_args))
err = ''
out = ''
rc, out_drush, err_drush = module.run_command(cmd, use_unsafe_shell=True, cwd=chdir)
out += out_drush
err += err_drush
......@@ -80,18 +155,18 @@ def _run_drush(module, drush, chdir, alias, command, extra_args, format):
return cmd, out, err
def _get_format(module, drush, chdir, alias, command, format):
def _get_format(module, drush, chdir, alias, command, output_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
if output_format is None:
output_format = 'json'
if output_format in response['options']['format']['description']:
return output_format
_fail(module, cmd, '', 'Format %s is not supported')
if format is not None:
if output_format is not None:
_fail(module, cmd, '', 'Option format is not supported')
return None
......@@ -138,13 +213,11 @@ def main():
chdir = tempfile.gettempdir()
drush = _get_drush(module, module.params['executable'])
format = _get_format(module, drush, chdir, alias, command, output_format)
cmd, out, err = _run_drush(module, drush, chdir, alias, command, extra_args, format)
output_format = _get_format(module, drush, chdir, alias, command, output_format)
changed = True
cmd, out, err = _run_drush(module, drush, chdir, alias, command, extra_args, output_format)
module.exit_json(changed=changed, cmd=cmd,
module.exit_json(changed=True, cmd=' '.join(cmd), chdir=chdir,
stdout=out, stderr=err)
finally:
if old_umask is not None:
......
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