2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: lbu
|
|
|
|
|
|
|
|
short_description: Local Backup Utility for Alpine Linux
|
|
|
|
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
description:
|
2023-02-20 17:46:37 +01:00
|
|
|
- Manage Local Backup Utility of Alpine Linux in run-from-RAM mode
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
options:
|
|
|
|
commit:
|
|
|
|
description:
|
|
|
|
- Control whether to commit changed files.
|
|
|
|
type: bool
|
|
|
|
exclude:
|
|
|
|
description:
|
|
|
|
- List of paths to exclude.
|
|
|
|
type: list
|
2020-11-04 09:02:50 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
include:
|
|
|
|
description:
|
|
|
|
- List of paths to include.
|
|
|
|
type: list
|
2020-11-04 09:02:50 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
author:
|
2023-02-20 17:46:37 +01:00
|
|
|
- Kaarle Ritvanen (@kunkku)
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Commit changed files (if any)
|
|
|
|
- name: Commit
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lbu:
|
2020-03-09 10:11:07 +01:00
|
|
|
commit: true
|
|
|
|
|
|
|
|
# Exclude path and commit
|
|
|
|
- name: Exclude directory
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lbu:
|
2020-03-09 10:11:07 +01:00
|
|
|
commit: true
|
|
|
|
exclude:
|
|
|
|
- /etc/opt
|
|
|
|
|
|
|
|
# Include paths without committing
|
|
|
|
- name: Include file and directory
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.lbu:
|
2020-03-09 10:11:07 +01:00
|
|
|
include:
|
|
|
|
- /root/.ssh/authorized_keys
|
|
|
|
- /var/lib/misc
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
msg:
|
|
|
|
description: Error message
|
|
|
|
type: str
|
|
|
|
returned: on failure
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
|
|
def run_module():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'commit': {'type': 'bool'},
|
|
|
|
'exclude': {'type': 'list', 'elements': 'str'},
|
|
|
|
'include': {'type': 'list', 'elements': 'str'}
|
|
|
|
},
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
def run_lbu(*args):
|
|
|
|
code, stdout, stderr = module.run_command(
|
|
|
|
[module.get_bin_path('lbu', required=True)] + list(args)
|
|
|
|
)
|
|
|
|
if code:
|
|
|
|
module.fail_json(changed=changed, msg=stderr)
|
|
|
|
return stdout
|
|
|
|
|
|
|
|
update = False
|
|
|
|
commit = False
|
|
|
|
|
|
|
|
for param in ('include', 'exclude'):
|
|
|
|
if module.params[param]:
|
|
|
|
paths = run_lbu(param, '-l').split('\n')
|
|
|
|
for path in module.params[param]:
|
|
|
|
if os.path.normpath('/' + path)[1:] not in paths:
|
|
|
|
update = True
|
|
|
|
|
|
|
|
if module.params['commit']:
|
|
|
|
commit = update or run_lbu('status') > ''
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=update or commit)
|
|
|
|
|
|
|
|
if update:
|
|
|
|
for param in ('include', 'exclude'):
|
|
|
|
if module.params[param]:
|
|
|
|
run_lbu(param, *module.params[param])
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
run_lbu('commit')
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|