comparison sat/bridge/pb.py @ 3634:3c7a64d6f49f

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
author Goffi <goffi@goffi.org>
date Sat, 28 Aug 2021 15:26:02 +0200
parents d71a163c0861
children 524856bd7b19
comparison
equal deleted inserted replaced
3632:7acf19bdca2f 3634:3c7a64d6f49f
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 import dataclasses 21 import dataclasses
22 from functools import partial
22 from pathlib import Path 23 from pathlib import Path
23 from twisted.spread import jelly, pb 24 from twisted.spread import jelly, pb
24 from twisted.internet import reactor 25 from twisted.internet import reactor
25 from sat.core.log import getLogger 26 from sat.core.log import getLogger
26 from sat.tools import config 27 from sat.tools import config
104 class Bridge(object): 105 class Bridge(object):
105 def __init__(self): 106 def __init__(self):
106 log.info("Init Perspective Broker...") 107 log.info("Init Perspective Broker...")
107 self.root = PBRoot() 108 self.root = PBRoot()
108 conf = config.parseMainConf() 109 conf = config.parseMainConf()
109 conn_type = config.getConfig( 110 getConf = partial(config.getConf, conf, "bridge_pb", "")
110 conf, 111 conn_type = getConf("connection_type", "unix_socket")
111 "",
112 "bridge_pb_connection_type",
113 "unix_socket"
114 )
115 if conn_type == "unix_socket": 112 if conn_type == "unix_socket":
116 local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() 113 local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
117 socket_path = local_dir / "bridge_pb" 114 socket_path = local_dir / "bridge_pb"
118 log.info(f"using UNIX Socket at {socket_path}") 115 log.info(f"using UNIX Socket at {socket_path}")
119 reactor.listenUNIX( 116 reactor.listenUNIX(
120 str(socket_path), pb.PBServerFactory(self.root), mode=0o600 117 str(socket_path), pb.PBServerFactory(self.root), mode=0o600
121 ) 118 )
122 elif conn_type == "socket": 119 elif conn_type == "socket":
123 port = int(config.getConfig( 120 port = int(getConf("port", 8789))
124 conf,
125 "",
126 "bridge_pb_port",
127 8789
128 ))
129 log.info(f"using TCP Socket at port {port}") 121 log.info(f"using TCP Socket at port {port}")
130 reactor.listenTCP(port, pb.PBServerFactory(self.root)) 122 reactor.listenTCP(port, pb.PBServerFactory(self.root))
131 else: 123 else:
132 raise ValueError(f"Unknown pb connection type: {conn_type!r}") 124 raise ValueError(f"Unknown pb connection type: {conn_type!r}")
133 125