Skip to content

Commit

Permalink
Merge pull request #324 from HiSPARC/update-datastore-server
Browse files Browse the repository at this point in the history
Sync real and development datastore server scripts
  • Loading branch information
153957 authored Mar 25, 2024
2 parents cccb65a + 1dc0e0a commit b8048fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
38 changes: 17 additions & 21 deletions provisioning/roles/datastore/templates/datastore-config-server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/bin/python
#!/usr/bin/env python
# {{ ansible_managed }}
""" Simple XML-RPC Server to run on the datastore server.
Expand All @@ -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()
Expand Down
16 changes: 7 additions & 9 deletions scripts/fake-datastore-xmlrpc-server.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'


Expand All @@ -26,22 +27,19 @@ 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

if new_hash == old_hash:
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

Expand Down

0 comments on commit b8048fb

Please sign in to comment.