def add_device(data): validate_data(data) syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config remote_device_id = data['id'] remove_device_name = data['name'] remote_device_address = data.get('address', 'dynamic') devices = list( filter(lambda d: d['deviceID'] == remote_device_id, config['devices'])) dirty = False if not devices: device = { 'addresses': [remote_device_address], 'certName': '', 'compression': 'metadata', 'deviceID': remote_device_id, 'introducer': False, 'name': remove_device_name } config['devices'].append(device) dirty = True if not dirty: return syncthing.set_config(config) syncthing.restart()
def add_device_to_share(data): validate_data(data) syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config remote_device_id = data['device_id'] folder_id = data['folder_id'] # add device to shared folder. folders = list(filter(lambda f: f['id'] == folder_id, config['folders'])) if not folders: raise Exception('No share with path=%s' % data['path']) folder = folders[0] dirty = False if not list( filter(lambda d: d['deviceID'] == remote_device_id, folder['devices'])): # share folder with device. folder['devices'].append({'deviceID': remote_device_id}) dirty = True if not dirty: return syncthing.set_config(config) syncthing.restart()
def create_share(data): validate_data(data) syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config # add device to shared folder. folders = list( filter(lambda f: f['path'] == data['path'], config['folders'])) folder_id = data['name'] folder_path = os.path.join(sync.settings['agent-home'], data['path']) dirty = False if not folders: # add folder. folder = { 'autoNormalize': False, 'copiers': 1, 'devices': [{ 'deviceID': syncthing.device_id }], 'hashers': 0, 'id': folder_id, 'ignorePerms': False, 'invalid': '', 'order': 'random', 'path': folder_path, 'pullers': 16, 'readOnly': data['readonly'], 'rescanIntervalS': 60, 'versioning': { 'params': {}, 'type': '' } } if not os.path.isdir(folder_path): os.makedirs(folder_path, 0o755) config['folders'].append(folder) dirty = True else: folder = folders[0] if dirty: syncthing.set_config(config) ignore = data['ignore'] or [] if ignore: ignore_url = '/rest/db/ignores?%s' % urllib.urlencode( {'folder': folder_id}) syncthing.post(ignore_url, {'ignore': ignore}) syncthing.restart() return folder
def list_shares(data): syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config folders = config['folders'] results = [] for folder in folders: skip = False for internal_path in INTERNAL: if folder['path'].startswith(internal_path): skip = True break if skip: continue results.append(folder) return results
def get_ignore(data): validate_data(data) syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config # add device to shared folder. folders = list(filter(lambda f: f['id'] == data['name'], config['folders'])) if not folders: raise Exception('No share with name=%s' % data['name']) else: folder = folders[0] response = syncthing.get('/rest/db/need', {'folder': folder['id']}) if not response.ok: raise Exception('Could not get share ignore list: %s' % response.reason) return response.json()
def sync_folder(data): """ This action is only (and should be only) used by the agent controller to setup the jumpscripts synching """ validate_data(data) syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config remote_device_id = data['device_id'] remote_device_address = data.get('address', 'dynamic') remove_device_name = data.get('name', remote_device_id.split('-')[0]) devices = list(filter(lambda d: d['deviceID'] == remote_device_id, config['devices'])) dirty = False if not devices: device = { 'addresses': [remote_device_address], 'certName': '', 'compression': 'metadata', 'deviceID': remote_device_id, 'introducer': False, 'name': remove_device_name } config['devices'].append(device) dirty = True else: device = devices[0] # Compare address, in case device changed it's address if device['addresses'][0] != remote_device_address: device['addresses'] = [remote_device_address] dirty = True # add device to shared folder. folders = list(filter(lambda f: f['id'] == data['folder_id'], config['folders'])) folder_path = os.path.join(sync.settings['agent-home'], data['path'], sync.settings['controller-name']) if not folders: # add folder. folder = { 'autoNormalize': False, 'copiers': 1, 'devices': [{'deviceID': syncthing.device_id}], 'hashers': 0, 'id': data['folder_id'], 'ignorePerms': False, 'invalid': '', 'order': 'random', 'path': folder_path, 'pullers': 16, 'readOnly': False, 'rescanIntervalS': 60, 'versioning': {'params': {}, 'type': ''} } if not os.path.isdir(folder_path): os.makedirs(folder_path, 0o755) config['folders'].append(folder) dirty = True else: folder = folders[0] if not list(filter(lambda d: d['deviceID'] == remote_device_id, folder['devices'])): # share folder with device. folder['devices'].append({ 'deviceID': remote_device_id }) dirty = True if not dirty: return syncthing.set_config(config)
def list_devices(data): syncthing = api.Syncthing(sync.SYNCTHING_URL) config = syncthing.config return config['devices']
def scan_folder(data): syncthing = api.Syncthing(sync.SYNCTHING_URL) syncthing.scan(data['name'], data['sub'])
def get_id(data): syncthing = api.Syncthing(sync.SYNCTHING_URL) return syncthing.device_id
def restart(data): syncthing = api.Syncthing(sync.SYNCTHING_URL) syncthing.restart()