Commit 4075e1e8 authored by Pavel Spirek's avatar Pavel Spirek

Minor code structure changes

parent 9eaed9f0
...@@ -2,24 +2,24 @@ GLOBAL: ...@@ -2,24 +2,24 @@ GLOBAL:
TIMEZONE: "Europe/Prague" TIMEZONE: "Europe/Prague"
LOGFILE: "-" LOGFILE: "-"
PIDFILE: "/tmp/jetconf.pid" PIDFILE: "/tmp/jetconf.pid"
PERSISTENT_CHANGES: false PERSISTENT_CHANGES: true
LOG_LEVEL: "debug" LOG_LEVEL: "info"
LOG_DBG_MODULES: ["usr_conf_data_handlers", "knot_api", "nacm", "data"] LOG_DBG_MODULES: ["usr_conf_data_handlers", "knot_api", "nacm", "data"]
DATA_JSON_FILE: "/home/pspirek/dev-sw/jetconf/data/example-data.json" DATA_JSON_FILE: "/home/user/jetconf/data/example-data.json"
HTTP_SERVER: HTTP_SERVER:
DOC_ROOT: "/home/pspirek/dev-sw/jetconf/data/doc-root" DOC_ROOT: "/home/user/jetconf/data/doc-root"
DOC_DEFAULT_NAME: "index.html" DOC_DEFAULT_NAME: "index.html"
API_ROOT: "/restconf" API_ROOT: "/restconf"
SERVER_NAME: "jetconf-h2" SERVER_NAME: "jetconf-h2"
SERVER_SSL_CERT: "/home/pspirek/dev-sw/jetconf/data/server.crt" SERVER_SSL_CERT: "/home/user/jetconf/data/server.crt"
SERVER_SSL_PRIVKEY: "/home/pspirek/dev-sw/jetconf/data/server.key" SERVER_SSL_PRIVKEY: "/home/user/jetconf/data/server.key"
CA_CERT: "/home/pspirek/dev-sw/jetconf/data/ca.pem" CA_CERT: "/home/user/jetconf/data/ca.pem"
DBG_DISABLE_CERTS: false DBG_DISABLE_CERTS: false
NACM: NACM:
ALLOWED_USERS: ["lojza@mail.cz"] ALLOWED_USERS: ["lojza@mail.cz"]
KNOT: KNOT:
SOCKET: "/home/pspirek/knot-conf/knot.sock" SOCKET: "/home/user/knot-conf/knot.sock"
This diff is collapsed.
...@@ -7,12 +7,16 @@ import signal ...@@ -7,12 +7,16 @@ import signal
from colorlog import error, info from colorlog import error, info
from yaml.parser import ParserError from yaml.parser import ParserError
from yangson.enumerations import ContentType, ValidationScope from yangson.enumerations import ContentType, ValidationScope
from yangson.exception import YangsonException
from yangson.schema import SchemaError, SemanticError
from . import usr_op_handlers, usr_state_data_handlers from . import usr_op_handlers, usr_state_data_handlers
from .rest_server import RestServer from .rest_server import RestServer
from .config import CONFIG_GLOBAL, load_config, print_config from .config import CONFIG_GLOBAL, load_config, print_config
from .data import JsonDatastore from .data import JsonDatastore
from .helpers import DataHelpers from .helpers import DataHelpers, ErrorHelpers
from .handler_list import OP_HANDLERS, STATE_DATA_HANDLES, CONF_DATA_HANDLES from .handler_list import OP_HANDLERS, STATE_DATA_HANDLES, CONF_DATA_HANDLES
from .knot_api import knot_global_init, knot_connect, knot_disconnect from .knot_api import knot_global_init, knot_connect, knot_disconnect
from .usr_conf_data_handlers import ( from .usr_conf_data_handlers import (
...@@ -26,17 +30,17 @@ from .usr_conf_data_handlers import ( ...@@ -26,17 +30,17 @@ from .usr_conf_data_handlers import (
def main(): def main():
config_file = "jetconf/config.yaml" config_file = "config.yaml"
# Parse command line arguments # Parse command line arguments
try: try:
opts, args = getopt.getopt(sys.argv[1:], "c:") opts, args = getopt.getopt(sys.argv[1:], "c:")
except getopt.GetoptError: except getopt.GetoptError:
print("Invalid argument detected. Possibles are: -c (config file)") print("Invalid argument detected. Possible options are: -c (config file)")
sys.exit(1) sys.exit(1)
for opt, arg in opts: for opt, arg in opts:
if opt == '-c': if opt == "-c":
config_file = arg config_file = arg
# Load configuration # Load configuration
...@@ -133,16 +137,29 @@ def main(): ...@@ -133,16 +137,29 @@ def main():
signal.signal(signal.SIGINT, sig_exit_handler) signal.signal(signal.SIGINT, sig_exit_handler)
# Load data model # Load data model
datamodel = DataHelpers.load_data_model(CONFIG_GLOBAL["YANG_LIB_DIR"], CONFIG_GLOBAL["YANG_LIB_DIR"] + "yang-library-data.json") datamodel = DataHelpers.load_data_model(
CONFIG_GLOBAL["YANG_LIB_DIR"],
CONFIG_GLOBAL["YANG_LIB_DIR"] + "yang-library-data.json"
)
# Datastore init # Datastore init
datastore = JsonDatastore(datamodel, CONFIG_GLOBAL["DATA_JSON_FILE"], "DNS data", with_nacm=False) datastore = JsonDatastore(datamodel, CONFIG_GLOBAL["DATA_JSON_FILE"], "DNS data", with_nacm=False)
try:
datastore.load() datastore.load()
datastore.load_yl_data(CONFIG_GLOBAL["YANG_LIB_DIR"] + "yang-library-data.json") datastore.load_yl_data(CONFIG_GLOBAL["YANG_LIB_DIR"] + "yang-library-data.json")
except (FileNotFoundError, YangsonException) as e:
error("Could not load JSON datastore " + CONFIG_GLOBAL["DATA_JSON_FILE"])
error(ErrorHelpers.epretty(e))
sig_exit_handler(0, None)
try:
datastore.get_data_root().validate(ValidationScope.all, ContentType.config) datastore.get_data_root().validate(ValidationScope.all, ContentType.config)
except (SchemaError, SemanticError) as e:
error("Validation of datastore failed")
error(ErrorHelpers.epretty(e))
sig_exit_handler(0, None)
# Register schema listeners # Register configuration data node listeners
CONF_DATA_HANDLES.register_handler(KnotConfServerListener(datastore, "/dns-server:dns-server/server-options")) CONF_DATA_HANDLES.register_handler(KnotConfServerListener(datastore, "/dns-server:dns-server/server-options"))
CONF_DATA_HANDLES.register_handler(KnotConfLogListener(datastore, "/dns-server:dns-server/knot-dns:log")) CONF_DATA_HANDLES.register_handler(KnotConfLogListener(datastore, "/dns-server:dns-server/knot-dns:log"))
CONF_DATA_HANDLES.register_handler(KnotConfZoneListener(datastore, "/dns-server:dns-server/zones")) CONF_DATA_HANDLES.register_handler(KnotConfZoneListener(datastore, "/dns-server:dns-server/zones"))
...@@ -153,7 +170,7 @@ def main(): ...@@ -153,7 +170,7 @@ def main():
# Register op handlers # Register op handlers
OP_HANDLERS.register_handler("generate-key", usr_op_handlers.sign_op_handler) OP_HANDLERS.register_handler("generate-key", usr_op_handlers.sign_op_handler)
# Create and register state data handlers # Create and register state data node listeners
usr_state_data_handlers.create_zone_state_handlers(STATE_DATA_HANDLES, datamodel) usr_state_data_handlers.create_zone_state_handlers(STATE_DATA_HANDLES, datamodel)
# Initialize Knot control interface # Initialize Knot control interface
......
...@@ -11,7 +11,8 @@ CONFIG_GLOBAL = { ...@@ -11,7 +11,8 @@ CONFIG_GLOBAL = {
"LOG_LEVEL": "info", "LOG_LEVEL": "info",
"LOG_DBG_MODULES": ["*"], "LOG_DBG_MODULES": ["*"],
"YANG_LIB_DIR": "yang-data/", "YANG_LIB_DIR": "yang-data/",
"DATA_JSON_FILE": "data.json" "DATA_JSON_FILE": "data.json",
"VALIDATE_TRANSACTIONS": True
} }
CONFIG_HTTP = { CONFIG_HTTP = {
......
import json import json
from threading import Lock from threading import Lock
from enum import Enum from enum import Enum
from colorlog import error, warning as warn, info from colorlog import error, warning as warn, info
...@@ -192,7 +193,8 @@ class UsrChangeJournal: ...@@ -192,7 +193,8 @@ class UsrChangeJournal:
try: try:
# Validate syntax and semantics of new data # Validate syntax and semantics of new data
# nr.validate(ValidationScope.all, ContentType.config) if CONFIG["GLOBAL"]["VALIDATE_TRANSACTIONS"] is True:
nr.validate(ValidationScope.all, ContentType.config)
new_data_valid = True new_data_valid = True
except (SchemaError, SemanticError) as e: except (SchemaError, SemanticError) as e:
error("Data validation error:") error("Data validation error:")
......
from enum import Enum from enum import Enum
from typing import List, Union, Dict, Any, Optional from typing import List, Union, Dict, Any, Optional
from threading import Lock from threading import Lock
from colorlog import info
from .libknot.control import KnotCtl, KnotCtlType from .libknot.control import KnotCtl, KnotCtlType
from .config import CONFIG from .config import CONFIG
......
...@@ -2,7 +2,6 @@ import collections ...@@ -2,7 +2,6 @@ import collections
from io import StringIO from io import StringIO
from threading import Lock from threading import Lock
from enum import Enum from enum import Enum
from colorlog import error, info from colorlog import error, info
from typing import List, Set, Optional from typing import List, Set, Optional
......
from colorlog import info, warning as warn, error
from typing import List, Dict, Union, Any from typing import List, Dict, Union, Any
from yangson.instance import InstanceRoute, ObjectValue, EntryKeys, MemberName from yangson.instance import InstanceRoute, ObjectValue, EntryKeys, MemberName
......
from typing import Dict, Any from .helpers import JsonNodeT
JsonNodeT = Dict[str, Any]
def play_op_handler(input_args: JsonNodeT) -> JsonNodeT:
print("Playing song {} in playlist \"{}\"".format(input_args.get("song-number"), input_args.get("playlist")))
ret = {"status": "OK"}
return ret
def sign_op_handler(input_args: JsonNodeT) -> JsonNodeT: def sign_op_handler(input_args: JsonNodeT) -> JsonNodeT:
......
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