@@ -25,6 +25,7 @@ let config_file = ref defaults.config_file
2525let  basepath =  ref  " /" 
2626let  log_file =  ref  None 
2727let  legacy_config_path =  ref  false 
28+ let  reload_active_config =  ref  false 
2829
2930(*  Global data *) 
3031let  sessions  : (string, Session.session_data) Hashtbl.t  =  Hashtbl. create 10 
@@ -43,6 +44,7 @@ let args = [
4344    (" --version"  , Arg. Unit  (fun  ()  -> print_endline @@  Version. version_info () ; exit 0 ), " Print version and exit"  );
4445    (" --legacy-config-path"  , Arg. Unit  (fun  ()  -> legacy_config_path :=  true ),
4546    (Printf. sprintf " Load config file from legacy path %s"   defaults.legacy_config_path));
47+     (" --reload-active-config"  , Arg. Unit  (fun  ()  -> reload_active_config :=  true ), " Reload active config file"  );
4648   ]
4749let  usage =  " Usage: "   ^  Sys. argv.(0 ) ^  "  [options]" 
4850
@@ -274,16 +276,13 @@ let commit world token (req: request_commit) =
274276    let  proposed_config =  Session. get_proposed_config world s in 
275277    let  req_dry_run =  Option. value req.dry_run ~default: false  in 
276278    let  commit_data = 
277-         Session. prepare_commit
278-         ~dry_run: req_dry_run
279-         world
280-         proposed_config
281-         token
282-         s.client_pid
283-         s.client_sudo_user
284-         s.client_user
279+         Session. prepare_commit ~dry_run: req_dry_run world s proposed_config token
285280    in 
286-     let % lwt received_commit_data =  VC. do_commit commit_data in 
281+     match  commit_data with 
282+     |  Error  msg  ->
283+         Lwt. return {response_tmpl with  status= Internal_error ; error= (Some  msg)}
284+     |  Ok  c_data  ->
285+     let % lwt received_commit_data =  VC. do_commit c_data in 
287286    let % lwt result_commit_data = 
288287        Lwt. return (CC. commit_update received_commit_data)
289288    in 
@@ -314,12 +313,25 @@ let commit world token (req: request_commit) =
314313                        world.Session. running_config
315314                        post_proposed;
316315                        aux_changeset =  [] ; }
317-                 in  Hashtbl. replace sessions token session
316+                 in 
317+                 Hashtbl. replace sessions token session
318318            else  () ;
319319
320-             let  success, msg_str = 
320+             let  write_msg = 
321+                 if  not  req_dry_run then 
322+                     match  Session. write_running_cache world with 
323+                     |  Ok  ()  -> " " 
324+                     |  Error  msg  -> msg
325+                 else  " " 
326+             in 
327+             let  success, result_msg = 
321328                result_commit_data.result.success, result_commit_data.result.out
322329            in 
330+             let  msg_str = 
331+                 match  write_msg with 
332+                 |  ""  -> result_msg
333+                 |  _  -> Printf. sprintf " %s\n  %s"   result_msg write_msg
334+             in 
323335            match  success with 
324336            |  true  -> Lwt. return {response_tmpl with  status= Success ; output= (Some  msg_str)}
325337            |  false  -> Lwt. return {response_tmpl with  status= Fail ; output= (Some  msg_str)}
@@ -426,6 +438,14 @@ let read_reference_tree file =
426438    |  Ok  r  -> r
427439    |  Error  s  -> Startup. panic s
428440
441+ let  init_write_cache  world  = 
442+     (*  initial cache of running config; this will be unnecessary once
443+        vyconfd is started at boot *)  
444+     let  res =  Session. write_running_cache world in 
445+     match  res with 
446+     |  Ok  _  ->  () 
447+     |  Error  msg  -> (Lwt_log. error msg) |>  Lwt. ignore_result
448+ 
429449let  make_world  config  dirs  = 
430450    let  open  Session  in 
431451    (*  the reference_tree json file is generated at vyos-1x build time *) 
@@ -446,8 +466,23 @@ let () =
446466      |  false  -> (FP. concat vc.config_dir vc.primary_config)
447467  in 
448468  let  failsafe_config =  (FP. concat vc.config_dir vc.fallback_config) in 
469+   let  restart_cache =  (FP. concat vc.session_dir vc.running_cache) in 
449470  let  config = 
450-       Startup. load_config_failsafe primary_config failsafe_config
471+       match  ! reload_active_config with 
472+       |  true  -> let  res =  Startup. load_config_cache restart_cache in 
473+                 begin 
474+                 match  res with 
475+                 |  Ok  c  -> c
476+                 |  Error  msg  ->
477+                     let  ()  =  (Lwt_log. error msg) |>  Lwt. ignore_result in 
478+                     Startup. load_config_failsafe primary_config failsafe_config
479+                 end 
480+       |  false  -> Startup. load_config_failsafe primary_config failsafe_config
451481  in 
452482  let  world =  Session. {world with  running_config= config} in 
483+   let  ()  = 
484+       match  ! reload_active_config with 
485+       |  true  -> () 
486+       |  false  -> init_write_cache world
487+   in 
453488  Lwt_main. run @@  main_loop ! basepath world () 
0 commit comments