diff --git a/provisioning/roles/datastore/templates/datastore-config-server.py b/provisioning/roles/datastore/templates/datastore-config-server.py index a64703cd..9937e3ef 100644 --- a/provisioning/roles/datastore/templates/datastore-config-server.py +++ b/provisioning/roles/datastore/templates/datastore-config-server.py @@ -1,4 +1,4 @@ -#!/usr/local/bin/python +#!/usr/bin/env python # {{ ansible_managed }} """ Simple XML-RPC Server to run on the datastore server. @@ -10,52 +10,48 @@ library documentation and extended. """ -from xmlrpc.server import SimpleXMLRPCServer -from xmlrpc.server import SimpleXMLRPCRequestHandler -import urllib.request, urllib.error, urllib.parse import hashlib -import subprocess -import os -HASH = '/tmp/hash_datastore' -DATASTORE_CFG = '{{ datastore_config }}' +from pathlib import Path +from urllib.request import urlopen +from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer + +HASH = Path('/tmp/hash_datastore') +DATASTORE_CFG = Path('{{ datastore_config }}') CFG_URL = '{{ datastore_config_url }}' -RELOAD_PATH = '/tmp/uwsgi-reload.me' +RELOAD_PATH = Path('/tmp/uwsgi-reload.me') def reload_datastore(): """Load datastore config and reload datastore, if necessary""" - datastore_cfg = urllib.request.urlopen(CFG_URL).read() + datastore_cfg = urlopen(CFG_URL).read() new_hash = hashlib.sha1(datastore_cfg).hexdigest() try: - with open(HASH) as file: - old_hash = file.readline() + old_hash = HASH.read_text() except OSError: old_hash = None if new_hash == old_hash: return True else: - with open(DATASTORE_CFG, 'wb') as file: - file.write(datastore_cfg) + DATASTORE_CFG.write_bytes(datastore_cfg) # reload uWSGI - with open(RELOAD_PATH, 'a'): - os.utime(RELOAD_PATH, None) + Path(RELOAD_PATH).touch() - with open(HASH, 'w') as file: - file.write(new_hash) + HASH.write_text(new_hash) return True -if __name__ == '__main__': +class RequestHandler(SimpleXMLRPCRequestHandler): # Restrict to a particular path. - class RequestHandler(SimpleXMLRPCRequestHandler): - rpc_paths = ('/RPC2',) + rpc_paths = ('/RPC2',) + +if __name__ == '__main__': # Create server server = SimpleXMLRPCServer(('{{ datastore_host }}', {{datastore_port}}), requestHandler=RequestHandler) server.register_introspection_functions() diff --git a/scripts/fake-datastore-xmlrpc-server.py b/scripts/fake-datastore-xmlrpc-server.py index 8dd2e1dc..6e32c673 100644 --- a/scripts/fake-datastore-xmlrpc-server.py +++ b/scripts/fake-datastore-xmlrpc-server.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python """ Simple XML-RPC Server to run on the datastore server. This daemon should be run on HiSPARC's datastore server. It will @@ -11,11 +11,12 @@ """ import hashlib +from pathlib import Path from urllib.request import urlopen from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer -HASH = '/tmp/hash_datastore' -DATASTORE_CFG = '/tmp/station_list.csv' +HASH = Path('/tmp/hash_datastore') +DATASTORE_CFG = Path('/tmp/station_list.csv') CFG_URL = 'http://publicdb:8000/config/datastore' @@ -26,8 +27,7 @@ def reload_datastore(): new_hash = hashlib.sha1(datastore_cfg).hexdigest() try: - with open(HASH) as file: - old_hash = file.readline() + old_hash = HASH.read_text() except OSError: old_hash = None @@ -35,13 +35,11 @@ def reload_datastore(): print("New hash is old hash") return True else: - with open(DATASTORE_CFG, 'wb') as file: - file.write(datastore_cfg) + DATASTORE_CFG.write_bytes(datastore_cfg) print("New hash received") - with open(HASH, 'w') as file: - file.write(new_hash) + HASH.write_text(new_hash) return True