bridge: bridge can now be set using environment variable:

The `LIBERVIA_BRIDGE_NAME` environment variable can now be used to specify the bridge to
use. If set and different from empty string, the environment has precedence over config
file value.

For `pb` bridge, the following environment variable can also be used:

- LIBERVIA_BRIDGE_PB_CONNECTION_TYPE
- LIBERVIA_BRIDGE_PB_HOST
- LIBERVIA_BRIDGE_PB_PORT
This commit is contained in:
Goffi 2021-08-28 15:26:02 +02:00
commit 547eb9feba
6 changed files with 47 additions and 61 deletions

View file

@ -19,6 +19,7 @@
import dataclasses import dataclasses
from functools import partial
from pathlib import Path from pathlib import Path
from twisted.spread import jelly, pb from twisted.spread import jelly, pb
from twisted.internet import reactor from twisted.internet import reactor
@ -106,12 +107,8 @@ class Bridge(object):
log.info("Init Perspective Broker...") log.info("Init Perspective Broker...")
self.root = PBRoot() self.root = PBRoot()
conf = config.parseMainConf() conf = config.parseMainConf()
conn_type = config.getConfig( getConf = partial(config.getConf, conf, "bridge_pb", "")
conf, conn_type = getConf("connection_type", "unix_socket")
"",
"bridge_pb_connection_type",
"unix_socket"
)
if conn_type == "unix_socket": if conn_type == "unix_socket":
local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
socket_path = local_dir / "bridge_pb" socket_path = local_dir / "bridge_pb"
@ -120,12 +117,7 @@ class Bridge(object):
str(socket_path), pb.PBServerFactory(self.root), mode=0o600 str(socket_path), pb.PBServerFactory(self.root), mode=0o600
) )
elif conn_type == "socket": elif conn_type == "socket":
port = int(config.getConfig( port = int(getConf("port", 8789))
conf,
"",
"bridge_pb_port",
8789
))
log.info(f"using TCP Socket at port {port}") log.info(f"using TCP Socket at port {port}")
reactor.listenTCP(port, pb.PBServerFactory(self.root)) reactor.listenTCP(port, pb.PBServerFactory(self.root))
else: else:

View file

@ -140,29 +140,15 @@ class Bridge(object):
def bridgeConnect(self, callback, errback): def bridgeConnect(self, callback, errback):
factory = pb.PBClientFactory() factory = pb.PBClientFactory()
conf = config.parseMainConf() conf = config.parseMainConf()
conn_type = config.getConfig( getConf = partial(config.getConf, conf, "bridge_pb", "")
conf, conn_type = getConf("connection_type", "unix_socket")
"",
"bridge_pb_connection_type",
"unix_socket"
)
if conn_type == "unix_socket": if conn_type == "unix_socket":
local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
socket_path = local_dir / "bridge_pb" socket_path = local_dir / "bridge_pb"
reactor.connectUNIX(str(socket_path), factory) reactor.connectUNIX(str(socket_path), factory)
elif conn_type == "socket": elif conn_type == "socket":
host = int(config.getConfig( host = getConf("host", "localhost")
conf, port = int(getConf("port", 8789))
"",
"bridge_pb_host",
"localhost"
))
port = int(config.getConfig(
conf,
"",
"bridge_pb_port",
8789
))
reactor.connectTCP(host, port, factory) reactor.connectTCP(host, port, factory)
else: else:
raise ValueError(f"Unknown pb connection type: {conn_type!r}") raise ValueError(f"Unknown pb connection type: {conn_type!r}")

View file

@ -19,6 +19,7 @@
import dataclasses import dataclasses
from functools import partial
from pathlib import Path from pathlib import Path
from twisted.spread import jelly, pb from twisted.spread import jelly, pb
from twisted.internet import reactor from twisted.internet import reactor
@ -106,12 +107,8 @@ class Bridge(object):
log.info("Init Perspective Broker...") log.info("Init Perspective Broker...")
self.root = PBRoot() self.root = PBRoot()
conf = config.parseMainConf() conf = config.parseMainConf()
conn_type = config.getConfig( getConf = partial(config.getConf, conf, "bridge_pb", "")
conf, conn_type = getConf("connection_type", "unix_socket")
"",
"bridge_pb_connection_type",
"unix_socket"
)
if conn_type == "unix_socket": if conn_type == "unix_socket":
local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
socket_path = local_dir / "bridge_pb" socket_path = local_dir / "bridge_pb"
@ -120,12 +117,7 @@ class Bridge(object):
str(socket_path), pb.PBServerFactory(self.root), mode=0o600 str(socket_path), pb.PBServerFactory(self.root), mode=0o600
) )
elif conn_type == "socket": elif conn_type == "socket":
port = int(config.getConfig( port = int(getConf("port", 8789))
conf,
"",
"bridge_pb_port",
8789
))
log.info(f"using TCP Socket at port {port}") log.info(f"using TCP Socket at port {port}")
reactor.listenTCP(port, pb.PBServerFactory(self.root)) reactor.listenTCP(port, pb.PBServerFactory(self.root))
else: else:

View file

@ -85,7 +85,10 @@ class SAT(service.Service):
trigger.TriggerManager() trigger.TriggerManager()
) )
bridge_name = self.memory.getConfig("", "bridge", "dbus") bridge_name = (
os.getenv("LIBERVIA_BRIDGE_NAME")
or self.memory.getConfig("", "bridge", "dbus")
)
bridge_module = dynamic_import.bridge(bridge_name) bridge_module = dynamic_import.bridge(bridge_name)
if bridge_module is None: if bridge_module is None:

View file

@ -23,6 +23,7 @@
import os import os
import csv import csv
import json import json
from typing import Any
from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError
from xdg import BaseDirectory from xdg import BaseDirectory
from sat.core.log import getLogger from sat.core.log import getLogger
@ -142,3 +143,29 @@ def getConfig(config, section, name, default=None):
except ValueError as e: except ValueError as e:
raise exceptions.ParsingError("Error while parsing data: {}".format(e)) raise exceptions.ParsingError("Error while parsing data: {}".format(e))
return value return value
def getConf(
conf: ConfigParser,
prefix: str,
section: str,
name: str,
default: Any
) -> Any:
"""Get configuration value from environment or config file
@param str: prefix to use for the varilable name (see `name` below)
@param section: config section to use
@param name: unsuffixed name.
For environment variable, `LIBERVIA_<prefix>_` will be prefixed (and name
will be set to uppercase).
For config file, `<prefix>_` will be prefixed (and DEFAULT section will be
used).
Environment variable has priority over config values. If Environment variable
is set but empty string, config value will be used.
@param default: default value to use if varilable is set neither in environment,
nor in config
"""
# XXX: This is a temporary method until parameters are refactored
value = os.getenv(f"LIBERVIA_{prefix}_{name}".upper())
return value or getConfig(conf, section, f"{prefix}_{name}", default)

View file

@ -140,29 +140,15 @@ class Bridge(object):
def bridgeConnect(self, callback, errback): def bridgeConnect(self, callback, errback):
factory = pb.PBClientFactory() factory = pb.PBClientFactory()
conf = config.parseMainConf() conf = config.parseMainConf()
conn_type = config.getConfig( getConf = partial(config.getConf, conf, "bridge_pb", "")
conf, conn_type = getConf("connection_type", "unix_socket")
"",
"bridge_pb_connection_type",
"unix_socket"
)
if conn_type == "unix_socket": if conn_type == "unix_socket":
local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
socket_path = local_dir / "bridge_pb" socket_path = local_dir / "bridge_pb"
reactor.connectUNIX(str(socket_path), factory) reactor.connectUNIX(str(socket_path), factory)
elif conn_type == "socket": elif conn_type == "socket":
host = int(config.getConfig( host = getConf("host", "localhost")
conf, port = int(getConf("port", 8789))
"",
"bridge_pb_host",
"localhost"
))
port = int(config.getConfig(
conf,
"",
"bridge_pb_port",
8789
))
reactor.connectTCP(host, port, factory) reactor.connectTCP(host, port, factory)
else: else:
raise ValueError(f"Unknown pb connection type: {conn_type!r}") raise ValueError(f"Unknown pb connection type: {conn_type!r}")