diff options
-rw-r--r-- | src/startup.ml | 26 | ||||
-rw-r--r-- | src/startup.mli | 4 | ||||
-rw-r--r-- | src/vyconfd.ml | 12 |
3 files changed, 38 insertions, 4 deletions
diff --git a/src/startup.ml b/src/startup.ml index c6e2f8c..69849e0 100644 --- a/src/startup.ml +++ b/src/startup.ml @@ -58,3 +58,29 @@ let create_server accept_connection sock = let rec serve () = Lwt_unix.accept sock >>= accept_connection >>= serve in serve + +(** Load the appliance configuration file *) +let load_config file = + try + let chan = open_in file in + let config = Curly_parser.config Curly_lexer.token (Lexing.from_channel chan) in + Ok config + with + | Sys_error msg -> Error msg + | Curly_parser.Error -> Error "Parse error" + +(** Load the appliance configuration file or the fallback config *) +let load_config_failsafe main fallback = + let res = load_config main in + match res with + | Ok config -> config + | Error msg -> + Lwt_log.error + (Printf.sprintf "Failed to load config file %s: %s. Attempting to load fallback config %s" main msg fallback) |> + Lwt.ignore_result; + let res = load_config fallback in + begin + match res with + | Ok config -> config + | Error msg -> panic (Printf.sprintf "Failed to load fallback config %s: %s, exiting" fallback msg) + end diff --git a/src/startup.mli b/src/startup.mli index 219d5a9..2191bdb 100644 --- a/src/startup.mli +++ b/src/startup.mli @@ -11,3 +11,7 @@ val create_socket : string -> Lwt_unix.file_descr Lwt.t val create_server : (Lwt_unix.file_descr * Lwt_unix.sockaddr -> unit Lwt.t) -> Lwt_unix.file_descr -> unit -> 'a Lwt.t + +val load_config : string -> (Config_tree.t, string) result + +val load_config_failsafe : string -> string -> Config_tree.t diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 16802fd..2670588 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -144,10 +144,14 @@ let make_world config dirs = let () = let () = Arg.parse args (fun f -> ()) usage in let () = config_file := FP.concat !basepath !config_file in - let config = Startup.load_daemon_config !config_file in - let () = Lwt_log.load_rules ("* -> " ^ config.log_level) in - let dirs = Directories.make !basepath config in + let vc = Startup.load_daemon_config !config_file in + let () = Lwt_log.load_rules ("* -> " ^ vc.log_level) in + let dirs = Directories.make !basepath vc in Startup.check_dirs dirs; - let world = make_world config dirs in + let world = make_world vc dirs in + let config = Startup.load_config_failsafe + (FP.concat vc.config_dir vc.primary_config) + (FP.concat vc.config_dir vc.fallback_config) in + let world = Session.{world with running_config=config} in Lwt_main.run @@ main_loop !basepath world () |