ansible-collection-nextcloud/plugins/modules/app.py

209 lines
7.4 KiB
Python

#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = r'''
---
module: config
short_description: Set Nextcloud configuration options.
# If this is part of a collection, you need to use semantic versioning,
# i.e. the version is of the form "2.5.0" and not "2.4".
version_added: "0.0.1"
description: Set Nextcloud configuration options via occ config.
options:
name:
description: Name of the app or apps to install/remove/enable/disable.
required: true
type: list
elements: str
state:
description: State the app or apps should be in.
required: false
default: enabled
type: str
choices:
- enabled
- disabled
- absent
force:
description: Install and enable apps that are not compatible with the current Nextcloud version.
required: false
default: false
type: bool
webroot:
description: Path to the Nextcloud webroot.
required: false
default: /var/www/html
type: str
php:
description: Path to the php-cli binary.
required: false
default: /usr/bin/php
type: str
# Specify this value according to your collection
# in format of namespace.collection.doc_fragment_name
#extends_documentation_fragment:
# - s3lph.nextcloud.app
author:
- s3lph
'''
EXAMPLES = r'''
- name: Install and enable photos app
s3lph.nextcloud.app:
name: photos
- name: Install and enable PIM apps
s3lph.nextcloud.app:
name:
- contacts
- calendar
- mail
- name: Disable resource hungry dashboard
s3lph.nextcloud.app:
name: dashboard
state: disabled
'''
RETURN = r'''
'''
from ansible.module_utils.basic import AnsibleModule
import json
import subprocess
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
webroot=dict(required=False, default='/var/www/html', type='str'),
php=dict(required=False, default='/usr/bin/php', type='str'),
name=dict(required=True, type='list'),
state=dict(required=False, default='enabled', type='str'),
force=dict(required=False, default=False, type='bool'),
)
# seed the result dict in the object
# we primarily care about changed and state
# changed is if this module effectively modified the target
# state will include any data that you want your module to pass back
# for consumption, for example, in a subsequent task
result = dict(
changed=False,
)
# the AnsibleModule object will be our abstraction working with Ansible
# this includes instantiation, a couple of common attr would be the
# args/params passed to the execution, as well as if the module
# supports check mode
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True,
)
state = module.params['state']
if state not in ['enabled', 'disabled', 'absent']:
module.fail_json(msg='state must be one of enabled, disabled or absent', **result)
apps = module.params['name']
if isinstance(apps, str):
apps = [apps]
# Gather Nextcloud installation status
sc = subprocess.run([module.params['php'], 'occ', 'status', '--output=json'],
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if sc.returncode != 0:
result['stdout'] = sc.stdout
result['stderr'] = sc.stderr
module.fail_json(msg='occ status returned non-zero exit code. Run with -vvv to view the output', **result)
status = json.loads(sc.stdout)
if not status['installed']:
module.fail_json(msg='Nextcloud installation has not been completed, so occ app is not available.', **result)
# Gather Nextcloud app list
ac = subprocess.run([module.params['php'], 'occ', 'app:list', '--output=json'],
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if ac.returncode != 0:
result['stdout'] = ac.stdout
result['stderr'] = ac.stderr
module.fail_json(msg='occ app:list returned non-zero exit code. Run with -vvv to view the output', **result)
app_status = json.loads(ac.stdout)
# Apply app configuration changes
for app in apps:
if state == 'absent' and (app in app_status['enabled'] or app in app_status['disabled']):
result['changed'] = True
if not module.check_mode:
c = subprocess.run([module.params['php'], 'occ', 'app:remove', '--keep-data', app],
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if c.returncode != 0:
result['stdout'] = c.stdout
result['stderr'] = c.stderr
module.fail_json(msg='occ app:remove returned non-zero exit code. Run with -vvv to view the output', **result)
elif state in ['enabled', 'disabled'] and app not in app_status['enabled'] and app not in app_status['disabled']:
result['changed'] = True
if not module.check_mode:
cmdline = [module.params['php'], 'occ', 'app:install']
if state == 'disabled':
cmdline.append('--keep-disabled')
if module.params['force']:
cmdline.append('--force')
cmdline.append(app)
c = subprocess.run(cmdline,
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if c.returncode != 0:
result['stdout'] = c.stdout
result['stderr'] = c.stderr
module.fail_json(msg='occ app:install returned non-zero exit code. Run with -vvv to view the output', **result)
elif state == 'enabled' and app in app_status['disabled']:
result['changed'] = True
if not module.check_mode:
cmdline = [module.params['php'], 'occ', 'app:enable']
if module.params['force']:
cmdline.append('--force')
cmdline.append(app)
c = subprocess.run(cmdline,
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if c.returncode != 0:
result['stdout'] = c.stdout
result['stderr'] = c.stderr
module.fail_json(msg='occ app:enable returned non-zero exit code. Run with -vvv to view the output', **result)
elif state == 'disabled' and app in app_status['enabled']:
result['changed'] = True
if not module.check_mode:
c = subprocess.run([module.params['php'], 'occ', 'app:disable', app],
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
if c.returncode != 0:
result['stdout'] = c.stdout
result['stderr'] = c.stderr
module.fail_json(msg='occ app:disable returned non-zero exit code. Run with -vvv to view the output', **result)
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()