Compare commits
24 Commits
bibz/maste
...
master
Author | SHA1 | Date | |
---|---|---|---|
7844e0ebde | |||
be5ee1d274 | |||
5a0ac3460a | |||
a4050ac521 | |||
4b476a8c3d | |||
31c955c5b9 | |||
e61ad55945 | |||
2c26c3a53e | |||
29d43b9ef0 | |||
34b7f2ccc1 | |||
b7e3210631 | |||
0b5fd3a2cf | |||
00ca2d2133 | |||
fb178b36cd | |||
b6ec505285 | |||
89a69e6e38 | |||
8b94a3b06a | |||
f9a895e8e1 | |||
fe8f9be567 | |||
6bd9731844 | |||
537a7b3365 | |||
c40784f6ca | |||
591c3f5a67 | |||
ea36487819 |
13
collection/galaxy.yml
Normal file
13
collection/galaxy.yml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
---
|
||||||
|
namespace: community
|
||||||
|
name: syncthing
|
||||||
|
version: 1.1.2
|
||||||
|
readme: README.md
|
||||||
|
authors:
|
||||||
|
- Rafael Bodill <justrafi at g>
|
||||||
|
- Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
- Peter Surda <peter@bitmessage.at>
|
||||||
|
description: >-
|
||||||
|
- Syncthing collection, allows to control a syncthing instance via ansible
|
||||||
|
repository: https://git.bitmessage.org/Sysdeploy/ansible-modules-syncthing
|
||||||
|
license: GPL-3.0-only
|
36
collection/plugins/module_utils/common.py
Normal file
36
collection/plugins/module_utils/common.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
def deep_equal(a, b):
|
||||||
|
"""
|
||||||
|
Compare two data structures for deep equality by converting them to JSON strings.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
a (any): First data structure to compare.
|
||||||
|
b (any): Second data structure to compare.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the two data structures are equal, False otherwise.
|
||||||
|
"""
|
||||||
|
return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True)
|
||||||
|
|
||||||
|
def get_changes(module_params, module_args_keys_list, current_config):
|
||||||
|
"""
|
||||||
|
Function to get changes by comparing module parameters with current configuration.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
module_params (dict): The parameters of the module.
|
||||||
|
module_args_keys_list (list): List of keys to check in module parameters.
|
||||||
|
current_config (dict): The current configuration to compare against.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: A dictionary containing the changes.
|
||||||
|
"""
|
||||||
|
changes = {}
|
||||||
|
|
||||||
|
for key, value in module_params.items():
|
||||||
|
if key in module_args_keys_list:
|
||||||
|
if value is not None:
|
||||||
|
if not deep_equal(value, current_config.get(key)):
|
||||||
|
changes[key] = value
|
||||||
|
|
||||||
|
return changes
|
111
collection/plugins/module_utils/syncthing_api.py
Normal file
111
collection/plugins/module_utils/syncthing_api.py
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
from xml.etree.ElementTree import parse
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
||||||
|
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
DEFAULT_ST_CONFIG_LOCATION = '%localappdata%/Syncthing/config.xml'
|
||||||
|
elif platform.system() == 'Darwin':
|
||||||
|
DEFAULT_ST_CONFIG_LOCATION = '$HOME/Library/Application Support/Syncthing/config.xml'
|
||||||
|
else:
|
||||||
|
DEFAULT_ST_CONFIG_LOCATION = '$HOME/.local/state/syncthing/config.xml'
|
||||||
|
|
||||||
|
|
||||||
|
class SyncthingModule(AnsibleModule):
|
||||||
|
def _make_headers(self, target=None):
|
||||||
|
url = '{}{}{}{}'.format(
|
||||||
|
self.params['host'],
|
||||||
|
self.api_url,
|
||||||
|
'/' if target else '',
|
||||||
|
target if target else '')
|
||||||
|
headers = {'X-Api-Key': self.params['api_key'] }
|
||||||
|
return url, headers
|
||||||
|
|
||||||
|
def _get_key_from_filesystem(self):
|
||||||
|
try:
|
||||||
|
if self.params['config_file']:
|
||||||
|
stconfigfile = self.params['config_file']
|
||||||
|
else:
|
||||||
|
stconfigfile = os.path.expandvars(DEFAULT_ST_CONFIG_LOCATION)
|
||||||
|
stconfig = parse(stconfigfile)
|
||||||
|
root = stconfig.getroot()
|
||||||
|
gui = root.find('gui')
|
||||||
|
self.params['api_key'] = gui.find('apikey').text
|
||||||
|
except Exception:
|
||||||
|
self.fail_json(msg="Auto-configuration failed. Please specify")
|
||||||
|
|
||||||
|
def _api_call(self, method='GET', data=None, target=None, missing_ok=False):
|
||||||
|
unix_socket = None
|
||||||
|
if 'unix_socket' in self.params:
|
||||||
|
unix_socket = self.params['unix_socket']
|
||||||
|
url, headers = self._make_headers(target=target)
|
||||||
|
if data:
|
||||||
|
headers['Content-Type'] = 'application/json'
|
||||||
|
data = json.dumps(data)
|
||||||
|
self.result = {
|
||||||
|
"changed": method != 'GET',
|
||||||
|
"response": None,
|
||||||
|
}
|
||||||
|
resp, info = fetch_url(self,
|
||||||
|
url=url, unix_socket=unix_socket,
|
||||||
|
data=data, headers=headers,
|
||||||
|
method=method,
|
||||||
|
timeout=self.params['timeout'])
|
||||||
|
if info:
|
||||||
|
self.result['response'] = info
|
||||||
|
else:
|
||||||
|
self.fail_json(msg='Error occured while calling host')
|
||||||
|
if info['status'] not in [200, 404]:
|
||||||
|
self.fail_json(msg='Error occured while calling host')
|
||||||
|
if info['status'] == 404:
|
||||||
|
if missing_ok:
|
||||||
|
return {}
|
||||||
|
else:
|
||||||
|
self.fail_json(msg='Error occured while calling host')
|
||||||
|
try:
|
||||||
|
content = resp.read()
|
||||||
|
except AttributeError:
|
||||||
|
self.result['content'] = info.pop('body', '')
|
||||||
|
self.fail_json(msg='Error occured while reading response')
|
||||||
|
try:
|
||||||
|
return json.loads(content)
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
return {'content': content}
|
||||||
|
|
||||||
|
def get_call(self, target=None):
|
||||||
|
return self._api_call(missing_ok=True, target=target)
|
||||||
|
def delete_call(self, target):
|
||||||
|
return self._api_call(method='DELETE', target=target)
|
||||||
|
def post_call(self, data=None, target=None):
|
||||||
|
return self._api_call(method='POST', data=data, target=target)
|
||||||
|
def put_call(self, data=None, target=None):
|
||||||
|
return self._api_call(method='PUT', data=data, target=target)
|
||||||
|
def patch_call(self, data=None, target=None):
|
||||||
|
return self._api_call(method='PATCH', data=data, target=target)
|
||||||
|
|
||||||
|
def exit_json(self):
|
||||||
|
super().exit_json(**self.result)
|
||||||
|
def fail_json(self, msg=""):
|
||||||
|
super().fail_json(msg, **self.result)
|
||||||
|
|
||||||
|
def __init__(self, api_url='/', argument_spec=None, supports_check_mode=True):
|
||||||
|
self.api_url = api_url
|
||||||
|
argument_spec_temp = url_argument_spec()
|
||||||
|
argument_spec_temp.update(dict(
|
||||||
|
host=dict(type='str', default='http://127.0.0.1:8384'),
|
||||||
|
unix_socket=dict(type='str', required=False),
|
||||||
|
api_key=dict(type='str', required=False, no_log=True),
|
||||||
|
config_file=dict(type='str', required=False),
|
||||||
|
timeout=dict(type='int', default=30),
|
||||||
|
))
|
||||||
|
if argument_spec is None:
|
||||||
|
argument_spec = {}
|
||||||
|
argument_spec_temp.update(argument_spec)
|
||||||
|
super().__init__(argument_spec=argument_spec_temp, supports_check_mode=True)
|
||||||
|
|
||||||
|
# Auto-configuration: Try to fetch API key from filesystem
|
||||||
|
if not self.params['api_key']:
|
||||||
|
self._get_key_from_filesystem()
|
157
collection/plugins/modules/device.py
Normal file
157
collection/plugins/modules/device.py
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: syncthing_device
|
||||||
|
|
||||||
|
short_description: Manage Syncthing devices
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This is my longer description explaining my sample module"
|
||||||
|
|
||||||
|
options:
|
||||||
|
id:
|
||||||
|
description:
|
||||||
|
- This is the unique id of this new device
|
||||||
|
required: true
|
||||||
|
name:
|
||||||
|
description:
|
||||||
|
- The name for this new device
|
||||||
|
required: false
|
||||||
|
host:
|
||||||
|
description:
|
||||||
|
- Host to connect to, including port
|
||||||
|
default: http://127.0.0.1:8384
|
||||||
|
unix_socket:
|
||||||
|
description:
|
||||||
|
- Use this unix socket instead of TCP
|
||||||
|
required: false
|
||||||
|
api_key:
|
||||||
|
description:
|
||||||
|
- API key to use for authentication with host.
|
||||||
|
If not provided, will try to auto-configure from filesystem.
|
||||||
|
required: false
|
||||||
|
config_file:
|
||||||
|
description:
|
||||||
|
- Path to the Syncthing configuration file for automatic
|
||||||
|
discovery (`api_key`). Note that the running user needs read
|
||||||
|
access to the file.
|
||||||
|
required: false
|
||||||
|
timeout:
|
||||||
|
description:
|
||||||
|
- The socket level timeout in seconds
|
||||||
|
default: 30
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Use present/absent to ensure device is added, or not.
|
||||||
|
default: present
|
||||||
|
choices: ['absent', 'present', 'paused']
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Add a device to share with
|
||||||
|
- name: Add syncthing device
|
||||||
|
syncthing_device:
|
||||||
|
id: 1234-1234-1234-1234
|
||||||
|
name: my-server-name
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
|
||||||
|
# Returns an object of a new device
|
||||||
|
def create_device(params):
|
||||||
|
device = {
|
||||||
|
'addresses': [
|
||||||
|
'dynamic'
|
||||||
|
],
|
||||||
|
'allowedNetworks': [],
|
||||||
|
'autoAcceptFolders': False,
|
||||||
|
'certName': '',
|
||||||
|
'compression': 'metadata',
|
||||||
|
'deviceID': params['id'],
|
||||||
|
'ignoredFolders': [],
|
||||||
|
'introducedBy': '',
|
||||||
|
'introducer': False,
|
||||||
|
'maxRecvKbps': 0,
|
||||||
|
'maxSendKbps': 0,
|
||||||
|
'name': params['name'],
|
||||||
|
'paused': True if params['state'] == 'paused' else False,
|
||||||
|
'pendingFolders': [],
|
||||||
|
'skipIntroductionRemovals': False
|
||||||
|
}
|
||||||
|
return device
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
# module arguments
|
||||||
|
module_args = {}
|
||||||
|
module_args.update(dict(
|
||||||
|
id=dict(type='str', required=True),
|
||||||
|
name=dict(type='str', required=False),
|
||||||
|
state=dict(type='str', default='present',
|
||||||
|
choices=['absent', 'present', 'paused']),
|
||||||
|
))
|
||||||
|
|
||||||
|
# the AnsibleModule object will be our abstraction working with Ansible
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/devices',
|
||||||
|
argument_spec=module_args
|
||||||
|
)
|
||||||
|
|
||||||
|
if module.params['state'] != 'absent' and not module.params['name']:
|
||||||
|
module.fail_json(msg='You must provide a name when creating')
|
||||||
|
|
||||||
|
want_pause = module.params['state'] == 'paused'
|
||||||
|
device_exists = False
|
||||||
|
device = module.get_call(target=module.params['id'])
|
||||||
|
if 'deviceID' in device and device['deviceID'] == module.params['id']:
|
||||||
|
device_exists = True
|
||||||
|
|
||||||
|
if module.params['state'] == 'absent':
|
||||||
|
if device_exists:
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
module.delete_call(target=device['deviceID'])
|
||||||
|
elif device_exists: # exists but maybe needs changing
|
||||||
|
if device['paused'] != want_pause:
|
||||||
|
device['paused'] = want_pause
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
module.patch_call(data=device, target=device['deviceID'])
|
||||||
|
else: # Doesn't exist but needs to be added
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
device = create_device(module.params)
|
||||||
|
module.post_call(data=device)
|
||||||
|
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
204
collection/plugins/modules/device_defaults.py
Normal file
204
collection/plugins/modules/device_defaults.py
Normal file
|
@ -0,0 +1,204 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: device_defaults
|
||||||
|
|
||||||
|
short_description: Manage Syncthing device default configurations
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This module allows you to manage Syncthing device default configurations.
|
||||||
|
You can update the default configurations using this module.
|
||||||
|
It uses the Syncthing REST API to perform these operations."
|
||||||
|
|
||||||
|
options:
|
||||||
|
addresses:
|
||||||
|
description:
|
||||||
|
- List of addresses for the device.
|
||||||
|
type: list
|
||||||
|
required: false
|
||||||
|
allowedNetworks:
|
||||||
|
description:
|
||||||
|
- List of allowed networks for the device.
|
||||||
|
type: list
|
||||||
|
required: false
|
||||||
|
autoAcceptFolders:
|
||||||
|
description:
|
||||||
|
- Whether the device should automatically accept shared folders.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
certName:
|
||||||
|
description:
|
||||||
|
- The certificate name for the device.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
compression:
|
||||||
|
description:
|
||||||
|
- The compression setting for the device.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
deviceID:
|
||||||
|
description:
|
||||||
|
- The ID of the device.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
ignoredFolders:
|
||||||
|
description:
|
||||||
|
- List of ignored folders for the device.
|
||||||
|
type: list
|
||||||
|
required: false
|
||||||
|
introducedBy:
|
||||||
|
description:
|
||||||
|
- The device that introduced this device.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
introducer:
|
||||||
|
description:
|
||||||
|
- Whether this device is an introducer.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
maxRecvKbps:
|
||||||
|
description:
|
||||||
|
- The maximum receive rate for the device in Kbps.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
maxRequestKiB:
|
||||||
|
description:
|
||||||
|
- The maximum request size for the device in KiB.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
maxSendKbps:
|
||||||
|
description:
|
||||||
|
- The maximum send rate for the device in Kbps.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
name:
|
||||||
|
description:
|
||||||
|
- The name of the device.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
numConnections:
|
||||||
|
description:
|
||||||
|
- The number of connections for the device.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
paused:
|
||||||
|
description:
|
||||||
|
- Whether the device is paused.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
remoteGUIPort:
|
||||||
|
description:
|
||||||
|
- The remote GUI port for the device.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
skipIntroductionRemovals:
|
||||||
|
description:
|
||||||
|
- Whether to skip introduction removals for the device.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
untrusted:
|
||||||
|
description:
|
||||||
|
- Whether the device is untrusted.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Get/Update device_defaults
|
||||||
|
- name: Get device_defaults
|
||||||
|
become: yes
|
||||||
|
become_user: syncthing
|
||||||
|
community.syncthing.device_defaults:
|
||||||
|
host: http://localhost
|
||||||
|
unix_socket: /run/syncthing/syncthing.sock
|
||||||
|
config_file: /var/lib/syncthing/.config/syncthing/config.xml
|
||||||
|
addresses: ["dynamic"]
|
||||||
|
autoAcceptFolders: false
|
||||||
|
compression: "metadata"
|
||||||
|
deviceID: "device-id"
|
||||||
|
name: "device-name"
|
||||||
|
paused: false
|
||||||
|
register: device_defaults
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
device_defaults:
|
||||||
|
description: The default configuration of the device after the operation.
|
||||||
|
type: dict
|
||||||
|
changed:
|
||||||
|
description: Whether any changes were made.
|
||||||
|
type: bool
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.common import get_changes
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
module_args = dict(
|
||||||
|
addresses=dict(type='list', required=False),
|
||||||
|
allowedNetworks=dict(type='list', required=False),
|
||||||
|
autoAcceptFolders=dict(type='bool', required=False),
|
||||||
|
certName=dict(type='str', required=False),
|
||||||
|
compression=dict(type='str', required=False),
|
||||||
|
deviceID=dict(type='str', required=False),
|
||||||
|
ignoredFolders=dict(type='list', required=False),
|
||||||
|
introducedBy=dict(type='str', required=False),
|
||||||
|
introducer=dict(type='bool', required=False),
|
||||||
|
maxRecvKbps=dict(type='int', required=False),
|
||||||
|
maxRequestKiB=dict(type='int', required=False),
|
||||||
|
maxSendKbps=dict(type='int', required=False),
|
||||||
|
name=dict(type='str', required=False),
|
||||||
|
numConnections=dict(type='int', required=False),
|
||||||
|
paused=dict(type='bool', required=False),
|
||||||
|
remoteGUIPort=dict(type='int', required=False),
|
||||||
|
skipIntroductionRemovals=dict(type='bool', required=False),
|
||||||
|
untrusted=dict(type='bool', required=False),
|
||||||
|
)
|
||||||
|
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/defaults/device',
|
||||||
|
argument_spec=module_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
current_config = module.get_call()
|
||||||
|
|
||||||
|
module_args_keys_list = list(module_args.keys())
|
||||||
|
|
||||||
|
# Using get_changes function to determine what has changed
|
||||||
|
changes = get_changes(module.params, current_config)
|
||||||
|
|
||||||
|
if module.check_mode or len(changes.keys()) == 0:
|
||||||
|
module.result['device_defaults'] = current_config
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
module.patch_call(data=module.params)
|
||||||
|
module.result['device_defaults'] = module.get_call()
|
||||||
|
module.result['changed'] = True
|
||||||
|
module.result['changes'] = changes
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
103
collection/plugins/modules/devices.py
Normal file
103
collection/plugins/modules/devices.py
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: syncthing_device
|
||||||
|
|
||||||
|
short_description: Manage Syncthing devices
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This is my longer description explaining my sample module"
|
||||||
|
|
||||||
|
options:
|
||||||
|
id:
|
||||||
|
description:
|
||||||
|
- This is the unique id of this new device
|
||||||
|
required: true
|
||||||
|
name:
|
||||||
|
description:
|
||||||
|
- The name for this new device
|
||||||
|
required: false
|
||||||
|
host:
|
||||||
|
description:
|
||||||
|
- Host to connect to, including port
|
||||||
|
default: http://127.0.0.1:8384
|
||||||
|
unix_socket:
|
||||||
|
description:
|
||||||
|
- Use this unix socket instead of TCP
|
||||||
|
required: false
|
||||||
|
api_key:
|
||||||
|
description:
|
||||||
|
- API key to use for authentication with host.
|
||||||
|
If not provided, will try to auto-configure from filesystem.
|
||||||
|
required: false
|
||||||
|
config_file:
|
||||||
|
description:
|
||||||
|
- Path to the Syncthing configuration file for automatic
|
||||||
|
discovery (`api_key`). Note that the running user needs read
|
||||||
|
access to the file.
|
||||||
|
required: false
|
||||||
|
timeout:
|
||||||
|
description:
|
||||||
|
- The socket level timeout in seconds
|
||||||
|
default: 30
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Use present/absent to ensure device is added, or not.
|
||||||
|
default: present
|
||||||
|
choices: ['absent', 'present', 'paused']
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Add a device to share with
|
||||||
|
- name: Add syncthing device
|
||||||
|
syncthing_device:
|
||||||
|
id: 1234-1234-1234-1234
|
||||||
|
name: my-server-name
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/devices',
|
||||||
|
)
|
||||||
|
|
||||||
|
devices = {}
|
||||||
|
devices_dict = module.get_call()
|
||||||
|
own_id = module.result['response']['x-syncthing-id']
|
||||||
|
for device in devices_dict:
|
||||||
|
if device['deviceID'] == own_id:
|
||||||
|
continue
|
||||||
|
devices[device['deviceID']] = device['name']
|
||||||
|
module.result['devices'] = devices
|
||||||
|
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
267
collection/plugins/modules/folder.py
Normal file
267
collection/plugins/modules/folder.py
Normal file
|
@ -0,0 +1,267 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at gmail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: syncthing_folder
|
||||||
|
|
||||||
|
short_description: Manage Syncthing folders
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This is my longer description explaining my sample module"
|
||||||
|
|
||||||
|
options:
|
||||||
|
id:
|
||||||
|
description:
|
||||||
|
- This is the unique id of this new folder
|
||||||
|
required: true
|
||||||
|
label:
|
||||||
|
description:
|
||||||
|
- The label for this new folder
|
||||||
|
required: false
|
||||||
|
path:
|
||||||
|
description:
|
||||||
|
- This is the path of the folder
|
||||||
|
required: false
|
||||||
|
devices:
|
||||||
|
description:
|
||||||
|
- List of device ids to share folder with. Always share with self.
|
||||||
|
required: false
|
||||||
|
fs_watcher:
|
||||||
|
description:
|
||||||
|
- Whether to activate the file-system watcher.
|
||||||
|
default: true
|
||||||
|
ignore_perms:
|
||||||
|
description:
|
||||||
|
- Whether to ignore permissions when looking for changes.
|
||||||
|
default: false
|
||||||
|
type:
|
||||||
|
description:
|
||||||
|
- The folder type: sending local chances, and/or receiving
|
||||||
|
remote changes.
|
||||||
|
default: sendreceive
|
||||||
|
choices: ['sendreceive', 'sendonly', 'receiveonly']
|
||||||
|
host:
|
||||||
|
description:
|
||||||
|
- Host to connect to, including port
|
||||||
|
default: http://127.0.0.1:8384
|
||||||
|
api_key:
|
||||||
|
description:
|
||||||
|
- API key to use for authentication with host.
|
||||||
|
If not provided, will try to auto-configure from filesystem.
|
||||||
|
required: false
|
||||||
|
config_file:
|
||||||
|
description:
|
||||||
|
- Path to the Syncthing configuration file for automatic
|
||||||
|
discovery (`api_key`). Note that the running user needs read
|
||||||
|
access to the file.
|
||||||
|
required: false
|
||||||
|
timeout:
|
||||||
|
description:
|
||||||
|
- The socket level timeout in seconds
|
||||||
|
default: 30
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Use present/absent to ensure folder is shared, or not.
|
||||||
|
default: present
|
||||||
|
choices: ['absent', 'present', 'paused']
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Add a folder to synchronize with another device
|
||||||
|
- name: Create folder
|
||||||
|
become: yes
|
||||||
|
become_user: syncthing
|
||||||
|
community.syncthing.folder:
|
||||||
|
host: http://localhost
|
||||||
|
unix_socket: /run/syncthing/syncthing.sock
|
||||||
|
config_file: /var/lib/syncthing/.config/syncthing/config.xml
|
||||||
|
id: 'my-folder-1'
|
||||||
|
label: 'my folder 1'
|
||||||
|
path: '/root/test-folder-1'
|
||||||
|
devices:
|
||||||
|
- '1234-1234-1234'
|
||||||
|
- '5678-5678-5678'
|
||||||
|
register: folder
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
|
||||||
|
# Returns an object of a new folder
|
||||||
|
def create_folder(params, self_id, existing_device_ids):
|
||||||
|
wanted_device_ids = {self_id}
|
||||||
|
for device_id in params['devices']:
|
||||||
|
if device_id in existing_device_ids:
|
||||||
|
wanted_device_ids.add(device_id)
|
||||||
|
|
||||||
|
# Keep the original ordering if collections are equivalent.
|
||||||
|
# Again, for idempotency reasons.
|
||||||
|
device_ids = sorted(wanted_device_ids)
|
||||||
|
|
||||||
|
# Sort the device IDs to keep idem-potency
|
||||||
|
devices = [
|
||||||
|
{
|
||||||
|
'deviceID': device_id,
|
||||||
|
'introducedBy': '',
|
||||||
|
} for device_id in device_ids
|
||||||
|
]
|
||||||
|
|
||||||
|
return {
|
||||||
|
'autoNormalize': True,
|
||||||
|
'copiers': 0,
|
||||||
|
'devices': devices,
|
||||||
|
'disableSparseFiles': False,
|
||||||
|
'disableTempIndexes': False,
|
||||||
|
'filesystemType': 'basic',
|
||||||
|
'fsWatcherDelayS': 10,
|
||||||
|
'fsWatcherEnabled': params['fs_watcher'],
|
||||||
|
'hashers': 0,
|
||||||
|
'id': params['id'],
|
||||||
|
'ignoreDelete': False,
|
||||||
|
'ignorePerms': params['ignore_perms'],
|
||||||
|
'label': params['label'] if params['label'] else params['id'],
|
||||||
|
'markerName': '.stfolder',
|
||||||
|
'maxConflicts': -1,
|
||||||
|
'minDiskFree': {
|
||||||
|
'unit': '%',
|
||||||
|
'value': 1
|
||||||
|
},
|
||||||
|
'order': 'random',
|
||||||
|
'path': params['path'],
|
||||||
|
'paused': True if params['state'] == 'paused' else False,
|
||||||
|
'pullerMaxPendingKiB': 0,
|
||||||
|
'pullerPauseS': 0,
|
||||||
|
'rescanIntervalS': 3600,
|
||||||
|
'scanProgressIntervalS': 0,
|
||||||
|
'type': params['type'],
|
||||||
|
'useLargeBlocks': False,
|
||||||
|
'versioning': {
|
||||||
|
'params': {},
|
||||||
|
'type': ''
|
||||||
|
},
|
||||||
|
'weakHashThresholdPct': 25
|
||||||
|
}
|
||||||
|
|
||||||
|
def update_folder(folder, params, self_id, existing_device_ids):
|
||||||
|
wanted_device_ids = {self_id}
|
||||||
|
for device_id in params['devices']:
|
||||||
|
if device_id in existing_device_ids:
|
||||||
|
wanted_device_ids.add(device_id)
|
||||||
|
|
||||||
|
current_device_ids = {d['deviceID'] for d in folder['devices']}
|
||||||
|
device_ids = (
|
||||||
|
current_device_ids
|
||||||
|
if current_device_ids == wanted_device_ids
|
||||||
|
else sorted(wanted_device_ids)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sort the device IDs to keep idem-potency
|
||||||
|
devices = [
|
||||||
|
{
|
||||||
|
'deviceID': device_id,
|
||||||
|
'introducedBy': '',
|
||||||
|
} for device_id in device_ids
|
||||||
|
]
|
||||||
|
|
||||||
|
folder.update({
|
||||||
|
'devices': devices,
|
||||||
|
'fsWatcherEnabled': params['fs_watcher'],
|
||||||
|
'ignorePerms': params['ignore_perms'],
|
||||||
|
'label': params['label'] if params['label'] else params['id'],
|
||||||
|
'path': params['path'],
|
||||||
|
'paused': True if params['state'] == 'paused' else False,
|
||||||
|
'type': params['type'],
|
||||||
|
})
|
||||||
|
|
||||||
|
return folder
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
# module arguments
|
||||||
|
module_args = {}
|
||||||
|
module_args.update(dict(
|
||||||
|
id=dict(type='str', required=True),
|
||||||
|
label=dict(type='str', required=False),
|
||||||
|
path=dict(type='path', required=False),
|
||||||
|
devices=dict(type='list', required=False, default=[]),
|
||||||
|
fs_watcher=dict(type='bool', default=True),
|
||||||
|
ignore_perms=dict(type='bool', required=False, default=False),
|
||||||
|
type=dict(type='str', default='sendreceive',
|
||||||
|
choices=['sendreceive', 'sendonly', 'receiveonly']),
|
||||||
|
state=dict(type='str', default='present',
|
||||||
|
choices=['absent', 'present', 'pause']),
|
||||||
|
))
|
||||||
|
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/folders',
|
||||||
|
argument_spec=module_args
|
||||||
|
)
|
||||||
|
|
||||||
|
device_module = SyncthingModule(
|
||||||
|
api_url='/rest/config/devices',
|
||||||
|
argument_spec=module_args
|
||||||
|
)
|
||||||
|
|
||||||
|
if module.params['state'] != 'absent' and not module.params['path']:
|
||||||
|
module.fail_json(msg='You must provide a path when creating')
|
||||||
|
|
||||||
|
folder_exists = False
|
||||||
|
folder = module.get_call(target=module.params['id'])
|
||||||
|
if 'id' in folder and folder['id'] == module.params['id']:
|
||||||
|
folder_exists = True
|
||||||
|
|
||||||
|
if module.params['state'] == 'absent':
|
||||||
|
if folder_exists:
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
module.delete_call(target=folder['id'])
|
||||||
|
else:
|
||||||
|
devices_list = device_module.get_call()
|
||||||
|
existing_device_ids = [device['deviceID'] for device in devices_list]
|
||||||
|
self_id = device_module.result['response']['x-syncthing-id']
|
||||||
|
|
||||||
|
if folder_exists: # exists but maybe needs changing
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
folder_with_updated_data = update_folder(
|
||||||
|
folder, module.params, self_id, existing_device_ids
|
||||||
|
)
|
||||||
|
module.patch_call(data=folder_with_updated_data, target=folder['id'])
|
||||||
|
else: # Doesn't exist but needs to be added
|
||||||
|
if module.check_mode:
|
||||||
|
module.result['changed'] = True
|
||||||
|
else:
|
||||||
|
folder_config_wanted = create_folder(
|
||||||
|
module.params, self_id, existing_device_ids
|
||||||
|
)
|
||||||
|
module.post_call(data=folder_config_wanted)
|
||||||
|
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
381
collection/plugins/modules/folder_defaults.py
Normal file
381
collection/plugins/modules/folder_defaults.py
Normal file
|
@ -0,0 +1,381 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: folder_defaults
|
||||||
|
|
||||||
|
short_description: Manage Syncthing folder default configurations
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This module allows you to manage Syncthing folder default configurations.
|
||||||
|
You can update the default configurations using this module.
|
||||||
|
It uses the Syncthing REST API to perform these operations."
|
||||||
|
|
||||||
|
options:
|
||||||
|
id:
|
||||||
|
description:
|
||||||
|
- The ID of the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
label:
|
||||||
|
description:
|
||||||
|
- The label for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
filesystemType:
|
||||||
|
description:
|
||||||
|
- The filesystem type for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: basic
|
||||||
|
path:
|
||||||
|
description:
|
||||||
|
- The path for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: ~
|
||||||
|
type:
|
||||||
|
description:
|
||||||
|
- The type of the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: sendreceive
|
||||||
|
choices: [sendreceive, sendonly, receiveonly]
|
||||||
|
devices:
|
||||||
|
description:
|
||||||
|
- List of devices for the module.
|
||||||
|
type: list
|
||||||
|
required: false
|
||||||
|
default: []
|
||||||
|
rescanIntervalS:
|
||||||
|
description:
|
||||||
|
- The rescan interval for the module in seconds.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 3600
|
||||||
|
fsWatcherEnabled:
|
||||||
|
description:
|
||||||
|
- Whether the filesystem watcher is enabled for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: true
|
||||||
|
fsWatcherDelayS:
|
||||||
|
description:
|
||||||
|
- The filesystem watcher delay for the module in seconds.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 10
|
||||||
|
ignorePerms:
|
||||||
|
description:
|
||||||
|
- Whether to ignore permissions for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
autoNormalize:
|
||||||
|
description:
|
||||||
|
- Whether to automatically normalize for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: true
|
||||||
|
minDiskFree:
|
||||||
|
description:
|
||||||
|
- The minimum disk free for the module.
|
||||||
|
type: dict
|
||||||
|
required: false
|
||||||
|
default: {value: 1, unit: '%'}
|
||||||
|
versioning:
|
||||||
|
description:
|
||||||
|
- The versioning for the module.
|
||||||
|
type: dict
|
||||||
|
required: false
|
||||||
|
default: {type: '', params: {}, cleanupIntervalS: 3600, fsPath: '', fsType: 'basic'}
|
||||||
|
copiers:
|
||||||
|
description:
|
||||||
|
- The number of copiers for the module.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
pullerMaxPendingKiB:
|
||||||
|
description:
|
||||||
|
- The maximum pending puller size for the module in KiB.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
hashers:
|
||||||
|
description:
|
||||||
|
- The number of hashers for the module.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
order:
|
||||||
|
description:
|
||||||
|
- The order for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: random
|
||||||
|
ignoreDelete:
|
||||||
|
description:
|
||||||
|
- Whether to ignore delete for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
scanProgressIntervalS:
|
||||||
|
description:
|
||||||
|
- The scan progress interval for the module in seconds.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
pullerPauseS:
|
||||||
|
description:
|
||||||
|
- The puller pause for the module in seconds.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
maxConflicts:
|
||||||
|
description:
|
||||||
|
- The maximum number of conflicts for the module.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 10
|
||||||
|
disableSparseFiles:
|
||||||
|
description:
|
||||||
|
- Whether to disable sparse files for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
disableTempIndexes:
|
||||||
|
description:
|
||||||
|
- Whether to disable temporary indexes for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
paused:
|
||||||
|
description:
|
||||||
|
- Whether the module is paused.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
weakHashThresholdPct:
|
||||||
|
description:
|
||||||
|
- The weak hash threshold for the module in percentage.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 25
|
||||||
|
markerName:
|
||||||
|
description:
|
||||||
|
- The marker name for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: .stfolder
|
||||||
|
copyOwnershipFromParent:
|
||||||
|
description:
|
||||||
|
- Whether to copy ownership from parent for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
modTimeWindowS:
|
||||||
|
description:
|
||||||
|
- The modification time window for the module in seconds.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 0
|
||||||
|
maxConcurrentWrites:
|
||||||
|
description:
|
||||||
|
- The maximum number of concurrent writes for the module.
|
||||||
|
type: int
|
||||||
|
required: false
|
||||||
|
default: 2
|
||||||
|
disableFsync:
|
||||||
|
description:
|
||||||
|
- Whether to disable fsync for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
blockPullOrder:
|
||||||
|
description:
|
||||||
|
- The block pull order for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: standard
|
||||||
|
copyRangeMethod:
|
||||||
|
description:
|
||||||
|
- The copy range method for the module.
|
||||||
|
type: str
|
||||||
|
required: false
|
||||||
|
default: standard
|
||||||
|
caseSensitiveFS:
|
||||||
|
description:
|
||||||
|
- Whether the filesystem is case sensitive for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
junctionsAsDirs:
|
||||||
|
description:
|
||||||
|
- Whether to treat junctions as directories for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
syncOwnership:
|
||||||
|
description:
|
||||||
|
- Whether to synchronize ownership for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
sendOwnership:
|
||||||
|
description:
|
||||||
|
- Whether to send ownership for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
syncXattrs:
|
||||||
|
description:
|
||||||
|
- Whether to synchronize extended attributes for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
sendXattrs:
|
||||||
|
description:
|
||||||
|
- Whether to send extended attributes for the module.
|
||||||
|
type: bool
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
xattrFilter:
|
||||||
|
description:
|
||||||
|
- The extended attribute filter for the module.
|
||||||
|
type: dict
|
||||||
|
required: false
|
||||||
|
default: {entries: [], maxSingleEntrySize: 1024, maxTotalSize: 4096}
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Get/Update folder_defaults
|
||||||
|
- name: Get folder_defaults
|
||||||
|
become: yes
|
||||||
|
become_user: syncthing
|
||||||
|
community.syncthing.folder_defaults:
|
||||||
|
host: http://localhost
|
||||||
|
unix_socket: /run/syncthing/syncthing.sock
|
||||||
|
config_file: /var/lib/syncthing/.config/syncthing/config.xml
|
||||||
|
id: "default"
|
||||||
|
label: "default"
|
||||||
|
path: "/"
|
||||||
|
paused: false
|
||||||
|
register: folder_defaults
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
folder_defaults:
|
||||||
|
description: The default configuration of the folder after the operation.
|
||||||
|
type: dict
|
||||||
|
changed:
|
||||||
|
description: Whether any changes were made.
|
||||||
|
type: bool
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
import json
|
||||||
|
|
||||||
|
def deep_equal(a, b):
|
||||||
|
return json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True)
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
module_args = dict(
|
||||||
|
id=dict(type='str', required=False),
|
||||||
|
label=dict(type='str', required=False),
|
||||||
|
filesystemType=dict(type='str', required=False, default='basic'),
|
||||||
|
path=dict(type='str', required=False, default='~'),
|
||||||
|
type=dict(type='str', default='sendreceive',
|
||||||
|
choices=['sendreceive', 'sendonly', 'receiveonly']),
|
||||||
|
devices=dict(type='list', required=False, default=[]),
|
||||||
|
rescanIntervalS=dict(type='int', required=False, default=3600),
|
||||||
|
fsWatcherEnabled=dict(type='bool', required=False, default=True),
|
||||||
|
fsWatcherDelayS=dict(type='int', required=False, default=10),
|
||||||
|
ignorePerms=dict(type='bool', required=False, default=False),
|
||||||
|
autoNormalize=dict(type='bool', required=False, default=True),
|
||||||
|
minDiskFree=dict(type='dict', required=False, default=dict(value=1, unit='%')),
|
||||||
|
versioning=dict(type='dict', required=False,
|
||||||
|
default=dict(type='', params={}, cleanupIntervalS=3600, fsPath='', fsType='basic')),
|
||||||
|
copiers=dict(type='int', required=False, default=0),
|
||||||
|
pullerMaxPendingKiB=dict(type='int', required=False, default=0),
|
||||||
|
hashers=dict(type='int', required=False, default=0),
|
||||||
|
order=dict(type='str', required=False, default='random'),
|
||||||
|
ignoreDelete=dict(type='bool', required=False, default=False),
|
||||||
|
scanProgressIntervalS=dict(type='int', required=False, default=0),
|
||||||
|
pullerPauseS=dict(type='int', required=False, default=0),
|
||||||
|
maxConflicts=dict(type='int', required=False, default=10),
|
||||||
|
disableSparseFiles=dict(type='bool', required=False, default=False),
|
||||||
|
disableTempIndexes=dict(type='bool', required=False, default=False),
|
||||||
|
paused=dict(type='bool', required=False, default=False),
|
||||||
|
weakHashThresholdPct=dict(type='int', required=False, default=25),
|
||||||
|
markerName=dict(type='str', required=False, default='.stfolder'),
|
||||||
|
copyOwnershipFromParent=dict(type='bool', required=False, default=False),
|
||||||
|
modTimeWindowS=dict(type='int', required=False, default=0),
|
||||||
|
maxConcurrentWrites=dict(type='int', required=False, default=2),
|
||||||
|
disableFsync=dict(type='bool', required=False, default=False),
|
||||||
|
blockPullOrder=dict(type='str', required=False, default='standard'),
|
||||||
|
copyRangeMethod=dict(type='str', required=False, default='standard'),
|
||||||
|
caseSensitiveFS=dict(type='bool', required=False, default=False),
|
||||||
|
junctionsAsDirs=dict(type='bool', required=False, default=False),
|
||||||
|
syncOwnership=dict(type='bool', required=False, default=False),
|
||||||
|
sendOwnership=dict(type='bool', required=False, default=False),
|
||||||
|
syncXattrs=dict(type='bool', required=False, default=False),
|
||||||
|
sendXattrs=dict(type='bool', required=False, default=False),
|
||||||
|
xattrFilter=dict(type='dict', required=False,
|
||||||
|
default=dict(entries=[], maxSingleEntrySize=1024, maxTotalSize=4096)),
|
||||||
|
)
|
||||||
|
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/defaults/folder',
|
||||||
|
argument_spec=module_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
current_config = module.get_call()
|
||||||
|
|
||||||
|
module_args_keys_list = list(module_args.keys())
|
||||||
|
|
||||||
|
changes = {}
|
||||||
|
|
||||||
|
for key, value in module.params.items():
|
||||||
|
# Check if the key is in module_args_keys_list
|
||||||
|
if key in module_args_keys_list:
|
||||||
|
# Check if the value is not None
|
||||||
|
if value is not None:
|
||||||
|
# Check if the value is different from the current_config
|
||||||
|
if not deep_equal(value, current_config.get(key)):
|
||||||
|
# If all conditions are met, add the key-value pair to changes
|
||||||
|
changes[key] = value
|
||||||
|
|
||||||
|
if module.check_mode or len(changes.keys()) == 0:
|
||||||
|
module.result['folder_defaults'] = current_config
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
module.patch_call(data=module.params)
|
||||||
|
module.result['folder_defaults'] = module.get_call()
|
||||||
|
module.result['changed'] = True
|
||||||
|
module.result['changes'] = changes
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
89
collection/plugins/modules/folders.py
Normal file
89
collection/plugins/modules/folders.py
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
||||||
|
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.1',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: syncthing_folders
|
||||||
|
|
||||||
|
short_description: Retrieve Syncthing folders
|
||||||
|
|
||||||
|
version_added: "2.7"
|
||||||
|
|
||||||
|
description:
|
||||||
|
- "This module retrieves the list of folders from Syncthing"
|
||||||
|
|
||||||
|
options:
|
||||||
|
host:
|
||||||
|
description:
|
||||||
|
- Host to connect to, including port
|
||||||
|
default: http://127.0.0.1:8384
|
||||||
|
unix_socket:
|
||||||
|
description:
|
||||||
|
- Use this unix socket instead of TCP
|
||||||
|
required: false
|
||||||
|
config_file:
|
||||||
|
description:
|
||||||
|
- Path to the Syncthing configuration file for automatic
|
||||||
|
discovery (`api_key`). Note that the running user needs read
|
||||||
|
access to the file.
|
||||||
|
required: false
|
||||||
|
timeout:
|
||||||
|
description:
|
||||||
|
- The socket level timeout in seconds
|
||||||
|
default: 30
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Rafael Bodill (@rafi)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Get folders
|
||||||
|
- name: Get folders
|
||||||
|
become: yes
|
||||||
|
become_user: syncthing
|
||||||
|
community.syncthing.folders:
|
||||||
|
host: http://localhost
|
||||||
|
unix_socket: /run/syncthing/syncthing.sock
|
||||||
|
config_file: /var/lib/syncthing/.config/syncthing/config.xml
|
||||||
|
register: folders
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = '''
|
||||||
|
folders:
|
||||||
|
description: The list of folders retrieved from Syncthing.
|
||||||
|
type: dict
|
||||||
|
response:
|
||||||
|
description: The API response, in-case of an error.
|
||||||
|
type: dict
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule
|
||||||
|
|
||||||
|
def run_module():
|
||||||
|
module = SyncthingModule(
|
||||||
|
api_url='/rest/config/folders',
|
||||||
|
)
|
||||||
|
|
||||||
|
folders = {}
|
||||||
|
folders_list = module.get_call()
|
||||||
|
for folder in folders_list:
|
||||||
|
folders[folder['id']] = folder
|
||||||
|
module.result['folders'] = folders
|
||||||
|
|
||||||
|
module.exit_json()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
run_module()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -1,243 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# Copyright: (c) 2018, Rafael Bodill <justrafi at google mail>
|
|
||||||
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {
|
|
||||||
'metadata_version': '1.1',
|
|
||||||
'status': ['preview'],
|
|
||||||
'supported_by': 'community'
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
|
||||||
---
|
|
||||||
module: syncthing_device
|
|
||||||
|
|
||||||
short_description: Manage Syncthing devices
|
|
||||||
|
|
||||||
version_added: "2.7"
|
|
||||||
|
|
||||||
description:
|
|
||||||
- "This is my longer description explaining my sample module"
|
|
||||||
|
|
||||||
options:
|
|
||||||
id:
|
|
||||||
description:
|
|
||||||
- This is the unique id of this new device
|
|
||||||
required: true
|
|
||||||
name:
|
|
||||||
description:
|
|
||||||
- The name for this new device
|
|
||||||
required: false
|
|
||||||
host:
|
|
||||||
description:
|
|
||||||
- Host to connect to, including port
|
|
||||||
default: http://127.0.0.1:8384
|
|
||||||
api_key:
|
|
||||||
description:
|
|
||||||
- API key to use for authentication with host.
|
|
||||||
If not provided, will try to auto-configure from filesystem.
|
|
||||||
required: false
|
|
||||||
config_file:
|
|
||||||
description:
|
|
||||||
- Path to the Syncthing configuration file for automatic
|
|
||||||
discovery (`api_key`). Note that the running user needs read
|
|
||||||
access to the file.
|
|
||||||
required: false
|
|
||||||
timeout:
|
|
||||||
description:
|
|
||||||
- The socket level timeout in seconds
|
|
||||||
default: 30
|
|
||||||
state:
|
|
||||||
description:
|
|
||||||
- Use present/absent to ensure device is added, or not.
|
|
||||||
default: present
|
|
||||||
choices: ['absent', 'present', 'paused']
|
|
||||||
|
|
||||||
author:
|
|
||||||
- Rafael Bodill (@rafi)
|
|
||||||
'''
|
|
||||||
|
|
||||||
EXAMPLES = '''
|
|
||||||
# Add a device to share with
|
|
||||||
- name: Add syncthing device
|
|
||||||
syncthing_device:
|
|
||||||
id: 1234-1234-1234-1234
|
|
||||||
name: my-server-name
|
|
||||||
'''
|
|
||||||
|
|
||||||
RETURN = '''
|
|
||||||
response:
|
|
||||||
description: The API response, in-case of an error.
|
|
||||||
type: dict
|
|
||||||
'''
|
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
import platform
|
|
||||||
from xml.etree.ElementTree import parse
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
|
||||||
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
|
||||||
|
|
||||||
SYNCTHING_API_URI = "/rest/system/config"
|
|
||||||
if platform.system() == 'Windows':
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '%localappdata%/Syncthing/config.xml'
|
|
||||||
elif platform.system() == 'Darwin':
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '$HOME/Library/Application Support/Syncthing/config.xml'
|
|
||||||
else:
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '$HOME/.config/syncthing/config.xml'
|
|
||||||
|
|
||||||
|
|
||||||
def make_headers(host, api_key):
|
|
||||||
url = '{}{}'.format(host, SYNCTHING_API_URI)
|
|
||||||
headers = {'X-Api-Key': api_key }
|
|
||||||
return url, headers
|
|
||||||
|
|
||||||
def get_key_from_filesystem(module):
|
|
||||||
try:
|
|
||||||
if module.params['config_file']:
|
|
||||||
stconfigfile = module.params['config_file']
|
|
||||||
else:
|
|
||||||
stconfigfile = os.path.expandvars(DEFAULT_ST_CONFIG_LOCATION)
|
|
||||||
stconfig = parse(stconfigfile)
|
|
||||||
root = stconfig.getroot()
|
|
||||||
gui = root.find('gui')
|
|
||||||
api_key = gui.find('apikey').text
|
|
||||||
return api_key
|
|
||||||
except Exception:
|
|
||||||
module.fail_json(msg="Auto-configuration failed. Please specify"
|
|
||||||
"the API key manually.")
|
|
||||||
|
|
||||||
# Fetch Syncthing configuration
|
|
||||||
def get_config(module):
|
|
||||||
url, headers = make_headers(module.params['host'], module.params['api_key'])
|
|
||||||
resp, info = fetch_url(
|
|
||||||
module, url, data=None, headers=headers,
|
|
||||||
method='GET', timeout=module.params['timeout'])
|
|
||||||
|
|
||||||
if not info or info['status'] != 200:
|
|
||||||
result['response'] = info
|
|
||||||
module.fail_json(msg='Error occured while calling host', **result)
|
|
||||||
|
|
||||||
try:
|
|
||||||
content = resp.read()
|
|
||||||
except AttributeError:
|
|
||||||
result['content'] = info.pop('body', '')
|
|
||||||
result['response'] = str(info)
|
|
||||||
module.fail_json(msg='Error occured while reading response', **result)
|
|
||||||
|
|
||||||
return json.loads(content)
|
|
||||||
|
|
||||||
# Post the new configuration to Syncthing API
|
|
||||||
def post_config(module, config, result):
|
|
||||||
url, headers = make_headers(module.params['host'], module.params['api_key'])
|
|
||||||
headers['Content-Type'] = 'application/json'
|
|
||||||
|
|
||||||
result['msg'] = config
|
|
||||||
resp, info = fetch_url(
|
|
||||||
module, url, data=json.dumps(config), headers=headers,
|
|
||||||
method='POST', timeout=module.params['timeout'])
|
|
||||||
|
|
||||||
if not info or info['status'] != 200:
|
|
||||||
result['response'] = str(info)
|
|
||||||
module.fail_json(msg='Error occured while posting new config', **result)
|
|
||||||
|
|
||||||
# Returns an object of a new device
|
|
||||||
def create_device(params):
|
|
||||||
device = {
|
|
||||||
'addresses': [
|
|
||||||
'dynamic'
|
|
||||||
],
|
|
||||||
'allowedNetworks': [],
|
|
||||||
'autoAcceptFolders': False,
|
|
||||||
'certName': '',
|
|
||||||
'compression': 'metadata',
|
|
||||||
'deviceID': params['id'],
|
|
||||||
'ignoredFolders': [],
|
|
||||||
'introducedBy': '',
|
|
||||||
'introducer': False,
|
|
||||||
'maxRecvKbps': 0,
|
|
||||||
'maxSendKbps': 0,
|
|
||||||
'name': params['name'],
|
|
||||||
'paused': True if params['state'] == 'paused' else False,
|
|
||||||
'pendingFolders': [],
|
|
||||||
'skipIntroductionRemovals': False
|
|
||||||
}
|
|
||||||
return device
|
|
||||||
|
|
||||||
def run_module():
|
|
||||||
# module arguments
|
|
||||||
module_args = url_argument_spec()
|
|
||||||
module_args.update(dict(
|
|
||||||
id=dict(type='str', required=True),
|
|
||||||
name=dict(type='str', required=False),
|
|
||||||
host=dict(type='str', default='http://127.0.0.1:8384'),
|
|
||||||
api_key=dict(type='str', required=False, no_log=True),
|
|
||||||
config_file=dict(type='str', required=False),
|
|
||||||
timeout=dict(type='int', default=30),
|
|
||||||
state=dict(type='str', default='present',
|
|
||||||
choices=['absent', 'present', 'pause']),
|
|
||||||
))
|
|
||||||
|
|
||||||
# seed the result dict in the object
|
|
||||||
result = {
|
|
||||||
"changed": False,
|
|
||||||
"response": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
# the AnsibleModule object will be our abstraction working with Ansible
|
|
||||||
module = AnsibleModule(
|
|
||||||
argument_spec=module_args,
|
|
||||||
supports_check_mode=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if module.params['state'] != 'absent' and not module.params['name']:
|
|
||||||
module.fail_json(msg='You must provide a name when creating', **result)
|
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Auto-configuration: Try to fetch API key from filesystem
|
|
||||||
if not module.params['api_key']:
|
|
||||||
module.params['api_key'] = get_key_from_filesystem(module)
|
|
||||||
|
|
||||||
config = get_config(module)
|
|
||||||
if module.params['state'] == 'absent':
|
|
||||||
# Remove device from list, if found
|
|
||||||
for idx, device in enumerate(config['devices']):
|
|
||||||
if device['deviceID'] == module.params['id']:
|
|
||||||
config['devices'].pop(idx)
|
|
||||||
result['changed'] = True
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# Bail-out if device is already added
|
|
||||||
for device in config['devices']:
|
|
||||||
if device['deviceID'] == module.params['id']:
|
|
||||||
want_pause = module.params['state'] == 'pause'
|
|
||||||
if (want_pause and device['paused']) or \
|
|
||||||
(not want_pause and not device['paused']):
|
|
||||||
module.exit_json(**result)
|
|
||||||
else:
|
|
||||||
device['paused'] = want_pause
|
|
||||||
result['changed'] = True
|
|
||||||
break
|
|
||||||
|
|
||||||
# Append the new device into configuration
|
|
||||||
if not result['changed']:
|
|
||||||
device = create_device(module.params)
|
|
||||||
config['devices'].append(device)
|
|
||||||
result['changed'] = True
|
|
||||||
|
|
||||||
if result['changed']:
|
|
||||||
post_config(module, config, result)
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
run_module()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
|
@ -1,348 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# Copyright: (c) 2018, Rafael Bodill <justrafi at gmail>
|
|
||||||
# Copyright: (c) 2020, Borjan Tchakaloff <first name at last name dot fr>
|
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {
|
|
||||||
'metadata_version': '1.1',
|
|
||||||
'status': ['preview'],
|
|
||||||
'supported_by': 'community'
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
|
||||||
---
|
|
||||||
module: syncthing_folder
|
|
||||||
|
|
||||||
short_description: Manage Syncthing folders
|
|
||||||
|
|
||||||
version_added: "2.7"
|
|
||||||
|
|
||||||
description:
|
|
||||||
- "This is my longer description explaining my sample module"
|
|
||||||
|
|
||||||
options:
|
|
||||||
id:
|
|
||||||
description:
|
|
||||||
- This is the unique id of this new folder
|
|
||||||
required: true
|
|
||||||
label:
|
|
||||||
description:
|
|
||||||
- The label for this new folder
|
|
||||||
required: false
|
|
||||||
path:
|
|
||||||
description:
|
|
||||||
- This is the path of the folder
|
|
||||||
required: false
|
|
||||||
devices:
|
|
||||||
description:
|
|
||||||
- List of devices to share folder with
|
|
||||||
required: false
|
|
||||||
fs_watcher:
|
|
||||||
description:
|
|
||||||
- Whether to activate the file-system watcher.
|
|
||||||
default: true
|
|
||||||
ignore_perms:
|
|
||||||
description:
|
|
||||||
- Whether to ignore permissions when looking for changes.
|
|
||||||
default: false
|
|
||||||
type:
|
|
||||||
description:
|
|
||||||
- The folder type: sending local chances, and/or receiving
|
|
||||||
remote changes.
|
|
||||||
default: sendreceive
|
|
||||||
choices: ['sendreceive', 'sendonly', 'receiveonly']
|
|
||||||
host:
|
|
||||||
description:
|
|
||||||
- Host to connect to, including port
|
|
||||||
default: http://127.0.0.1:8384
|
|
||||||
api_key:
|
|
||||||
description:
|
|
||||||
- API key to use for authentication with host.
|
|
||||||
If not provided, will try to auto-configure from filesystem.
|
|
||||||
required: false
|
|
||||||
config_file:
|
|
||||||
description:
|
|
||||||
- Path to the Syncthing configuration file for automatic
|
|
||||||
discovery (`api_key`). Note that the running user needs read
|
|
||||||
access to the file.
|
|
||||||
required: false
|
|
||||||
timeout:
|
|
||||||
description:
|
|
||||||
- The socket level timeout in seconds
|
|
||||||
default: 30
|
|
||||||
state:
|
|
||||||
description:
|
|
||||||
- Use present/absent to ensure folder is shared, or not.
|
|
||||||
default: present
|
|
||||||
choices: ['absent', 'present', 'paused']
|
|
||||||
|
|
||||||
author:
|
|
||||||
- Rafael Bodill (@rafi)
|
|
||||||
'''
|
|
||||||
|
|
||||||
EXAMPLES = '''
|
|
||||||
# Add a folder to synchronize with another device
|
|
||||||
- name: Add syncthing folder
|
|
||||||
syncthing_folder:
|
|
||||||
id: box
|
|
||||||
path: ~/box
|
|
||||||
devices:
|
|
||||||
- 1234-1234-1234-1234
|
|
||||||
'''
|
|
||||||
|
|
||||||
RETURN = '''
|
|
||||||
response:
|
|
||||||
description: The API response, in-case of an error.
|
|
||||||
type: dict
|
|
||||||
'''
|
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
import platform
|
|
||||||
from xml.etree.ElementTree import parse
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
|
||||||
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
|
||||||
|
|
||||||
SYNCTHING_API_BASE_URI = "/rest"
|
|
||||||
if platform.system() == 'Windows':
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '%localappdata%/Syncthing/config.xml'
|
|
||||||
elif platform.system() == 'Darwin':
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '$HOME/Library/Application Support/Syncthing/config.xml'
|
|
||||||
else:
|
|
||||||
DEFAULT_ST_CONFIG_LOCATION = '$HOME/.config/syncthing/config.xml'
|
|
||||||
|
|
||||||
|
|
||||||
def make_headers(host, api_key, resource):
|
|
||||||
url = '{}{}/{}'.format(host, SYNCTHING_API_BASE_URI, resource)
|
|
||||||
headers = {'X-Api-Key': api_key }
|
|
||||||
return url, headers
|
|
||||||
|
|
||||||
def get_key_from_filesystem(module):
|
|
||||||
try:
|
|
||||||
if module.params['config_file']:
|
|
||||||
stconfigfile = module.params['config_file']
|
|
||||||
else:
|
|
||||||
stconfigfile = os.path.expandvars(DEFAULT_ST_CONFIG_LOCATION)
|
|
||||||
stconfig = parse(stconfigfile)
|
|
||||||
root = stconfig.getroot()
|
|
||||||
gui = root.find('gui')
|
|
||||||
api_key = gui.find('apikey').text
|
|
||||||
return api_key
|
|
||||||
except Exception:
|
|
||||||
module.fail_json(msg="Auto-configuration failed. Please specify"
|
|
||||||
"the API key manually.")
|
|
||||||
|
|
||||||
def get_data_from_rest_api(module, resource):
|
|
||||||
url, headers = make_headers(
|
|
||||||
module.params['host'], module.params['api_key'], resource
|
|
||||||
)
|
|
||||||
resp, info = fetch_url(
|
|
||||||
module,
|
|
||||||
url,
|
|
||||||
data=None,
|
|
||||||
headers=headers,
|
|
||||||
method='GET',
|
|
||||||
timeout=module.params['timeout']
|
|
||||||
)
|
|
||||||
|
|
||||||
if not info or info['status'] != 200:
|
|
||||||
result['response'] = info
|
|
||||||
module.fail_json(msg='Error occured while calling host', **result)
|
|
||||||
|
|
||||||
try:
|
|
||||||
content = resp.read()
|
|
||||||
except AttributeError:
|
|
||||||
result['content'] = info.pop('body', '')
|
|
||||||
result['response'] = str(info)
|
|
||||||
module.fail_json(msg='Error occured while reading response', **result)
|
|
||||||
|
|
||||||
return json.loads(content)
|
|
||||||
|
|
||||||
# Fetch Syncthing configuration
|
|
||||||
def get_config(module):
|
|
||||||
return get_data_from_rest_api(module, 'system/config')
|
|
||||||
|
|
||||||
# Fetch Syncthing status
|
|
||||||
def get_status(module):
|
|
||||||
return get_data_from_rest_api(module, 'system/status')
|
|
||||||
|
|
||||||
# Get the device name -> device ID mapping.
|
|
||||||
def get_devices_mapping(config):
|
|
||||||
return {
|
|
||||||
device['name']: device['deviceID'] for device in config['devices']
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the folder configuration from the global configuration, if it exists
|
|
||||||
def get_folder_config(folder_id, config):
|
|
||||||
for folder in config['folders']:
|
|
||||||
if folder['id'] == folder_id:
|
|
||||||
return folder
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Post the new configuration to Syncthing API
|
|
||||||
def post_config(module, config, result):
|
|
||||||
url, headers = make_headers(
|
|
||||||
module.params['host'],
|
|
||||||
module.params['api_key'],
|
|
||||||
'system/config',
|
|
||||||
)
|
|
||||||
headers['Content-Type'] = 'application/json'
|
|
||||||
|
|
||||||
result['msg'] = config
|
|
||||||
resp, info = fetch_url(
|
|
||||||
module, url, data=json.dumps(config), headers=headers,
|
|
||||||
method='POST', timeout=module.params['timeout'])
|
|
||||||
|
|
||||||
if not info or info['status'] != 200:
|
|
||||||
result['response'] = str(info)
|
|
||||||
module.fail_json(**result)
|
|
||||||
|
|
||||||
# Returns an object of a new folder
|
|
||||||
def create_folder(params, self_id, current_device_ids, devices_mapping):
|
|
||||||
# We need the current device ID as per the Syncthing API.
|
|
||||||
# If missing, Syncthing will add it alright, but we don't want to give
|
|
||||||
# the false idea that this configuration is different just because of that.
|
|
||||||
wanted_device_ids = {self_id}
|
|
||||||
for device_name_or_id in params['devices']:
|
|
||||||
if device_name_or_id in devices_mapping:
|
|
||||||
wanted_device_ids.add(devices_mapping[device_name_or_id])
|
|
||||||
else:
|
|
||||||
# Purposefully do not validate we already know this device ID or
|
|
||||||
# name as per previous behavior. This will need to be fixed.
|
|
||||||
wanted_device_ids.add(device_name_or_id)
|
|
||||||
|
|
||||||
# Keep the original ordering if collections are equivalent.
|
|
||||||
# Again, for idempotency reasons.
|
|
||||||
device_ids = (
|
|
||||||
current_device_ids
|
|
||||||
if set(current_device_ids) == wanted_device_ids
|
|
||||||
else sorted(wanted_device_ids)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Sort the device IDs to keep idem-potency
|
|
||||||
devices = [
|
|
||||||
{
|
|
||||||
'deviceID': device_id,
|
|
||||||
'introducedBy': '',
|
|
||||||
} for device_id in device_ids
|
|
||||||
]
|
|
||||||
|
|
||||||
return {
|
|
||||||
'autoNormalize': True,
|
|
||||||
'copiers': 0,
|
|
||||||
'devices': devices,
|
|
||||||
'disableSparseFiles': False,
|
|
||||||
'disableTempIndexes': False,
|
|
||||||
'filesystemType': 'basic',
|
|
||||||
'fsWatcherDelayS': 10,
|
|
||||||
'fsWatcherEnabled': params['fs_watcher'],
|
|
||||||
'hashers': 0,
|
|
||||||
'id': params['id'],
|
|
||||||
'ignoreDelete': False,
|
|
||||||
'ignorePerms': params['ignore_perms'],
|
|
||||||
'label': params['label'] if params['label'] else params['id'],
|
|
||||||
'markerName': '.stfolder',
|
|
||||||
'maxConflicts': -1,
|
|
||||||
'minDiskFree': {
|
|
||||||
'unit': '%',
|
|
||||||
'value': 1
|
|
||||||
},
|
|
||||||
'order': 'random',
|
|
||||||
'path': params['path'],
|
|
||||||
'paused': True if params['state'] == 'paused' else False,
|
|
||||||
'pullerMaxPendingKiB': 0,
|
|
||||||
'pullerPauseS': 0,
|
|
||||||
'rescanIntervalS': 3600,
|
|
||||||
'scanProgressIntervalS': 0,
|
|
||||||
'type': params['type'],
|
|
||||||
'useLargeBlocks': False,
|
|
||||||
'versioning': {
|
|
||||||
'params': {},
|
|
||||||
'type': ''
|
|
||||||
},
|
|
||||||
'weakHashThresholdPct': 25
|
|
||||||
}
|
|
||||||
|
|
||||||
def run_module():
|
|
||||||
# module arguments
|
|
||||||
module_args = url_argument_spec()
|
|
||||||
module_args.update(dict(
|
|
||||||
id=dict(type='str', required=True),
|
|
||||||
label=dict(type='str', required=False),
|
|
||||||
path=dict(type='path', required=False),
|
|
||||||
devices=dict(type='list', required=False, default=False),
|
|
||||||
fs_watcher=dict(type='bool', default=True),
|
|
||||||
ignore_perms=dict(type='bool', required=False, default=False),
|
|
||||||
type=dict(type='str', default='sendreceive',
|
|
||||||
choices=['sendreceive', 'sendonly', 'receiveonly']),
|
|
||||||
host=dict(type='str', default='http://127.0.0.1:8384'),
|
|
||||||
api_key=dict(type='str', required=False, no_log=True),
|
|
||||||
config_file=dict(type='path', required=False),
|
|
||||||
timeout=dict(type='int', default=30),
|
|
||||||
state=dict(type='str', default='present',
|
|
||||||
choices=['absent', 'present', 'pause']),
|
|
||||||
))
|
|
||||||
|
|
||||||
# seed the result dict in the object
|
|
||||||
result = {
|
|
||||||
"changed": False,
|
|
||||||
"response": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
# the AnsibleModule object will be our abstraction working with Ansible
|
|
||||||
module = AnsibleModule(
|
|
||||||
argument_spec=module_args,
|
|
||||||
supports_check_mode=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if module.params['state'] != 'absent' and not module.params['path']:
|
|
||||||
module.fail_json(msg='You must provide a path when creating', **result)
|
|
||||||
|
|
||||||
if module.check_mode:
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Auto-configuration: Try to fetch API key from filesystem
|
|
||||||
if not module.params['api_key']:
|
|
||||||
module.params['api_key'] = get_key_from_filesystem(module)
|
|
||||||
|
|
||||||
config = get_config(module)
|
|
||||||
self_id = get_status(module)['myID']
|
|
||||||
devices_mapping = get_devices_mapping(config)
|
|
||||||
if module.params['state'] == 'absent':
|
|
||||||
# Remove folder from list, if found
|
|
||||||
for idx, folder in enumerate(config['folders']):
|
|
||||||
if folder['id'] == module.params['id']:
|
|
||||||
config['folders'].pop(idx)
|
|
||||||
result['changed'] = True
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
folder_config = get_folder_config(module.params['id'], config)
|
|
||||||
folder_config_devices = (
|
|
||||||
[d['deviceID'] for d in folder_config['devices']] if folder_config else []
|
|
||||||
)
|
|
||||||
folder_config_wanted = create_folder(
|
|
||||||
module.params, self_id, folder_config_devices, devices_mapping
|
|
||||||
)
|
|
||||||
|
|
||||||
if folder_config is None:
|
|
||||||
config['folders'].append(folder_config_wanted)
|
|
||||||
result['changed'] = True
|
|
||||||
elif folder_config != folder_config_wanted:
|
|
||||||
# Update the folder configuration in-place
|
|
||||||
folder_config.clear()
|
|
||||||
folder_config.update(folder_config_wanted)
|
|
||||||
result['changed'] = True
|
|
||||||
|
|
||||||
if result['changed']:
|
|
||||||
post_config(module, config, result)
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
run_module()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Loading…
Reference in New Issue
Block a user