Compare commits

..

No commits in common. "3a49d17dfd7e98499c7aef55de89ecd197407451" and "8b7b85b8f189e5eb4c785f31b5763379412a6058" have entirely different histories.

3 changed files with 48 additions and 140 deletions

View File

@ -1,8 +1,5 @@
# Ansible Modules for Syncthing
Forked from [github.com/rafi/ansible-modules-syncthing]
(https://github.com/rafi/ansible-modules-syncthing).
Collection of modules for [Syncthing](https://syncthing.net) management.
## Install
@ -15,9 +12,9 @@ Copy the `./library` directory to your Ansible project and ensure your
library = ./library
```
Please note this module was tested on:
Please note this module was test on:
* Debian Buster with Syncthing v1.0.0
* Ubuntu 16.04 with Syncthing v0.14.52
Please report successful usage on other platforms/versions.
@ -25,9 +22,12 @@ Please report successful usage on other platforms/versions.
See [example playbooks](./playbooks) for robust feature usage:
* [install_syncthing.yml] - Install Syncthing on Debian/Ubuntu (with systemd)
* [install_syncthing.yml] - Install Syncthing on Ubuntu (with systemd)
* [manage.yml] - Ensure Syncthing devices and folders across devices
[install_syncthing.yml]: http://
[manage.yml]: http://
## Modules
### Module: `syncthing_device`
@ -108,5 +108,4 @@ Examples:
## License
Copyright: (c) 2018, Rafael Bodill `<justrafi at g>`
Copyright: (c) 2020--2021, 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)

View File

@ -2,7 +2,6 @@
# -*- 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 = {
@ -40,12 +39,6 @@ options:
- 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
@ -98,9 +91,6 @@ def make_headers(host, api_key):
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()
@ -176,7 +166,6 @@ def run_module():
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']),

View File

@ -2,7 +2,6 @@
# -*- 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 = {
@ -39,20 +38,6 @@ options:
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
@ -62,12 +47,6 @@ options:
- 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
@ -106,7 +85,7 @@ 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"
SYNCTHING_API_URI = "/rest/system/config"
if platform.system() == 'Windows':
DEFAULT_ST_CONFIG_LOCATION = '%localappdata%/Syncthing/config.xml'
elif platform.system() == 'Darwin':
@ -115,16 +94,13 @@ 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)
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()
@ -135,18 +111,12 @@ def get_key_from_filesystem(module):
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
)
# 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']
)
module, url, data=None, headers=headers,
method='GET', timeout=module.params['timeout'])
if not info or info['status'] != 200:
result['response'] = info
@ -161,34 +131,9 @@ def get_data_from_rest_api(module, resource):
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',
)
url, headers = make_headers(module.params['host'], module.params['api_key'])
headers['Content-Type'] = 'application/json'
result['msg'] = config
@ -198,51 +143,23 @@ def post_config(module, config, result):
if not info or info['status'] != 200:
result['response'] = str(info)
module.fail_json(**result)
module.fail_json(msg='Error occured while posting new config', **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 {
def create_folder(params):
folder = {
'autoNormalize': True,
'copiers': 0,
'devices': devices,
'devices': [],
'disableSparseFiles': False,
'disableTempIndexes': False,
'filesystemType': 'basic',
'fsWatcherDelayS': 10,
'fsWatcherEnabled': params['fs_watcher'],
'fsWatcherEnabled': True,
'hashers': 0,
'id': params['id'],
'ignoreDelete': False,
'ignorePerms': params['ignore_perms'],
'ignorePerms': False,
'label': params['label'] if params['label'] else params['id'],
'markerName': '.stfolder',
'maxConflicts': -1,
@ -257,7 +174,7 @@ def create_folder(params, self_id, current_device_ids, devices_mapping):
'pullerPauseS': 0,
'rescanIntervalS': 3600,
'scanProgressIntervalS': 0,
'type': params['type'],
'type': 'sendreceive',
'useLargeBlocks': False,
'versioning': {
'params': {},
@ -266,6 +183,15 @@ def create_folder(params, self_id, current_device_ids, devices_mapping):
'weakHashThresholdPct': 25
}
# Collect wanted devices to share folder with
for device_id in params['devices']:
folder['devices'].append({
'deviceID': device_id,
'introducedBy': '',
})
return folder
def run_module():
# module arguments
module_args = url_argument_spec()
@ -274,13 +200,8 @@ def run_module():
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']),
@ -309,8 +230,6 @@ def run_module():
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']):
@ -319,21 +238,22 @@ def run_module():
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)
# Bail-out if folder is already added
for folder in config['folders']:
if folder['id'] == module.params['id']:
want_pause = module.params['state'] == 'pause'
if (want_pause and folder['paused']) or \
(not want_pause and not folder['paused']):
module.exit_json(**result)
else:
folder['paused'] = want_pause
result['changed'] = True
elif folder_config != folder_config_wanted:
# Update the folder configuration in-place
folder_config.clear()
folder_config.update(folder_config_wanted)
break
# Append the new folder into configuration
if not result['changed']:
folder = create_folder(module.params)
config['folders'].append(folder)
result['changed'] = True
if result['changed']: