1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00

python: autodetect API URL

This commit is contained in:
Steffen Vogel 2021-02-24 09:51:27 +01:00
parent 8e53d2a329
commit 94eeded67d

View file

@ -1,4 +1,5 @@
import json import json
import os
import tempfile import tempfile
import subprocess import subprocess
import logging import logging
@ -20,19 +21,31 @@ class Node(object):
self.api_url = api_url self.api_url = api_url
self.log_filename = log_filename self.log_filename = log_filename
self.config = config
self.config_filename = config_filename
self.executable = executable self.executable = executable
if config_filename and config:
raise RuntimeError('Can\'t provide config_filename and '
'config at the same time!')
if config_filename:
with open(self.config_filename) as f:
self.config = json.load(f)
else:
self.config = config
# Try to deduct api_url from config
if self.api_url is None:
port = config.get('http', {}).get('port')
if port is None:
port = 80 if os.getuid() == 0 else 8080
self.api_url = f'http://localhost:{port}'
def start(self): def start(self):
self.config_file = tempfile.NamedTemporaryFile(mode='w+', self.config_file = tempfile.NamedTemporaryFile(mode='w+',
suffix='.json') suffix='.json')
if self.config_filename: json.dump(self.config, self.config_file)
with open(self.config_filename) as f:
self.config_file.write(f.read())
else:
json.dump(self.config, self.config_file)
self.config_file.flush() self.config_file.flush()