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

224 lines
7.3 KiB
Python
Raw Normal View History

2023-02-07 09:04:38 +01:00
#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = r'''
---
module: install
short_description: Run occ maintenance:install
# 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: Bootstrap Nextcloud with an invocation of occ maintenance:install.
options:
database:
description: Supported database type.
required: false
default: sqlite
type: str
database_name:
description: Name of the database.
required: false
type: str
database_host:
description: Hostname of the database.
required: false
default: localhost
type: str
database_port:
description: Port the database is listening on.
required: false
type: int
database_user:
description: User name to connect to the database.
required: false
type: str
database_pass:
description: Password of the database user.
required: false
type: str
database_table_space:
description: Table space of the database (oci only).
required: false
type: str
admin_user:
description: User name of the admin account.
required: false
default: admin
type: str
admin_pass:
description: Password of the admin account.
required: true
type: str
admin_email:
description: E-Mail of the admin account.
required: false
type: str
data_dir:
description: Path to data directory.
required: false
default: <webroot>/data
type: str
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.install
author:
- s3lph
'''
EXAMPLES = r'''
'''
RETURN = r'''
status:
type: dict
description: Parsed output from occ status.
returned: success
sample:
installed: true
version: "25.0.3.2"
versionstring: "25.0.3
edition: ""
maintenance: false
needsDbUpgrade: false
productname: Nextcloud
extendedSupport: false
'''
from ansible.module_utils.basic import AnsibleModule
import json
import os
import subprocess
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
database=dict(required=False, default='sqlite', type='str'),
database_name=dict(required=False, type='str'),
2023-02-07 22:34:46 +01:00
database_host=dict(required=False, default='localhost', type='str'),
2023-02-07 09:04:38 +01:00
database_port=dict(required=False, type='int'),
database_user=dict(required=False, type='str'),
database_pass=dict(required=False, type='str', no_log=True),
2023-02-07 09:04:38 +01:00
database_table_space=dict(required=False, type='str'),
admin_user=dict(required=False, default='admin', type='str'),
admin_pass=dict(required=True, type='str', no_log=True),
2023-02-07 09:04:38 +01:00
admin_email=dict(required=False, type='str'),
data_dir=dict(required=False, default='./data', type='str'),
webroot=dict(required=False, default='/var/www/html', type='str'),
php=dict(required=False, default='/usr/bin/php', type='str'),
)
# 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,
)
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)
result['status'] = status
if status['installed']:
# Nextcloud installation has already been completed
module.exit_json(**result)
# Build installation cmdline
cmdline = [module.params['php'], 'occ', 'maintenance:install', '--no-interaction', '--no-ansi']
datadir = os.path.normpath(os.path.join(module.params['webroot'], module.params['data_dir']))
cmdline.append('--data-dir=' + datadir)
cmdline.append('--database=' + module.params['database'])
if module.params['database_name'] is not None:
cmdline.append('--database-name=' + module.params['database_name'])
if module.params['database_host'] is not None:
cmdline.append('--database-host=' + module.params['database_host'])
if module.params['database_port'] is not None:
cmdline.append('--database-port=' + module.params['database_port'])
if module.params['database_user'] is not None:
cmdline.append('--database-user=' + module.params['database_user'])
if module.params['database_pass'] is not None:
cmdline.append('--database-pass=' + module.params['database_pass'])
if module.params['database_table_space'] is not None:
cmdline.append('--database-table-space=' + module.params['database_table_space'])
cmdline.append('--admin-user=' + module.params['admin_user'])
cmdline.append('--admin-pass=' + module.params['admin_pass'])
if module.params['admin_email'] is not None:
cmdline.append('--admin-email=' + module.params['admin_email'])
if not module.check_mode:
# Perform Nextcloud installation
ic = subprocess.run(cmdline,
cwd=module.params['webroot'],
capture_output=True, encoding='utf-8')
result['stdout'] = ic.stdout
result['stderr'] = ic.stderr
if ic.returncode != 0:
module.fail_json(msg='occ maintenance:install returned non-zero exit code. Run with -vvv to view the output', **result)
result['changed'] = True
# Get occ status once more
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)
result['status'] = status
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()