refactored syncthing client methods into SyncthingClient class
This commit is contained in:
		
							parent
							
								
									3589064dd0
								
							
						
					
					
						commit
						4c45a7d72b
					
				|  | @ -1,21 +1,24 @@ | |||
| from syncthing_monitor.config_xml import parse_api_key, set_listen_ip_to_any | ||||
| import syncthing_monitor.config_xml as xml | ||||
| from syncthing_monitor.etcd_cluster_info import append_device_id, get_device_list | ||||
| from syncthing_monitor.syncthing_rest import get_device_id, print_debug_info, post_devices | ||||
| from syncthing_monitor.syncthing_rest import SyncthingClient | ||||
| 
 | ||||
| DEFAULT_CONFIG_XML_PATH = '/config/config.xml' | ||||
| 
 | ||||
| 
 | ||||
| def loop(gui_port="8384", host="sync"): | ||||
|     api_key = parse_api_key() | ||||
|     api_key = xml.parse_api_key(DEFAULT_CONFIG_XML_PATH) | ||||
|     print("Found API Key: {0}".format(api_key)) | ||||
| 
 | ||||
|     set_listen_ip_to_any() | ||||
|     xml.set_listen_ip_to_any(DEFAULT_CONFIG_XML_PATH, DEFAULT_CONFIG_XML_PATH, 8384) | ||||
| 
 | ||||
|     device_id = get_device_id(host, gui_port, api_key) | ||||
|     print("Found Device ID: {0}".format(device_id)) | ||||
|     syncthing = SyncthingClient(host, gui_port, api_key) | ||||
|     my_device_id = syncthing.get_my_device_id() | ||||
|     print("Found Device ID: {0}".format(my_device_id)) | ||||
| 
 | ||||
|     append_device_id(device_id, 'etcd') | ||||
|     append_device_id(my_device_id, 'etcd') | ||||
|     device_ids = get_device_list('etcd') | ||||
|     post_devices(device_ids, host, gui_port, api_key) | ||||
|     print_debug_info(host, gui_port, api_key) | ||||
|     syncthing.post_devices(device_ids) | ||||
|     syncthing.print_debug_info() | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|  |  | |||
|  | @ -4,46 +4,53 @@ import requests | |||
| from retrying import retry | ||||
| 
 | ||||
| 
 | ||||
| @retry | ||||
| def get_device_id(host, gui_port, api_key): | ||||
|     syncthing_headers = {'X-API-Key': api_key} | ||||
|     response = requests.get("http://" + host + ":" + gui_port + "/rest/system/status", headers=syncthing_headers) | ||||
|     return json.loads(response.content)["myID"] | ||||
| class SyncthingClient: | ||||
| 
 | ||||
|     def __init__(self, host, port, key): | ||||
|         self.host = host | ||||
|         self.port = port | ||||
|         self.key = key | ||||
| 
 | ||||
| @retry | ||||
| def print_debug_info(host, gui_port, api_key): | ||||
|     syncthing_headers = {'X-API-Key': api_key} | ||||
|     response = requests.get("http://" + host + ":" + gui_port + "/rest/config/devices", headers=syncthing_headers) | ||||
|     print("/rest/config/devices: {0}".format(response.content)) | ||||
|     @retry | ||||
|     def get_my_device_id(self): | ||||
|         syncthing_headers = {'X-API-Key': self.key} | ||||
|         response = requests.get("http://" + self.host + ":" + self.port + "/rest/system/status", | ||||
|                                 headers=syncthing_headers) | ||||
|         return json.loads(response.content)["myID"] | ||||
| 
 | ||||
|     @retry | ||||
|     def print_debug_info(self): | ||||
|         syncthing_headers = {'X-API-Key': self.key} | ||||
|         response = requests.get("http://" + self.host + ":" + self.port + "/rest/config/devices", | ||||
|                                 headers=syncthing_headers) | ||||
|         print("/rest/config/devices: {0}".format(response.content)) | ||||
| 
 | ||||
| def post_devices(device_ids, host, gui_port, api_key): | ||||
|     syncthing_headers = {'X-API-Key': api_key} | ||||
|     def post_devices(self, device_ids): | ||||
|         syncthing_headers = {'X-API-Key': self.key} | ||||
| 
 | ||||
|     for device_id in device_ids: | ||||
|         post_data = { | ||||
|             "deviceID": device_id, | ||||
|             "name": "Laptop", | ||||
|             "addresses": [ | ||||
|                 "dynamic", | ||||
|                 "tcp://192.168.1.2:22000" | ||||
|             ], | ||||
|             "compression": "metadata", | ||||
|             "certName": "", | ||||
|             "introducer": False, | ||||
|             "skipIntroductionRemovals": False, | ||||
|             "introducedBy": "", | ||||
|             "paused": False, | ||||
|             "allowedNetworks": [], | ||||
|             "autoAcceptFolders": False, | ||||
|             "maxSendKbps": 0, | ||||
|             "maxRecvKbps": 0, | ||||
|             "ignoredFolders": [], | ||||
|             "pendingFolders": [], | ||||
|             "maxRequestKiB": 0 | ||||
|         } | ||||
|         for device_id in device_ids: | ||||
|             post_data = { | ||||
|                 "deviceID": device_id, | ||||
|                 "name": "Laptop", | ||||
|                 "addresses": [ | ||||
|                     "dynamic", | ||||
|                     "tcp://192.168.1.2:22000" | ||||
|                 ], | ||||
|                 "compression": "metadata", | ||||
|                 "certName": "", | ||||
|                 "introducer": False, | ||||
|                 "skipIntroductionRemovals": False, | ||||
|                 "introducedBy": "", | ||||
|                 "paused": False, | ||||
|                 "allowedNetworks": [], | ||||
|                 "autoAcceptFolders": False, | ||||
|                 "maxSendKbps": 0, | ||||
|                 "maxRecvKbps": 0, | ||||
|                 "ignoredFolders": [], | ||||
|                 "pendingFolders": [], | ||||
|                 "maxRequestKiB": 0 | ||||
|             } | ||||
| 
 | ||||
|         response = requests.post("http://" + host + ":" + gui_port + "/rest/config/devices", | ||||
|                                  headers=syncthing_headers, data=json.dumps(post_data)) | ||||
|         print("Attempt to add device {0} to syncthing: {1}".format(device_id, response.content)) | ||||
|             response = requests.post("http://" + self.host + ":" + self.port + "/rest/config/devices", | ||||
|                                      headers=syncthing_headers, data=json.dumps(post_data)) | ||||
|             print("Attempt to add device {0} to syncthing: {1}".format(device_id, response.content)) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user