diff options
| author | John Estabrook <jestabro@vyos.io> | 2025-05-09 21:32:41 -0500 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2025-05-12 17:24:24 -0500 |
| commit | b50a8d2b1d7af48a66446d71dd3875307cc2b1e0 (patch) | |
| tree | be11d3f1e97ca5818e02c9155f953e0df42a552b | |
| parent | d4d8d619a70e81229927bf0268d8c1257fd7e5cd (diff) | |
| download | vyconf-b50a8d2b1d7af48a66446d71dd3875307cc2b1e0.tar.gz vyconf-b50a8d2b1d7af48a66446d71dd3875307cc2b1e0.zip | |
T7363: add get_config(s) request for Config instance initialization
Request cache of session running and proposed configs for loading on
intialization of Config instance. As used elsewhere (for example
vyos-commitd), this is a much faster method of exchanging information
than render and re-parse.
| -rw-r--r-- | data/vyconf.proto | 5 | ||||
| -rw-r--r-- | src/session.ml | 30 | ||||
| -rw-r--r-- | src/session.mli | 4 | ||||
| -rw-r--r-- | src/vyconf_pbt.ml | 57 | ||||
| -rw-r--r-- | src/vyconf_pbt.mli | 20 | ||||
| -rw-r--r-- | src/vyconfd.ml | 15 |
6 files changed, 128 insertions, 3 deletions
diff --git a/data/vyconf.proto b/data/vyconf.proto index 308bd6b..30f213c 100644 --- a/data/vyconf.proto +++ b/data/vyconf.proto @@ -26,6 +26,10 @@ message Request { required int32 ClientPid = 1; } + message GetConfig { + optional int32 dummy = 1; + } + message Teardown { optional int32 OnBehalfOf = 1; } @@ -170,6 +174,7 @@ message Request { SessionChanged session_changed = 26; SessionOfPid session_of_pid = 27; SessionUpdatePid session_update_pid = 28; + GetConfig get_config = 29; } } diff --git a/src/session.ml b/src/session.ml index e7be5d7..9ce2807 100644 --- a/src/session.ml +++ b/src/session.ml @@ -160,6 +160,36 @@ let prepare_commit ?(dry_run=false) w s id = in CC.make_commit_data ~dry_run:dry_run rt at wt id +let get_config w s id = + let at = w.running_config in + let wt = s.proposed_config in + let vc = w.vyconf_config in + let running_cache = Printf.sprintf "%s_%s" vc.running_cache id in + let session_cache = Printf.sprintf "%s_%s" vc.session_cache id in + let () = + try + IC.write_internal at (FP.concat vc.session_dir running_cache) + with + Vyos1x.Internal.Write_error msg -> raise (Session_error msg) + in + let () = + try + IC.write_internal wt (FP.concat vc.session_dir session_cache) + with + Vyos1x.Internal.Write_error msg -> raise (Session_error msg) + in id + +let cleanup_config w id = + let remove_file file = + if Sys.file_exists file then + Sys.remove file + in + let vc = w.vyconf_config in + let running_cache = Printf.sprintf "%s_%s" vc.running_cache id in + let session_cache = Printf.sprintf "%s_%s" vc.session_cache id in + remove_file (FP.concat vc.session_dir running_cache); + remove_file (FP.concat vc.session_dir session_cache) + let get_value w s path = if not (VT.exists s.proposed_config path) then raise (Session_error ("Config path does not exist")) diff --git a/src/session.mli b/src/session.mli index f7469e0..8e5805d 100644 --- a/src/session.mli +++ b/src/session.mli @@ -53,4 +53,8 @@ val string_of_op : cfg_op -> string val prepare_commit : ?dry_run:bool -> world -> session_data -> string -> Commitd_client.Commit.commit_data +val get_config : world -> session_data -> string -> string + +val cleanup_config : world -> string -> unit + val show_config : world -> session_data -> string list -> Vyconf_connect.Vyconf_pbt.request_config_format -> string diff --git a/src/vyconf_pbt.ml b/src/vyconf_pbt.ml index 55d4438..0afa0bd 100644 --- a/src/vyconf_pbt.ml +++ b/src/vyconf_pbt.ml @@ -24,6 +24,10 @@ type request_session_update_pid = { client_pid : int32; } +type request_get_config = { + dummy : int32 option; +} + type request_teardown = { on_behalf_of : int32 option; } @@ -163,6 +167,7 @@ type request = | Session_changed of request_session_changed | Session_of_pid of request_session_of_pid | Session_update_pid of request_session_update_pid + | Get_config of request_get_config type request_envelope = { token : string option; @@ -216,6 +221,12 @@ let rec default_request_session_update_pid client_pid; } +let rec default_request_get_config + ?dummy:((dummy:int32 option) = None) + () : request_get_config = { + dummy; +} + let rec default_request_teardown ?on_behalf_of:((on_behalf_of:int32 option) = None) () : request_teardown = { @@ -440,6 +451,14 @@ let default_request_session_update_pid_mutable () : request_session_update_pid_m client_pid = 0l; } +type request_get_config_mutable = { + mutable dummy : int32 option; +} + +let default_request_get_config_mutable () : request_get_config_mutable = { + dummy = None; +} + type request_teardown_mutable = { mutable on_behalf_of : int32 option; } @@ -716,6 +735,12 @@ let rec pp_request_session_update_pid fmt (v:request_session_update_pid) = in Pbrt.Pp.pp_brk pp_i fmt () +let rec pp_request_get_config fmt (v:request_get_config) = + let pp_i fmt () = + Pbrt.Pp.pp_record_field ~first:true "dummy" (Pbrt.Pp.pp_option Pbrt.Pp.pp_int32) fmt v.dummy; + in + Pbrt.Pp.pp_brk pp_i fmt () + let rec pp_request_teardown fmt (v:request_teardown) = let pp_i fmt () = Pbrt.Pp.pp_record_field ~first:true "on_behalf_of" (Pbrt.Pp.pp_option Pbrt.Pp.pp_int32) fmt v.on_behalf_of; @@ -908,6 +933,7 @@ let rec pp_request fmt (v:request) = | Session_changed x -> Format.fprintf fmt "@[<hv2>Session_changed(@,%a)@]" pp_request_session_changed x | Session_of_pid x -> Format.fprintf fmt "@[<hv2>Session_of_pid(@,%a)@]" pp_request_session_of_pid x | Session_update_pid x -> Format.fprintf fmt "@[<hv2>Session_update_pid(@,%a)@]" pp_request_session_update_pid x + | Get_config x -> Format.fprintf fmt "@[<hv2>Get_config(@,%a)@]" pp_request_get_config x let rec pp_request_envelope fmt (v:request_envelope) = let pp_i fmt () = @@ -982,6 +1008,15 @@ let rec encode_pb_request_session_update_pid (v:request_session_update_pid) enco Pbrt.Encoder.key 1 Pbrt.Varint encoder; () +let rec encode_pb_request_get_config (v:request_get_config) encoder = + begin match v.dummy with + | Some x -> + Pbrt.Encoder.int32_as_varint x encoder; + Pbrt.Encoder.key 1 Pbrt.Varint encoder; + | None -> (); + end; + () + let rec encode_pb_request_teardown (v:request_teardown) encoder = begin match v.on_behalf_of with | Some x -> @@ -1312,6 +1347,9 @@ let rec encode_pb_request (v:request) encoder = | Session_update_pid x -> Pbrt.Encoder.nested encode_pb_request_session_update_pid x encoder; Pbrt.Encoder.key 28 Pbrt.Bytes encoder; + | Get_config x -> + Pbrt.Encoder.nested encode_pb_request_get_config x encoder; + Pbrt.Encoder.key 29 Pbrt.Bytes encoder; end let rec encode_pb_request_envelope (v:request_envelope) encoder = @@ -1455,6 +1493,24 @@ let rec decode_pb_request_session_update_pid d = client_pid = v.client_pid; } : request_session_update_pid) +let rec decode_pb_request_get_config d = + let v = default_request_get_config_mutable () in + let continue__= ref true in + while !continue__ do + match Pbrt.Decoder.key d with + | None -> ( + ); continue__ := false + | Some (1, Pbrt.Varint) -> begin + v.dummy <- Some (Pbrt.Decoder.int32_as_varint d); + end + | Some (1, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_get_config), field(1)" pk + | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind + done; + ({ + dummy = v.dummy; + } : request_get_config) + let rec decode_pb_request_teardown d = let v = default_request_teardown_mutable () in let continue__= ref true in @@ -2046,6 +2102,7 @@ let rec decode_pb_request d = | Some (26, _) -> (Session_changed (decode_pb_request_session_changed (Pbrt.Decoder.nested d)) : request) | Some (27, _) -> (Session_of_pid (decode_pb_request_session_of_pid (Pbrt.Decoder.nested d)) : request) | Some (28, _) -> (Session_update_pid (decode_pb_request_session_update_pid (Pbrt.Decoder.nested d)) : request) + | Some (29, _) -> (Get_config (decode_pb_request_get_config (Pbrt.Decoder.nested d)) : request) | Some (n, payload_kind) -> ( Pbrt.Decoder.skip d payload_kind; loop () diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli index d7eef26..d9eb8ef 100644 --- a/src/vyconf_pbt.mli +++ b/src/vyconf_pbt.mli @@ -31,6 +31,10 @@ type request_session_update_pid = { client_pid : int32; } +type request_get_config = { + dummy : int32 option; +} + type request_teardown = { on_behalf_of : int32 option; } @@ -170,6 +174,7 @@ type request = | Session_changed of request_session_changed | Session_of_pid of request_session_of_pid | Session_update_pid of request_session_update_pid + | Get_config of request_get_config type request_envelope = { token : string option; @@ -227,6 +232,12 @@ val default_request_session_update_pid : request_session_update_pid (** [default_request_session_update_pid ()] is the default value for type [request_session_update_pid] *) +val default_request_get_config : + ?dummy:int32 option -> + unit -> + request_get_config +(** [default_request_get_config ()] is the default value for type [request_get_config] *) + val default_request_teardown : ?on_behalf_of:int32 option -> unit -> @@ -426,6 +437,9 @@ val pp_request_session_of_pid : Format.formatter -> request_session_of_pid -> un val pp_request_session_update_pid : Format.formatter -> request_session_update_pid -> unit (** [pp_request_session_update_pid v] formats v *) +val pp_request_get_config : Format.formatter -> request_get_config -> unit +(** [pp_request_get_config v] formats v *) + val pp_request_teardown : Format.formatter -> request_teardown -> unit (** [pp_request_teardown v] formats v *) @@ -531,6 +545,9 @@ val encode_pb_request_session_of_pid : request_session_of_pid -> Pbrt.Encoder.t val encode_pb_request_session_update_pid : request_session_update_pid -> Pbrt.Encoder.t -> unit (** [encode_pb_request_session_update_pid v encoder] encodes [v] with the given [encoder] *) +val encode_pb_request_get_config : request_get_config -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_get_config v encoder] encodes [v] with the given [encoder] *) + val encode_pb_request_teardown : request_teardown -> Pbrt.Encoder.t -> unit (** [encode_pb_request_teardown v encoder] encodes [v] with the given [encoder] *) @@ -636,6 +653,9 @@ val decode_pb_request_session_of_pid : Pbrt.Decoder.t -> request_session_of_pid val decode_pb_request_session_update_pid : Pbrt.Decoder.t -> request_session_update_pid (** [decode_pb_request_session_update_pid decoder] decodes a [request_session_update_pid] binary value from [decoder] *) +val decode_pb_request_get_config : Pbrt.Decoder.t -> request_get_config +(** [decode_pb_request_get_config decoder] decodes a [request_get_config] binary value from [decoder] *) + val decode_pb_request_teardown : Pbrt.Decoder.t -> request_teardown (** [decode_pb_request_teardown decoder] decodes a [request_teardown] binary value from [decoder] *) diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 816ca69..0b92fd1 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -120,9 +120,10 @@ let exit_conf_mode world token = in Hashtbl.replace sessions token session; response_tmpl -let teardown token = +let teardown world token = try - Hashtbl.remove sessions token; + let () = Hashtbl.remove sessions token in + let () = Session.cleanup_config world token in {response_tmpl with status=Success} with Not_found -> {response_tmpl with status=Fail; error=(Some "Session not found")} @@ -131,6 +132,13 @@ let session_changed world token (_req: request_session_changed) = if Session.session_changed world (find_session token) then response_tmpl else {response_tmpl with status=Fail} +let get_config world token (_req: request_get_config) = + try + let id = + Session.get_config world (find_session token) token + in {response_tmpl with output=(Some id)} + with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)} + let exists world token (req: request_exists) = if Session.exists world (find_session token) req.path then response_tmpl else {response_tmpl with status=Fail} @@ -296,7 +304,7 @@ let rec handle_connection world ic oc () = | _, Reload_reftree r -> reload_reftree world r | None, _ -> {response_tmpl with status=Fail; output=(Some "Operation requires session token")} | Some t, Session_update_pid r -> session_update_pid world t r - | Some t, Teardown _ -> teardown t + | Some t, Teardown _ -> teardown world t | Some t, Enter_configuration_mode r -> enter_conf_mode r t | Some t, Exit_configuration_mode -> exit_conf_mode world t | Some t, Exists r -> exists world t r @@ -309,6 +317,7 @@ let rec handle_connection world ic oc () = | Some t, Delete r -> delete world t r | Some t, Discard r -> discard world t r | Some t, Session_changed r -> session_changed world t r + | Some t, Get_config r -> get_config world t r | Some t, Load r -> load world t r | Some t, Save r -> save world t r | _ -> failwith "Unimplemented" |
