From 209271cbf77e1127056abda96379088db339f3e6 Mon Sep 17 00:00:00 2001 From: Alexander Dietrich Date: Mon, 10 Jul 2017 21:48:25 +0200 Subject: [PATCH] Use tempfiles when updating JSON --- backend.py | 15 ++++++++++++--- mkmap.sh | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend.py b/backend.py index b2f38b7..74d3e52 100755 --- a/backend.py +++ b/backend.py @@ -27,8 +27,13 @@ def main(params): os.makedirs(params['dest_dir'], exist_ok=True) nodes_fn = os.path.join(params['dest_dir'], 'nodes.json') + tmp_nodes_fn = os.path.join(params['dest_dir'], 'nodes.json.tmp') + graph_fn = os.path.join(params['dest_dir'], 'graph.json') + tmp_graph_fn = os.path.join(params['dest_dir'], 'graph.json.tmp') + nodelist_fn = os.path.join(params['dest_dir'], 'nodelist.json') + tmp_nodelist_fn = os.path.join(params['dest_dir'], 'nodelist.json.tmp') now = datetime.utcnow().replace(microsecond=0) @@ -137,18 +142,22 @@ def main(params): batadv_graph = graph.to_undirected(batadv_graph) # write processed data to dest dir - with open(nodes_fn, 'w') as f: + with open(tmp_nodes_fn, 'w') as f: json.dump(nodedb, f) graph_out = {'batadv': json_graph.node_link_data(batadv_graph), 'version': GRAPH_VERSION} - with open(graph_fn, 'w') as f: + with open(tmp_graph_fn, 'w') as f: json.dump(graph_out, f) - with open(nodelist_fn, 'w') as f: + with open(tmp_nodelist_fn, 'w') as f: json.dump(export_nodelist(now, nodedb), f) + os.rename(tmp_nodes_fn, nodes_fn) + os.rename(tmp_graph_fn, graph_fn) + os.rename(tmp_nodelist_fn, nodelist_fn) + # optional rrd graphs (trigger with --rrd) if params['rrd']: script_directory = os.path.dirname(os.path.realpath(__file__)) diff --git a/mkmap.sh b/mkmap.sh index b49f9a5..2d9b0f6 100755 --- a/mkmap.sh +++ b/mkmap.sh @@ -3,4 +3,4 @@ FFMAPPATH='/opt/ffmap-backend/' PEERS="/etc/fastd/ffhh-mesh-vpn/peers" python2 $FFMAPPATH/generate_aliases.py $PEERS > $FFMAPPATH/aliases.json -python3 $FFMAPPATH/backend.py -d /var/www/meshviewer/ --aliases $FFMAPPATH/aliases.json $FFMAPPATH/gateway.json -m bat0:/var/run/alfred.sock -p 30 --vpn de:ad:be:ff:01:01 --vpn de:ad:be:ff:05:05 --vpn de:ad:be:ff:05:06 --vpn de:ad:be:ff:03:03 --vpn de:ad:be:ff:22:22 --vpn de:ad:be:ff:22:23 --vpn de:ad:be:ff:88:88 --vpn de:ad:be:ff:88:89 --vpn de:ad:bf:ff:88:88 --vpn de:ad:bf:ff:22:22 --vpn de:ad:bf:ff:03:03 --vpn de:ad:bf:ff:05:05 --vpn de:ad:bf:ff:01:01 +python3 $FFMAPPATH/backend.py -d /var/www/meshviewer/ --aliases $FFMAPPATH/aliases.json $FFMAPPATH/gateway.json -m bat0:/var/run/alfred.sock -p 30 --vpn de:ad:be:ff:01:01 --vpn de:ad:be:ff:05:05 --vpn de:ad:be:ff:05:06 --vpn de:ad:be:ff:03:03 --vpn de:ad:be:ff:22:22 --vpn de:ad:be:ff:22:23 --vpn de:ad:be:ff:88:88 --vpn de:ad:be:ff:88:89 --vpn de:ad:bf:ff:88:88 --vpn de:ad:bf:ff:22:22 --vpn de:ad:bf:ff:03:03 --vpn de:ad:bf:ff:05:05 --vpn de:ad:bf:ff:01:01 --vpn de:ad:be:fc:03:03 --vpn 00:16:3e:53:75:0d --vpn de:ad:be:fc:05:05 --vpn de:ad:be:fc:01:01 --vpn de:ad:be:ef:03:03 --vpn de:ad:be:ef:01:01 --vpn de:ad:be:ef:05:05