config: clean up the configuration valu of file paths

parent d3ec6d68
BEIRAN_LOG_LEVEL=DEBUG
BEIRAN_LOG_FILE=$(pwd)/beirand.log
BEIRAN_RUN_DIR=/var/run
BEIRAN_SOCK=${BEIRAN_RUN_DIR}/beirand.sock
BEIRAN_DB_PATH=$(pwd)/beiran.db
BEIRAN_SOCKET_FILE=${BEIRAN_RUN_DIR}/beirand.sock
BEIRAN_DB_FILE=$(pwd)/beiran.db
BEIRAN_CONFIG_DIR=$(pwd)
\ No newline at end of file
......@@ -72,8 +72,8 @@ $ ./dev.sh
```sh
export BEIRAN_LOG_LEVEL=DEBUG
export BEIRAN_LOG_FILE=$(pwd)/beirand.log
export BEIRAN_SOCK=$(pwd)/beirand.sock
export BEIRAN_DB_PATH=$(pwd)/beiran.db
export BEIRAN_SOCKET_FILE=$(pwd)/beirand.sock
export BEIRAN_DB_FILE=$(pwd)/beiran.db
export BEIRAN_CONFIG_DIR=$(pwd)
```
......@@ -111,7 +111,7 @@ All has default values.
LISTEN_INTERFACE
LISTEN_ADDR
HOSTNAME
BEIRAN_SOCK
BEIRAN_SOCKET_FILE
```
## Using (PoC)
......
......@@ -21,7 +21,6 @@
#!/bin/env python
"""command line client for managing beiran daemon"""
import os
import sys
import logging
import importlib
......@@ -47,12 +46,8 @@ class BeiranContext:
"""Context object for Beiran Commands which keeps clients and other common objects"""
def __init__(self) -> None:
if 'BEIRAN_SOCK' in os.environ:
daemon_url = "http+unix://" + os.environ['BEIRAN_SOCK']
elif 'BEIRAN_URL' in os.environ:
daemon_url = os.environ['BEIRAN_URL']
else:
daemon_url = "http+unix://{}/beirand.sock".format(config.run_dir)
daemon_url = config.url if config.url else \
"http+unix://" + config.socket_file
peer_address = PeerAddress(address=daemon_url)
self.beiran_url = peer_address.location
......
......@@ -34,16 +34,21 @@ LOGGER = logging.getLogger()
DEFAULTS = {
'LISTEN_PORT': 8888,
'LISTEN_ADDRESS': '0.0.0.0',
'LOG_FILE': '/var/log/beirand.log',
'LOG_LEVEL': 'DEBUG',
'CONFIG_DIR': '/etc/beiran',
'DATA_DIR': '/var/lib/beiran',
'CACHE_DIR': '/var/cache/beiran',
'RUN_DIR': '/var/run',
'DISCOVERY_METHOD': 'zeroconf',
'URL': None,
'KNOWN_NODES': [],
}
DEFAULT_FILE_PATHS = {
'LOG_FILE': '/var/log/beirand.log',
'DB_FILE': os.path.join(DEFAULTS['DATA_DIR'], 'beirand.log'), # type: ignore
'SOCKET_FILE': os.path.join(DEFAULTS['RUN_DIR'], 'beirand.sock'), # type: ignore
}
class ConfigMeta(type):
"""
......@@ -86,7 +91,8 @@ class Config(metaclass=ConfigMeta):
val = val[key]
return val
def get_config(self, ckey: str = '', ekey: str = '') -> Union[Any, object]:
def get_config(self, ckey: str = '', ekey: str = '',
isfile: bool = False) -> Union[Any, object]:
"""
Seek for config val through environment and config depending
on given keys.
......@@ -104,18 +110,20 @@ class Config(metaclass=ConfigMeta):
if not any([ckey, ekey]):
return None
defaults = DEFAULT_FILE_PATHS if isfile else DEFAULTS
env_value = os.getenv("BEIRAN_{}".format(ekey))
if env_value:
if isinstance(DEFAULTS[ekey], list):
if isinstance(defaults[ekey], list):
return env_value.split(',')
if isinstance(DEFAULTS[ekey], int):
if isinstance(defaults[ekey], int):
return int(env_value)
return \
env_value or \
self.get_config_from_file(ckey) or \
DEFAULTS.get(ekey, None)
defaults.get(ekey, None)
@staticmethod
def load_from_file(config_file: str):
......@@ -220,7 +228,7 @@ class Config(metaclass=ConfigMeta):
Environment variable: ``BEIRAN_LOG_FILE``
"""
return self.get_config('beiran.log_file', 'LOG_FILE')
return self.get_config('beiran.log_file', 'LOG_FILE', isfile=True)
@property
def discovery_method(self):
......@@ -262,6 +270,18 @@ class Config(metaclass=ConfigMeta):
"""
return self.get_config('beiran.listen_port', 'LISTEN_PORT')
@property
def url(self):
"""
Beiran daemon's URL. The default value is ``None``
config.toml: section ``beiran``, key ``url``
Environment variable: ``BEIRAN_URL``
"""
return self.get_config('beiran.url', 'URL')
@property
def known_nodes(self):
"""
......@@ -275,6 +295,32 @@ class Config(metaclass=ConfigMeta):
"""
return self.get_config('beiran.known_nodes', 'KNOWN_NODES')
@property
def db_file(self):
"""
A file path for database. The default value is
``/var/lib/beiran/beiran.db``.
config.toml: section ``beiran``, key ``db_file``
Environment variable: ``BEIRAN_DB_FILE``
"""
return self.get_config('beiran.db_file', 'DB_FILE', isfile=True)
@property
def socket_file(self):
"""
A file path for socket. The default value is
``/var/run/beirand.sock``.
config.toml: section ``beiran``, key ``socket_file``
Environment variable: ``BEIRAN_SOCKET_FILE``
"""
return self.get_config('beiran.socket_file', 'SOCKET_FILE', isfile=True)
@property
def plugin_types(self):
"""Return the list of supported plugin types"""
......
......@@ -9,8 +9,8 @@ data_dir = '/var/lib/beiran'
run_dir = '/var/run'
listen_port = 8888
listen_address = '0.0.0.0'
socket_path = '/var/run/beirand.sock'
daemon_url = ''
socket_file = '/var/run/beirand.sock'
url = ''
discovery_method = 'zeroconf'
# docker plugin settings
......
......@@ -19,10 +19,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""HTTP and WS API implementation of beiran daemon"""
import os
from tornado import websocket, web
from tornado.options import options, define
from tornado.options import define
from tornado.web import HTTPError
from beiran.config import config
......@@ -43,14 +41,10 @@ define('listen_port',
help='Listen port')
define('unix_socket',
group='webserver',
default=config.run_dir + "/beirand.sock",
default=config.socket_file,
help='Path to unix socket to bind')
if 'BEIRAN_SOCK' in os.environ:
options.unix_socket = os.environ['BEIRAN_SOCK']
class EchoWebSocket(websocket.WebSocketHandler):
""" Websocket implementation for test
"""
......
......@@ -216,7 +216,7 @@ class BeiranDaemon(EventEmitter):
logger.setLevel(logging.INFO)
# check database file exists
beiran_db_path = os.getenv("BEIRAN_DB_PATH", '{}/beiran.db'.format(config.data_dir))
beiran_db_path = config.db_file
db_file_exists = os.path.exists(beiran_db_path)
if not db_file_exists:
......
......@@ -66,10 +66,10 @@ export PATH="${VIRTUAL_ENV_DIR}/bin:$PATH"
export BEIRAN_LOG_LEVEL=DEBUG
export BEIRAN_LOG_FILE=${DIR}/beirand.log
export BEIRAN_RUN_DIR=${DIR}
export BEIRAN_SOCK=${BEIRAN_RUN_DIR}/beirand.sock
export BEIRAN_SOCKET_FILE=${BEIRAN_RUN_DIR}/beirand.sock
export BEIRAN_PORT=${BEIRAN_PORT:-8888}
export BEIRAN_URL=http://localhost:${BEIRAN_PORT}
export BEIRAN_DB_PATH=${DIR}/beiran.db
export BEIRAN_DB_FILE=${DIR}/beiran.db
export LISTEN_ADDR=0.0.0.0
export BEIRAN_CONFIG_DIR=${DIR}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment